Online player score ranking

Last Updated: Jul 14, 2017

Scenario introduction

ApsaraDB for Redis functions basically the same as Redis. You can use this product to create a ranking function for an online game easily.

Sample code

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Set;
  4. import java.util.UUID;
  5. import redis.clients.jedis.Jedis;
  6. import redis.clients.jedis.Tuple;
  7. public class GameRankSample {
  8. static int TOTAL_SIZE = 20;
  9. public static void main(String[] args)
  10. {
  11. //Connection information, obtained from the console
  12. String host = "xxxxxxxxxx.m.cnhz1.kvstore.aliyuncs.com";
  13. int port = 6379;
  14. Jedis jedis = new Jedis(host, port);
  15. try {
  16. //Instance password
  17. String authString = jedis.auth("password");//password
  18. if (!authString.equals("OK"))
  19. {
  20. System.err.println("AUTH Failed: " + authString);
  21. return;
  22. }
  23. //Key
  24. String key = "Game name:Keep Running, Ali!";
  25. //Clear any data that already exists
  26. jedis.del(key);
  27. //Generate several simulated players
  28. List<String> playerList = new ArrayList<String>();
  29. for (int i = 0; i < TOTAL_SIZE; ++i)
  30. {
  31. //Randomly generate an ID for each player
  32. playerList.add(UUID.randomUUID().toString());
  33. }
  34. System.out.println("Input all players");
  35. //Record the score for each player
  36. for (int i = 0; i < playerList.size(); i++)
  37. {
  38. //Randomly generate numbers as the scores of the simulated players
  39. int score = (int)(Math.random()*5000);
  40. String member = playerList.get(i);
  41. System.out.println("Player ID:" + member + ", Player score: " + score);
  42. //Add the player ID and score to the SortedSet of the corresponding key
  43. jedis.zadd(key, score, member);
  44. }
  45. //Print out the ranking of all players
  46. System.out.println();
  47. System.out.println(" "+key);
  48. System.out.println(" All players ranking ");
  49. //Obtain the sorted list of players from the SortedSet of the corresponding key
  50. Set<Tuple> scoreList = jedis.zrevrangeWithScores(key, 0, -1);
  51. for (Tuple item : scoreList) {
  52. System.out.println("Player ID:"+item.getElement()+", Player score:"+Double.valueOf(item.getScore()).intValue());
  53. }
  54. //Print out Top 5 players
  55. System.out.println();
  56. System.out.println(" "+key);
  57. System.out.println(" Top players");
  58. scoreList = jedis.zrevrangeWithScores(key, 0, 4);
  59. for (Tuple item : scoreList) {
  60. System.out.println("Player ID:"+item.getElement()+", Player score:"+Double.valueOf(item.getScore()).intValue());
  61. }
  62. //Print out a list of specific players
  63. System.out.println();
  64. System.out.println(" "+key);
  65. System.out.println(" players with scores from 1000 to 2000");
  66. //Obtain a list of players with scores from 1000 to 2000 from the SortedSet of the corresponding key
  67. scoreList = jedis.zrangeByScoreWithScores(key, 1000, 2000);
  68. for (Tuple item : scoreList) {
  69. System.out.println("Player ID:"+item.getElement()+", Player score:"+Double.valueOf(item.getScore()).intValue());
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }finally{
  74. jedis.quit();
  75. jedis.close();
  76. }
  77. }
  78. }

Output

After you access the ApsaraDB for Redis instance with the correct address and password and run the above Java code, the following output is displayed:

  1. Enter all players
  2. Player ID: 9193e26f-6a71-4c76-8666-eaf8ee97ac86; score: 3860
  3. Player ID: db03520b-75a3-48e5-850a-071722ff7afb; score: 4853
  4. Player ID: d302d24d-d380-4e15-a4d6-84f71313f27a; score: 2931
  5. Player ID: bee46f9d-4b05-425e-8451-8aa6d48858e6; score: 1796
  6. Player ID: ec24fb9e-366e-4b89-a0d5-0be151a8cad0; score: 2263
  7. Player ID: e11ecc2c-cd51-4339-8412-c711142ca7aa; score: 1848
  8. Player ID: 4c396f67-da7c-4b99-a783-25919d52d756; score: 958
  9. Player ID: a6299dd2-4f38-4528-bb5a-aa2d48a9f94a; score: 2428
  10. Player ID: 2e4ec631-1e4e-4ef0-914f-7bf1745f7d65; score: 4478
  11. Player ID: 24235a85-85b9-476e-8b96-39f294f57aa7; score: 1655
  12. Player ID: e3e8e1fa-6aac-4a0c-af80-4c4a1e126cd1; score: 4064
  13. Player ID: 99bc5b4f-e32a-4295-bc3a-0324887bb77e; score: 4852
  14. Player ID: 19e2aa6b-a2d8-4e56-bdf7-8b59f64bd8e0; score: 3394
  15. Player ID: cb62bb24-1318-4af2-9d9b-fbff7280dbec; score: 3405
  16. Player ID: ec0f06da-91ee-447b-b935-7ca935dc7968; score: 4391
  17. Player ID: 2c814a6f-3706-4280-9085-5fe5fd56b71c; score: 2510
  18. Player ID: 9ee2ed6d-08b8-4e7f-b52c-9adfe1e32dda; score: 63
  19. Player ID: 0293b43a-1554-4157-a95b-b78de9edf6dd; score: 1008
  20. Player ID: 674bbdd1-2023-46ae-bbe6-dfcd8e372430; score: 2265
  21. Player ID: 34574e3e-9cc5-43ed-ba15-9f5405312692; score: 3734
  22. Game Name: Keep Running, Ali!
  23. Full Player Ranking
  24. Player ID: db03520b-75a3-48e5-850a-071722ff7afb; score: 4853
  25. Player ID: 99bc5b4f-e32a-4295-bc3a-0324887bb77e; score: 4852
  26. Player ID: 2e4ec631-1e4e-4ef0-914f-7bf1745f7d65; score: 4478
  27. Player ID: ec0f06da-91ee-447b-b935-7ca935dc7968; score: 4391
  28. Player ID: e3e8e1fa-6aac-4a0c-af80-4c4a1e126cd1; score: 4064
  29. Player ID: 9193e26f-6a71-4c76-8666-eaf8ee97ac86; score: 3860
  30. Player ID: 34574e3e-9cc5-43ed-ba15-9f5405312692; score: 3734
  31. Player ID: cb62bb24-1318-4af2-9d9b-fbff7280dbec; score: 3405
  32. Player ID: 19e2aa6b-a2d8-4e56-bdf7-8b59f64bd8e0; score: 3394
  33. Player ID: d302d24d-d380-4e15-a4d6-84f71313f27a; score: 2931
  34. Player ID: 2c814a6f-3706-4280-9085-5fe5fd56b71c; score: 2510
  35. Player ID: a6299dd2-4f38-4528-bb5a-aa2d48a9f94a; score: 2428
  36. Player ID: 674bbdd1-2023-46ae-bbe6-dfcd8e372430; score: 2265
  37. Player ID: ec24fb9e-366e-4b89-a0d5-0be151a8cad0; score: 2263
  38. Player ID: e11ecc2c-cd51-4339-8412-c711142ca7aa; score: 1848
  39. Player ID: bee46f9d-4b05-425e-8451-8aa6d48858e6; score: 1796
  40. Player ID: 24235a85-85b9-476e-8b96-39f294f57aa7; score: 1655
  41. Player ID: 0293b43a-1554-4157-a95b-b78de9edf6dd; score: 1008
  42. Player ID: 4c396f67-da7c-4b99-a783-25919d52d756; score: 958
  43. Player ID: 9ee2ed6d-08b8-4e7f-b52c-9adfe1e32dda; score: 63
  44. Game Name: Keep Running, Ali!
  45. Top Players
  46. Player ID: db03520b-75a3-48e5-850a-071722ff7afb; score: 4853
  47. Player ID: 99bc5b4f-e32a-4295-bc3a-0324887bb77e; score: 4852
  48. Player ID: 2e4ec631-1e4e-4ef0-914f-7bf1745f7d65; score: 4478
  49. Player ID: ec0f06da-91ee-447b-b935-7ca935dc7968; score: 4391
  50. Player ID: e3e8e1fa-6aac-4a0c-af80-4c4a1e126cd1; score: 4064
  51. Game Name: Keep Running, Ali!
  52. Players with Scores from 1000 to 2000
  53. Player ID: 0293b43a-1554-4157-a95b-b78de9edf6dd; score: 1008
  54. Player ID: 24235a85-85b9-476e-8b96-39f294f57aa7; score: 1655
  55. Player ID: bee46f9d-4b05-425e-8451-8aa6d48858e6; score: 1796
  56. Player ID: e11ecc2c-cd51-4339-8412-c711142ca7aa; score: 1848
Thank you! We've received your feedback.