Leaderboard Help

Question:
So I am making a lottery esq. game (it doesn’t give out money) and I am making a leaderboard for players with the most amount of money and my current code doesn’t work and gives this error:

Repl link:
https://replit.com/@SalladShooter/Scratchoff-Cycles-Lottery#main.py

Code that isn’t working:

def leaderboard():
  e.clear()
  # Sort user array
  users = db["game"]
  users.sort(key=lambda a: a[1], reverse=True)
  # Format the users
  leaderboard = map(lambda user: user[0] + "| " + str(user[1]), users)
  # Output
  print("\n".join(leaderboard))
  enter()
  Game()

All code:

import random
import extrapy as e
import time
import sys
import os
import threading

from replit import db

BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m" 

running = True
username = os.environ['REPL_OWNER']
if username in db:
    money = db[username]["money"]
    takeAwayAmount = db[username]["takeAwayAmount"]
else:
    game = []
    game.append(username)
    db["game"] = game
    db[username] = {"money": 100, "takeAwayAmount" : 5}
    takeAwayAmount = db[username]["takeAwayAmount"]
    money = db[username]["money"]

def enter():
  input(f"\nPress | {BLUE}Enter{LIGHT_WHITE} | to continue...")

def scratch():
  input(f"\nPress | {BLUE}Enter{LIGHT_WHITE} | to scratchoff...")

def bar():
  print("------------------------")

def write(write):
  for i in write:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(.05)

def titleBlink():
  for _ in range(5):
    text = f"{BOLD}Scratchoff Cycles Lottery!"
    sys.stdout.write(f'\r{text}')
    sys.stdout.flush()
    time.sleep(0.5)
    b = "Loading"
    sys.stdout.write('\r' + ' ' * len(text))
    sys.stdout.flush()
    time.sleep(0.5)

print(f"{BOLD}Welcome To")

titleBlink()
running = False

print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")

bar()
enter()

e.clear()

def printTicket(winNum, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8):
  global money, username
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print("|-------|   | // | // | // | // | |")
  print("|       |   --------------------- |")
  print("|  //   |   | // | // | // | // | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")
  
  scratch()
  e.clear()
  
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print(f"|-------|   | {BOLD}{spot1}{END} | {BOLD}{spot2}{END} | {BOLD}{spot3}{END} | {BOLD}{spot4}{END} | |")
  print("|       |   --------------------- |")
  print(f"|  {BOLD}{winNum}{END}   |   | {BOLD}{spot5}{END} | {BOLD}{spot6}{END} | {BOLD}{spot7}{END} | {BOLD}{spot8}{END} | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")

  winMoney = 0

  amounts = []
  chances = []
  
  for j in range(1000):
    amounts.append(random.randint(10,5000))
    chances.append(random.randint(1, 30))
  
  if spot1 == winNum:
    winMoney += random.choice(amounts)

  if spot2 == winNum:
    winMoney += random.choice(amounts)

  if spot3 == winNum:
    winMoney += random.choice(amounts)
    
  if spot4 == winNum:
    winMoney += random.choice(amounts)
    
  if spot5 == winNum:
    winMoney += random.choice(amounts)
    
  if spot6 == winNum:
    winMoney += random.choice(amounts)
    
  if spot7 == winNum:
    winMoney += random.choice(amounts)
    
  if spot8 == winNum:
    winMoney += random.choice(amounts)

  if winMoney >= 5000:
    print(f"{BOLD}You hit the {YELLOW}JACKPOT{LIGHT_WHITE}!{END}")
    takeAwayAmount = takeAwayAmount * 1.5
    db[username]["takeAwayAmount"] = takeAwayAmount

  winMoney = 0
    
  print(f"You won {BOLD}🌀{winMoney}{END}!")
  money += winMoney
  db[username]["money"] = money
  enter()
  e.clear()
  
  print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")
  print(f"You have {BOLD}🌀{money}{END}")
  print()
  print("What do you want to do?")
  print(" |1| Buy Ticket")
  print(" |2| Leaderboard")
  choice = input("\n> ")
  if choice == "1":
    buyTicket()
  elif choice == "2":
    leaderboard()

