public void FindMoves_ReturnsExpectedNodesForMovementThree() { BoardGraph boardGraph = new BoardGraph(); MoveCalculator moveCalculator = new MoveCalculator(); List <Node> occupiedNodes = new List <Node>(); occupiedNodes.Add(boardGraph.Nodes[13]); occupiedNodes.Add(boardGraph.Nodes[14]); occupiedNodes.Add(boardGraph.Nodes[17]); occupiedNodes.Add(boardGraph.Nodes[18]); occupiedNodes.Add(boardGraph.Nodes[19]); occupiedNodes.Add(boardGraph.Nodes[20]); occupiedNodes.Add(boardGraph.Nodes[23]); occupiedNodes.Add(boardGraph.Nodes[24]); IEnumerable <NodePath> actual; actual = moveCalculator.FindMoves(boardGraph.Nodes[13], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[14], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[17], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[18], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[19], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[20], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[23], 3, occupiedNodes); actual = moveCalculator.FindMoves(boardGraph.Nodes[24], 3, occupiedNodes); }
public void ProcessAttackAction(int attackingMonsterTypeId, int defendingMonsterTypeId) { Monster attacker; Monster defender; bool isHostAttacker = ActionNumber == 1 || ActionNumber == 2; if (isHostAttacker) { attacker = HostMonsters.Single(m => m.MonsterTypeId == attackingMonsterTypeId); defender = GuestMonsters.Single(m => m.MonsterTypeId == defendingMonsterTypeId); } else { attacker = GuestMonsters.Single(m => m.MonsterTypeId == attackingMonsterTypeId); defender = HostMonsters.Single(m => m.MonsterTypeId == defendingMonsterTypeId); } AttackResult attackResult = AttackCalculator.Calculate(attacker.AttackRating, defender.DefenseRating); IEnumerable <Node> friendlyOccupiedNodes = HostMonsters.Select(monster => monster.CurrentNode).ToList(); IEnumerable <Node> enemyOccupiedNodes = GuestMonsters.Select(monster => monster.CurrentNode).ToList(); switch (attackResult) { case AttackResult.Kill: if (isHostAttacker) { GuestMonsters.Remove(defender); } else { HostMonsters.Remove(defender); } SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.AttackKillResponse, new AttackKillResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, AttackingMonsterTypeId = attackingMonsterTypeId, DefendingMonsterTypeId = defendingMonsterTypeId, AttackResultId = (int)AttackResult.Kill, Message = "Kill" }); break; case AttackResult.CounterKill: if (isHostAttacker) { HostMonsters.Remove(attacker); } else { GuestMonsters.Remove(attacker); } SelectedMonster = null; SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.AttackKillResponse, new AttackKillResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, AttackingMonsterTypeId = attackingMonsterTypeId, DefendingMonsterTypeId = defendingMonsterTypeId, AttackResultId = (int)AttackResult.CounterKill, Message = "Counter Kill" }); break; case AttackResult.Push: AvailablePushDestinations = MoveCalculator.FindMoves(defender.CurrentNode, 1, friendlyOccupiedNodes.Union(enemyOccupiedNodes)).Select(m => m.DestinationNode); if (!AvailablePushDestinations.Any()) { SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.GameState, new GameStateMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = "No available push destinations.", HostMonsterState = JsonConvert.SerializeObject(HostMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)), GuestMonsterState = JsonConvert.SerializeObject(GuestMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)) }); } else { SubActionNumber = 6; _hostServer.SendToAll(CustomMessageTypes.AttackPushResponse, new AttackPushResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, AttackingMonsterTypeId = attackingMonsterTypeId, DefendingMonsterTypeId = defendingMonsterTypeId, AvailablePushDestinationIds = AvailablePushDestinations.Select(d => d.Id).ToArray(), AttackResultId = (int)AttackResult.Push, Message = "Push" }); } break; case AttackResult.CounterPush: AvailablePushDestinations = MoveCalculator.FindMoves(attacker.CurrentNode, 1, friendlyOccupiedNodes.Union(enemyOccupiedNodes)).Select(m => m.DestinationNode); if (!AvailablePushDestinations.Any()) { SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.GameState, new GameStateMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = "No available push destinations.", HostMonsterState = JsonConvert.SerializeObject(HostMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)), GuestMonsterState = JsonConvert.SerializeObject(GuestMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)) }); } else { SubActionNumber = 7; _hostServer.SendToAll(CustomMessageTypes.AttackPushResponse, new AttackPushResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, AttackingMonsterTypeId = attackingMonsterTypeId, DefendingMonsterTypeId = defendingMonsterTypeId, AvailablePushDestinationIds = AvailablePushDestinations.Select(d => d.Id).ToArray(), AttackResultId = (int)AttackResult.CounterPush, Message = "Counter Push" }); } break; default: throw new ArgumentOutOfRangeException(); } }
private void SubActionThree() { if (SelectedMonster != null) { IEnumerable <Node> friendlyOccupiedNodes; IEnumerable <Node> enemyOccupiedNodes; if (ActionNumber == 1 || ActionNumber == 2) { friendlyOccupiedNodes = HostMonsters.Select(monster => monster.CurrentNode).ToList(); enemyOccupiedNodes = GuestMonsters.OrderBy(t => t.DefenseRating).Select(monster => monster.CurrentNode).ToList(); // order enemies defense rating } else { friendlyOccupiedNodes = GuestMonsters.Select(monster => monster.CurrentNode).ToList(); enemyOccupiedNodes = HostMonsters.OrderBy(t => t.DefenseRating).Select(monster => monster.CurrentNode).ToList(); // order enemies defense rating } IEnumerable <int> availableMoveActionNodeIds = MoveCalculator.FindMoves(SelectedMonster.CurrentNode, SelectedMonster.MovementRating, friendlyOccupiedNodes.Union(enemyOccupiedNodes)) .Select(a => a.DestinationNode.Id) .OrderBy(a => a); // order nodes by id so center nodes are prioritized IEnumerable <int> availableAttackActionNodeIds = MoveCalculator.FindAttackMoves(SelectedMonster.CurrentNode, enemyOccupiedNodes).Select(a => a.Id); SubActionNumber = 4; if (friendlyOccupiedNodes.Count() <= 1 && !availableAttackActionNodeIds.Any() && !availableMoveActionNodeIds.Any()) { SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.GameState, new PassActionMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = "No available moves.", HostMonsterState = JsonConvert.SerializeObject(HostMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)), GuestMonsterState = JsonConvert.SerializeObject(GuestMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)) }); } else { if (friendlyOccupiedNodes.Count() == 1 && availableAttackActionNodeIds.Any()) { availableMoveActionNodeIds = new List <int>(); } _hostServer.SendToAll(CustomMessageTypes.AvailableMovesResponse, new AvailableMovesResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, AvailableAttackNodeIds = availableAttackActionNodeIds.ToArray(), AvailableMoveNodeIds = availableMoveActionNodeIds.ToArray(), Message = "Available actions", SelectedMonsterTypeId = SelectedMonster.MonsterTypeId }); } } }
public void SelectAction(int selectedNodeId) { if (selectedNodeId < 0) { SubActionNumber = 0; _hostServer.SendToAll(CustomMessageTypes.GameState, new PassActionMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = "No available moves.", HostMonsterState = JsonConvert.SerializeObject(HostMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)), GuestMonsterState = JsonConvert.SerializeObject(GuestMonsters.Select(m => new { m.MonsterTypeId, m.CurrentNode.Id }).ToDictionary(k => k.MonsterTypeId, v => v.Id)) }); } IEnumerable <Node> friendlyOccupiedNodes; IEnumerable <Node> enemyOccupiedNodes; List <Monster> friendlyMonsters = ActionNumber == 1 || ActionNumber == 2 ? HostMonsters : GuestMonsters; List <Monster> enemyMonsters = ActionNumber == 1 || ActionNumber == 2 ? GuestMonsters : HostMonsters; friendlyOccupiedNodes = friendlyMonsters.Select(monster => monster.CurrentNode).ToList(); enemyOccupiedNodes = enemyMonsters.Select(monster => monster.CurrentNode).ToList(); IEnumerable <NodePath> movementPaths = MoveCalculator.FindMoves(SelectedMonster.CurrentNode, SelectedMonster.MovementRating, friendlyOccupiedNodes.Union(enemyOccupiedNodes)); IEnumerable <Node> availableMoveActions = movementPaths.Select(p => p.DestinationNode); IEnumerable <Node> availableAttackActions = MoveCalculator.FindAttackMoves(SelectedMonster.CurrentNode, enemyOccupiedNodes); if (friendlyOccupiedNodes.Select(n => n.Id).Contains(selectedNodeId)) { SelectedMonster = friendlyMonsters.Single(m => m.CurrentNode.Id == selectedNodeId); SelectedAttackNode = null; SubActionNumber = 3; _hostServer.SendToAll(CustomMessageTypes.SelectMonsterResponse, new SelectMonsterResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, SelectedMonsterTypeId = SelectedMonster.MonsterTypeId, Message = SelectedMonster.Name, }); } else if (availableAttackActions.Select(a => a.Id).Contains(selectedNodeId)) { SelectedAttackNode = availableAttackActions.Single(a => a.Id == selectedNodeId); SelectedMovementPath = null; SubActionNumber = 5; _hostServer.SendToAll(CustomMessageTypes.SelectAttackActionResponse, new SelectAttackResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = "Attack selected", AttackNodeId = SelectedAttackNode.Id }); } else if (availableMoveActions.Select(m => m.Id).Contains(selectedNodeId)) { SelectedMovementPath = movementPaths.Single(m => m.DestinationNode.Id == selectedNodeId); SelectedAttackNode = null; SubActionNumber = 5; _hostServer.SendToAll(CustomMessageTypes.SelectMoveActionResponse, new SelectMoveResponseMessage { ActionNumber = ActionNumber, SubActionNumber = SubActionNumber, Message = SelectedMovementPath.ToString(), MovementPathIds = SelectedMovementPath.PathToDestination.Select(n => n.Id).ToArray(), DestinationNodeId = SelectedMovementPath.DestinationNode.Id }); } }