@@ -372,21 +372,7 @@ def end_game(stats):
372
372
print ('\n I hope you are happy with what you have achieved becuase I am.' )
373
373
print ('Let\' s see if you managed to secure a place on our leaderboard.' )
374
374
375
- top_10 = check_top_10 ()
376
- for count , key in enumerate (top_10 [1 :10 ], 2 ):
377
- if cash > float (key [1 ]):
378
- print (f"\n { green ('CONGRATULATIONS!!!' )} " )
379
- print (f'You placed number { gold (count - 1 )} on our leaderboard!\n ' )
380
- row = count
381
- data_to_save = [
382
- stats ["name" ],
383
- stats ["cash" ]
384
- ]
385
- worksheet = SHEET .worksheet ("leaderboard" )
386
- save_loop (row , data_to_save , len (data_to_save ), worksheet )
387
- break
388
- else :
389
- print ('Sadly you didn\' t make the top 10 this time. Maybe next time?' )
375
+ check_top_10 (stats )
390
376
391
377
stats = {}
392
378
@@ -397,10 +383,34 @@ def end_game(stats):
397
383
break
398
384
399
385
400
- def check_top_10 ():
386
+ def check_top_10 (stats ):
401
387
'''
402
388
Checks top 10 of leaderboard to see if user qualifies
389
+ If qualifies:
390
+ - Move entries below new entry down one
391
+ - Remove bottom entry
392
+ - Add new entry in
393
+ - Save new table to Google Worksheet
403
394
'''
404
395
highscore = SHEET .worksheet ('leaderboard' )
405
396
data = highscore .get_all_values ()
406
- return data
397
+ # Go through current leader board entries and see if placed higher than any
398
+ # of the current entries.
399
+ for count , key in enumerate (data [1 :10 ], 2 ):
400
+ # Player has placed higher then a player.
401
+ if stats ["cash" ] > float (key [1 ]):
402
+ print (f"\n { green ('CONGRATULATIONS!!!' )} " )
403
+ print (f'You placed number { gold (count - 1 )} on our leaderboard!\n ' )
404
+ # Insert data of player into correct place
405
+ data .insert (count - 1 , [stats ["name" ], stats ["cash" ]])
406
+ # Remove data for player who is no in 11th place.
407
+ data .pop ()
408
+ # Update Google sheet
409
+ SHEET .values_update (
410
+ 'leaderboard!A1' ,
411
+ params = {'valueInputOption' : 'RAW' },
412
+ body = {'values' : data }
413
+ )
414
+ break
415
+ else :
416
+ print ('Sadly you didn\' t make the top 10 this time. Maybe next time?' )
0 commit comments