public void Log(Guid gameId, LogLevel level, object ctx, Exception ex) { try { using (var dbctx = new GoGEntities()) { var serializedCtx = ctx == null ? null : Serialize(ctx); var exStr = ex == null ? null : ex.ToString(); // Trim to db constraint. string exTypeStr = null; if (ex != null) { var t = ex.GetType().Name; // Database takes only 50 characters. exTypeStr = t.Length > 50 ? t.Substring(0, 50) : t; } dbctx.Logs.Add(new Database.Log() { GameId = gameId == Guid.Empty ? (Guid?)null : gameId, Date = DateTimeOffset.Now, ExceptionType = exTypeStr, Level = level.ToString(), Context = serializedCtx, Exception = exStr }); dbctx.SaveChanges(); } } catch (Exception) { // eat it } }
public void RemoveGameIfExists(Guid gameId) { using (var ctx = new GoGEntities()) { var g = ctx.Games.FirstOrDefault(game => game.GameId == gameId); if (g == null) { return; } ctx.Games.Remove(g); ctx.SaveChanges(); } }
public void SaveGameState(Guid gameId, GoGameState gameState) { var newMoveHistory = gameState.GoMoveHistory; using (var ctx = new GoGEntities()) { // locate game by GameId var g = ctx.Games.FirstOrDefault(game => game.GameId == gameId); if (g == null) { // add game var newGame = new Game { BlackStones = gameState.BlackPositions, BlacksTurn = gameState.WhoseTurn == GoColor.Black, GameId = gameId, Size = gameState.Size, WhiteStones = gameState.WhitePositions, Started = DateTimeOffset.Now, LastMove = DateTimeOffset.Now, Player1Level = (byte)gameState.Player1.Level, Player2Level = (byte)gameState.Player2.Level, Player1Name = gameState.Player1.Name, Player2Name = gameState.Player2.Name, Player1Score = gameState.Player1.Score, Player2Score = gameState.Player2.Score, Player1Type = (byte)gameState.Player1.PlayerType, Player2Type = (byte)gameState.Player2.PlayerType, Status = (byte)gameState.Status, WinMargin = gameState.WinMargin }; UpdateMoves(ctx, newGame, newMoveHistory); ctx.Games.Add(newGame); } else { // update game g.BlackStones = gameState.BlackPositions; g.BlacksTurn = gameState.WhoseTurn == GoColor.Black; g.GameId = gameId; g.Size = gameState.Size; g.WhiteStones = gameState.WhitePositions; g.LastMove = DateTimeOffset.Now; g.Player1Level = (byte)gameState.Player1.Level; g.Player2Level = (byte)gameState.Player2.Level; g.Player1Name = gameState.Player1.Name; g.Player2Name = gameState.Player2.Name; g.Player1Score = gameState.Player1.Score; g.Player2Score = gameState.Player2.Score; g.Player1Type = (byte)gameState.Player1.PlayerType; g.Player2Type = (byte)gameState.Player2.PlayerType; g.Status = (byte)gameState.Status; g.WinMargin = gameState.WinMargin; UpdateMoves(ctx, g, newMoveHistory); } ctx.SaveChanges(); } }