static void Main(string[] args) { //TODO 起网络请求,让用户通过网页加入 var userList = new ModelUser[8]; for (var i = 1; i <= userList.Length; i++) userList[i - 1] = new ModelUser() { Name = i.ToString() + "号" }; Table table = Table.GetOneTable(); for (var i = 1; i <= userList.Length; i++) { var modelUser = userList[i - 1]; table.Join(i, modelUser); } var roleCounts = new Dictionary<Role, int>{ {Role.Werewolve,3}, {Role.Seer, 1}, {Role.Hunter, 1}, {Role.Witch, 1}, {Role.Villager, 2} }; table.SetRoleCount(roleCounts); foreach (var p in table.TheContext.Players) { Console.WriteLine("编号{0},角色{1},状态{2}", p.theUser.Name, p.theRoles[0], p.theStatus); } table.Start(); Console.ReadLine(); }
/// <summary> /// 用户登录加入到游戏中,可以不断的调用,表示不断的有人加入到游戏中 /// </summary> /// <param name="idx">玩家编号</param> /// <param name="user"></param> /// <returns>如果游戏开始(Start)则无法加入</returns> public bool Join(int idx, ModelUser user) { //TODO 考虑用锁保证和SetRoleCount的并发问题 if (isGameStarted) return false; if (TheContext.DicIdxPlayer.ContainsKey(idx)) return false; Player newPlayer = new Player(); newPlayer.theUser = user; newPlayer.Idx = idx; newPlayer.theStatus = PlayerStatus.Ready; TheContext.Players.Add(newPlayer); TheContext.DicIdxPlayer.Add(idx, newPlayer); Console.WriteLine("{0}加入了游戏,编号{1}", newPlayer.theUser.Name, newPlayer.Idx); return true; }