Building Game Leaderboards with Redis on Alibaba Cloud

Objective

Prerequisites

Implementation

  1. Setup Elastic Compute Service (ECS) and ApsaraDB for Redis. This tutorial uses Ubuntu 16.04 as the OS, but the solution itself can be done on any OS. The code may differ slightly based on your OS of choice.
  2. Login into the ECS server:
  • ssh -i root@
  1. Run steps to install the environment:
  • rm /usr/bin/python # Change python into version 3 ln -s /usr/bin/python3 /usr/bin/python apt-get update # Update Ubuntu export LC_ALL=C # Set Locale apt-get install python3-pip # Install pip pip install redis # Install python-redis apt-get install apache2 # Install apache mkdir /var/www/python # Set Environment a2dismod mpm_event a2enmod mpm_prefork cgi
  1. Replace /etc/apache2/sites-enabled/000-default.conf
  • ----- <VirtualHost *:80> DocumentRoot /var/www/python <Directory /var/www/python> Options +ExecCGI DirectoryIndex leaderboards.py </Directory> AddHandler cgi-script .py ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> -----
  1. Put the file in /var/www/leaderboards.py, and edit the settings on line 8 (source code below).
  2. Edit permission in the file.
  • chmod 755 /var/www/python/leaderboards.py
  1. Restart Apache
  • service apache2 restart
  1. Go to your web browser and type the public IP address. You should see it working now.

Code Explanation

  1. Connect to Redis (Line 8):
  • r = redis.StrictRedis(host='', port=6379, db=0, password='')
  1. For this to work correctly, you’ll need to update the hostname and password. You can get the from the “Connection Address” during ApsaraDB for Redis creation. The is the password you set during creation as well.
  1. Adding score to leaderboard (Line 16):
  • r.zincrby(leaderboardName, gamer, random.randint(1,1000000))
  1. LeaderboardName is the key that you set for your leaderboard’s name, gamer is the username or id of the gamer, and the last parameter is where you put the score (which is in this case is a random number).
  2. Get the top 10 highest score (Line 19):
  • r.zrange(leaderboardName, 0, 9, desc=True, withscores=True)
  1. LeaderboardName is the key that you set for your leaderboard’s name, the second parameter is from which rank do you want to start (0 is the start), and the third parameter is where do you want to stop (-1 to show until the last). The value desc=True is to make the leaderboard’s sorted in descending order (False by default) while withscores=True is to return the names along with the scores (False by default).
  2. Get the current player’s rank (Line 30):
  • r.zrevrank(leaderboardName, gamer)+1
  1. LeaderboardName is the key that you set for your leaderboard’s name and gamer is the username or id of the gamer. You’ll need to add one (+1) because the rank starts at 0 instead of 1 in the database.
  2. Get the current player (or anyone)’s score (Line 34):
  • r.zscore(leaderboardName, gamer)
  1. LeaderboardName is the key that you set for your leaderboard’s name and gamer is the username or id of the gamer.

Running the Code

Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Real-Time Database Backup to OSS on the OSS Console

What is Enterprise Architecture?

How to use Git Client to Trace Commit History

Scrum Estimation — How we size up the User Story

Web Scraping using Scrapy in python

5 Tips to Make Your Website Faster

How to Scale Out Your ClickHouse Cluster

ubuntu:21.10 and fedora:35 do not work on the latest Docker (20.10.9)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Apache Kafka Guide

Experiences on building a status incident management page from private to open-source

Building a landing page with React, Flask and PostgreSQL on AWS in 2022 — A definitive guide —…

Make postman request

Integrate AWS GraphQL in your Ionic/Angular applications using Amplify.