Telegram-bot as an server for multiplayer games

MTProto protocol can be used to interact with your own authoritarian server for a turn-based multiplayer games, if you do it in the form of telegram-bot. Below is shown full code of the server multiplayer game Tic-tac-toe, which I wrote in 10 minutes, using one of the public libraries Telegram Bot API. Just as easily, you can write in any other language (PHP, Java, Node.js, Python, Ruby, Go, Lua, Haskell, Scala, ...)

using System.Collections.Generic;
 
using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums;
 
namespace Telegram.Bot.TicTacToe
{
    class Program
    {
        private static readonly TelegramBotClient Bot = new TelegramBotClient("");
 
        static void Main(string[] args)
        {
            Bot.OnMessage += BotOnMessageReceived;
            Bot.OnReceiveError += BotOnReceiveError;
            
            Bot.StartReceiving();
            System.Console.ReadLine();
            Bot.StopReceiving();
        }
 
        private static void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs)
        {
            System.Diagnostics.Debugger.Break();
        }
        
        private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            var message = messageEventArgs.Message;
 
            if (message == null || message.Type != MessageType.TextMessage) return;
 
 
            switch (message.Text)
            {
                case "9":
                    await Bot.SendTextMessageAsync(message.Chat.Id, GetGame(message.Chat.Id).Print());
                    break;
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                    Game game = GetGame(message.Chat.Id);
                    
                    string error = game.MakeTurn(message.Chat.Id, System.Int32.Parse(message.Text));
 
                    if (error != null)
                    {
                        await Bot.SendTextMessageAsync(message.Chat.Id, error);
                    }
                    else
                    {
                        await Bot.SendTextMessageAsync(game.users[0], game.Print());
                        if (game.users[1] != default(long))
                            await Bot.SendTextMessageAsync(game.users[1], game.Print());
 
                        if (game.IsFull() || game.CheckWin() != 2)
                            RemoveGame(game);
                    }
                    break;
                default:
                    await Bot.SendTextMessageAsync(message.Chat.Id, "bad_request");
                    break;
            }
        }
        
        
        private static List games = new List();
        
        private static Game GetGame(long id)
        {
            foreach (var game in games)
            {
                if (game.users[0] == id || game.users[1] == id)
                    return game;
            }
            foreach (var game in games)
            {
                if (game.users[1] == default(long))
                {
                    game.users[1] = id;
                    return game;
                }
            }
            var newgame = new Game(id);
            games.Add(newgame);
            
            return newgame;
        }
        private static void RemoveGame(Game game)
        {
            games.Remove(game);
        }
        // 0 1 2
        // 3 4 5
        // 6 7 8
        class Game
        {
            int turn = 0;
            public long[] users = new long[2];
            private int[] board = new int[9] { 2, 2, 2, 2, 2, 2, 2, 2, 2 };
            
            public Game(long id)
            {
                users[0] = id;
            }
            public bool IsFull()
            {
                for (int i = 0; i < 9; i++)
                {
                    if (board[i] == 2)
                        return false;
                }
                return true;
            }
            public int CheckWin()
            {
                if (board[4] != 2 && ((board[0] == board[4] && board[8] == board[4]) || (board[1] == board[4] && board[7] == board[4]) || (board[2] == board[4] && board[6] == board[4]) || (board[5] == board[4] && board[3] == board[4])))
                    return board[4];
                if (board[0] != 2 && ((board[0] == board[1] && board[2] == board[0]) || (board[0] == board[3] && board[6] == board[0])))
                    return board[0];
                if (board[8] != 2 && ((board[8] == board[5] && board[2] == board[0]) || (board[8] == board[7] && board[6] == board[0])))
                    return board[8];
                return 2;
            }
            public string MakeTurn(long id, int i)
            {
                if (id != users[turn])
                    return "bad_user";
                if (board[i] != 2)
                    return "bad_cell";
                board[i] = turn;
                if (turn == 0)
                    turn++;
                else
                    turn--;
                return null;
            }
            public string Print()
            {
                return string.Join("", board);
            }
        }
    }
}

This code server multiplayer game Tic-Tac-Toe. Nothing more, written in 10 minutes, including the time to download the library for working with API (https://github.com/MrRoundRobin/telegram.bot).
You do not need to think about such things as user authentication, the visibility of the IP of your server, etc. This whole part will assume Telegram Cloud.

Client side?

Work on the protocol MTProto on the client side is easily implemented using the TUN library.

Demo Videos



PayPal, WMZ, Alipay,...

BestChange.com monitors automatic electronic currency exchangers. The service permanently monitors the largest and most reliable exchangers for you to always know at what exchanger you can exchange one electronic currency for another at the best rate. The exchange rates and currency reserves are updated every 5 seconds for all exchangers in the list.