-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplayer.py
64 lines (53 loc) · 1.99 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import config
import csv
import json
import logging
logger = logging.getLogger(__name__)
class Player():
def __init__(self):
self.username = None
self.angel = None
self.mortal = None
self.chat_id = None
def loadPlayers(players: dict):
with open(config.PLAYERS_FILENAME) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
logger.info(f'Column names are {", ".join(row)}')
line_count += 1
else:
playerName = row[0].strip().lower()
angelName = row[1].strip().lower()
mortalName = row[2].strip().lower()
logger.info(f'\t{playerName} has angel {angelName} and mortal {mortalName}.')
players[playerName].username = playerName
players[playerName].angel = players[angelName]
players[playerName].mortal = players[mortalName]
line_count += 1
logger.info(f'Processed {line_count} lines.')
validatePairings(players)
loadChatID(players)
def validatePairings(players: dict):
for _, player in players.items():
if player.angel.mortal.username != player.username or player.mortal.angel.username != player.username:
print(f'Error with {player.username} pairings')
logger.error(f'Error with {player.username} pairings')
exit(1)
logger.info(f'Validation complete, no issues with pairings.')
def saveChatID(players: dict):
temp = {}
for k, v in players.items():
temp[k] = v.chat_id
with open(config.CHAT_ID_JSON, 'w+') as f:
json.dump(temp, f)
def loadChatID(players: dict):
try:
with open(config.CHAT_ID_JSON, 'r') as f:
temp = json.load(f)
logger.info(temp)
for k, v in temp.items():
players[k].chat_id = v
except:
logger.warn('Fail to load chat ids')