public List <BaseMessage> GetStartGameMessages() { //TODO: refactor var agents = gameMaster.Agents; var config = gameMaster.Configuration; var messages = new List <BaseMessage>(); foreach (var a in agents) { int[] alliesIds = agents.Where(ag => ag.Team == a.Team && ag.Id != a.Id).Select(a => a.Id).ToArray(); int[] enemiesIds = agents.Where(ag => ag.Team != a.Team).Select(a => a.Id).ToArray(); int leaderId = agents.FirstOrDefault(ag => ag.Team == a.Team && ag.IsTeamLeader).Id; var payload = new StartGamePayload( a.Id, alliesIds, leaderId, enemiesIds, a.Team, new Point(config.BoardX, config.BoardY), config.GoalAreaHeight, alliesIds.Length, enemiesIds.Length, config.NumberOfPieces, config.NumberOfGoals, config.GetTimeouts(), config.ShamProbability, a.Position ); messages.Add(MessageFactory.GetMessage(payload, a.Id)); } return(messages); }
public Message CreateStartMessage() { StartGamePayload payloadStart = new StartGamePayload { AgentID = 1, AlliesIDs = new int[1] { 2 }, LeaderID = 1, EnemiesIDs = new int[2] { 3, 4 }, TeamID = Team.Red, BoardSize = playerBoardSize, GoalAreaSize = 1, NumberOfPlayers = new NumberOfPlayers { Allies = 2, Enemies = 2 }, NumberOfPieces = 2, NumberOfGoals = 2, Penalties = new Penalties(), ShamPieceProbability = 0.5f, Position = new Position { X = 1, Y = 1 }, }; return(new Message(MessageID.StartGame, agentID, payloadStart)); }
public void GetGoalDirectionDoesNotReturnOppositeDirection() { int shortTime = 4; AgentConfiguration agentConfiguration = new AgentConfiguration(); agentConfiguration.TeamID = "Red"; agentConfiguration.CsIP = "127.0.0.1"; agentConfiguration.CsPort = 54321; Agent.Agent agentRed = new Agent.Agent(agentConfiguration); var teamMates = new int[3] { 2, 3, 4 }; var enemiesIds = new int[3] { 5, 7, 6 }; startGamePayload = new StartGamePayload(1, teamMates, 1, enemiesIds, TeamId.Red, new Point(5, 5), 1, 3, 3, 4, 4, new System.Collections.Generic.Dictionary <ActionType, TimeSpan>(), 0.5f, new Point(0, 0)); agentRed.StartGameComponent.Initialize(startGamePayload); Assert.AreNotEqual(Common.GetGoalDirection(agentRed, shortTime, out _), Direction.South); agentConfiguration.TeamID = "Blue"; Agent.Agent agentBlue = new Agent.Agent(agentConfiguration); startGamePayload = new StartGamePayload(1, teamMates, 1, enemiesIds, TeamId.Blue, new Point(5, 5), 1, 3, 3, 4, 4, new System.Collections.Generic.Dictionary <ActionType, TimeSpan>(), 0.5f, new Point(0, 0)); agentBlue.StartGameComponent.Initialize(startGamePayload); Assert.AreNotEqual(Common.GetGoalDirection(agentBlue, shortTime, out _), Direction.North); }
public void Initialize(StartGamePayload startGamePayload) { IsLeader = agent.Id == startGamePayload.LeaderId ? true : false; Team = startGamePayload.TeamId; TeamMates = startGamePayload.AlliesIds.Where(x => x != agent.Id).ToArray(); Penalties = startGamePayload.Penalties; AverageTime = startGamePayload.Penalties.Count > 0 ? startGamePayload.Penalties.Values.Max() : TimeSpan.FromSeconds(1.0); ShamPieceProbability = startGamePayload.ShamPieceProbability; agent.BoardLogicComponent = new BoardLogicComponent(agent, startGamePayload.BoardSize, startGamePayload.GoalAreaHeight, startGamePayload.Position); agent.AgentInformationsComponent.AssignToOwnGoalArea(); FindFirstTeamMateToAsk(); logger.Info("[Agent {id}] Initialized", agent.Id); }
public async Task TestAcceptMessageStartGameShouldSetFields() { int agentID = 1; int leaderId = 1; Team teamId = Team.Red; int[] alliesId = new int[1] { 2 }; int[] enemiesId = new int[2] { 3, 4 }; BoardSize boardSize = new BoardSize { X = 3, Y = 3 }; int goalAreaSize = 1; NumberOfPlayers numberOfPlayers = new NumberOfPlayers { Allies = 2, Enemies = 2 }; int numberOfPieces = 2; int numberOfGoals = 2; Penalties penalties = new Penalties() { Move = 100, Ask = 100, Response = 100, Discovery = 100, Pickup = 100, CheckForSham = 100, PutPiece = 100, DestroyPiece = 100, }; float shanProbability = 0.5f; Position position = new Position { X = 1, Y = 1 }; // Arrange StartGamePayload startGamePayload = new StartGamePayload { AgentID = agentID, AlliesIDs = alliesId, LeaderID = leaderId, EnemiesIDs = enemiesId, TeamID = teamId, BoardSize = boardSize, GoalAreaSize = goalAreaSize, NumberOfPlayers = numberOfPlayers, NumberOfPieces = numberOfPieces, NumberOfGoals = numberOfGoals, Penalties = penalties, ShamPieceProbability = shanProbability, Position = position }; Message startMessage = new Message(MessageID.StartGame, agentID, startGamePayload); BufferBlock <Message> inputBuffer = new BufferBlock <Message>(); inputBuffer.Post(startMessage); PlayerConfiguration configuration = GenerateSampleConfiguration(); var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger); // Act bool expectedisLeader = true; (int, int)expectedBoardSize = (boardSize.X, boardSize.Y); (int, int)expectedPosition = (position.X, position.Y); await player.AcceptMessage(CancellationToken.None); var agentIDResult = player.GetValue <Player.Models.Player, int>("id"); var leaderIdResult = player.LeaderId; var teamMatesResult = player.TeamMatesIds; var isLeaderResult = player.IsLeader; var teamResult = player.Team; var boardSizeResult = player.BoardSize; var penaltiesResult = player.PenaltiesTimes; var positionResult = player.Position; var enemiesResult = player.EnemiesIds; var goalAreaSizeResult = player.GoalAreaSize; var numOfPlayersResult = player.NumberOfPlayers; var numOfPiecesResult = player.NumberOfPieces; var numOfGoalsResult = player.NumberOfGoals; var shamProbabilityResult = player.ShamPieceProbability; // Assert Assert.Equal(agentID, agentIDResult); Assert.Equal(leaderId, leaderIdResult); Assert.Equal(alliesId, teamMatesResult); Assert.Equal(expectedisLeader, isLeaderResult); Assert.Equal(teamId, teamResult); Assert.Equal(expectedBoardSize, boardSizeResult); Assert.True(penalties.AreAllPropertiesTheSame(penaltiesResult), $"Penalties should be the same,\n expected: {penalties},\n actual {penaltiesResult}"); Assert.Equal(expectedPosition, positionResult); Assert.Equal(enemiesId, enemiesResult); Assert.Equal(goalAreaSize, goalAreaSizeResult); Assert.True(numberOfPlayers.Allies == numOfPlayersResult.Allies && numberOfPlayers.Enemies == numOfPlayersResult.Enemies); Assert.Equal(numberOfPieces, numOfPiecesResult); Assert.Equal(numberOfGoals, numOfGoalsResult); Assert.Equal(shanProbability, shamProbabilityResult); }
public async Task TestExecuteAsyncShouldReadMessages() { // Arrange IServiceCollection services = new ServiceCollection(); services.AddSingleton(logger); var clientMock = MockGenerator.Get <ISocketClient <Message, Message> >(); services.AddSingleton(clientMock); var queue = new BufferBlock <Message>(); StartGamePayload payloadStart = new StartGamePayload { AgentID = 1, AlliesIDs = new int[2] { 1, 2 }, LeaderID = 1, EnemiesIDs = new int[2] { 3, 4 }, TeamID = Team.Red, BoardSize = new BoardSize { X = 3, Y = 3 }, GoalAreaSize = 1, NumberOfPlayers = new NumberOfPlayers { Allies = 2, Enemies = 2 }, NumberOfPieces = 2, NumberOfGoals = 2, Penalties = new Penalties(), ShamPieceProbability = 0.5f, Position = new Position { X = 1, Y = 1 }, }; Message messageStart = new Message() { MessageID = MessageID.StartGame, Payload = payloadStart, }; queue.Post(messageStart); services.AddSingleton(queue); services.AddSingleton <PlayerConfiguration>(); services.AddSingleton(MockGenerator.Get <IStrategy>()); services.AddSingleton <Models.Player>(); services.AddSingleton(Mock.Of <IApplicationLifetime>()); var context = new ServiceSynchronization(1, 1); services.AddSingleton(context); services.AddHostedService <PlayerService>(); var serviceProvider = services.BuildServiceProvider(); var hostedService = (PlayerService)serviceProvider.GetService <IHostedService>(); // Act int delay = 500; await Task.Run(async() => { await hostedService.StartAsync(CancellationToken.None); await Task.Delay(delay); await hostedService.StopAsync(CancellationToken.None); }); // Assert Assert.Equal(0, queue.Count); }
protected void StartGame(StartGamePayload m) { throw new NotImplementedException(); }