public ActionResult <Game> Create(Game game) { using var gameDataContext = new GameDataContext(); using var matchContext = new MatchContext(); long id = (gameDataContext.GameDatas.Max(m => (int?)m.Id) ?? 0) + 1; game.Id = id; if (game.Date == null) { game.Date = DateTime.Now.ToString("u", System.Globalization.CultureInfo.InvariantCulture); } gameDataContext.Add(new GameData { Id = id, Date = game.Date, }); gameDataContext.SaveChanges(); var matches = new List <Match>(); foreach (var player in game.Team1) { matches.Add(new Match { GameId = id, Player = player, Team = 1, }); } foreach (var player in game.Team2) { matches.Add(new Match { GameId = id, Player = player, Team = 2, }); } matchContext.AddRange(matches); matchContext.SaveChanges(); using var scoreContext = new ScoreContext(); float?result = null; if (game.Score == '1') { result = 1; } else if (game.Score == '2') { result = 0; } else if (game.Score == 'D') { result = 0.5f; } else if (game.Score == 'C') { result = -1; } scoreContext.Add(new Score { GameId = id, Result = result, }); scoreContext.SaveChanges(); return(CreatedAtAction(nameof(GetById), new { id = id }, game)); }
private static void SeedDb() { using (var context = new GameDataContext()) { #region SaveFiles var saveFile = new SaveFile { Id = new Guid(), LastSavedOn = DateTime.Now, }; context.Add(saveFile); #endregion SaveFiles #region Heroes var basicAttackSkill = new Skill { Id = new Guid(), Name = "Basic Attack", Cost = 0, Damage = 2, Accuracy = 1, SaveFile = saveFile, }; context.Add(basicAttackSkill); var fireballSkill = new Skill { Id = new Guid(), Name = "Fireball", Cost = 2, Damage = 4, Accuracy = 1, SaveFile = saveFile, }; context.Add(fireballSkill); var waitSkill = new Skill { Id = new Guid(), Name = "Wait", Cost = 0, Damage = 0, Accuracy = 1, SaveFile = saveFile, }; context.Add(waitSkill); var endgameSkill = new Skill { Id = new Guid(), Name = "Endgame", Cost = 0, Damage = 200, Accuracy = 1, SaveFile = saveFile, }; context.Add(endgameSkill); var hero = new Heroes { Id = new Guid(), SaveFile = saveFile, Name = "Roland", Health = 10, Mana = 5, Skill1Navigation = basicAttackSkill, Skill2Navigation = fireballSkill, Skill3Navigation = waitSkill, Skill4Navigation = endgameSkill }; context.Add(hero); #endregion Heroes #region Monsters var stab = new Skill { Name = "Stab", Cost = 0, Damage = 6, Accuracy = .9, SaveFile = saveFile, }; context.Add(stab); var goblin = new Monsters { Id = new Guid(), Name = "Goblin", Health = 3, Skill1Navigation = stab, }; context.Add(goblin); var rockSlam = new Skill { Name = "Rock Slam", Cost = 0, Damage = 2, Accuracy = .7, SaveFile = saveFile, }; context.Add(rockSlam); var golem = new Monsters { Id = new Guid(), Name = "Golem", Health = 9, Skill1Navigation = rockSlam, }; context.Add(golem); var slash = new Skill { Name = "Slash", Cost = 0, Damage = 3, Accuracy = .8, SaveFile = saveFile, }; context.Add(slash); var skeleton = new Monsters { Id = new Guid(), Name = "Skeleton", Health = 7, Skill1Navigation = slash, }; context.Add(skeleton); #endregion Monsters context.SaveChanges(); } }