예제 #1
0
        private void onGameEnd(object sender, EndGameEventArgs eventArgs)
        {
            ranking.AddTeam(eventArgs.LoserTeam);
            ranking.AddWinForTeam(eventArgs.WinnerTeam);
            ranking.Print();

            RegisterNewGame(eventArgs.Handler);
        }
        public async Task HandleMessage(PlacePiece message, Socket handler)
        {
            if (!ValidateMessage(message))
            {
                return;
            }
            string resp = "";
            await Task.Delay((int)GameMasterClient.Settings.ActionCosts.PlacingDelay);

            Wrapper.Player currentPlayer = Players.Single(p => p.Guid == message.playerGuid);
            GameMasterClient.Logger.Log(message, currentPlayer);
            var dmb = new DataMessageBuilder(currentPlayer.Id);

            lock (BoardLock)
            {
                Wrapper.Piece carriedPiece =
                    Pieces.SingleOrDefault(
                        pc =>
                        pc.PlayerId == currentPlayer.Id);

                if (carriedPiece != null && !IsPlayerInGoalArea(currentPlayer))
                {
                    Wrapper.Piece lyingPiece =
                        Pieces.FirstOrDefault(
                            pc =>
                            pc.PlayerId != currentPlayer.Id && pc.Location.Equals(currentPlayer.Location));

                    if (lyingPiece == null) //leaving our piece there
                    {
                        carriedPiece.PlayerId   = null;
                        carriedPiece.Location.x = currentPlayer.Location.x;
                        carriedPiece.Location.y = currentPlayer.Location.y;
                        (Board.Fields[carriedPiece.Location.x, carriedPiece.Location.y] as Wrapper.TaskField).PieceId =
                            carriedPiece.Id;
                        Board.UpdateDistanceToPiece(Pieces);
                    }
                    else //destroying piece
                    {
                        Pieces.Remove(carriedPiece);
                    }
                    Wrapper.TaskField tf =
                        Board.Fields[currentPlayer.X, currentPlayer.Y] as Wrapper.TaskField;
                    resp = dmb.AddTaskField(tf.SchemaField as TaskField).GetXml();
                    GameMasterClient.Connection.SendFromClient(handler, resp);
                    return;
                }
                if (carriedPiece == null ||
                    carriedPiece.Type == PieceType.sham)
                {
                    if (IsPlayerInGoalArea(currentPlayer))
                    {
                        //send empty piece collection
                        resp = dmb
                               .SetGoalFields(new GoalField[0])
                               .GetXml();
                    }
                    else
                    {
                        resp = dmb
                               .SetTaskFields(new TaskField[0])
                               .GetXml();
                    }
                    GameMasterClient.Connection.SendFromClient(handler, resp);
                    return;
                }
                Wrapper.GoalField gf = Board.Fields[currentPlayer.X, currentPlayer.Y] as Wrapper.GoalField;
                // remove piece and goal
                if (gf.Type == GoalFieldType.goal)
                {
                    gf.Type = GoalFieldType.nongoal;
                }
                Pieces.Remove(carriedPiece);

                bool blueWon = false;

                EndGame(Board, TeamColour.blue);
                if (endGame)
                {
                    blueWon = true;
                }
                else
                {
                    EndGame(Board, TeamColour.red);
                }

                if (endGame)
                {
                    GameInProgress = false;
                    EndGameEventArgs args;
                    GameMasterClient.CancelToken.Cancel();

                    if (blueWon)
                    {
                        ConsoleDebug.Good("Blue team won!");
                        args = new EndGameEventArgs(TeamBlue, TeamRed)
                        {
                            Handler = handler
                        };
                    }
                    else
                    {
                        ConsoleDebug.Good("Red team won!");
                        args = new EndGameEventArgs(TeamRed, TeamBlue)
                        {
                            Handler = handler
                        };
                    }

                    foreach (var player in Players)
                    {
                        string endGameResponse = new DataMessageBuilder(player.Id, true)
                                                 .SetWrapperTaskFields(Board.GetTaskFields())
                                                 .SetWrapperGoalFields(Board.GetGoalFields())
                                                 .SetWrapperPieces(Pieces)
                                                 .SetPlayerLocation(player.Location)
                                                 .GetXml();

                        GameMasterClient.Connection.SendFromClient(handler, endGameResponse);
                    }
                    gameMaster.Logger.LogEndGame(this, blueWon ? TeamColour.blue : TeamColour.red);
                    OnGameEnd(this, args);
                    return;
                }

                resp = new DataMessageBuilder(currentPlayer.Id, endGame)
                       .AddGoalField(gf.SchemaField as GoalField)
                       .GetXml();
            }
            GameMasterClient.Connection.SendFromClient(handler, resp);
        }