示例#1
0
        private void _actionLogin(OBJ_Client from, MSG_MESSAGE message)
        {
            MSG_Login login = (MSG_Login)message.Values;

            from.Login = login.Name;
            this._log("Client : " + from.IP + ":" + from.Port.ToString() + "connected on \"" + from.Login + "\"");
        }
示例#2
0
        private void _actionCreateGame(OBJ_Client from, MSG_MESSAGE message)
        {
            // if user is Logged
            if (from.Login != null)
            {
                Guid     guid       = Guid.NewGuid();
                MSG_Game createGame = (MSG_Game)message.Values;

                while (this._games.ContainsKey(guid))
                {
                    guid = Guid.NewGuid();
                }

                OBJ_Game game = new OBJ_Game(guid, from, createGame);
                this._games.Add(guid, game);

                from.send(new MSG_MESSAGE("GAME_JOINED", game));
            }

            // User not logged.
            else
            {
                from.send(new MSG_MESSAGE("ERROR", "Please connect before"));
            }
        }
示例#3
0
        private void _action(OBJ_Client from, MSG_MESSAGE message)
        {
            switch (message.Message)
            {
            case "CREATE_GAME":
                this._actionCreateGame(from, message);
                break;

            case "ERROR":
                this._actionError(from, message);
                break;

            case "JOIN_GAME":
                this._actionJoinGame(from, message);
                break;

            case "LOGIN":
                this._actionLogin(from, message);
                break;

            case "SEND_GAMES_LIST":
                this._actionSendGamesList(from, message);
                break;

            default:
                break;
            }
        }
示例#4
0
        public MSG_MESSAGE listen()
        {
            String      read    = this._reader.ReadLine();
            MSG_MESSAGE message = null;

            if (null == read)
            {
                this._closeClient();
            }

            else
            {
                message = this._deserialiseMessage(read);
            }

            return(message);
        }
示例#5
0
        private void _actionSendGamesList(OBJ_Client from, MSG_MESSAGE message)
        {
            // if user is Logged
            if (from.Login != null)
            {
                List <MSG_Game> games = new List <MSG_Game>();
                foreach (KeyValuePair <Guid, OBJ_Game> game in this._games)
                {
                    games.Add(game.Value.MSG_Game);
                }

                from.send(new MSG_MESSAGE("GAMES_LIST", games));
            }

            // User not logged.
            else
            {
                from.send(new MSG_MESSAGE("ERROR", "Please connect before"));
            }
        }
示例#6
0
        private void _actionJoinGame(OBJ_Client from, MSG_MESSAGE message)
        {
            // if user is Logged
            if (from.Login != null)
            {
                MSG_Game game = (MSG_Game)message.Values;

                // If game exist.
                if (this._games.ContainsKey(game.ID))
                {
                    OBJ_Game selectedGame = this._games[game.ID];

                    // If game is free
                    if (null == selectedGame.Player2)
                    {
                        selectedGame.Player2 = from;
                        selectedGame.Player1.send(new MSG_MESSAGE("PLAYER_CONNECTED", from.Login));
                        from.send(new MSG_MESSAGE("GAME_JOINED", game));
                    }

                    // if game is full
                    else
                    {
                        from.send(new MSG_MESSAGE("ERROR", "Game is full"));
                    }
                }

                // If game not exist.
                else
                {
                    from.send(new MSG_MESSAGE("ERROR", "Game not exist"));
                }
            }

            // User not logged.
            else
            {
                from.send(new MSG_MESSAGE("ERROR", "Please connect before"));
            }
        }
示例#7
0
        public void HandleClient(object obj)
        {
            // Init client
            TcpClient  tcpClient = (TcpClient)obj;
            OBJ_Client client    = new OBJ_Client(tcpClient);

            // Log connect
            this._log("New Client from : " + client.IP + ":" + client.Port.ToString());

            client.send(new MSG_MESSAGE("WELCOME"));

            // Read from client
            MSG_MESSAGE listen = null;

            while (client.Connected)
            {
                listen = client.listen();

                if (null != listen)
                {
                    this._action(client, listen);
                }
            }
        }
示例#8
0
 private void _actionError(OBJ_Client from, MSG_MESSAGE message)
 {
     this._log("Client : " + from.IP + ":" + from.Port.ToString() + "Error : \"" + message.Values.ToString() + "\"");
 }
示例#9
0
 private String _serialiseMessage(MSG_MESSAGE message)
 {
     return(JsonConvert.SerializeObject(message));
 }
示例#10
0
 public void send(MSG_MESSAGE message)
 {
     this._writer.WriteLine(this._serialiseMessage(message));
     this._writer.Flush();
 }