Thursday, 19 September 2013

Highscores - Best time period for a user

Highscores - Best time period for a user

I have a table which keeps track of data from a game, here is an example
of the table:
id | player_name | date | score | kills
1 | test1 | 2013-01-01 00:00:00 | 10000 | 200
2 | test1 | 2013-01-01 00:01:00 | 12000 | 300
I have a leaderboards for players, it ranks people who gain the most
score/kills, etc. in a certain time period. At the moment I have only got
it so that it ranks players in the previous 24 hours. I am doing this by
selecting the first and last records in a specified time period and then
subtracting them to get the difference.
This is my current query:
SELECT date, score FROM datapoints WHERE player_name = :player AND date =
(SELECT MIN(date) FROM datapoints WHERE player_name = :player AND date >
DATE_SUB(CURDATE(), INTERVAL 24 HOUR))
UNION ALL
SELECT date, score FROM datapoints WHERE player_name = :player AND date =
(SELECT MAX(date) FROM datapoints WHERE player_name = :player AND date >
DATE_SUB(CURDATE(), INTERVAL 24 HOUR))
After subtracting I use the PHP arsort() function to order them and then
display them on the page.
However, I want to add another feature. I want to be able to see the on
which day was the users best day for score/kills.
I have been thinking of how I could possibly do it and one was was using
the above query but having a loop for each day and taking out the best
day, however this probably isn't very efficient and I was wondering, if
there was a better way of doing this?

No comments:

Post a Comment