public void DoSideEffects(Player pacman, Player ghost, PacManDataContext dataContext) { if (pacman.Game.StateEnum == GameState.SuperPacMan) { return; } var pacMenWhichWereNotKilledYet = dataContext .Players .Where(x => x.GameId == pacman.GameId) .Where(x => x.Role == (int)PlayerRole.PacMan) .Where(x => x.State != (int)PlayerState.JoinedEatenNotPlaying) .Where(x => x.Id != pacman.Id); if(pacMenWhichWereNotKilledYet.Any()) { return; } dataContext.AddGameEvent(EventType.GameEnded, string.Format("Game Over. Ghost {0} ate the last PacMan {1}. Ghosts win!", ghost.Name, pacman.Name), ghost.Id, ghost.GameId); pacman.Game.StateEnum = GameState.GameOver; pacman.Game.StateChangeTime = DateTime.Now; }
public GameEvent[] GetAllGameEvents(int gameId) { using (var dbConnection = _ConnectionProvider.GetConnection()) using (var pacManDataContext = new PacManDataContext(dbConnection)) { return pacManDataContext.GameEvents.Where(x => x.GameId == gameId).ToArray(); } }
public void ThrowErrorIfInvalid(PacManDataContext pacManDataContext, string gameName, string entityName, PlayerRole role) { var gameExists= pacManDataContext.Games.Any(x => x.Name == gameName); if(gameExists==false) { throw new ArgumentException(string.Format("Game '{0}' does not exists", gameName)); } }
public void DoSideEffects(Player pacman, Player ghost, PacManDataContext dataContext) { if (pacman.Game.StateEnum == GameState.SuperPacMan) { pacman.Score += ScoreForEatingGhost; pacman.Game.Score += ScoreForEatingGhost; } }
public static void Clear() { using (var sqlConnection = new SqlConnection(new ConnectionStringProvider().GetConnectionString())) using (var pacManDataContext = new PacManDataContext(sqlConnection)) { pacManDataContext.Games.DeleteAllOnSubmit(pacManDataContext.Games); pacManDataContext.Entities.DeleteAllOnSubmit(pacManDataContext.Entities); pacManDataContext.Orders.DeleteAllOnSubmit(pacManDataContext.Orders); pacManDataContext.SubmitChanges(); } }
public void ThrowErrorIfInvalid(PacManDataContext pacManDataContext, string gameName, string entityName, PlayerRole role) { var allPlayersHaveDifferentNames = pacManDataContext .Games .Single(x => x.Name == gameName) .Players .All(x => x.Name != entityName); if (allPlayersHaveDifferentNames == false) { throw new InvalidOperationException(string.Format("There is already a player called {0} in the game", entityName)); } }
public Player GetEntityById(int entityId) { using (var dbConnection = _ConnectionProvider.GetConnection()) using (var pacManDataContext = new PacManDataContext(dbConnection)) { var dataLoadOptions = new DataLoadOptions(); dataLoadOptions.LoadWith<Player>(x => x.Game); pacManDataContext.LoadOptions = dataLoadOptions; return pacManDataContext .Players .Where(x => x.Id == entityId) .FirstOrDefault(); } }
public void DoSideEffects(Player pacman, Player ghost, PacManDataContext dataContext) { if(pacman.Game.StateEnum == GameState.SuperPacMan) { ghost.StateEnum = PlayerState.JoinedEatenNotPlaying; dataContext.AddGameEvent(EventType.PacManAteGhost, string.Format("PacMan {0} ate ghost {1}", pacman.Name, ghost.Name),pacman.Id,pacman.GameId); } else { pacman.StateEnum = PlayerState.JoinedEatenNotPlaying; dataContext.AddGameEvent(EventType.GhostAtePacMan, string.Format("Ghost {0} ate PacMan {1}", ghost.Name, pacman.Name), ghost.Id, ghost.GameId); pacman.Game.StateEnum = GameState.WatingForNextPacManToStart; pacman.Game.StateChangeTime = DateTime.Now; } }
public void ThrowErrorIfInvalid(PacManDataContext pacManDataContext, string gameName, string entityName, PlayerRole role) { if (role != PlayerRole.Ghost) { return ; } var tooManyGhosts= pacManDataContext .Games .Single(x => x.Name == gameName) .Players .Where(x => x.RoleEnum == PlayerRole.Ghost) .Count() >= 8; if(tooManyGhosts) { throw new InvalidOperationException("There are already 8 ghosts in the game. Cannot join as ghost."); } }
public void ThrowErrorIfInvalid(PacManDataContext pacManDataContext, string gameName, string entityName, PlayerRole role) { if(role==PlayerRole.Observer) { return; } var gameStateEnum = pacManDataContext .Games .Single(x => x.Name == gameName) .StateEnum; bool valigGameState = gameStateEnum == GameState.Created || gameStateEnum == GameState.WaitingForMorePlayersToJoin; if(valigGameState==false) { throw new InvalidOperationException(string.Format("Game '{0}' state is {1}, and you cannot join it", gameName, gameStateEnum)); } }
public Player UpdateEntity(int entityId, bool isEnabled, double geoLat, double geoLng) { using (var dbConnection = _ConnectionProvider.GetConnection()) using (var pacManDataContext = new PacManDataContext(dbConnection)) { var dataLoadOptions = new DataLoadOptions(); dataLoadOptions.LoadWith<Player>(x => x.Game); pacManDataContext.LoadOptions = dataLoadOptions; var updateEntity = pacManDataContext .Players .Where(x => x.Id == entityId) .First(); updateEntity.State = (int)(isEnabled?PlayerState.JoinedActivePlaying:PlayerState.JoinedDisabled); updateEntity.LastGeoLat = geoLat; updateEntity.LastGeoLon = geoLng; updateEntity.LastCommunicationAt = DateTime.Now; pacManDataContext.SubmitChanges(); return updateEntity; } }
public void Handle(PacManDataContext dataContext, ServerPollParameters serverPollParameters, ref bool shouldContinueHandling) { if(serverPollParameters.IsEnabled==false) { return; } var player = dataContext.Players.Where(x=>x.Id==serverPollParameters.EntityId).Single(); if(player.RoleEnum!=PlayerRole.PacMan) { return; } var game = player.Game; if (game.StateEnum != GameState.WatingForNextPacManToStart && game.StateEnum != GameState.WaitingForMorePlayersToJoin) { return; } game.StateEnum = GameState.Started; game.StateChangeTime = DateTime.Now; dataContext.AddGameEvent(EventType.GameStateIsNormalPacMan, player.Name + " started the game", player.Id, player.GameId); }
public void ThrowErrorIfInvalid(PacManDataContext pacManDataContext, string gameName, string entityName, PlayerRole role) { if (string.IsNullOrEmpty(entityName)) { throw new ArgumentException("Player name must not be null or empty string"); } }