def buyTicket():
  global money, username, takeAwayAmount
  money -= takeAwayAmount
  db[username]["money"] = money
  e.clear()
  winNum = random.randint(1,99)
  spot1 = random.randint(1,99)
  spot2 = random.randint(1,99)
  spot3 = random.randint(1,99)
  spot4 = random.randint(1,99)
  spot5 = random.randint(1,99)
  spot6 = random.randint(1,99)
  spot7 = random.randint(1,99)
  spot8 = random.randint(1,99)
  if winNum < 10:
    winNum = f"0{winNum}"
  if spot1 < 10:
    spot1 = f"0{spot1}"
  if spot2 < 10:
    spot2 = f"0{spot2}"
  if spot3 < 10:
    spot3 = f"0{spot3}"
  if spot4 < 10:
    spot4 = f"0{spot4}"
  if spot5 < 10:
    spot5 = f"0{spot5}"
  if spot6 < 10:
    spot6 = f"0{spot6}"
  if spot7 < 10:
    spot7 = f"0{spot7}"
  if spot8 < 10:
    spot8 = f"0{spot8}"
  printTicket(winNum, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8)
  

def leaderboard():
  e.clear()
  # Sort user array
  users = db["game"]
  users.sort(key=lambda a: a[1], reverse=True)
  # Format the users
  leaderboard = map(lambda user: user[0] + "| " + str(user[1]), users)
  # Output
  print("\n".join(leaderboard))
  enter()
  Game()
  
class Game:
  global takeAwayAmount
  print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")
  print(f"You have {BOLD}🌀{money}{END}")
  print()
  print("What do you want to do?")
  print(" |1| Buy Ticket")
  print(" |2| Leaderboard")
  choice = input("\n> ")
  if choice == "1":
    buyTicket()
  elif choice == "2":
    leaderboard()

Game()
1 Like

So you’re storing a list of users, where are you storing the actual scores?

1 Like

@QwertyQwerty88 oh I forgot about that let me change it.

1 Like

@QwertyQwerty88 I store it here:

if username in db:
    money = db[username]["money"]
    takeAwayAmount = db[username]["takeAwayAmount"]
else:
    db[username] = {"money": 100, "takeAwayAmount" : 5}
    takeAwayAmount = db[username]["takeAwayAmount"]
    money = db[username]["money"]
    game = []
    game.append(username)
    # store for leaderboard
    db["game"] = {"username" : username, "money" : money}
1 Like

So you store one user, and share money for all users :face_with_raised_eyebrow:

Maybe do this:

db["game"][username] = money

And change leaderboard to

def leaderboard():
  e.clear()
  # Sort user array
  users = dict(sorted(db["game"].items(), key=lambda a: a[1], reverse=True)).items()
  # Format the users
  leaderboard = "| "
  for user, money in users:
    leaderboard += f"{user}: {money} | "
  # Output
  print(leaderboard)
  enter()
  Game()
4 Likes

@QwertyQwerty88 it doesn’t work it says this about the db: Object of type “None” is not subscriptable

1 Like

I think you need to clear the db (db.clear())

1 Like

@QwertyQwerty88 I nuked it does that work?

1 Like

I mean yeah, but you could have just cleared it. Try running and see if there are any errors?

1 Like

you need a server that can store all of them globally.

4 Likes

Yeah, cause ghost forks and stuff. Maybe I should have started with that…

3 Likes

@QwertyQwerty88 no I nuked it before running, this the error I get:

1 Like

After you import the db:

# Set db[game] if it doesn't already exist
db["game"] = db.get("game", {})
2 Likes

@QwertyQwerty88 thank you so much it works now!

2 Likes

why are you using globals in a class?

1 Like

@Sky Im not anymore I was just testing something when it wasn’t working.

2 Likes

alright, I was just asking because you don’t use globals in classes. Well, I mean you could but I don’t think it’s feasible.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.