-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_handler.py
More file actions
191 lines (173 loc) · 7.51 KB
/
data_handler.py
File metadata and controls
191 lines (173 loc) · 7.51 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# data_handler is responsible for the operations with db and json's
import json
import sys
from ui_handler import texts
from __init__ import VERSION, RUNNING_DIR
sport = 0
try:
with open(f'{RUNNING_DIR}/preferences.json', 'r', encoding='utf-8') as f:
preferences = json.load(f)
if preferences['sport'] == 1:
sport = 1
elif preferences['sport'] == 2:
sport = 2
else:
pass
except KeyError:
pass
while True:
try:
if sport != 1 and sport != 2:
sport = int(input(f'''
{texts["select_sport"]}:
[1] - {texts["basketball"]}
[2] - {texts["soccer"]}
>>>'''))
if sport > 2 or sport < 1:
print("Invalid option")
else:
with open(f'{RUNNING_DIR}/preferences.json', 'w', encoding='utf-8') as f:
preferences['sport'] = sport
json.dump(preferences, f, ensure_ascii=False, indent=4)
break
else:
break
except ValueError:
print('Invalid input')
input(f'{texts["enter_to_continue"]}')
except Exception as e:
print(f'ERROR: {e}')
input(f'Press enter to exit...')
sys.exit(1)
positions_basketball = ['PG', 'SG', 'SF', 'PF', 'C']
positions_soccer = ['GK', 'SW', 'LB', 'CB', 'RB', 'LWB', 'RWB', 'DM', 'LM', 'CM', 'RM', 'AM', 'LW', 'SS', 'RW', 'LF', 'CF', 'RF']
if sport == 1:
try:
with open(f'{RUNNING_DIR}/basketball.json', 'r', encoding='utf-8') as f:
db = json.load(f)
if tuple(map(int, db['version'].split('.'))) < (2, 0, 0):
print(f'{texts["unsupported_version"]}: {db["version"]}version')
input('Enter to continue')
sys.exit(1)
except FileNotFoundError:
player = input(f"{texts["enter_name"]}: ")
db = {"version": VERSION, "player": player, "games": []}
except json.decoder.JSONDecodeError as e:
print(f"The file is not a valid Json file or is empty\nERROR: {e}")
input(f'{texts["enter_to_continue"]}')
sys.exit(1)
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')
sys.exit(1)
elif sport == 2:
try:
with open(f'{RUNNING_DIR}/soccer.json', 'r', encoding='utf-8') as f:
db = json.load(f)
if tuple(map(int, db['version'].split('.'))) < (2, 0, 0):
print(f'{texts["unsupported_version"]}: {db["version"]}')
input('Enter to continue')
sys.exit(1)
except FileNotFoundError:
player = input(f'{texts["enter_name"]}: ')
db = {"version": VERSION, "player": player, "games": []}
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')
sys.exit(1)
def change_sport(preferences):
with open(f'{RUNNING_DIR}/preferences.json', 'w', encoding='utf-8') as f:
if preferences['sport'] == 1:
preferences['sport'] = 2
json.dump(preferences, f, ensure_ascii=False, indent=4)
print(f'{texts["changed_sport"]} {texts["soccer"]}')
else:
preferences['sport'] = 1
json.dump(preferences, f, ensure_ascii=False, indent=4)
print(f'{texts["changed_sport"]} {texts["basketball"]}')
def add_match():
if sport == 1:
while True:
try:
new_game = {
"name": input(f"{texts["game_name"]}: "),
"date": input(f"{texts["game_date"]}: "),
"opponent": input("Opponent: "),
"position": positions_basketball[int(input(f'{texts["game_basketball_position"]}\n>>> ').strip()) - 1],
"minutes": int(input(f'{texts["game_minutes"]}: ')),
"points": int(input(f"{texts["game_points"]}: ")),
"assists": int(input(f"{texts["game_assists"]}: ")),
"2pt_attempts": int(input(f'{texts["game_2pta"]}: ')),
"3pt_attempts": int(input(f'{texts["game_3pta"]}: ')),
"2pt_shots_made": int(input(f'{texts["game_2ptm"]}: ')),
"3pt_shots_made": int(input(f'{texts["game_3ptm"]}: ')),
"rebounds": int(input(f"{texts["game_rebounds"]}: ")),
"blocks": int(input(f"{texts["game_blocks"]}: ")),
"steals": int(input(f"{texts["game_steals"]}: ")),
"personal_fouls": int(input(f"{texts["game_personal_fouls"]}: ")),
"missed_free_throws": int(input(f"{texts["game_missed_free_throws"]}: ")),
"turnovers": int(input(f"{texts["game_turnovers"]}: ")),
"result": input(f"{texts["game_result"]}: ")
}
break
except ValueError:
print(f'Invalid input')
input(f'{texts["enter_to_continue"]}')
except IndexError:
print('Out of range')
input(f'{texts["enter_to_continue"]}')
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')
sys.exit(1)
elif sport == 2:
while True:
try:
new_game = {
"name": input(f"{texts["game_name"]}: "),
"date": input(f"{texts["game_date"]}: "),
"opponent": input("Opponent: "),
"position": positions_soccer[int(input(f'{texts["game_soccer_position"]}\n>>> ').strip()) - 1],
"minutes": int(input(f'{texts["game_minutes"]}: ')),
"goals": int(input(f'{texts["game_goals"]}: ')),
"assists": int(input(f'{texts["game_assists"]}: ')),
"saves": int(input(f'{texts["game_saves"]}: ')),
"steals": int(input(f'{texts["game_steals"]}: ')),
"shots": int(input(f'{texts["game_shots"]}: ')),
"yellow_cards": int(input(f"{texts["game_yellow_cards"]}: ")),
"red_cards": int(input(f'{texts["game_red_cards"]}: ')),
"fouls": int(input(f'{texts["game_personal_fouls"]}: ')),
"result": input(f"{texts["game_result"]}: ")
}
break
except ValueError:
print('Invalid input')
input(f'{texts["enter_to_continue"]}')
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')
sys.exit(1)
db["games"].append(new_game)
def save():
if db['version'] != VERSION:
db['version'] = VERSION
if sport == 1:
try:
with open(f'{RUNNING_DIR}/basketball.json', 'w', encoding='utf-8') as f:
json.dump(db, f, ensure_ascii=False, indent=4)
except FileNotFoundError:
print('File basketball.json does not exist.')
input(f'{texts["enter_to_continue"]}')
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')
if sport == 2:
try:
with open(f'{RUNNING_DIR}/soccer.json', 'w', encoding='utf-8') as f:
json.dump(db, f, ensure_ascii=False, indent=4)
except FileNotFoundError:
print('File soccer.json does not exist.')
input(f'{texts["enter_to_continue"]}')
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}')