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 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)); } }
public void AddPlayer(Player player) { using (WerewolfContext _db = new WerewolfContext()) { _db.Players.Add(player); _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 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 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 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 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 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 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); }
public void SetGameDay(string id, int day) { long lid = Int64.Parse(id); Game g = _db.Games.Where(game => game.Id == lid).ToList()[0]; g.Day = day; _db.SaveChanges(); }
private void InitRoles() { using (WerewolfContext _db = new WerewolfContext()) { if (_db == null) { return; } if (_db.Roles.Count() == 0) { _db.Roles.Add(new Role { Id = 1, Name = ROLE_SEER, Description = "Every night can uncover a role of a player they choose.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 2, Name = ROLE_AURA_SEER, Description = "Every night can uncover the aura of a player they choose.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 3, Name = ROLE_PRIEST, Description = "Can throw \"Holy water\" on a player they pick during the day once each game. If they hit a werewolf, the werewolf dies; otherwise, the priest dies instead.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 4, Name = ROLE_DOCTOR, Description = "Every night can protect a player from dying that night.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 5, Name = ROLE_WEREWOLF, Description = "Votes every night with other werewolves who to kill.", Type = Role.TypeEnum.WolfEnum }); _db.Roles.Add(new Role { Id = 6, Name = ROLE_WEREWOLF_SHAMAN, Description = "Can enchant a player every day. Overnight, that player will appear as a werewolf to seers and aura seers.", Type = Role.TypeEnum.WolfEnum }); _db.Roles.Add(new Role { Id = 7, Name = ROLE_ALPHA_WEREWOLF, Description = "Same as the Werewolf role, but if the werewolves can't decide who to kill (draw while voting), the alpha's vote is worth double.", Type = Role.TypeEnum.WolfEnum }); _db.Roles.Add(new Role { Id = 8, Name = ROLE_WEREWOLF_SEER, Description = "Can see the role of a player once per night.", Type = Role.TypeEnum.WolfEnum }); _db.Roles.Add(new Role { Id = 9, Name = ROLE_MEDIUM, Description = "Can talk to the dead players and revive one of them once each game.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 10, Name = ROLE_BODYGUARD, Description = "Can protect a player he picks. If that player is attacked that night, the bodyguard gets attacked instead. The bodyguard survives the first attack, but dies on the second one.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 11, Name = ROLE_JAILER, Description = "Can put any player in jail during the day. The night after, that player can't use their role ability and the jailer will be able to talk to their prisoner anonymously. Once every game, he can kill the prisoner.", Type = Role.TypeEnum.VillagerEnum }); _db.Roles.Add(new Role { Id = 12, Name = ROLE_FOOL, Description = "If the fool gets killed by getting lynched, he wins.", Type = Role.TypeEnum.NeutralEnum }); _db.Roles.Add(new Role { Id = 13, Name = ROLE_HEAD_HUNTER, Description = "If the headhunter's target gets lynched, he wins", Type = Role.TypeEnum.NeutralEnum }); _db.Roles.Add(new Role { Id = 14, Name = ROLE_SERIAL_KILLER, Description = "Can kill a player he chooses every night. If he is the last player alive, he wins. That said, werewolves can't kill the serial killer.", Type = Role.TypeEnum.NeutralEnum }); _db.Roles.Add(new Role { Id = 15, Name = ROLE_GUNNER, Description = "You have two bullets which you can use to kill somebody. The shots are very loud so that your role will be revealed after the first shot.", Type = Role.TypeEnum.VillagerEnum }); _db.SaveChanges(); } } _roleInitialized = true; }
private void InitActions() { using (WerewolfContext _db = new WerewolfContext()) { if (_db == null || _roleInitialized == false) { return; } if (_db.Actions.Count() == 0) { Role seer = GetRoleByName(ROLE_SEER); Role auraseer = GetRoleByName(ROLE_AURA_SEER); Role priest = GetRoleByName(ROLE_PRIEST); Role doctor = GetRoleByName(ROLE_DOCTOR); Role werewolf = GetRoleByName(ROLE_WEREWOLF); Role werewolfshaman = GetRoleByName(ROLE_WEREWOLF_SHAMAN); Role alphawerewolf = GetRoleByName(ROLE_ALPHA_WEREWOLF); Role werewolfseer = GetRoleByName(ROLE_WEREWOLF_SEER); Role medium = GetRoleByName(ROLE_MEDIUM); Role bodyguard = GetRoleByName(ROLE_BODYGUARD); Role jailer = GetRoleByName(ROLE_JAILER); Role fool = GetRoleByName(ROLE_FOOL); Role headhunter = GetRoleByName(ROLE_HEAD_HUNTER); Role serialkiller = GetRoleByName(ROLE_SERIAL_KILLER); Role gunner = GetRoleByName(ROLE_GUNNER); Action dayVote = new Action { Id = 1, Name = ACTION_DAY_VOTE, Description = "Vote to burn a player in a day time", }; _db.Actions.Add(dayVote); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = seer.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = auraseer.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = priest.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = doctor.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = werewolf.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = werewolfshaman.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = alphawerewolf.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = werewolfseer.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = medium.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = bodyguard.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = jailer.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = fool.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = headhunter.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = serialkiller.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = dayVote.Id, RoleId = gunner.Id }); Action holywater = new Action { Id = 2, Name = ACTION_HOLYWATER, Description = "Throw holy water to kill a werewolf", }; _db.Actions.Add(holywater); _db.ActionRoles.Add(new ActionRole { ActionId = holywater.Id, RoleId = priest.Id }); Action shoot = new Action { Id = 3, Name = ACTION_SHOOT, Description = "Shoot to kill a player", }; _db.Actions.Add(shoot); _db.ActionRoles.Add(new ActionRole { ActionId = shoot.Id, RoleId = gunner.Id }); Action jail = new Action { Id = 4, Name = ACTION_JAIL, Description = "Jail a player at night", }; _db.Actions.Add(jail); _db.ActionRoles.Add(new ActionRole { ActionId = jail.Id, RoleId = jailer.Id }); Action enchant = new Action { Id = 5, Name = ACTION_ENCHANT, Description = "Enchant a player at night", }; _db.Actions.Add(enchant); _db.ActionRoles.Add(new ActionRole { ActionId = enchant.Id, RoleId = werewolfshaman.Id }); Action nightVote = new Action { Id = 6, Name = ACTION_NIGHT_VOTE, Description = "Werewolf vote to kill a player in a night time", }; _db.Actions.Add(nightVote); _db.ActionRoles.Add(new ActionRole { ActionId = nightVote.Id, RoleId = werewolf.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = nightVote.Id, RoleId = werewolfshaman.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = nightVote.Id, RoleId = alphawerewolf.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = nightVote.Id, RoleId = werewolfseer.Id }); Action guard = new Action { Id = 7, Name = ACTION_GUARD, Description = "Protect a player", }; _db.Actions.Add(guard); _db.ActionRoles.Add(new ActionRole { ActionId = guard.Id, RoleId = bodyguard.Id }); Action heal = new Action { Id = 8, Name = ACTION_HEAL, Description = "Heal a player", }; _db.Actions.Add(heal); _db.ActionRoles.Add(new ActionRole { ActionId = heal.Id, RoleId = doctor.Id }); Action kill = new Action { Id = 9, Name = ACTION_KILL, Description = "Kill a player", }; _db.Actions.Add(kill); _db.ActionRoles.Add(new ActionRole { ActionId = kill.Id, RoleId = serialkiller.Id }); Action reveal = new Action { Id = 10, Name = ACTION_REVEAL, Description = "Reveal a player's role", }; _db.Actions.Add(reveal); _db.ActionRoles.Add(new ActionRole { ActionId = reveal.Id, RoleId = seer.Id }); _db.ActionRoles.Add(new ActionRole { ActionId = reveal.Id, RoleId = werewolfseer.Id }); Action aura = new Action { Id = 11, Name = ACTION_AURA, Description = "See a player's aura", }; _db.Actions.Add(aura); _db.ActionRoles.Add(new ActionRole { ActionId = aura.Id, RoleId = auraseer.Id }); Action revive = new Action { Id = 12, Name = ACTION_REVIVE, Description = "Revive a dead player", }; _db.Actions.Add(revive); _db.ActionRoles.Add(new ActionRole { ActionId = revive.Id, RoleId = medium.Id }); _db.SaveChanges(); } } _actionInitialized = true; }