public async Task <ActionResult <ApiGame> > PlayerMove(string token, [FromBody] ApiMove body) { var game = await iRepository.GetGame(token); if (game is null) { return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game")); } if (game.Moving != game.GetPlayerColor(body.Player)) { return(StatusCode((int)HttpStatusCode.Unauthorized, "It is not your move")); } if (game.Status != GameStatus.Running) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Game hasn't started yet")); } if (!game.Move(body.Row, body.Col)) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Invalid move")); } await iRepository.UpdateGame(game); return(ApiGame.GameToApiGame(game)); }
/// <summary> /// Maps an ApiGame to a Game (Entity) object /// </summary> /// <param name="apiGame">ApiGame to be mapped</param> /// <returns>Game object</returns> public static Game MapGame(ApiGame apiGame) { //Platforms IList <GamePlatform> gamePlatforms = new List <GamePlatform>(); foreach (ApiGamePlatform gamePlatform in apiGame.GamePlatforms) { gamePlatforms.Add(new GamePlatform() { GameId = apiGame.ID, PlatformId = gamePlatform.PlatformId, Platform = apiGame.Platforms.FirstOrDefault(p => p.Id == gamePlatform.PlatformId), ReleaseDate = Constants.UnixEpoch.AddMilliseconds(gamePlatform.ReleaseDate) }); } //Companies IList <GameCompany> gameCompanies = new List <GameCompany>(); foreach (Company company in apiGame.Developers) { gameCompanies.Add(new GameCompany() { GameId = apiGame.ID, CompanyId = company.Id, Company = company, Role = CompanyRole.Developer }); } foreach (Company company in apiGame.Publishers) { gameCompanies.Add(new GameCompany() { GameId = apiGame.ID, CompanyId = company.Id, Company = company, Role = CompanyRole.Publisher }); } return(new Game() { Id = apiGame.ID, Name = apiGame.Name, Cover = apiGame.Cover != null ? new Image { Id = apiGame.Cover.Id } : null, FirstReleaseDate = Constants.UnixEpoch.AddMilliseconds(apiGame.FirstReleaseDate), Rating = apiGame.AggregatedRating, Genres = apiGame.Genres.ToArray(), Screenshots = apiGame.Screenshots.Select(s => new Image { Id = s.Id }).ToList(), Summary = apiGame.Summary, GamePlatforms = gamePlatforms, GameCompanies = gameCompanies }); }
public async Task <ActionResult <ApiGame> > GetGameWithToken(string token) { var game = await iRepository.GetGame(token); if (game is null) { return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game")); } return(ApiGame.GameToApiGame(game)); }
public async Task <ApiGame> AddNewGame([FromBody] ApiGame game) { var result = await iRepository.AddGame(new Game() { Description = game.Description, Player1Token = game.Player1Token, Status = GameStatus.Waiting, }); return(ApiGame.GameToApiGame(result)); }
public async Task <ActionResult <ApiGame> > JoinGame(string token, [FromBody] ApiMove body) { var game = await iRepository.GetGame(token); if (game is null) { return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game")); } if (!game.Join(body.Player)) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Not allowed to join")); } var result = await iRepository.UpdateGame(game); return(ApiGame.GameToApiGame(game)); }
public async Task <ActionResult <ApiGame> > Surrender(string token, [FromBody] ApiMove body) { var game = await iRepository.GetGame(token); if (game is null) { return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game")); } if (body.Player is null) { return(StatusCode((int)HttpStatusCode.NotFound, "No player found")); } game.SurrenderGame(body.Player); await iRepository.UpdateGame(game); return(ApiGame.GameToApiGame(game)); }
public async Task <ActionResult <ApiGame> > StartGame(string token) { var game = await iRepository.GetGame(token); if (game is null) { return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game")); } var startingGame = game.StartGame(); if (startingGame == false) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Could not start game")); } await iRepository.UpdateGame(game); return(ApiGame.GameToApiGame(game)); }
public Game GetGame(int id) { ApiGame apiGame = _apiClient.Get <ApiGame>($"games/{id}?fields=id,name,summary,first_release_date,screenshots.cloudinary_id,cover.cloudinary_id,release_dates,release_dates,aggregated_rating&expand=game,genres,developers,publishers"); //If the game does not exist, throw an exception if (apiGame == null) { throw new GameDoesNotExistException(); } //Get the platforms this game is on apiGame.GamePlatforms = apiGame.GamePlatforms .GroupBy(x => x.PlatformId) //Group by platform .Select(x => x.Aggregate((p1, p2) => p1.ReleaseDate < p2.ReleaseDate ? p1 : p2)); //Takes the platform with the minimum release date IEnumerable <int> platformIds = apiGame.GamePlatforms.Select(x => x.PlatformId); apiGame.Platforms = _platformApiRepository.GetPlatforms(platformIds); return(ApiGameMapper.MapGame(apiGame)); }
public bool CompileData() { if (this.CompilerReady()) { var currentTime = DateTime.Now.ToString("yyyyMMddHHmmss"); var filePath = Path.Combine(this.OutputPath, this.FileNameFormat + ".xml"); var savePath = ""; var userProfile = new UserProfile(); var profileXml = XDocument.Load(Path.Combine(this.RawDataPath, "profile.xml")).Root; userProfile.Id = profileXml.Element("onlinename").Value; userProfile.ImageUrl = profileXml.Element("avatarurl").Value; userProfile.AboutMe = profileXml.Element("aboutme").Value; userProfile.Country = profileXml.Element("country").Value; userProfile.PSPlus = profileXml.Element("plusicon").Value == "1"; var trophyXml = XDocument.Load(Path.Combine(this.RawDataPath, "trophy.xml")).Root; userProfile.EarnedPoints = int.Parse(trophyXml.Element("point").Value); userProfile.Level = int.Parse(trophyXml.Element("level").Value); userProfile.LevelProgress = int.Parse(trophyXml.Element("level").Attribute("progress").Value); userProfile.LevelBasePoints = int.Parse(trophyXml.Element("level").Attribute("base").Value); userProfile.LevelNextPoints = int.Parse(trophyXml.Element("level").Attribute("next").Value); userProfile.BronzeEarned = int.Parse(trophyXml.Element("types").Attribute("bronze").Value); userProfile.SilverEarned = int.Parse(trophyXml.Element("types").Attribute("silver").Value); userProfile.GoldEarned = int.Parse(trophyXml.Element("types").Attribute("gold").Value); userProfile.PlatinumEarned = int.Parse(trophyXml.Element("types").Attribute("platinum").Value); userProfile.TotalEarned = userProfile.BronzeEarned + userProfile.SilverEarned + userProfile.GoldEarned + userProfile.PlatinumEarned; var gamesFiles = new DirectoryInfo(this.RawDataPath).GetFiles("games_*.xml").Select(f => f.FullName); var apiGames = new List<ApiGame>(); foreach (var gamesFile in gamesFiles) { var gamesXml = XDocument.Load(gamesFile).Root; var totalGames = int.Parse(gamesXml.Element("title").Value); if (userProfile.TotalGames == 0 || userProfile.TotalGames < totalGames) { userProfile.TotalGames = totalGames; } foreach (var game in gamesXml.Descendants("info")) { var gameInfo = new ApiGame(); gameInfo.Id = game.Attribute("npcommid").Value; gameInfo.Platform = game.Attribute("pf").Value; gameInfo.Bronze = int.Parse(game.Element("types").Attribute("bronze").Value); gameInfo.Silver = int.Parse(game.Element("types").Attribute("silver").Value); gameInfo.Gold = int.Parse(game.Element("types").Attribute("gold").Value); gameInfo.Platinum = int.Parse(game.Element("types").Attribute("platinum").Value); gameInfo.Updated = DateTime.Parse(game.Element("last-updated").Value); var existingGame = apiGames.FirstOrDefault(g => g.Id == gameInfo.Id); if (existingGame != null && existingGame.Updated <= gameInfo.Updated) { // This game is already added, but needs to be updated. apiGames.Remove(existingGame); apiGames.Add(gameInfo); } else { apiGames.Add(gameInfo); } } } apiGames = apiGames.OrderByDescending(g => g.Updated).ToList(); userProfile.LastUpdate = apiGames.Select(g => g.Updated).FirstOrDefault(); if (userProfile.LastUpdate == DateTime.MinValue) { userProfile.LastUpdate = null; } // Profile is done at this point. var userGames = new List<UserGame>(); int orderPlayed = 1; // Get game data and add to games plus create trophies- file. foreach (var apiGame in apiGames) { var hasTrophyDetails = File.Exists(Path.Combine(this.TrophyDetailsPath, string.Format("{0}.xml", apiGame.Id))); var hasEarnedDetails = File.Exists(Path.Combine(this.RawDataPath, string.Format("game-{0}.xml", apiGame.Id))); if (hasTrophyDetails && hasEarnedDetails) { var trophyDetailsXml = XDocument.Load(Path.Combine(this.TrophyDetailsPath, string.Format("{0}.xml", apiGame.Id))).Root; var earnedDetailsXml = XDocument.Load(Path.Combine(this.RawDataPath, string.Format("game-{0}.xml", apiGame.Id))).Root; var gameDetails = trophyDetailsXml.Descendants("Game").FirstOrDefault(); var userGame = new UserGame(); userGame.Id = apiGame.Id; userGame.IdEurope = gameDetails.Element("IdEurope").Value; userGame.Title = gameDetails.Element("Title").Value; userGame.ImageUrl = gameDetails.Element("Image").Value; userGame.BronzeEarned = apiGame.Bronze; userGame.SilverEarned = apiGame.Silver; userGame.GoldEarned = apiGame.Gold; userGame.PlatinumEarned = apiGame.Platinum; userGame.TotalEarned = apiGame.Bronze + apiGame.Silver + apiGame.Gold + apiGame.Platinum; userGame.OrderPlayed = orderPlayed++; userGame.LastUpdated = apiGame.Updated; userGame.TotalPoints = int.Parse(gameDetails.Element("TotalPoints").Value); // It appears that PS4 games now include platinums in the total points count. Should probably just overwrite this for all platforms. userGame.EarnedPoints = userGame.CalculateEarnedPoints(apiGame.Platform.ToLower() == "ps4"); userGame.Progress = userGame.CalculateProgress().ToString(); userGame.Platform = apiGame.Platform; var trophyDetails = trophyDetailsXml.Descendants("Trophy"); userGame.PossibleTrophies = trophyDetails.Count(); userGames.Add(userGame); // Compile trophy data. var userTrophies = new List<UserTrophy>(); foreach (var trophyDetail in trophyDetails) { var userTrophy = new UserTrophy(); userTrophy.Id = trophyDetail.Element("Id").Value; userTrophy.GameId = apiGame.Id; userTrophy.Title = trophyDetail.Element("Title").Value; userTrophy.ImageUrl = trophyDetail.Element("Image").Value; userTrophy.Description = trophyDetail.Element("Description").Value; userTrophy.Type = ParseTrophyType(trophyDetail.Element("Type").Value); // Trophy data can now have null values for this. If that's the case, assume it's hidden to be on the safe side. userTrophy.Hidden = !string.IsNullOrWhiteSpace(trophyDetail.Element("Hidden").Value) ? bool.Parse(trophyDetail.Element("Hidden").Value) : true; userTrophy.Platform = apiGame.Platform; foreach (var earnedDetail in earnedDetailsXml.Descendants("trophy")) { if (earnedDetail.Attribute("id").Value == userTrophy.Id) { userTrophy.Earned = DateTime.Parse(earnedDetail.Value).ToUniversalTime().ToLocalTime(); break; } } userTrophies.Add(userTrophy); } PerformBackup("trophies-" + apiGame.Id, "__{0}-" + currentTime); savePath = string.Format(filePath, "trophies-" + apiGame.Id); SaveTrophyXml(savePath, userTrophies); } else { DebugInfo.Add(apiGame.Id); DebugInfo.Add(hasTrophyDetails); DebugInfo.Add(hasEarnedDetails); } } #region Save data // Profile PerformBackup("profile", "__{0}-" + currentTime); savePath = string.Format(filePath, "profile"); var psnProfile = new XmlSerializer(userProfile.GetType()); using (StreamWriter writer = new StreamWriter(savePath)) { psnProfile.Serialize(writer, userProfile); } // Games PerformBackup("games", "__{0}-" + currentTime); savePath = string.Format(filePath, "games"); var psnGames = new XmlSerializer(userGames.GetType()); using (StreamWriter writer = new StreamWriter(savePath)) { psnGames.Serialize(writer, userGames); } // Trophies // done above #endregion return true; } return false; }
private static string GetSourceGameUrl(ApiGame game) { var gameFile = game.ASLVersion >= 500 ? "game.aslx" : System.IO.Path.GetFileName(game.OnlineRef); return string.Format("http://textadventures.blob.core.windows.net/gameresources/{0}/{1}", game.UniqueId, gameFile); }
private static string GetSourceGameUrl(ApiGame game) { var gameFile = game.ASLVersion >= 500 ? "game.aslx" : System.IO.Path.GetFileName(game.OnlineRef); return(string.Format("http://textadventures.blob.core.windows.net/gameresources/{0}/{1}", game.UniqueId, gameFile)); }
public bool CompileData() { if (this.CompilerReady()) { var currentTime = DateTime.Now.ToString("yyyyMMddHHmmss"); var filePath = Path.Combine(this.OutputPath, this.FileNameFormat + ".xml"); var savePath = ""; var userProfile = new UserProfile(); var profileXml = XDocument.Load(Path.Combine(this.RawDataPath, "profile.xml")).Root; userProfile.Id = profileXml.Element("onlinename").Value; userProfile.ImageUrl = profileXml.Element("avatarurl").Value; userProfile.AboutMe = profileXml.Element("aboutme").Value; userProfile.Country = profileXml.Element("country").Value; userProfile.PSPlus = profileXml.Element("plusicon").Value == "1"; var trophyXml = XDocument.Load(Path.Combine(this.RawDataPath, "trophy.xml")).Root; userProfile.EarnedPoints = int.Parse(trophyXml.Element("point").Value); userProfile.Level = int.Parse(trophyXml.Element("level").Value); userProfile.LevelProgress = int.Parse(trophyXml.Element("level").Attribute("progress").Value); userProfile.LevelBasePoints = int.Parse(trophyXml.Element("level").Attribute("base").Value); userProfile.LevelNextPoints = int.Parse(trophyXml.Element("level").Attribute("next").Value); userProfile.BronzeEarned = int.Parse(trophyXml.Element("types").Attribute("bronze").Value); userProfile.SilverEarned = int.Parse(trophyXml.Element("types").Attribute("silver").Value); userProfile.GoldEarned = int.Parse(trophyXml.Element("types").Attribute("gold").Value); userProfile.PlatinumEarned = int.Parse(trophyXml.Element("types").Attribute("platinum").Value); userProfile.TotalEarned = userProfile.BronzeEarned + userProfile.SilverEarned + userProfile.GoldEarned + userProfile.PlatinumEarned; var gamesFiles = new DirectoryInfo(this.RawDataPath).GetFiles("games_*.xml").Select(f => f.FullName); var apiGames = new List <ApiGame>(); foreach (var gamesFile in gamesFiles) { var gamesXml = XDocument.Load(gamesFile).Root; var totalGames = int.Parse(gamesXml.Element("title").Value); if (userProfile.TotalGames == 0 || userProfile.TotalGames < totalGames) { userProfile.TotalGames = totalGames; } foreach (var game in gamesXml.Descendants("info")) { var gameInfo = new ApiGame(); gameInfo.Id = game.Attribute("npcommid").Value; gameInfo.Platform = game.Attribute("pf").Value; gameInfo.Bronze = int.Parse(game.Element("types").Attribute("bronze").Value); gameInfo.Silver = int.Parse(game.Element("types").Attribute("silver").Value); gameInfo.Gold = int.Parse(game.Element("types").Attribute("gold").Value); gameInfo.Platinum = int.Parse(game.Element("types").Attribute("platinum").Value); gameInfo.Updated = DateTime.Parse(game.Element("last-updated").Value); var existingGame = apiGames.FirstOrDefault(g => g.Id == gameInfo.Id); if (existingGame != null && existingGame.Updated <= gameInfo.Updated) { // This game is already added, but needs to be updated. apiGames.Remove(existingGame); apiGames.Add(gameInfo); } else { apiGames.Add(gameInfo); } } } apiGames = apiGames.OrderByDescending(g => g.Updated).ToList(); userProfile.LastUpdate = apiGames.Select(g => g.Updated).FirstOrDefault(); if (userProfile.LastUpdate == DateTime.MinValue) { userProfile.LastUpdate = null; } // Profile is done at this point. var userGames = new List <UserGame>(); int orderPlayed = 1; // Get game data and add to games plus create trophies- file. foreach (var apiGame in apiGames) { var hasTrophyDetails = File.Exists(Path.Combine(this.TrophyDetailsPath, string.Format("{0}.xml", apiGame.Id))); var hasEarnedDetails = File.Exists(Path.Combine(this.RawDataPath, string.Format("game-{0}.xml", apiGame.Id))); if (hasTrophyDetails && hasEarnedDetails) { var trophyDetailsXml = XDocument.Load(Path.Combine(this.TrophyDetailsPath, string.Format("{0}.xml", apiGame.Id))).Root; var earnedDetailsXml = XDocument.Load(Path.Combine(this.RawDataPath, string.Format("game-{0}.xml", apiGame.Id))).Root; var gameDetails = trophyDetailsXml.Descendants("Game").FirstOrDefault(); var userGame = new UserGame(); userGame.Id = apiGame.Id; userGame.IdEurope = gameDetails.Element("IdEurope").Value; userGame.Title = gameDetails.Element("Title").Value; userGame.ImageUrl = gameDetails.Element("Image").Value; userGame.BronzeEarned = apiGame.Bronze; userGame.SilverEarned = apiGame.Silver; userGame.GoldEarned = apiGame.Gold; userGame.PlatinumEarned = apiGame.Platinum; userGame.TotalEarned = apiGame.Bronze + apiGame.Silver + apiGame.Gold + apiGame.Platinum; userGame.OrderPlayed = orderPlayed++; userGame.LastUpdated = apiGame.Updated; userGame.TotalPoints = int.Parse(gameDetails.Element("TotalPoints").Value); // It appears that PS4 games now include platinums in the total points count. Should probably just overwrite this for all platforms. userGame.EarnedPoints = userGame.CalculateEarnedPoints(apiGame.Platform.ToLower() == "ps4"); userGame.Progress = userGame.CalculateProgress().ToString(); userGame.Platform = apiGame.Platform; var trophyDetails = trophyDetailsXml.Descendants("Trophy"); userGame.PossibleTrophies = trophyDetails.Count(); userGames.Add(userGame); // Compile trophy data. var userTrophies = new List <UserTrophy>(); foreach (var trophyDetail in trophyDetails) { var userTrophy = new UserTrophy(); userTrophy.Id = trophyDetail.Element("Id").Value; userTrophy.GameId = apiGame.Id; userTrophy.Title = trophyDetail.Element("Title").Value; userTrophy.ImageUrl = trophyDetail.Element("Image").Value; userTrophy.Description = trophyDetail.Element("Description").Value; userTrophy.Type = ParseTrophyType(trophyDetail.Element("Type").Value); // Trophy data can now have null values for this. If that's the case, assume it's hidden to be on the safe side. userTrophy.Hidden = !string.IsNullOrWhiteSpace(trophyDetail.Element("Hidden").Value) ? bool.Parse(trophyDetail.Element("Hidden").Value) : true; userTrophy.Platform = apiGame.Platform; foreach (var earnedDetail in earnedDetailsXml.Descendants("trophy")) { if (earnedDetail.Attribute("id").Value == userTrophy.Id) { userTrophy.Earned = DateTime.Parse(earnedDetail.Value).ToUniversalTime().ToLocalTime(); break; } } userTrophies.Add(userTrophy); } PerformBackup("trophies-" + apiGame.Id, "__{0}-" + currentTime); savePath = string.Format(filePath, "trophies-" + apiGame.Id); SaveTrophyXml(savePath, userTrophies); } else { DebugInfo.Add(apiGame.Id); DebugInfo.Add(hasTrophyDetails); DebugInfo.Add(hasEarnedDetails); } } #region Save data // Profile PerformBackup("profile", "__{0}-" + currentTime); savePath = string.Format(filePath, "profile"); var psnProfile = new XmlSerializer(userProfile.GetType()); using (StreamWriter writer = new StreamWriter(savePath)) { psnProfile.Serialize(writer, userProfile); } // Games PerformBackup("games", "__{0}-" + currentTime); savePath = string.Format(filePath, "games"); var psnGames = new XmlSerializer(userGames.GetType()); using (StreamWriter writer = new StreamWriter(savePath)) { psnGames.Serialize(writer, userGames); } // Trophies // done above #endregion return(true); } return(false); }