public static void UpdateGame(MyContext db, UpdateGameInput input) { //Add new state var newstate = new GamesDataStates { StateId = GuidGenerator.GenerateSequentialGuid(), GameId = input.Gamerec.EntryId, State = input.Gameobj.Serialize(), RenderRep = input.Gameobj.Render(), Timestamp = DateTime.UtcNow }; db.GamesDataStates.Add(newstate); //Add chat if (!String.IsNullOrEmpty(input.Gameobj.ChatMsgs)) { var newchat = new GamesDataChats { ChatId = GuidGenerator.GenerateSequentialGuid(), GameId = input.Gamerec.EntryId, OwnerId = null, Message = input.Gameobj.ChatMsgs }; db.GamesDataChats.Add(newchat); } if (!input.Gameobj.Gameover) { //Update clocks var clock = db.GamesDataClocks.Single(x => x.GameId.Equals(input.Gamerec.EntryId) && x.OwnerId.Equals(input.Mover)); var newbank = clock.Bank + input.Gamerec.ClockInc; if (newbank > input.Gamerec.ClockMax) { newbank = input.Gamerec.ClockMax; } clock.Bank = (short)newbank; } //Update whoseturn db.GamesDataWhoseturn.RemoveRange(db.GamesDataWhoseturn.Where(x => x.GameId.Equals(input.Gamerec.EntryId))); foreach (var p in input.Gameobj.Whoseturn()) { var node = new GamesDataWhoseturn { GameId = input.Gamerec.EntryId, OwnerId = GuidGenerator.HelperStringToBA(PlayerId2OwnerId(db, p)) }; db.GamesDataWhoseturn.Add(node); } //Check for end of game if (input.Gameobj.Gameover) { input.Gamerec.Closed = true; //Archive the game List <GamesDataStates> allstates = db.GamesDataStates.Where(x => x.GameId.Equals(input.Gamerec.EntryId)).ToList(); allstates.Add(newstate); JObject rec = JObject.FromObject(new { header = new { reportId = GuidGenerator.HelperBAToString(input.Gamerec.EntryId), game = new { id = GuidGenerator.HelperBAToString(input.Gamerec.GameMetaId), name = input.Gamerec.GameMeta.Name, variants = String.IsNullOrWhiteSpace(input.Gamerec.Variants) ? new string[0] : input.Gamerec.Variants.Split('\n'), }, dates = new { start = allstates.First().Timestamp, end = allstates.Last().Timestamp.Truncate(TimeSpan.FromSeconds(1)) }, //Add `event` one day timeControl = String.Join('/', new string[] { input.Gamerec.ClockStart.ToString(), input.Gamerec.ClockInc.ToString(), input.Gamerec.ClockMax.ToString() }), players = input.Gameobj.Players, results = input.Gameobj.Results() //Add `termination` one day }, moves = input.Gameobj.MovesArchive(allstates.Select(x => x.State).ToArray()) } ); GamesArchive archive = new GamesArchive { ReportId = input.Gamerec.EntryId, Json = rec.ToString() }; db.GamesArchive.Add(archive); } }