Is it possible to make Global leaderboards in Python?

I was wondering for a project of mine if there was a way to make global leaderboards in python? I am making a new Python project which is going really well so if you can help I would really appreciate it. Thanks for reading. Project Link

2 Likes

Hey @HenryMiles3!

If you are using ReplitDB I am currently doing something similar, and right now it is currently undoable to make it secure. People are working on it though.

3 Likes

If you want to have a publicly-available leaderboard (meaning anyone can read its contents), you can simply save your data on a file and use that to create the leaderboard.
The simplest solution would be using JSON/CSV to save the data to a file in your repl for later read, but actual best way would be to use a database, and sqlite should be more than enough for your use case. You can then use SQL queries to ask the database for the top n players. An example of said query can be something like this:

SELECT name, points FROM players
ORDER BY points DESC
LIMIT 10;

Edit: If you want it private, there’s no way to do that on Replit as far as I know. You will need to use a third-party tool like a cloud or self-hosted database, but that’s a little more complicated and it seems like you’re still a beginner when it comes to data

1 Like

I think the main things I need to know are how to save variable so that they don’t reset when you restart the project and how to order them into a table.

The only problem with that is that I don’t know any JSON of CSV.

1 Like

well, for one you can move all logic such as random number generation and db operations to the server side, and make it so that the client ghost-fork sends a request to the server about drawing a lottery card, and then the server does everything then sends the results back to the client while doing everything with the db.

2 Likes

There’s always something to learn :slightly_smiling_face:
Explained very simply, JSON is a highly compatible language to save data. As the name suggests (JavaScript Object Notation), it stores everything as objects - the Python equivalent of a dictionary.
An object is defined between two curly brackets, and contains key-value pairs, separating the key and the value with a colon, and the various pairs with commas.
A very simple user object could be this:

{
    name: "John Doe",
    age: 25
}

As you can see, values can be strings, but also numbers, lists or other objects. For example, let’s say that our friend John has two siblings. We could represent that like so:

{
    name: "John Doe",
    age: 25,
    siblings:["Mark", "Diana"]
}

Don’t worry about the syntax, since Python will deal with it. There’s a module called json with which you can transform anything in Python to JSON and anything JSON to Python (json.load, json.loads, json.dump, json.dumps). You can use those to load and read your players

1 Like

The question:

Global Leaderboards means a leaderboard that is shared by all users.

When a user runs your Console Repl, they “ghost-fork” it, meaning that any changes happening on the user’s end does not have an effect on the master repl. Implementations of saving data such as JSON and/or SQL can allow for persistent storage, but doesn’t answer the OP’s question.

It’s okay! As long as you can learn, then there is no problem not knowing about it at the moment.

Yes, that is correct. However, I would like to point out that instead of saving JSON to a file (which would have no effect on the main repl and/or other user’s ghost-fork), you can upload the JSON to your database proxy (web)server as a string (json.dumps(myObject) # String containing json) and have your web server update the db, because webservers don’t ghost-fork.

Similar to @SalladShooter’s problem, your db proxy would also need to have a verification process. First, use Repl Identity (https://blog.replit.com/repl-identity) to verify the client’s authenticity, and then move all critical logic and operations (such as drawing random numbers for a lottery game, or managing player HP and lives) to the db server, essentially making it so that the client’s “ghost-fork” repl is just taking data from the database proxy webserver and displaying it in a nice format. Overall , the whole process is very complicated, and I would suggest creating a web game or something else to avoid the need for verification and code authenticity.

2 Likes

I interpreted “global” as “available to everyone”; If we go with your interpretation, then yes - what I said is unrelated

2 Likes

Yes what I meant by global is that it is open to everybody.

1 Like

@Nicolello with how yours is it isn’t available to everyone only to your account. python660’s way is trying to solve the problem (that a lot of people have been having recently).

2 Likes