コード例 #1
0
        public void PlaceNewPiece(Wrapper.TaskField field)
        {
            var pieceType = rng.NextDouble() < GameMasterClient.Settings.GameDefinition.ShamProbability
                ? PieceType.sham
                : PieceType.normal;

            lock (BoardLock)
            {
                var newPiece = new Wrapper.Piece((ulong)Pieces.Count, pieceType, DateTime.Now);
                newPiece.Id = pieceid++;
                if (field == null)
                {
                    ConsoleDebug.Warning("There are no empty places for a new Piece!");
                    return; //TODO BUSYWAITING HERE probably
                }
                //remove old piece
                if (field.PieceId != null)
                {
                    var oldPiece = Pieces.Where(p => p.Id == field.PieceId.Value).Single();
                    Pieces.Remove(oldPiece);
                }
                field.PieceId     = newPiece.Id;
                newPiece.Location = new Location()
                {
                    x = field.X, y = field.Y
                };
                Pieces.Add(newPiece);
                Board.UpdateDistanceToPiece(Pieces);
                ConsoleDebug.Good($"Placed new Piece at: ({field.X}, {field.Y})");
            }
            //BoardPrinter.Print(Board);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }