private void ProcessSet(string response) { Clients.Caller.updateRequestProgress("Processing set data..."); var set = JsonConvert.DeserializeObject<CardSet>(response, new CardSetConverter()); using (var context = new MagicDbContext()) { context.InsertOrUpdate(set); } Clients.Caller.updateRequestProgress("Set data saved!"); }
public void AddCard(string cardId, PlayerCardLocation location) { using (var context = new MagicDbContext()) { var card = context.Cards.Find(cardId); Cards.Add(new PlayerCard { Card = card, GameId = GameId, UserId = UserId, Index = Cards.Count + 1, Location = location, }); context.InsertOrUpdate(this); } }
public void TogglePlayerReady(string gameId, bool isReady) { var userId = Context.User.Identity.GetUserId(); using (var context = new MagicDbContext()) { var player = context.Players.Find(userId, gameId); //DisplayPlayerReady(player.User, gameId, isReady); player.Status = isReady ? PlayerStatus.Ready : PlayerStatus.Unready; context.InsertOrUpdate(player, true); Clients.Group(gameId).togglePlayerReady(player.User.UserName, (isReady ? player.User.ColorCode : "")); var chatHubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); chatHubContext.Clients.Group(gameId).addMessage(gameId, DateTime.Now.ToString("HH:mm:ss"), player.User.UserName, player.User.ColorCode, (isReady ? " is ready for action." : " seems to be not prepared!")); if (!isReady || (player.Game.Players.Count(p => p.Player.Status == PlayerStatus.Ready) != player.Game.PlayerCapacity)) return; StartGame(gameId); } }
public static async Task PauseGame(User user, string gameId, DateTime dateSuspended, CancellationToken token) { using (var context = new MagicDbContext()) { var gameHubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>(); gameHubContext.Clients.Group(gameId).pauseGame("has paused the game.", user.UserName, user.ColorCode); var pause = Task.Delay(10000, token); var chatRoom = context.ChatRooms.Find(gameId); var chatHubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); chatHubContext.Clients.Group(gameId).addMessage(gameId, DateTime.Now.ToString("HH:mm:ss"), user.UserName, user.ColorCode, "has paused the game."); var game = context.Games.Find(gameId); game.UpdateTimePlayed(dateSuspended); try { await pause; } catch (OperationCanceledException) { } finally { game.DateResumed = DateTime.Now; gameHubContext.Clients.Group(gameId).activateGame(game.TimePlayed.ToTotalHoursString()); context.InsertOrUpdate(game, true); } } }
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); } } }
public static void ResetReadyStatus(string gameId) { using (var context = new MagicDbContext()) { var game = context.Games.Find(gameId); foreach (var player in game.Players.Select(p => p.Player)) { player.Status = PlayerStatus.Unready; } context.InsertOrUpdate(game, true); } var gameHubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>(); gameHubContext.Clients.Group(gameId).resetReadyStatus(); }
public void StartGame(string gameId) { System.Diagnostics.Debug.WriteLine("LET THE GAMES BEGIN!"); using (var context = new MagicDbContext()) { var game = context.Games.Find(gameId); if (game.DateStarted.HasValue) { game.DateResumed = DateTime.Now; } else { game.DateStarted = DateTime.Now; } foreach (var player in game.Players) { player.Status = GameStatus.InProgress; } context.InsertOrUpdate(game, true); Clients.Group(gameId).activateGame(); } }
public void DrawHand(int cards = 7) { using (var context = new MagicDbContext()) { Shuffle(new Random()); for (var i = 0; i < cards; i++) { Cards.ElementAt(i).Location = PlayerCardLocation.Hand; } context.InsertOrUpdate(this); } }
public void PutCardToGraveyard(int index) { using (var context = new MagicDbContext()) { Cards.ElementAt(index).Location = PlayerCardLocation.Graveyard; context.InsertOrUpdate(this); } }
private async void ProcessCards(string response) { Clients.Caller.updateRequestProgress("Processing card data..."); var cards = JArray.Parse(response).Select(c => JsonConvert.DeserializeObject<Card>(c.ToString(), new CardConverter())).ToList(); foreach (var card in cards) { var path = FetchCardImage(card.MultiverseId, card.Id); using (var context = new MagicDbContext()) { card.AssignTypes(context); card.DecodeManaCost(context); card.Image = await path; card.ImagePreview = card.Image.Replace(".jpg", ".jpeg"); context.InsertOrUpdate(card); } } Clients.Caller.updateRequestProgress("Card data saved!"); }
public void SetAsGameConnection(string gameId) { var userId = Context.User.Identity.GetUserId(); using (var context = new MagicDbContext()) { var connection = context.Connections.Find(Context.ConnectionId, userId); connection.GameId = gameId; context.InsertOrUpdate(connection, true); } }
public static void UserStatusUpdate(string userId, UserStatus status, string roomId) { using (var context = new MagicDbContext()) { var user = context.Users.Find(userId); user.Status = status; context.InsertOrUpdate(user, true); var chatHubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); if (roomId.Length > 0) { chatHubContext.Clients.Group(roomId).addMessage(roomId, DateTime.Now.ToString("HH:mm:ss"), user.UserName, user.ColorCode, UserStatusBroadcastMessage(status)); } else { chatHubContext.Clients.All.addMessage(roomId, DateTime.Now.ToString("HH:mm:ss"), user.UserName, user.ColorCode, UserStatusBroadcastMessage(status)); } } }
public void Send(string messageText, string roomId) { using (var context = new MagicDbContext()) { var userId = Context.User.Identity.GetUserId(); var sender = context.Users.Find(userId); var message = new ChatMessage(messageText) { Sender = sender, Message = messageText }; var chatRoom = context.ChatRooms.Include(r => r.Users.Select(u => u.User)).First(r => r.Id == roomId); foreach (var user in chatRoom.Users) { SubscribeActiveConnections(chatRoom.Id, user.UserId); } Clients.Group(roomId).addMessage(roomId, message.TimeSend.ToString("HH:mm:ss"), message.Sender.UserName, message.Sender.ColorCode, message.Message); chatRoom.AddMessageToLog(message); context.InsertOrUpdate(chatRoom); foreach (var notification in chatRoom.Users.Where(u => u.User.Status == UserStatus.Offline).Select(u => new ChatMessageNotification { RecipientId = u.UserId, MessageId = message.Id, LogId = message.LogId })) { context.Insert(notification); } } }