Skip to content

Commit bf89510

Browse files
committed
Fix bug - New leaderboard entries overwriting old.
- Update code to insert new entries and not overwrite old. - The last / lowest entry of list
1 parent 3f8b1d7 commit bf89510

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

game.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,7 @@ def end_game(stats):
372372
print('\nI hope you are happy with what you have achieved becuase I am.')
373373
print('Let\'s see if you managed to secure a place on our leaderboard.')
374374

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)
390376

391377
stats = {}
392378

@@ -397,10 +383,34 @@ def end_game(stats):
397383
break
398384

399385

400-
def check_top_10():
386+
def check_top_10(stats):
401387
'''
402388
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
403394
'''
404395
highscore = SHEET.worksheet('leaderboard')
405396
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

Comments
 (0)