Build a game leaderboard on Alibaba cloud using Python and Redis

In this guide, you will use ** Python ** and ** Redis ** to build a game leaderboard on ** Alibaba Cloud **.

Purpose

The main idea of this demo is to use the cache layer (Redis) instead of hitting the database to generate a leaderboard in a gaming application. This approach is suitable for large databases that require real-time responses.

Prerequisites

It's not required, but a basic understanding of Python is a plus (sample code is Python). I'm using Redis this time, so it's a good idea to read it to find out what Redis is like.

For information on creating a Redis cluster on Alibaba Cloud, read the separate step-by-step command at this guide (https://www.alibabacloud.com/help/en/doc-detail/26351.htm) and read it. I will omit it here because I can proceed along it. The settings are simple and easy to understand.

State of implementation

  1. Elastic Compute Service (ECS) and [ApsaraDB for Redis](https://www.alibabacloud.com/ Help / ja / doc-detail / 26351.htm) is set up. This tutorial uses Ubuntu 16.04 as the OS, but the solution itself can be any OS. The code may vary slightly depending on the OS selected.
  2. Log in to the ECS server.
ssh -i  root@
  1. Follow the 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

4, 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. Place the file in /var/www/leaderboards.py and edit the settings on line 8 (source code below).
  2. Edit the permissions in the file.
chmod 755 /var/www/python/leaderboards.py
  1. Restart Apache.
service apache2 restart
  1. Access your web browser and enter your public IP address. You need to see if it works.

Code description

Below is a code sample in Python, but I'll explain this further.

image.png

Code description: 1, connect to Redis (8th line):

r = redis.StrictRedis(host='', port=6379, db=0, password='') 

The host name and password must be updated for this to work properly. It can be obtained from "Connection Address" when creating ApsaraDB for Redis. It is also the password set at the time of creation.

image.png

2, add the score to the leaderboard (line 16)

r.zincrby(leaderboardName, gamer, random.randint(1,1000000)) 

LeaderboardName is the key you set for the leaderboard name, gamer is the gamer's username or ID, and the last parameter is where you put the score (in this case a random number).

  1. Get the top 10 with the highest score (line 19).
r.zrange(leaderboardName, 0, 9, desc=True, withscores=True)

LeaderboardName is the key to set the name of the leaderboard, the second parameter is from which rank to start (0 starts) and the third parameter is where to stop (-1 to show to the end). The value desc = True sorts the leaderboards in descending order (False by default).

4, get the ranking of the current player (line 30).

r.zrevrank(leaderboardName, gamer)+1

LeaderboardName is the key you set for the leaderboard name, and gamer is the gamer's username or ID. In the database the rank starts at 0 instead of 1, so you need to add one (+1). 5. Get the score of the current player (or anyone) (line 34).

r.zscore(leaderboardName, gamer) 

LeaderboardName is the key you set for the leaderboard name, and gamer is the gamer's username or ID.

Run code

Below is the expected response when running code on a web server.

image.png

Conclusion

Redis stores data in-memory and can reach the performance of millions of requests per second depending on the maturity of the product. This makes it the perfect database for this use case and other caching needs.

For more information on Redis on Alibaba Cloud, please visit the ApsaraDB for Redis (https://www.alibabacloud.com/en/product/apsaradb-for-redis) page.

Recommended Posts

Build a game leaderboard on Alibaba cloud using Python and Redis
Build a Python + OpenCV environment on Cloud9
Build a Python environment on your Mac using pyenv
Build a Python development environment using pyenv on MacOS
Building a Python environment on a Mac and using Jupyter lab
Build a python3 environment on CentOS7
Build a Chainer environment using CUDA and cuDNN on a p2 instance
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
Build a detonation velocity website with Cloud Run and Python (Flask)
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
A memo with Python2.7 and Python3 on CentOS
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Notes on installing Python3 and using pip on Windows7
Build a python environment with ansible on centos6
Build a Python environment on Mac (Mountain Lion)
Create a web map using Python and GDAL
Build a Python development environment on your Mac
Try using tensorflow ① Build python environment and introduce tensorflow
Create a Mac app using py2app and Python3! !!
Build a Python development environment on Raspberry Pi
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Encrypt ACME on Alibaba Cloud: Build an ACME request and sign the JWS payload
Initial settings for using Python3.8 and pip on CentOS8
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Install Python3 on Mac and build environment [Definitive Edition]
Build a Django development environment using pyenv-virtualenv on Mac
Try creating a compressed file using Python and zlib
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
Build a python virtual environment with virtualenv and virtualenvwrapper
How to deploy a Django application on Alibaba Cloud
Build a machine learning Python environment on Mac OS
Causes and solutions when python sam build fails in a newly created Cloud9 environment
Build a Python extension for E-Cell 4 on Windows 7 (64bit)
Let's make a simple game with Python 3 and iPhone
Build a GVim-based Python development environment on Windows 10 (1) Installation
How to build a Django (python) environment on docker
Explore Alibaba Cloud Function Compute for DevOps using Python 3.0
Build a Python development environment on Mac OS X
Implementing a generator using Python> link> yield and next ()> yield
Build a Python virtual environment using venv (Django + MySQL ①)
Build a python virtual environment with virtualenv and virtualenvwrapper
Speech transcription procedure using Python and Google Cloud Speech API
build Python on Ubuntu
Create a decent shell and python environment on Windows
How to build a Python environment on amazon linux 2
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
Build a python machine learning study environment on macOS sierra
Install LAMP on Amazon Linux 2 and build a WordPress environment.
Build an environment for machine learning using Python on MacOSX
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
IP spoof using tor on macOS and check with python
Setting up a CentOS 7 server hosted on Alibaba Cloud ECS
[Treasure Data] [Python] Execute a query on Treasure Data using TD Client
Build a python data analysis environment on Mac (El Capitan)
Build a GVim-based Python development environment on Windows 10 (2) Basic settings