示例#1
0
        private void Run()
        {
            if (this.OnMessage != null)
            {
                NetworkStream networkStream = new NetworkStream(this.socket);

                Message message;

                while (this.isRunning == true)
                {
                    try
                    {
                        message = Message.Deserialize(networkStream);
                        this.OnMessage.Invoke(this, new MessageEventArgs()
                        {
                            Message = message
                        });
                    }
                    catch
                    {
                        this.OnKill.Invoke(this, new KillEvent()
                        {
                            threadClient = this
                        });
                        this.isRunning = false;
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// When A Message is Received from a client
        /// </summary>
        /// <param name="packet"></param>
        private void TcpListener_MessageReceivedEvent(Message packet)
        {
            var rawMessage      = Message.Deserialize(packet);
            var receivedMessage = rawMessage[0];
            var player          = GameStore.Instance.Game.GetPlayerFromIpEndPoint(packet.Sender);

            Write("Received from " + player.PlayerAddress + " -> " + packet.Data, Color.OrangeRed);
            switch (_serverState)
            {
            case GameModes.ConnectionOpen:
                GameStore.Instance.Game.AttackOrder.Clear();
                GameController.Instance.ClearStack();

                //Is the client already connected?
                //The client already has a name dont change
                if (player.PlayerName != null)
                {
                    break;
                }
                Write("Players : " + GameStore.Instance.Game.PlayerList.Count, Color.Blue);
                player.PlayerName = receivedMessage;
                break;

            case GameModes.ConnectionClosed:
                break;

            case GameModes.GameInitializing:
                break;

            case GameModes.GameStarted:
                //Message Received as a ability
                var used = int.Parse(receivedMessage);

                Write("||" + player.PlayerName + "|| Used: " + (Abilities)used, Color.Red);
                GameStore.Instance.Game.AttackOrder.Add(player);
                GameController.Instance.AddAttack((Abilities)used);

                tcpListener.Send(player,
                                 $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message} You are now waiting for other Players to play {CodeMessages.PLAYER_WAITING}");

                if (GameStore.Instance.Game.AttackOrder.Count == GameStore.Instance.Game.PlayerList.Count)
                {
                    Write("Round Ended", Color.Red);
                    //All Players Are Ready!
                    //Show Who Won and Who Lost
                    var state = GameController.Instance.Battle();

                    if (state != BattleState.None)
                    {
                        Write("Round Finished", Color.Red);
                        var p1 = GameStore.Instance.Game.AttackOrder[0];
                        var p2 = GameStore.Instance.Game.AttackOrder[1];
                        if (state == BattleState.Lost)
                        {
                            Write(p1.PlayerName + " Lost", Color.Red);
                            Write(p2.PlayerName + " Won", Color.Green);
                            tcpListener.Send(p1, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {state} {CodeMessages.MESSAGE_SPLITER_CODE}");
                            tcpListener.Send(p2, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {BattleState.Won} {CodeMessages.MESSAGE_SPLITER_CODE}");
                        }
                        if (state == BattleState.Won)
                        {
                            Write(p1.PlayerName + " Won", Color.Green);
                            Write(p2.PlayerName + " Lost", Color.Red);
                            tcpListener.Send(p1, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {state} {CodeMessages.MESSAGE_SPLITER_CODE}");
                            tcpListener.Send(p2, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {BattleState.Lost} {CodeMessages.MESSAGE_SPLITER_CODE}");
                        }
                        if (state == BattleState.Draw)
                        {
                            Write(p1.PlayerName + " Draw", Color.Red);
                            Write(p2.PlayerName + " Draw", Color.Green);
                            tcpListener.Send(p1, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {state} {CodeMessages.MESSAGE_SPLITER_CODE}");
                            tcpListener.Send(p2, $"{CodeMessages.PUBLIC_SERVER_MESSAGE.Message}{CodeMessages.MESSAGE_SPLITER_CODE} State {CodeMessages.MESSAGE_SPLITER_CODE} {state} {CodeMessages.MESSAGE_SPLITER_CODE}");
                        }
                    }
                    _serverState = GameModes.RoundEnded;
                    tcpListener.SendAll(CodeMessages.GAME_ROUND_ENDED.Message);
                }
                break;

            case GameModes.RoundEnded:
                //A player replied with success
                readyCounter++;
                if (readyCounter == 2)
                {
                    //Get Stuff Ready
                    Write("Players can now Play", Color.GreenYellow);
                    //NOT USING THIS AT THE MOMENT
                    //tcpListener.SendAll($"{CodeMessages.INTERNAL_SERVER_MESSAGE.Message}canplay");
                    GameStore.Instance.Game.AttackOrder.Clear();
                    readyCounter = 0;
                    GameStore.Instance.Game.OnGameStateChangeEvent(GameModes.GameStarted);
                    tcpListener.SendAll(CodeMessages.GAME_STARTING.Message);
                }
                break;

            case GameModes.GameEnded:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }