public void XmlObjects_ConvertXmlToClass_ShouldSerializeObject() { string xml = File.ReadAllText(@"C:\Projects\Travian\Web\Travian.Web\Travian.Web.API\Data\player_tx3_2403.xml"); ApiRoot api = new ApiRoot(); api = XmlObjects.ConvertXmlToClass<ApiRoot>(xml); Assert.IsNotNull(api.Player); Assert.IsInstanceOfType(api.Player, typeof(Player)); }
/// <summary> /// Get content from travianstats.de as json by id. /// </summary> /// <param name="id">The uid of the object to get</param> /// <param name="category">Valid categories are: "player", "village", "alliance".</param> /// <param name="server">Valid servers are (i.e.): "tx3", "ts19"</param> /// <returns></returns> //[Route("Player")] public async Task<HttpResponseMessage> GetJsonAsync(int id, string server = "ts19.travian.se", bool force = false) { string json = "{}"; var response = new HttpResponseMessage(HttpStatusCode.OK); // if force = true, always get updated values from travianstats and update database // otherwise: check if id exist in database, it not go to travianstats // check if exist in database // if player exist, and not force... get existing // if player exist, and force... get from stats, write over existing // if player not exist, and not force... get from stats, save to db // if player not exist, and force ... -- " -- Player existingPlayer = _context.Players?.Include("Villages")?.FirstOrDefault(p => p.Uid == id && p.Server == server); if (existingPlayer != null && force == false) { ApiRoot root = new ApiRoot(existingPlayer); string xml = XmlObjects.ConvertClassToXml<ApiRoot>(root); json = Newtonsoft.Json.JsonConvert.SerializeXNode(XElement.Parse(xml)); } else if (existingPlayer != null && force == true) { XElement xml = await GetPlayerXml(id, server); if (xml != null) { // update existing player in db json = Newtonsoft.Json.JsonConvert.SerializeXNode(xml); ApiRoot root = XmlObjects.ConvertXmlToClass<ApiRoot>(xml.ToString()); existingPlayer.Update(_context, root); _context.SaveChanges(); } else { response = new HttpResponseMessage(HttpStatusCode.NotFound); } } else // if player not exist... get from stats and save to db { XElement xml = await GetPlayerXml(id, server); if (xml != null) { json = Newtonsoft.Json.JsonConvert.SerializeXNode(xml); // save new player to db ApiRoot root = XmlObjects.ConvertXmlToClass<ApiRoot>(xml.ToString()); Player player = root.Player; player.Villages = root.Villages; _context.Players.Add(player); _context.SaveChanges(); } else { response = new HttpResponseMessage(HttpStatusCode.NotFound); } } response.Content = new StringContent(json, Encoding.UTF8, "application/json"); return response; }
/// <summary> /// Update the properties recursive (mapped to root) /// </summary> /// <param name="root">the new values to update with</param> public void Update(IDbContext context, ApiRoot root) { this.Uid = root.Player.Uid; this.Name = root.Player.Name; this.AllianceId = root.Player.AllianceId; this.Tribe = root.Player.Tribe; this.Server = root.Player.Server; if (root.Villages != null) { // remove and replace the villages (they may have been lost or new ones built) context.Villages.RemoveRange(this.Villages); context.SaveChanges(); this.Villages = root.Villages; } }