Пример #1
0
            public void HandleMessage(AcceptExchangeRequest message)
            {
                //the other player accepted our request, send data to him
                DataMessageBuilder builder = new DataMessageBuilder(message.senderPlayerId);

                builder.SetGoalFields(Player.game.Fields.Cast<Common.Schema.Field>().Where(f => f is Common.Schema.GoalField).Cast<Common.Schema.GoalField>());
                builder.SetTaskFields(Player.game.Fields.Cast<Common.Schema.Field>().Where(f => f is TaskField).Cast<TaskField>());
                builder.SetPieces(Player.game.Pieces);
                builder.SetPlayerLocation(Player.game.Location);

                var data = builder.GetXml();
                Player.Send(data);
                //do not call play, we call play already when we get our data
            }
        public async void HandleMessage(TestPiece message, Socket handler)
        {
            if (!ValidateMessage(message))
            {
                return;
            }
            string resp = "";
            await Task.Delay((int)GameMasterClient.Settings.ActionCosts.TestDelay);

            Wrapper.Player currentPlayer = Players.SingleOrDefault(p => p.Guid == message.playerGuid);
            if (currentPlayer == null)
            {
                return;
            }
            GameMasterClient.Logger.Log(message, currentPlayer);
            lock (BoardLock)
            {
                Wrapper.Piece piece =
                    Pieces.SingleOrDefault(
                        pc =>
                        pc.PlayerId == currentPlayer.Id);
                if (piece == null) // not carrying anything
                {
                    ConsoleDebug.Warning("Not carrying a piece!");
                    piece = Pieces.FirstOrDefault(pc => pc.Location.Equals(currentPlayer.Location));
                }
                if (piece == null)
                {
                    ConsoleDebug.Warning("Not on a piece!");
                    //send empty piece collection
                    resp = new DataMessageBuilder(currentPlayer.Id, endGame)
                           .SetPieces(new Piece[0])
                           .GetXml();
                }
                else
                {
                    ConsoleDebug.Warning("On a piece!");
                    resp = new DataMessageBuilder(currentPlayer.Id, endGame)
                           .AddPiece(piece.SchemaPiece)
                           .GetXml();
                }
            }
            GameMasterClient.Connection.SendFromClient(handler, resp);
        }
        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);
        }
        public async Task HandleMessage(PickUpPiece message, Socket handler)
        {
            if (!ValidateMessage(message))
            {
                return;
            }
            string resp = "";
            await Task.Delay((int)gameMaster.Settings.ActionCosts.PickUpDelay);

            Wrapper.Player currentPlayer = Players.Single(p => p.Guid == message.playerGuid);
            gameMaster.Logger.Log(message, currentPlayer);
            lock (BoardLock)
            {
                Wrapper.Piece piece =
                    Pieces.FirstOrDefault(
                        pc =>
                        pc.Location.x == currentPlayer.Location.x && pc.Location.y == currentPlayer.Location.y &&
                        !pc.PlayerId.HasValue);
                if (piece == null || Pieces.Any(pc => pc.PlayerId == currentPlayer.Id))
                {
                    ConsoleDebug.Warning("No piece here or you have already a piece!");
                    //send empty piece collection
                    resp = new DataMessageBuilder(currentPlayer.Id, endGame)
                           .SetPieces(new Piece[0])
                           .GetXml();
                }
                else
                {
                    ConsoleDebug.Warning("Piece picked up!");
                    piece.PlayerId = currentPlayer.Id;
                    var taskField = Board.Fields[currentPlayer.X, currentPlayer.Y] as Wrapper.TaskField;
                    if (taskField != null) //update taskField
                    {
                        ConsoleDebug.Warning("Updating TaskField");
                        taskField.PieceId = null;
                        Board.UpdateDistanceToPiece(Pieces);
                        //clocest neighbour to piece + 1
                        // taskField.DistanceToPiece = new[]
                        // {
                        //     FieldAt(gameMaster.Board.Fields, currentPlayer.X + 1, currentPlayer.Y)
                        //     ?.DistanceToPiece,
                        //     FieldAt(gameMaster.Board.Fields, currentPlayer.X - 1, currentPlayer.Y)
                        //     ?.DistanceToPiece,
                        //     FieldAt(gameMaster.Board.Fields, currentPlayer.X, currentPlayer.Y + 1)
                        //     ?.DistanceToPiece,
                        //     FieldAt(gameMaster.Board.Fields, currentPlayer.X, currentPlayer.Y - 1)
                        //     ?.DistanceToPiece
                        //}.Where(u => u.HasValue).Select(u => u.Value).Min() + 1;
                    }
                    resp = new DataMessageBuilder(currentPlayer.Id, endGame)
                           .AddPiece(new Piece()
                    {
                        id                = piece.Id,
                        timestamp         = piece.TimeStamp,
                        playerId          = currentPlayer.Id,
                        playerIdSpecified = true,
                        type              = PieceType.unknown
                    })
                           .GetXml();
                }
            }
            GameMasterClient.Connection.SendFromClient(handler, resp);
        }