-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenus.py
More file actions
354 lines (329 loc) · 11 KB
/
Menus.py
File metadata and controls
354 lines (329 loc) · 11 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from __future__ import division #for correct integer division
import pickle #for saving, loading files
import subprocess as sp #for screen clearing
import pprint #Pretty Printing
import msvcrt as m #for wait() function
import time
from functools import partial #for dictionary functions w/ specific parameters
#This program is for recording games over time, using the Elo rating system developed for chess tournaments to rank players.
#It is able to generate likelihood of winning, it keeps records of all operations in the menus, it allows individual scores
#management in case a score needs fixing or handicap, and it is able to reset, add, or remove players at any time.
#Code written by Charlie Staich
#DONE
#track ratings
#calculate new ratings
#record games
#player manager
#ability to check scores
#ability to view past games
#better menus
#TO-DO
#confirmation prompt for high-risk menu options
#ability to edit most recent game
#ratings-over-time display
#MENU FUNCTIONS
def main_menu():
clearscreen()
entry = menu(['Record a Game', 'Roster', 'Records', 'Tools'], 'main')
choices = {
0: partial(return_to,'main'),
1: play_game,
2: roster_menu,
3: records_menu,
4: tools_menu,
}
choices[entry]()
return_to('main')
return
def roster_menu():
display_roster()
entry = menu(['Add a player', 'Remove a player', 'Modify a rating', 'Reset all ratings', 'Delete Roster'], 'roster')
choices = {
0: partial(return_to,'main'),
1: add_player,
2: delete_player,
3: modify_rating,
4: reset_ratings,
5: delete_roster,
}
choices[entry]()
return_to('roster')
return
def records_menu():
entry = menu(['Display Records', 'Delete All'], 'records')
choices = {
0: partial(return_to,'main'),
1: display_records,
2: delete_records,
}
choices[entry]()
return_to('records')
return
def tools_menu():
entry = menu(['Calculate Odds', 'Track Ratings'], 'tools')
choices = {
0: partial(return_to,'main'),
1: calculate_odds,
#2: track_ratings, #not added
}
choices[entry]()
return_to('tools')
return
def menu(options, return_menu = 'main'):
opt_dict = {0: 'Return'}
count = 1
for item in options:
opt_dict[count] = item
count = count + 1
key_list = opt_dict.keys()
key_list.sort()
for item in key_list:
print "{}: {}".format(item, opt_dict[item])
entry = prompt_number("Enter Selection: ", key_list)
clearscreen()
return entry
def return_to(return_menu, type = 0):
if type == 1: #incorrect input
print "Invalid Entry, returning to {} menu".format(return_menu)
wait()
clearscreen()
menu_list[return_menu]()
return
def play_game():
roster = load_obj('roster')
#Initialize player dictionaries
p1 = {}
p2 = {}
display_roster()
p1['name'] = prompt_name('Name of Player 1: ', 2)
p2['name'] = prompt_name('Name of Player 2: ', 2)
p1['rating'] = roster[p1['name']]
p2['rating'] = roster[p2['name']]
#Calculate adjusted logarithmic ratings
p1['log'] = 10 ** (p1['rating']/ 400.00)
p2['log'] = 10 ** (p2['rating'] / 400.00)
#Calculate relative EVs
p1['EV'] = p1['log'] / (p1['log'] + p2['log'])
p2['EV'] = p2['log'] / (p1['log'] + p2['log'])
#Display ratings and relative EVs
print "{}: {:.2f} ({:.2f}% to win)".format(p1['name'], p1['rating'], p1['EV']*100)
print "{}: {:.2f} ({:.2f}% to win)".format(p2['name'], p2['rating'], p2['EV']*100)
p1['score'] = prompt_number("Enter {}'s score: ".format(p1['name']))
p2['score'] = prompt_number("Enter {}'s score: ".format(p2['name']))
#Outcome logic (used to generate new rankings)
if p1['score'] > p2['score']:
p1['outcome'] = 1
p2['outcome'] = 0
elif p1['score'] < p2['score']:
p1['outcome'] = 0
p2['outcome'] = 1
elif p1['score'] == p2['score']:
p1['outcome'] = 0.5
p2['outcome'] = 0.5
#Calculate new ratings
multiplier = 32
p1['new_rating'] = p1['rating'] + multiplier * (p1['outcome'] - p1['EV'])
p2['new_rating'] = p2['rating'] + multiplier * (p2['outcome'] - p2['EV'])
p1['difference'] = p1['new_rating'] - p1['rating']
p2['difference'] = p2['new_rating'] - p2['rating']
#Display new ratings
print "{}: {:.2f} ---> {:.2f} [{:.2f}]".format(p1['name'], p1['rating'], p1['new_rating'], p1['difference'])
print "{}: {:.2f} ---> {:.2f} [{:.2f}]".format(p2['name'], p2['rating'], p2['new_rating'], p2['difference'])
#Record new ratings on the roster
roster[p1['name']] = p1['new_rating']
roster[p2['name']] = p2['new_rating']
save_obj(roster, 'roster')
#Record game for records
metadata = {}
metadata['time'] = time.time()
records.append({
'type': 'game',
'metadata': metadata,
'p1': p1,
'p2': p2,
})
save_obj(records, 'records')
wait()
return_to('main')
return
#ROSTER MENU FUNCTIONS
def display_roster():
roster = load_obj('roster')
print "=== Current Roster ==="
pp.pprint(roster)
print "\n"
return
def add_player():
roster = load_obj('roster')
player = raw_input("Enter player: ").lower()
if player in roster.keys():
print "{} is already on the roster.".format(player)
wait()
return_to('roster')
roster[player] = 400
save_obj(roster, 'roster')
metadata = {}
metadata['time'] = time.time()
metadata['description'] = "Added {} to roster with score of {}".format(player, roster[player])
records.append({
'type': 'records',
'metadata': metadata
})
save_obj(records, 'records')
return
def delete_player():
roster = load_obj('roster')
player = prompt_name()
old_rating = roster[player]
roster.pop(player, None)
save_obj(roster, 'roster')
metadata = {}
metadata['time'] = time.time()
metadata['description'] = "Deleted {} from roster with score of {}".format(player, old_rating)
records.append({
'type': 'records',
'metadata': metadata
})
save_obj(records, 'records')
return
def modify_rating():
roster = load_obj('roster')
player = prompt_name()
old_rating = roster[player]
new_rating = prompt_number("Enter new rating: ")
roster[player] = new_rating
save_obj(roster, 'roster')
metadata = {}
metadata['time'] = time.time()
metadata['description'] = "Changed {}'s rating from {} to {}".format(player, old_rating, new_rating)
records.append({
'type': 'records',
'metadata': metadata
})
save_obj(records, 'records')
return
def reset_ratings():
roster = load_obj('roster')
for player in roster:
roster[player] = 400
save_obj(roster, 'roster')
return
def delete_roster():
roster = {}
save_obj(roster, 'roster')
roster = load_obj('roster')
return
#RECORDS MENU FUNCTIONS
def display_records(max_records = -1):
clearscreen()
records = load_obj('records')
count = 1
for entry in records:
metadata = entry['metadata']
if count == max_records:
break
if entry['type'] == 'game':
p1 = entry['p1']
p2 = entry['p2']
data = []
data.append([" ", p1['name'], p2['name']])
data.append(["Score", p1['score'], p2['score']])
data.append(['EV', p1['EV'], p2['EV']])
data.append(['Rating Change', p1['difference'], p2['difference']])
data.append(['New Rating', p1['new_rating'], p2['new_rating']])
#printing
print "\n=== Game at {} ===\n".format(metadata['time'])
col_width = max(len(str(item)) for line in data for item in line) + 2
for line in data:
print "\t" + "".join(str(item).ljust(col_width) for item in line)
else:
print "\n=== {} change at {} ===\n".format(entry['type'], metadata['time'])
print "\t" + metadata['description']
count = count + 1
print "\n"
wait()
return
def delete_records():
metadata = {}
metadata['time'] = time.time()
metadata['description'] = "Wiped All Records".format()
records = [{
'type': 'records',
'metadata': metadata
}]
save_obj(records, 'records')
#TOOLS MENU FUNCTIONS
def calculate_odds():
#Initialize player dictionaries
p1 = {}
p2 = {}
#Poll user for participants
p1['name'] = raw_input('Name of Player 1: ').lower()
p2['name'] = raw_input('Name of Player 2: ').lower()
#Get records for participants
roster = load_obj('roster')
p1['rating'] = roster[p1['name']]
p2['rating'] = roster[p2['name']]
#Calculate adjusted logarithmic ratings
p1['log'] = 10 ** (p1['rating']/ 400.00)
p2['log'] = 10 ** (p2['rating'] / 400.00)
#Calculate relative EVs
p1['EV'] = p1['log'] / (p1['log'] + p2['log'])
p2['EV'] = p2['log'] / (p1['log'] + p2['log'])
#Display ratings and relative EVs
print "{}: {:.2f} ({:.2f}% to win)".format(p1['name'], p1['rating'], p1['EV']*100)
print "{}: {:.2f} ({:.2f}% to win)".format(p2['name'], p2['rating'], p2['EV']*100)
wait()
return
#COMMON FUNCTIONS
def save_obj(obj, name):
with open('obj/'+ name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open('obj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
def wait(): #waits for keystrike before continuing
m.getch()
return
def prompt_name(message = "Enter player: ", min_length = 1):
roster = load_obj('roster')
if len(roster) < min_length:
print "The roster is not long enough!"
wait()
return_to('main')2
player = raw_input(message).lower()
try:
roster[player]
except:
print "That name is not on the roster. "
return prompt_name(message, min_length)
return player
def prompt_number(message = "Enter number: ", values = -1):
number = raw_input(message)
try:
number = int(number)
except ValueError:
print "Invalid entry."
return prompt_number(message, values)
if values != -1:
for value in values:
value = int(value)
if number not in values:
print "Out of range."
return prompt_number(message, values)
return number
def clearscreen():
sp.call('cls',shell=True) #clears the screen
return
#COMMON VARIABLES
roster = load_obj('roster')
records = load_obj('records')
pp = pprint.PrettyPrinter()
menu_list = {
'main': main_menu,
'roster': roster_menu,
'records': records_menu,
'tools': tools_menu,
}
#BODY
main_menu()