Ejemplo n.º 1
0
        public static void MessageProcessor(Encapsulation message, Player player)
        {
            switch (message.MessageType)
            {
            case MessageType.Disconnect:
                Console.WriteLine($"Connection with Client {player.id} closed !");
                Response.DisconnectResponse(player);
                Thread.Sleep(1000);
                player.tcpClient.Close();
                break;

            case MessageType.Discover:

                Discover client = Encapsulation.Deserialize <Discover>(message);
                player.name = client.UserName;
                Console.WriteLine($"Player username is : {player.name}");
                break;

            case MessageType.Searching:
                Console.WriteLine($"Player : {player.name} is searching for a game ");
                player.status = Status.Searching;
                break;

            case MessageType.Answer:
                Console.WriteLine($"Player {player.id} : {player.name} answered ! ");
                SaveRound(player, message);
                break;
            }
        }
Ejemplo n.º 2
0
        private static void setGameInfo(Encapsulation message)
        {
            var gameInfo = Encapsulation.Deserialize <SharedClasses.GameInfo>(message);

            GameInfo.myId         = gameInfo.playerId;
            GameInfo.UniqueId     = gameInfo.UniqueId;
            GameInfo.RoundGuid    = gameInfo.FirstRoundGuid;
            GameInfo.BestOf       = gameInfo.BestOf;
            GameInfo.TimeToAnswer = gameInfo.TimeToAnswer;
            GameInfo.EnnemyName   = gameInfo.EnnemyName;
        }
Ejemplo n.º 3
0
        private static void gameEndProcessor(Form form, Encapsulation message)
        {
            var gameEnd = Encapsulation.Deserialize <GameEnd>(message);

            if (gameEnd.PlayerWinStatus == WinStatus.Win)
            {
                openAppWindow.OpenWindow(form, new Form_Win());
            }
            else
            {
                openAppWindow.OpenWindow(form, new Form_Lose());
            }
        }
Ejemplo n.º 4
0
        private static void setNextRoundInfo(Form form, Encapsulation message)
        {
            var roundInfo = Encapsulation.Deserialize <RoundInfo>(message);

            GameInfo.RoundGuid = roundInfo.UniqueId;

            if (roundInfo.PlayerWinStatus != WinStatus.Tie)
            {
                string label_name = (roundInfo.PlayerWinStatus == WinStatus.Win) ? "lbl_youScore" : "lbl_ennemyScore";
                form.Invoke(new MethodInvoker(delegate
                {
                    form.Controls.Find(label_name, true).FirstOrDefault().Text = $"{(int.Parse(form.Controls.Find(label_name, true).FirstOrDefault().Text) + 1)}";
                }));
            }
        }
Ejemplo n.º 5
0
        private void ReadMessage()
        {
            byte[] buffer     = new byte[Constants.BUFFER_SIZE];
            int    byteLength = 0;

            try
            {
                while ((byteLength = _client.tcpClient.Client.Receive(buffer, 0, Constants.BUFFER_SIZE, SocketFlags.None)) > 0)
                {
                    byte[] message = new byte[Constants.BUFFER_SIZE];
                    Array.Copy(buffer, message, byteLength);
                    buffer = new byte[Constants.BUFFER_SIZE];
                    Encapsulation encapsulation = Encapsulation.Deserialize(message);
                    Processor.MessageProcessor(this, encapsulation, _client);
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 6
0
        private void HandleClient(object obj)
        {
            Player player = (Player)obj;

            byte[] buffer     = new byte[Constants.BUFFER_SIZE];
            int    byteLength = 0;

            Console.WriteLine($"Client {player.id} connected");
            Response.DiscoverResponse(player);
            try
            {
                while (player.tcpClient != null && (byteLength = player.tcpClient.GetStream().Read(buffer, 0, Constants.BUFFER_SIZE)) > 0)
                {
                    Encapsulation message = Encapsulation.Deserialize(buffer);
                    Processor.MessageProcessor(message, player);
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 7
0
        public static void SaveRound(Player player, Encapsulation message)
        {
            Answer answer       = Encapsulation.Deserialize <Answer>(message);
            Game   current_game = TemporaryStorage.AllGames.FirstOrDefault(g => g.UniqueId == answer.GameId);

            if (current_game.Rounds.Count != 0)
            {
                if (current_game.Rounds.Last().UniqueId == answer.RoundId)
                {
                    SaveAnswer(current_game.Rounds.Last(), answer, player);
                }
                else
                {
                    AddRound(current_game, answer, player);
                }
            }
            else
            {
                AddRound(current_game, answer, player);
            }

            CheckWhoWin(current_game);
        }