public WerewolfGame() { using (WerewolfContext _db = new WerewolfContext()) { try { if (!_roleInitialized) { InitRoles(); } if (!_actionInitialized) { InitActions(); } //FIXME, should move to database init part } catch (Exception ex) { Console.Out.WriteLine(ex.ToString()); } } if (observers == null) { observers = new List <IObserver <WerewolfEvent> >(); } }
public List <Game> GetGames() { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <List <Game> >(_db.Games.Include(game => game.Players).ToList())); } }
public List <Action> GetActions() { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <List <Action> >(_db.Actions.Include(ar => ar.ActionRoles).ThenInclude(r => r.Role).ToList())); } }
public bool IsPlayerDead(string id) { var deadReasonArray = new[] { Player.StatusEnum.VoteDeadEnum, Player.StatusEnum.ShotDeadEnum, Player.StatusEnum.JailDeadEnum, Player.StatusEnum.HolyDeadEnum, Player.StatusEnum.KillDeadEnum, }; long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Player player = _db.Players.Where(_p => _p.Id == lid).ToList()[0]; foreach (Player.StatusEnum reason in deadReasonArray) { if (player.Status == reason) { return(true); } } return(false); } }
public Player GetPlayerByName(string name) { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Player>(_db.Players.Where(player => player.Name.ToUpper() == name.ToUpper()).ToList()[0])); } }
public List <Player> GetPlayers() { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <List <Player> >(_db.Players.ToList())); } }
public Game JoinGame(Game g, Player p) { using (WerewolfContext _db = new WerewolfContext()) { Game game = _db.Games.Where(_g => _g.Id == g.Id).Include(_game => _game.Players).ToList()[0]; if (game.Status != Game.StatusEnum.WaitingEnum) { throw new Exception("Game is already ended or running"); } List <Player> players = game.Players.ToList(); if (players.Count >= MAX_PLAYERS) { throw new Exception("Game is fulled already"); } Player player = _db.Players.Where(_p => _p.Id == p.Id).ToList()[0]; if (players.Contains(player)) { throw new Exception("User in game already"); } //player.Game = game.GameId.ToString(); player.Status = Player.StatusEnum.AliveEnum; players.Add(player); game.Players = players; _db.Games.Update(game); _db.Players.Update(player); _db.SaveChanges(); NotifyObserver(WerewolfEvent.PLAYER_JOIN, game.Id); return(DeepClone <Game>(game)); } }
private Action GetActionByName(string name) { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Action>(_db.Actions.Where(action => action.Name == name).ToList()[0])); } }
public List <Role> GetRoles() { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <List <Role> >(_db.Roles.Include(ar => ar.ActionRoles).ThenInclude(a => a.Action).ToList())); } }
public Role GetRoleByName(string name) { using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Role>(_db.Roles.Where(role => role.Name == name).ToList()[0])); } }
public void StartGame(string id) { Game game = null; using (WerewolfContext _db = new WerewolfContext()) { long lid = Int64.Parse(id); game = _db.Games.Where(g => g.Id == lid).ToList()[0]; if (game != null) { game.Status = Game.StatusEnum.PlayingEnum; game.ResetDayVoteList(); game.ResetNightVoteList(); } else { throw new Exception(); } _db.Games.Update(game); _db.SaveChanges(); } if (game != null) { NotifyObserver(WerewolfEvent.GAME_STARTED, game.Id); } }
public void AddPlayer(Player player) { using (WerewolfContext _db = new WerewolfContext()) { _db.Players.Add(player); _db.SaveChanges(); } }
public Player GetPlayer(string id) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Player>(_db.Players.Where(player => player.Id == lid).Include(player => player.Role).ToList()[0])); } }
public Game GetGame(string id) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Game>(_db.Games.Include(game => game.Players).ThenInclude(player => player.Role).Where(game => game.Id == lid).ToList()[0])); } }
public List <Player> GetPlayerByGame(string gameid) { long gid = Int64.Parse(gameid); using (WerewolfContext _db = new WerewolfContext()) { Game _game = _db.Games.Where(game => game.Id == gid).Include(game => game.Players).ToList()[0]; return(DeepClone <List <Player> >(_game.Players.ToList())); } }
public Action GetAction(string id) { // Throws exception long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Action>(_db.Actions.Where(action => action.Id == lid).Include(ar => ar.ActionRoles).ThenInclude(r => r.Role).ToList()[0])); } }
public Role GetRole(string id) { // Throws exception long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { return(DeepClone <Role>(_db.Roles.Where(role => role.Id == lid).Include(ar => ar.ActionRoles).ThenInclude(a => a.Action).ToList()[0])); } }
public Boolean IsPlayerExists(string name) { using (WerewolfContext _db = new WerewolfContext()) { if (_db.Players.Where(player => player.Name.ToUpper() == name.ToUpper()).Count() > 0) { return(true); } return(false); } }
public void SetGameDay(string id, int day) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Game game = _db.Games.Where(g => g.Id == lid).ToList()[0]; game.Day = day; _db.Games.Update(game); _db.SaveChanges(); } }
public void SetPlayerStatus(string id, Player.StatusEnum status) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Player player = _db.Players.Where(p => p.Id == lid).ToList()[0]; player.Status = status; _db.Players.Update(player); _db.SaveChanges(); } }
public void SetGameStatus(string id, Game.StatusEnum s) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Game game = _db.Games.Where(g => g.Id == lid).ToList()[0]; game.Status = s; _db.Games.Update(game); _db.SaveChanges(); } }
public Game CreateGame() { Game game = new Game(); game.Hash = Guid.NewGuid().ToString(); game.Status = Game.StatusEnum.WaitingEnum; //game.Players = new List<Player>(); game.Period = Game.PeriodEnum.ProcessingEnum; using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Add(game); _db.SaveChanges(); NotifyObserver(WerewolfEvent.GAME_CREATED, game.Id); return(DeepClone <Game>(_db.Games.OrderBy(g => g.Id).Last())); } }
public Game LeaveGame(Game g, Player p) { using (WerewolfContext _db = new WerewolfContext()) { Game game = _db.Games.Where(_g => _g.Id == g.Id).ToList()[0]; List <Player> players = game.Players.ToList(); Player player = _db.Players.Where(_p => _p.Id == p.Id).ToList()[0]; players.Remove(player); player.Status = Player.StatusEnum.NotInGameEnum; game.Players = players; _db.Games.Update(game); _db.Players.Update(player); _db.SaveChanges(); return(DeepClone <Game>(game)); } }
public void ResetGameState(string id) { long lid = long.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Game game = _db.Games.Where(g => g.Id == lid).ToList()[0]; game.KillBySerialKiller = null; game.HealedByDoctor = null; game.Jailed = null; game.ProtectedByBodyguard = null; game.ResetNightVoteList(); game.ResetDayVoteList(); _db.Games.Update(game); _db.SaveChanges(); } }
public void DeletePlayer(string id) { long lid = Int64.Parse(id); using (WerewolfContext _db = new WerewolfContext()) { Player player = _db.Players.Where(p => p.Id == lid).ToList()[0]; if (player != null) { _db.Players.Remove(player); } else { throw new Exception(); } _db.SaveChanges(); } }
public List <Action> GetActionByRoleId(string id) { long lid = Int64.Parse(id); //return DeepClone<List<Action>>(_db.Actions.Where(action => action.Roles.Any(role => role.RoleId == lid)).ToList()); using (WerewolfContext _db = new WerewolfContext()) { Role role = _db.Roles.Where(r => r.Id == lid).Include(ar => ar.ActionRoles).ThenInclude(a => a.Action).ToList()[0]; List <Action> actions = new List <Action>(); foreach (ActionRole ar in role.ActionRoles) { // ar.Action.ActionRoles = null; // ar.Action.Roles = null; actions.Add(ar.Action); } return(actions); } //return DeepClone<List<Action>>(role.Actions.ToList()); //return DeepClone<List<Action>>(_db.Roles.Where(r => r.Id == lid).Include(ar => ar.ActionRoles).ThenInclude(a => a.Action).ToList()[0].Actions.ToList()); }
public void UpdatePlayer(Player player) { using (WerewolfContext _db = new WerewolfContext()) { Player p = _db.Players.Where(pr => pr.Id == player.Id).ToList()[0]; if (p == null) { throw new Exception("User not found"); } if (p.Name != player.Name) { throw new Exception("Not allow to change name"); } if (p != null) { p.Password = player.Password; p.Session = player.Session; p.GameId = player.GameId; if (p.GameId == null) { p.Game = null; } else { p.Game = _db.Games.Where(g => g.Id == player.GameId).ToList()[0]; } if (player.Role != null) { p.Role = _db.Roles.Where(r => r.Id == player.Role.Id).ToList()[0]; } _db.Players.Update(p); _db.SaveChanges(); } else { throw new Exception("Player not found"); } } }
public Player GetPlayerBySession(string session) { List <Player> players = null; using (WerewolfContext _db = new WerewolfContext()) { //return DeepClone<Player>(_db.Players.Where(player => player.Session == session).Include(player => player.Game).ThenInclude(game => game.Players).ThenInclude(p => p.Role).ThenInclude(r => r.ActionRoles).ToList()[0]); try { players = _db.Players.Where(player => player.Session == session).Include(player => player.Game).ThenInclude(game => game.Players).ThenInclude(p => p.Role).ThenInclude(r => r.ActionRoles).ToList(); } catch (Exception ex) { throw ex; } } if (players != null && players.Count > 0) { return(DeepClone <Player>(players[0])); } return(null); }
public void DeleteGame(string id) { long lid = Int64.Parse(id); Game game = null; using (WerewolfContext _db = new WerewolfContext()) { game = _db.Games.Where(g => g.Id == lid).ToList()[0]; if (game != null) { _db.Games.Remove(game); } else { throw new Exception(); } _db.SaveChanges(); } if (game != null) { NotifyObserver(WerewolfEvent.GAME_DELETED, game.Id); } }
public OutcomeEnum PostAction(string sessionID, string actionID, string targetID) { Game game; Player player; Player target; DNWS.Werewolf.Action action; Role role; try { player = GetPlayerBySession(sessionID); } catch (Exception) { throw new Exception("Player not found."); } if (player.Status != Player.StatusEnum.AliveEnum) { throw new Exception("Player is not alived."); } try { action = GetAction(actionID); } catch (Exception) { throw new Exception("Action not found."); } try { target = GetPlayer(targetID); } catch (Exception) { throw new Exception("Target not found."); } if (target.Id == player.Id) { throw new Exception("You can't perform on yourself."); } game = GetGame(player.Game.Id.ToString()); if (game == null) { throw new Exception("Player is not in a game."); } if (game.Period == Game.PeriodEnum.ProcessingEnum) { throw new Exception("Please wait for processing."); } role = GetRole(player.Role.Id.ToString()); if (role == null) { throw new Exception("Player does not have any role."); } List <int> action_ids = role.ActionRoles.Select(ar => ar.Action.Id).ToList(); //if (!role.Actions.Contains(action)) if (!action_ids.Contains(action.Id)) { throw new Exception("Player's role does not have this action."); } if (game.Status != Game.StatusEnum.PlayingEnum) { throw new Exception("Game is not playable."); } if (game.Period == Game.PeriodEnum.DayEnum) { if (target.Status != Player.StatusEnum.AliveEnum) { return(OutcomeEnum.TargetIsNotAlivedEnum); } if (action.Name == WerewolfGame.ACTION_DAY_VOTE) { if (game.DayVoteList.ContainsKey(player.Id) && game.DayVoteList[player.Id] == target.Id) { game.DayVoteList.Remove(player.Id); } else { game.DayVoteList[player.Id] = target.Id; } using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Update(game); _db.SaveChanges(); } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_ENCHANT) { if (game.Enchanted == target) { game.Enchanted = null; } else { game.Enchanted = target; } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_JAIL) { if (game.Jailed == target) { game.Jailed = null; } else { game.Jailed = target; } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_SHOOT) { SetPlayerStatus(target.Id.ToString(), Player.StatusEnum.ShotDeadEnum); return(OutcomeEnum.TargetDeadEnum); } else if (action.Name == WerewolfGame.ACTION_HOLYWATER) { if ((new[] { WerewolfGame.ROLE_WEREWOLF, WerewolfGame.ROLE_ALPHA_WEREWOLF, WerewolfGame.ROLE_WEREWOLF_SEER, WerewolfGame.ROLE_WEREWOLF_SHAMAN }).Contains(target.Role.Name)) { SetPlayerStatus(target.Id.ToString(), Player.StatusEnum.HolyDeadEnum); return(OutcomeEnum.TargetDeadEnum); } else { SetPlayerStatus(player.Id.ToString(), Player.StatusEnum.HolyDeadEnum); return(OutcomeEnum.PlayerDeadEnum); } } } else if (game.Period == Game.PeriodEnum.NightEnum) { if (game.Jailed != null && player.Id == game.Jailed.Id) { return(OutcomeEnum.ActionPerformedEnum); } if (action.Name == WerewolfGame.ACTION_REVIVE) { if (IsPlayerDead(target.Id.ToString())) { throw new Exception("Target is not dead."); } if (game.ReviveByMedium == target) { game.ReviveByMedium = null; } else { game.ReviveByMedium = target; } return(OutcomeEnum.ActionPerformedEnum); } if (target.Status != Player.StatusEnum.AliveEnum) { throw new Exception("Target is not alived."); } if (action.Name == WerewolfGame.ACTION_NIGHT_VOTE) { if (game.NightVoteList.ContainsKey(player.Id) && game.NightVoteList[player.Id] == target.Id) { game.NightVoteList.Remove(player.Id); } else { game.NightVoteList[player.Id] = target.Id; } using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Update(game); _db.SaveChanges(); } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_GUARD) { if (game.ProtectedByBodyguard == target) { game.ProtectedByBodyguard = null; } else { game.ProtectedByBodyguard = target; } using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Update(game); _db.SaveChanges(); } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_HEAL) { if (game.HealedByDoctor == target) { game.HealedByDoctor = null; } else { game.HealedByDoctor = target; } using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Update(game); _db.SaveChanges(); } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_KILL) { if (game.KillBySerialKiller == target) { game.KillBySerialKiller = null; } else { game.KillBySerialKiller = target; } using (WerewolfContext _db = new WerewolfContext()) { _db.Games.Update(game); _db.SaveChanges(); } return(OutcomeEnum.ActionPerformedEnum); } else if (action.Name == WerewolfGame.ACTION_REVEAL) { if (target == game.Enchanted) { return(OutcomeEnum.EnchantedEnum); } return(OutcomeEnum.RevealedEnum); } else if (action.Name == WerewolfGame.ACTION_AURA) { if (new[] { WerewolfGame.ROLE_GUNNER, WerewolfGame.ROLE_JAILER, WerewolfGame.ROLE_MEDIUM, WerewolfGame.ROLE_ALPHA_WEREWOLF, WerewolfGame.ROLE_HEAD_HUNTER, WerewolfGame.ROLE_SERIAL_KILLER }.Contains(target.Name)) { return(OutcomeEnum.UnknownEnum); } else if (new[] { WerewolfGame.ROLE_WEREWOLF, WerewolfGame.ROLE_WEREWOLF_SEER, WerewolfGame.ROLE_WEREWOLF_SHAMAN }.Contains(target.Name)) { return(OutcomeEnum.WerewolfEnum); } return(OutcomeEnum.VillagerEnum); } } return(OutcomeEnum.NotValidActionEnum); }