public void OwnPlayerSlotForAi(int playerNumber) { var playerToOwn = Players.Where(x => x.PlayerNumber.Equals(playerNumber)).First(); if (string.IsNullOrWhiteSpace(playerToOwn.PlayerId)) { playerToOwn.PlayerId = Guid.NewGuid().ToString(); playerToOwn.PlayerName = AiPlayerFactory.GetRandomNameNoDuplicates(Players); playerToOwn.Npc = AiPlayerFactory.BuildAi(playerToOwn, this); UpdateEveryone(); } }
public Game LoadGame(string partitionKey) { try { //Game TableOperation loadGame = TableOperation.Retrieve <Game>(partitionKey, "Game"); TableResult loadGameResult = tableService.Execute(loadGame, "Games"); Game game = loadGameResult.Result as Game; //Players TableQuery <Player> loadPlayers = new TableQuery <Player>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)); var players = tableService.ExecuteQuery(loadPlayers, "Players"); game.Players = players.ToList <Player>(); //Teams TableQuery <Team> loadTeams = new TableQuery <Team>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)); var teams = tableService.ExecuteQuery(loadTeams, "Teams"); game.Teams = teams.ToList <Team>(); //Wire-up references between Teams and Players: foreach (Player p in game.Players) { p.Team = game.Teams[p.PlayerNumber % game.Teams.Count]; game.Teams[p.PlayerNumber % game.Teams.Count].Players.Add(p); if (p.IsAI.Equals(true)) { p.Npc = AiPlayerFactory.BuildAi(p, game); } } //Board (aka BoardLocations) TableQuery <BoardLocation> loadBoard = new TableQuery <BoardLocation>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)); var board = tableService.ExecuteQuery(loadBoard, "BoardLocations"); game.Board = board.ToList <BoardLocation>(); //Wire-up references within BoardLocation: foreach (BoardLocation bl in game.Board) { if (bl.FilledByPlayerNumber >= 0) { bl.FilledBy = game.Players[bl.FilledByPlayerNumber]; } } //Plays TableQuery <Play> loadPlays = new TableQuery <Play>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)); var plays = tableService.ExecuteQuery(loadPlays, "Plays"); game.Plays = plays.ToList <Play>(); //Play has no references to rehydrate. //Current Player game.CurrentPlayer = game.Players[game.TurnNumber % game.Players.Count]; //Winning Player if (game.Won == true && !game.DeadCardDraw) { game.WinningPlayer = game.Players[game.Plays.OrderByDescending(x => x.TurnNumber).ToList().Last().PlayerNumber]; } game.RunAI(); return(game); } catch (StorageException e) { throw; } }