How To Make A Leaderboard?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

armanbm
Posts: 17
Joined: 17 Nov 2016, 06:32

How To Make A Leaderboard?

Postby armanbm » 09 Mar 2017, 08:21

Hello.

I have connected my user database to sfs.
i have a field named "XP"
i Want a leader board base on this xp. how can i achieve top 10 xp of my-sql database (best performance)?
is it a matter of SQL-Query? or SFS has some build in codes to help me through this?

tanks
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How To Make A Leaderboard?

Postby Lapo » 09 Mar 2017, 08:45

Hi,
your question suggests that you don't have much experience with databases and SQL, am I wrong?

If so I would recommend investing some time studying how MySQL works, learning the basics of SQL and experimenting a bit with it before venturing into more complex stuff.

Once you've done that you can easily apply what you have learned in SmartFoxServer following these tutorials:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item2
http://docs2x.smartfoxserver.com/Develo ... se-recipes

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
armanbm
Posts: 17
Joined: 17 Nov 2016, 06:32

Re: How To Make A Leaderboard?

Postby armanbm » 09 Mar 2017, 11:03

Tanks, but i've already connected MyDb to SmartFox And i have a bunch of users who have "xp" in db.
i've just wonder if sfs already implemented a component like "SignUp Extension" that can make implementing leader board fairy easy.

so i think i should implement leader board like following:

1. create a table named top10 and push first 10 high xp's in it
2. every time i update a user's xp, i should check his/her xp against top 10.

is it the right way?
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How To Make A Leaderboard?

Postby Lapo » 09 Mar 2017, 11:18

armanbm wrote:
1. create a table named top10 and push first 10 high xp's in it
2. every time i update a user's xp, i should check his/her xp against top 10.

is it the right way?

Yes it is the right way.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: How To Make A Leaderboard?

Postby meanvel » 10 Mar 2017, 00:36

armanbm wrote:so i think i should implement leader board like following:

1. create a table named top10 and push first 10 high xp's in it
2. every time i update a user's xp, i should check his/her xp against top 10.

is it the right way?


Here is a simple way...

Make XP a index in your database, once the users EXP is updated, SELECT with a ascending order by exp LIMIT 9 where exp > user exp to select nine players with exp higher than your player then select your player too. When displaying the values, reverse the array. Viola... Users exp and nine users with better score than him.

Or, you could just select the top nine and the user, then sort and display that.

There is no need to create a top10 table. Just index EXP and do the top 10/20/etc via select and sorting by exp. If you need high performance and your leaderboard gets a lot of hits, just cache the selects and set them to expire after like ten minutes or a one hour cron.
User avatar
meanvel
Posts: 129
Joined: 19 Jan 2017, 14:06

Re: How To Make A Leaderboard?

Postby meanvel » 10 Mar 2017, 13:38

My explanation might have sounded complicated, but it's as simple as this in my DB...

"SELECT * FROM users ORDER BY exp DESC LIMIT 10"

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 54 guests