public async Task MoveGameToArena(GameResultModel gameModel, ArenaSessionModel arenaModel) { using (var context = dbContext()) { var sourceArena = context.ArenaSessions.Query().First(x => x.Id == gameModel.ArenaSessionId); var targetarena = context.ArenaSessions.Query().First(x => x.Id == arenaModel.Id); var game = context.Games.Query().First(x => x.Id == gameModel.Id); if (sourceArena.Games.Contains(game)) { sourceArena.Games.Remove(game); if (game.Victory) { sourceArena.Wins--; } else { sourceArena.Losses--; } } if (!(Equals(targetarena.Hero, game.Hero))) { game.Hero = targetarena.Hero; } AddGameToArena(game, targetarena); SetEndDateIfNeeded(targetarena); await context.SaveChangesAsync(); var latestId = context.ArenaSessions.OrderByDescending(x => x.StartDate).Select(x => x.Id).FirstOrDefault(); events.PublishOnBackgroundThread(new ArenaSessionUpdated(sourceArena.Id, latestId == sourceArena.Id)); events.PublishOnBackgroundThread(new ArenaSessionUpdated(targetarena.Id, latestId == targetarena.Id)); events.PublishOnBackgroundThread(new GameResultUpdated(gameModel.Id, game.ArenaSessionId)); } }
/// <summary> /// 検索のコマンド実行 /// </summary> public void Execute() { // 初期化 ColResult = new ObservableCollection <DataGridResult>(); // 一応リロード var displayTuple = Load(); ColPerson = displayTuple.Item1; ColRule = displayTuple.Item2; ColGame = displayTuple.Item3; // クラスを作って var gameResultModel = new GameResultModel() { // オブジェクトを渡して ColPerson = ColPerson, ColRule = ColRule, ColGame = ColGame, FindFrom = FindFrom, FindTo = FindTo }; // 動かして戻す ColResult = gameResultModel.Execute(); }
public async Task AddGame(GameResultModel gameModel) { using (var context = dbContext()) { var game = new GameResult(); game.InjectFrom(gameModel); if (gameModel.Hero != null) { game.Hero = context.Heroes.Find(gameModel.Hero.Id); } if (gameModel.OpponentHero != null) { game.OpponentHero = context.Heroes.Find(gameModel.OpponentHero.Id); } ArenaSessionModel arenaModel = null; ArenaSession arena = null; if (gameModel.ArenaSession != null) { gameModel.Deck = null; game.DeckKey = null; game.Deck = null; arenaModel = gameModel.ArenaSession; arena = context.ArenaSessions.Query().FirstOrDefault(x => x.Id == arenaModel.Id); if (arena == null) { throw new InvalidOperationException("Add arena using gameManager first!"); } // context.Entry(arena).CurrentValues.SetValues(arenaModel); AddGameToArena(game, arena); SetEndDateIfNeeded(arena); arena.Modified = DateTime.Now; } if (gameModel.Deck != null) { game.Deck = context.Decks.Find(gameModel.Deck.Id); } context.Games.Add(game); await context.SaveChangesAsync(); gameModel.InjectFrom(game); events.PublishOnBackgroundThread(new GameResultAdded(this, gameModel)); if (arenaModel != null) { arenaModel.InjectFrom(arena); gameModel.ArenaSession = arenaModel; arenaModel.Games.Add(gameModel); var latestId = context.ArenaSessions.OrderByDescending(x => x.StartDate).Select(x => x.Id).FirstOrDefault(); events.PublishOnBackgroundThread(new ArenaSessionUpdated(arenaModel.Id, latestId == arenaModel.Id)); } } }
public void Load(GameResultModel gameResultModel) { LoadGameResult(gameResultModel); //if (IsOpen) //{ // IsOpen = false; //} IsOpen = true; events.PublishOnUIThread(new SelectedGameChanged(this, gameResultModel)); }
private void Restart() { Messenger.RemoveListener(Signals.TargetOut(), Restart); int newStage = 0; GameResultModel resultModel = new GameResultModel(newStage, false); Messenger.Broadcast <IGameResultModel>(Signals.GameResult(), resultModel); StartGame(); }
private void NextStage() { Messenger.RemoveListener(Signals.TargetOut(), NextStage); int newStage = _stage + 1; GameResultModel resultModel = new GameResultModel(newStage, true); Messenger.Broadcast <IGameResultModel>(Signals.GameResult(), resultModel); StartGame(newStage); }
public void SetGameResult(GameResultModel gameResult) { if (gameResult == null) { throw new ArgumentNullException("gameResult"); } this.gameResult = gameResult; this.Hero = gameResult.Hero; this.OpponentHero = gameResult.OpponentHero; this.Victory = gameResult.Victory; this.GameMode = gameResult.GameMode; this.Turns = gameResult.Turns; }
public Task <HttpResponseMessage> Post(GameResultModel gameResult) { HttpResponseMessage response = new HttpResponseMessage(); try { var balance = _service.SaveGameResult(gameResult.GetPlayerId(), gameResult.GameDetails); response = Request.CreateResponse(HttpStatusCode.OK, new { Balance = balance.Points }); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); } var task = new TaskCompletionSource <HttpResponseMessage>(); task.SetResult(response); return(task.Task); }
/// <summary>The set game result.</summary> /// <param name="context">The context.</param> /// <param name="game">The game.</param> private void SetGameResult(GameResultModel game) { game.GameMode = this.GameMode; if (this.SelectedDeck != null) { game.Deck = deckManager.GetDeckById(this.SelectedDeck.Id); } game.GameMode = this.GameMode; game.GoFirst = this.GoFirst; game.Hero = this.Hero; game.OpponentHero = this.OpponentHero; game.Notes = this.Notes; game.Started = this.StartTime; game.Stopped = this.EndTime; game.Victory = this.Victory; game.Turns = this.Turns; game.Notes = this.Notes; game.Conceded = this.Conceded; game.Server = this.SelectedServer.Name; }
private GameResultModel GetGameResult(Guid arenaId, Guid gameId, out int index) { index = -1; GameResultModel found = null; foreach (var arena in arenaSessions) { index++; if (arena.Id != arenaId) { continue; } found = arena.Games.FirstOrDefault(x => x.Id == gameId); if (found != null) { break; } } return(found); }
/// <summary>The set game result.</summary> /// <param name="context">The context.</param> /// <param name="game">The game.</param> private void SetGameResult(GameResultModel game) { game.GameMode = GameMode; if (SelectedDeck != null) { game.Deck = deckManager.GetDeckById(SelectedDeck.Id); } game.GameMode = GameMode; game.GoFirst = GoFirst; game.Hero = Hero; game.OpponentHero = OpponentHero; game.Notes = Notes; game.Started = StartTime; game.Stopped = EndTime; game.Victory = Victory; game.Turns = Turns; game.Notes = Notes; game.Conceded = Conceded; game.Server = SelectedServer.Name; }
private void LoadGameResult(GameResultModel game) { EnsureInitialized(); SelectedGame = game; Hero = game.Hero; OpponentHero = game.OpponentHero; StartTime = game.Started; EndTime = game.Stopped; Victory = game.Victory; GoFirst = game.GoFirst; GameMode = game.GameMode; // force notify even if not changed NotifyOfPropertyChange(() => GameMode); Notes = game.Notes; Turns = game.Turns; ArenaSession = game.ArenaSession; LastGameId = game.Id; Conceded = game.Conceded; SelectedServer = servers.FirstOrDefault(x => x.Name == game.Server); if (game.Deck != null) { if (game.Deck.Deleted && Decks.All(x => x.Id != game.Deck.Id)) { var model = game.Deck.ToModel(); model.Name += " (deleted)"; Decks.Insert(0, model); } SelectedDeck = Decks.FirstOrDefault(x => x.Id == game.Deck.Id); } NotifyOfPropertyChange(() => CanSaveAsNew); //Execute.OnUIThread( // () => // { // var v = (UIElement)this.GetView(); // Panel.SetZIndex(v, 10); // }); }
public async Task AssignGameToArena(GameResultModel gameModel, ArenaSessionModel arenaModel) { using (var context = dbContext()) { // var lastGame = context.Games.Where(x => x.ArenaSessionId == arenaModel.Id).OrderByDescending(x => x.ArenaGameNo).FirstOrDefault(); var arena = context.ArenaSessions.Query().First(x => x.Id == arenaModel.Id); var game = context.Games.Query().First(x => x.Id == gameModel.Id); // game.ArenaGameNo = lastGame == null ? 1 : lastGame.ArenaGameNo + 1; AddGameToArena(game, arena); SetEndDateIfNeeded(arena); arena.Modified = DateTime.Now; game.Modified = DateTime.Now; await context.SaveChangesAsync(); gameModel.InjectFrom(game); arenaModel.InjectFrom(arena); gameModel.ArenaSession = arenaModel; arenaModel.Games.Add(gameModel); } }
public HttpResponseMessage Register([FromBody] GameResultModel gameResultModel) { String name = HttpContext.Current.User.Identity.Name; User user = userRepository.GetByFacebookId(gameResultModel.FacebookID); if (user != null && user.Name.Equals(name)) { if (user.Record < gameResultModel.Score) { user = userRepository.SetNewRecord(gameResultModel.Score, gameResultModel.FacebookID); } GameResult gameResult = new GameResult(user, gameResultModel.Score); if (gameResult.Validate()) { gameResult = gameResultRepository.RegisterNewGame(gameResult); GameResultModelReturn answerObject = new GameResultModelReturn(gameResult.Id, gameResult.User.FacebookId, gameResult.Score, gameResult.GameDate); return(ResponderOK(answerObject)); } return(ResponderErro(gameResult.Messages)); } return(ResponderErro("Usuário inválido")); }
public async Task Save() { using (var busy = Busy.GetTicket()) { GameResultModel gameResult = null; var added = false; if (LastGameId == null) { gameResult = new GameResultModel(); added = true; } else { // gameResult = (await gameRepository.FirstOrDefaultAsync(x => x.Id == this.LastGameId)).ToModel(); gameResult = SelectedGame; } SetGameResult(gameResult); if (ArenaSession != null) { gameResult.ArenaSession = ArenaSession; } if (added) { await gameManager.AddGame(gameResult); } else { await gameManager.UpdateGame(gameResult); } events.PublishOnBackgroundThread(new SendNotification("Game successfully saved.")); LastGameId = gameResult.Id; LoadGameResult(gameResult); } }
public GameResultAdded(object source, GameResultModel gameResult) { Source = source; GameResult = gameResult; }
public GameResultAdded(object source, GameResultModel gameResult) { this.Source = source; this.GameResult = gameResult; }
public async Task UpdateGame(GameResultModel gameModel) { using (var context = dbContext()) { var game = context.Games.FirstOrDefault(x => x.Id == gameModel.Id); if (game == null) { throw new ArgumentException("game does not exist", "gameModel"); } var oldVictory = game.Victory; if (gameModel.ArenaSession != null) { gameModel.Deck = null; } context.Entry(game).CurrentValues.SetValues(gameModel); if (!Equals(gameModel.Hero, game.Hero) && gameModel.Hero != null) { game.Hero = context.Heroes.Find(gameModel.Hero.Id); } if (!Equals(gameModel.OpponentHero, game.OpponentHero) && gameModel.OpponentHero != null) { game.OpponentHero = context.Heroes.Find(gameModel.OpponentHero.Id); } if (!Equals(gameModel.Deck, game.Deck) && gameModel.Deck != null) { game.Deck = context.Decks.Find(gameModel.Deck.Id); } game.Modified = DateTime.Now; await context.SaveChangesAsync(); ArenaSession arena = null; if (gameModel.ArenaSession != null) { arena = context.ArenaSessions.Query().First(x => x.Id == game.ArenaSessionId); if (game.Victory && !oldVictory) { arena.Wins++; arena.Losses--; } else if (!game.Victory && oldVictory) { arena.Wins--; arena.Losses++; } SetEndDateIfNeeded(arena); gameModel.ArenaSession.InjectFrom(arena); arena.Modified = DateTime.Now; await context.SaveChangesAsync(); } gameModel.InjectFrom(game); events.PublishOnBackgroundThread(new GameResultUpdated(gameModel.Id, game.ArenaSessionId)); if (gameModel.ArenaSession != null) { events.PublishOnBackgroundThread(new ArenaSessionUpdated(gameModel.ArenaSession.Id)); } } }
public SelectedGameChanged(object source, GameResultModel game) { Source = source; Game = game; }
public JsonResult SubmitAction(int gameResultID, int playerID, string gameActionString) { GameResultPlayer gameResult = null; using (RockPaperWinnersContext context = new RockPaperWinnersContext()) { GameAction gameAction; gameResult = context.GameResultPlayers.Where(g => g.GameResultID == gameResultID && g.UserID == playerID).FirstOrDefault(); // If there is already an action, just end if (gameResult.Action.HasValue) { return(Json("This action has already been processed", JsonRequestBehavior.AllowGet)); } // If the action supplied is invalid, just end if (!Enum.TryParse(gameActionString, true, out gameAction)) { return(Json("Invalid action supplied", JsonRequestBehavior.AllowGet)); } gameResult.Action = gameAction; context.SaveChanges(); } // Record the current datetimeutc, set variable to be a time when we will give up trying to find someone DateTime waitUntilTime = DateTime.UtcNow.AddSeconds(15); // BEGIN TRAN // While not timed out, check the game result entities to find any active game results with my id while (DateTime.UtcNow <= waitUntilTime) { using (RockPaperWinnersContext context = new RockPaperWinnersContext()) { // Get the opponents action var opponentGameResult = context.GameResultPlayers.Where(g => g.GameResultID == gameResultID && g.UserID != playerID).FirstOrDefault(); if (opponentGameResult != null && opponentGameResult.Action.HasValue && !opponentGameResult.ResultOutcome.HasValue) { int winner = (3 + (int)gameResult.Action.Value - (int)opponentGameResult.Action.Value) % 3; var myProfile = context.UserProfiles.Find(playerID); var opponentProfile = context.UserProfiles.Where(u => u.ID == opponentGameResult.UserID).FirstOrDefault(); switch (winner) { case 1: gameResult.ResultOutcome = GamePlayerResultOutcome.IWin; opponentGameResult.ResultOutcome = GamePlayerResultOutcome.OpponentWin; myProfile.Money = myProfile.Money + gameResult.BetAmount; opponentProfile.Money = opponentProfile.Money - opponentGameResult.BetAmount; context.SaveChanges(); break; case 2: gameResult.ResultOutcome = GamePlayerResultOutcome.OpponentWin; opponentGameResult.ResultOutcome = GamePlayerResultOutcome.IWin; myProfile.Money = myProfile.Money - gameResult.BetAmount; opponentProfile.Money = opponentProfile.Money + opponentGameResult.BetAmount; context.SaveChanges(); break; case 0: gameResult.ResultOutcome = GamePlayerResultOutcome.Draw; opponentGameResult.ResultOutcome = GamePlayerResultOutcome.Draw; break; } var game = context.GameResults.Find(gameResultID); game.IsActive = false; var activeUser = context.ActiveUsers.Where(u => u.UserID == playerID).FirstOrDefault(); if (activeUser != null) { context.ActiveUsers.Remove(activeUser); } context.SaveChanges(); var result = new GameResultModel() { GameResult = (int)gameResult.ResultOutcome, MyFunds = myProfile.Money }; return(Json(result, JsonRequestBehavior.AllowGet)); } else if (opponentGameResult != null && opponentGameResult.Action.HasValue && opponentGameResult.ResultOutcome.HasValue) { var activeUser = context.ActiveUsers.Where(u => u.UserID == playerID).FirstOrDefault(); if (activeUser != null) { context.ActiveUsers.Remove(activeUser); } context.SaveChanges(); var result = new GameResultModel() { GameResult = (int)gameResult.ResultOutcome, MyFunds = context.UserProfiles.Find(playerID).Money }; return(Json(result, JsonRequestBehavior.AllowGet)); } else if (opponentGameResult != null && opponentGameResult.Action.HasValue) { return(Json("Has Opponent Action, but result outcome is screwed.", JsonRequestBehavior.AllowGet)); } else if (opponentGameResult == null) { return(Json("opponent game result is null???", JsonRequestBehavior.AllowGet)); } } // Nobody found, so try again in 1 seconds Thread.Sleep(1000); } // The opponent has buggered off, or something went wrong using (RockPaperWinnersContext context = new RockPaperWinnersContext()) { var gameResultPlayers = context.GameResultPlayers.Where(g => g.GameResultID == gameResultID).ToList(); // Set the game result as a draw and remove both players from active users as one has gone and the other needs to find a new opponent foreach (var player in gameResultPlayers) { player.ResultOutcome = GamePlayerResultOutcome.Draw; context.SaveChanges(); var activeUser = context.ActiveUsers.Where(u => u.UserID == player.UserID).FirstOrDefault(); context.ActiveUsers.Remove(activeUser); context.SaveChanges(); } // Mark game as inactive var game = context.GameResults.Find(gameResultID); game.IsActive = false; context.SaveChanges(); // Return the draw result to the person who submitted the action var result = new GameResultModel() { GameResult = 3, MyFunds = context.UserProfiles.Find(playerID).Money }; return(Json(result, JsonRequestBehavior.AllowGet)); } }
private async Task Save() { using (var bsy = Busy.GetTicket()) { var gameResult = new GameResultModel(); ArenaSessionModel arenasession = null; var newArena = false; gameResult.GameMode = GameMode; gameResult.Conceded = Conceded; if (Deck != null) { gameResult.Deck = Deck; } gameResult.GoFirst = GoFirst; gameResult.Hero = Hero; gameResult.OpponentHero = OpponentHero; gameResult.Started = StartTime; gameResult.Stopped = Stopped; gameResult.Turns = Turns; gameResult.Victory = Victory; gameResult.Notes = Notes; gameResult.Server = BindableServerCollection.Instance.DefaultName; if (gameResult.GameMode == GameMode.Arena) { var serverName = gameResult.Server; var latestArena = arenaRepository.Query(a => a.Where(x => x.Server == serverName).OrderByDescending(x => x.StartDate).FirstOrDefault()); if (latestArena == null || latestArena.IsEnded || (gameResult.Hero != null && latestArena.Hero.Key != gameResult.Hero.Key)) { Log.Debug("Creating new arena."); newArena = true; arenasession = new ArenaSessionModel { Hero = gameResult.Hero, StartDate = gameResult.Started }; try { GlobalLocks.NewArenaLock.Reset(); await gameManager.AddArenaSession(arenasession); } finally { GlobalLocks.NewArenaLock.Set(); } } else { arenasession = latestArena.ToModel(); } gameResult.ArenaSession = arenasession; } await gameManager.AddGame(gameResult); // for webapi if (arenasession != null) { if (newArena) { events.PublishOnBackgroundThread(new ArenaSessionStarted(arenasession.StartDate, arenasession.Hero.Key, arenasession.Wins, arenasession.Losses)); } else { if (arenasession.IsEnded && arenasession.EndDate.HasValue) { events.PublishOnBackgroundThread(new ArenaSessionEnded(arenasession.StartDate, arenasession.EndDate.Value, arenasession.Hero.Key, arenasession.Wins, arenasession.Losses)); } } } // notifications //var wonString = gameResult.Victory ? "You won!" : "You lost!"; var title = "New game tracked."; //var hero = gameResult.Hero != null ? gameResult.Hero.ClassName : String.Empty; //var oppHero = gameResult.OpponentHero != null ? gameResult.OpponentHero.ClassName : String.Empty; //var msg = string.Format("Hero: {0}, Opponent: {1}, {2}", hero, oppHero, wonString); //events.PublishOnBackgroundThread(new SendNotification(String.Format("{0} {1}", title, msg))); var vm = IoC.Get <GameResultBalloonViewModel>(); vm.SetGameResult(gameResult); events.PublishOnBackgroundThread(new TrayNotification(title, vm, 10000) { BalloonType = BalloonTypes.GameStartEnd }); // reset Clear(); IsOpen = false; } }