// This function is called by schedule from Global.asax and uses the static context to save recent chat messages. //public static bool SaveChatLogToDatabase(ChatLog currentLog) //{ // ChatLog todayLog = context.ChatLogs.Find(currentLog.DateCreated);//context.Set<ChatLog>().AsNoTracking().FirstOrDefault(c => c.DateCreated == currentLog.DateCreated); // if (todayLog != null) // { // todayLog.AppendMessages(currentLog.Messages); // return context.InsertOrUpdate(todayLog, true); // } // // Create new log for Today. // todayLog = new ChatLog // { // Messages = currentLog.Messages // }; // return context.Insert(todayLog); //} #endregion CHATLOG HANDLING #region REMOVE INACTIVE USERS public static void RemoveInactiveConnections() { using (var context = new MagicDbContext()) { foreach (var connection in context.Connections) { context.Delete(connection); } } //context.Database.ExecuteSqlCommand("TRUNCATE TABLE [AspNetUserConnections]"); }
public async static Task LeaveGame(UserConnection connection) { var dateSuspended = DateTime.Now; var gameHubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>(); gameHubContext.Groups.Remove(connection.Id, connection.GameId); gameHubContext.Clients.Group(connection.GameId).userLeft(connection.User.UserName); using (var context = new MagicDbContext()) { var game = context.Games.Find(connection.GameId); if (game.Players.All(p => p.UserId != connection.UserId)) { // Remove observer who left. game.Observers.Remove(game.Observers.First(o => o.Id == connection.UserId)); return; } if (game.DateStarted.HasValue && !game.DateEnded.HasValue) { // TODO: STOP THE GAME, A PLAYER IS MISSING! Ask players to start game timeout? game.UpdateTimePlayed(dateSuspended); game.DateResumed = null; gameHubContext.Clients.Group(connection.GameId).pauseGame("has fled the battle!", connection.User.UserName, connection.User.ColorCode); var chatHubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); chatHubContext.Clients.Group(connection.GameId).addMessage(connection.GameId, DateTime.Now.ToString("HH:mm:ss"), connection.User.UserName, connection.User.ColorCode, "has fled the battle, the game will be interrupted."); var playerStatus = game.Players.First(ps => ps.UserId == connection.UserId); playerStatus.Status = GameStatus.Unfinished; playerStatus.Player.Status = PlayerStatus.Missing; context.InsertOrUpdate(game, true); } else { var playerStatus = context.GameStatuses.Find(connection.UserId, connection.GameId); context.Delete(playerStatus, true); ResetReadyStatus(game.Id); } } }
private async void DeleteConnection() { await Task.Delay(1500); using (var context = new MagicDbContext()) { var connection = context.Connections.FirstOrDefault(c => c.Id == Context.ConnectionId); if (connection == null) return; if (!string.IsNullOrWhiteSpace(connection.GameId)) { UnsubscribeGameChat(); } System.Diagnostics.Debug.WriteLine("Disconnected: " + connection.Id); var isLastConnection = connection.User.Connections.Count == 1; if (!isLastConnection) { context.Delete(connection, true); return; } // If this is the user's last connection update chat room users. var userId = connection.User.Id; var chatRooms = GetUserChatRooms(userId); context.Delete(connection, true); UserStatusUpdate(userId, UserStatus.Offline, DefaultRoomId); foreach (var chatRoom in chatRooms) { UpdateChatRoomUsers(chatRoom.Id); } //if (connection.GetType() == typeof(ApplicationUserGameConnection)) //{ // //SubscribeGameChat(connection.ChatRoomId, false); // //GameHub.DisplayUserLeft(connection.User.UserName, connection.ChatRoomId); // GameHub.LeaveGame((ApplicationUserGameConnection) connection); //} } }