コード例 #1
0
 public void HandleMessage(RegisteredGames message)
 {
     if (message.GameInfo == null || message.GameInfo.Length == 0 || !message.GameInfo.Where(g => g.gameName == Player.Options.GameName).Any())
     {
         Task.Run(() =>
         {
             Thread.Sleep((int)Player.Settings.RetryJoinGameInterval);
             string xmlMessage = XmlMessageConverter.ToXml(new GetGames());
             Player.Send(xmlMessage);
         });
     }
     else
     {
         ConsoleDebug.Good("Games available");
         if (Player.Options.GameName == null)
         {
             ConsoleDebug.Warning("Game name not specified");
             return;
         }
         if (message.GameInfo.Count(info => info.gameName == Player.Options.GameName) == 1)
         {
             string xmlMessage = XmlMessageConverter.ToXml(new JoinGame()
             {
                 gameName = Player.Options.GameName,
                 playerIdSpecified = false,
                 preferredRole = Player.Options?.PreferredRole == "player" ? PlayerType.member : PlayerType.leader,
                 preferredTeam = Player.Options?.PreferredTeam == "red" ? Common.Schema.TeamColour.red : Common.Schema.TeamColour.blue
             });
             Player.Send(xmlMessage);
         }
     }
 }
コード例 #2
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);
        }
コード例 #3
0
 public void HandleMessage(Common.Schema.Game message)
 {
     Player.game.Players = message.Players;
     Player.game.Board = message.Board;
     Player.game.Location = message.PlayerLocation;
     Player.game.Fields = new Common.SchemaWrapper.Field[message.Board.width, 2 * message.Board.goalsHeight + message.Board.tasksHeight];
     ConsoleDebug.Good("Game started");
     Player.ReadyForAction?.Invoke();
 }
コード例 #4
0
            public void HandleMessage(ConfirmJoiningGame message)
            {

                if (message == null)
                    return;

                ConsoleDebug.Good(message.gameId.ToString());

                Player.game.Id = message.playerId;
                Player.game.GameId = message.gameId;
                Player.game.Guid = message.privateGuid;
                Player.game.Team = message.PlayerDefinition.team;
                Player.game.Type = message.PlayerDefinition.type;

                return;
            }
コード例 #5
0
            public void HandleMessage(Data message)
            {
                if (message.PlayerLocation != null)
                {
                    Player.game.Location = message.PlayerLocation;
                    ConsoleDebug.Message($"My location: ({message.PlayerLocation.x}, {message.PlayerLocation.y})");
                }
                if (message.TaskFields != null)
                {
                    Common.SchemaWrapper.TaskField[] taskFields = new Common.SchemaWrapper.TaskField[message.TaskFields.Length];
                    for (int i = 0; i < taskFields.Length; i++)
                        taskFields[i] = new Common.SchemaWrapper.TaskField(message.TaskFields[i]);
                    Player.game.UpdateFields(taskFields); ;
                }
                if (message.GoalFields != null)
                {
                    Common.SchemaWrapper.GoalField[] goalFields = new Common.SchemaWrapper.GoalField[message.GoalFields.Length];
                    for (int i = 0; i < goalFields.Length; i++)
                        goalFields[i] = new Common.SchemaWrapper.GoalField(message.GoalFields[i]);
                    Player.game.UpdateFields(goalFields);
                }
                if (message.Pieces != null)
                {
                    foreach (Piece piece in message.Pieces)
                    {
                        lock (pieceLock)
                        {
                            if (piece.playerIdSpecified)
                            {
                                Player.game.Pieces = Player.game.Pieces.Where(piece1 => piece1.playerId != piece.playerId).ToList();
                            }
                            if (Player.game.Pieces.Count(p => p.id == piece.id) == 0)
                                Player.game.Pieces.Add(piece);
                            else
                            {

                                var pp = Player.game.Pieces.Single(p => p.id == piece.id);
                                pp.playerId = piece.playerId;
                                pp.playerIdSpecified = piece.playerIdSpecified;
                                pp.timestamp = piece.timestamp;
                                if (pp.type == PieceType.unknown)
                                    pp.type = piece.type;

                            }

                        }
                    }
                    //args.PlayerClient.Pieces.Clear();
                    //foreach (var piece in message.Pieces)
                    //{
                    //    args.PlayerClient.Pieces.Add(piece);
                    //}
                }
                if (message.gameFinished == true)
                {
                    ConsoleDebug.Good("\nGame just ended\n");
                    BoardPrinter.Print(Player.game.Fields);
                    Player.RegisterForNextGameAfterGameEnd();
                    return;
                }

                Player.ReadyForAction?.Invoke();
            }
コード例 #6
0
 public void HandleMessage(ConfirmGameRegistration message, Socket handler)
 {
     ConsoleDebug.Good("I get gameId = " + message.gameId);
     gameId = message.gameId;
 }
コード例 #7
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);
        }