//进入房间 public void EnterRoom(string userName, int roomId) { MyUser user = CC.GetUser(userName); user.inRoom = roomId; user.Score = 0; //初始化新房间 if (CC.Rooms.ContainsKey(roomId) == false) { CC.Rooms.Add(roomId, new Room()); CC.Rooms[roomId].users = new List <MyUser>(); CC.Rooms[roomId].question = new questions(); } //该用户添加到房间 CC.Rooms[roomId].users.Add(user); //给该房间的所有用户发送最新用户消息 foreach (var item in CC.Rooms[roomId].users) { //string s = ""; //foreach (var v in CC.Rooms[roomId].users) //{ // s += v.Name + "," + v.Score.ToString() + ","; //} item.callback.ShowRoom(); } }
public void Talk(string userName, string message) { MyUser user = CC.GetUser(userName); string ans = CC.Rooms[user.inRoom].question.answer; if (message == ans) { foreach (var item in CC.Rooms[user.inRoom].users) { if (item.Name == userName) { item.callback.ShowTalk("系统消息", "你猜中了"); } else { item.callback.ShowTalk("系统消息", string.Format("{0}猜中了", item.Name)); } item.callback.ShowWin(userName, CC.Rooms[user.inRoom].users.First().Name); } RollUserAndRestart(user.inRoom); } else { foreach (var item in CC.Rooms[user.inRoom].users) { item.callback.ShowTalk(userName, message); } } }
/// <summary>用户退出</summary> public void Logout(int id, string userName) { MyUser logoutUser = CC.GetUser(userName); CC.Users.Remove(logoutUser); List <Userdata> userdatas = new List <Userdata>(); foreach (var item in CC.Users) { if (item.id == id) { Userdata t = new Userdata(); t.Acount = item.Acount; t.Avart = item.Avart; t.Grade = item.Grade; t.Name = item.Name; t.Room = item.Room; t.Score = item.Score; t.Sign = item.Sign; userdatas.Add(t); } } foreach (var item in CC.Users) { if (item.id == id) { item.callback.ShowLogout(userName); item.callback.ShowInfo(userdatas); } } logoutUser = null; //将其设置为null后,WCF会自动关闭该客户端 }
/// <summary>用户退出</summary> public void Logout(string userName) { MyUser logoutUser = CC.GetUser(userName); foreach (var user in CC.Users) { //不需要发给退出用户 if (user.UserName != logoutUser.UserName) { user.callback.ShowLogout(userName); } } CC.Users.Remove(logoutUser); logoutUser = null; //将其设置为null后,WCF会自动关闭该客户端 }
public void StartGame(string userName, int roomId) { //当前用户已准备 MyUser user = CC.GetUser(userName); user.ready = true; //判断当前房间内所有用户是否准备好 foreach (var item in CC.Rooms[roomId].users) { if (!item.ready) { return; } } foreach (var item in CC.Rooms[roomId].users) { item.callback.ShowStart(CC.Rooms[roomId].users.First().Name, CC.Rooms[roomId].question.answer, CC.Rooms[roomId].question.tip); } }