//收到玩家局内数据时触发 private void GameSokReceive(Player ply, NetInfoBase info) { switch ((GameOrderType)info.OrderType) { case GameOrderType.玩家叫分: { int score = int.Parse(info.Tag); this.SomeoneJiaofen(ply, score); break; } case GameOrderType.出牌: { var p = JsonConvert.DeserializeAnonymousType(info.JsonData, new { pks = new NetPoker[0], type = OutPokerType.Error_Card }); OutPokerType ty = p.type; if (ty == OutPokerType.炸弹 || ty == OutPokerType.王炸) { this.Multiple *= 2; } this.SomeoneOutPoker(ply, p.pks); break; } case GameOrderType.玩家已准备: { this.readyCount++; if (readyCount >= 3) { AllPlayerReady(); } break; } } }
/// <summary> /// 发送数据到客户端 /// </summary> /// <param name="data"></param> /// <param name="type"></param> public void SendData(string data, HallOrderType type, string tag = "") { if (this.Context.WebSocket.ReadyState == WebSocketState.Open) { var bas = new NetInfoBase((int)type, data, tag); string json = JsonConvert.SerializeObject(bas); base.Send(json); } }
/// <summary> /// 将字符串发送到客户端 /// </summary> /// <param name="data"></param> public void SendData(string data, int order, string tag = "") { if (this.IsConnect) { NetInfoBase info = new NetInfoBase(order, data, tag); var json = JsonConvert.SerializeObject(info); base.Send(json); } }
protected override async Task OnMessage(MessageEventArgs e) { using (var rs = new System.IO.StreamReader(e.Data)) { string json = rs.ReadToEnd(); NetInfoBase bas = JsonConvert.DeserializeObject <NetInfoBase>(json); Player ply = this.BindPlayer; json = bas.JsonData; if (bas != null && this.OnHallOrder != null) { this.OnHallOrder(this, bas); } try { switch ((HallOrderType)bas.OrderType) { case HallOrderType.请求大厅数据: { //绑定IP this.IP = base.Context.UserEndPoint.Address.ToString(); Log.Print("玩家" + this.IP + "已连接到服务器"); string pid = bas.Tag; Player py = null; if (Management.HasPlayerInOnline(pid)) { py = Management.GetOnlinePlayer(pid); } if (py == null) { py = new Player(); Management.AddToOnline(py); } py.SetHallWebSok(this); this.BindPlayer = py; this.SendHallData(new NetPlayer(py), false); break; } case HallOrderType.进入匹配队列: { MatchingQueue.Enqueue(ply); Log.Print("玩家" + this.BindPlayer.Name + "进入了匹配队列"); break; } case HallOrderType.退出匹配队列: { Log.Print("玩家" + this.BindPlayer.Name + "退出了匹配队列"); MatchingQueue.Remove(ply.PlayerID); break; } case HallOrderType.更新玩家信息: { var info = JsonConvert.DeserializeAnonymousType(json, new { name = "", roleid = 1, headid = 1, gender = 0, }); Management.UpdatePlayer(ply.PlayerID, info.headid, info.roleid, info.gender, info.name); break; } case HallOrderType.创建房间: { var rom = JsonConvert.DeserializeAnonymousType(json, new { pwd = "", bts = 0 }); GameRoom room = new GameRoom(ply, rom.bts, RoomModel.Room, rom.pwd); ply.RoomWhich = room; Management.AddRoom(room); this.SendData(JsonConvert.SerializeObject(new { pid = BindPlayer.PlayerID, roomid = room.RoomID }), HallOrderType.房间创建完毕); break; } case HallOrderType.请求房间列表: { //获取房间信息 var roms = Management.GetRooms().Select(r => { return(new NetRoom() { RoomID = r.RoomID.ToString(), BtScore = r.BtScore, MasterName = r.RoomMaster.Name, MasterHead = r.RoomMaster.HeadID, NowCount = r.MemberCount, }); }).ToArray(); var romjson = JsonConvert.SerializeObject(roms); ply.SendHallData(romjson, HallOrderType.推送房间列表); break; } case HallOrderType.请求排名信息: { //获取排名信息 var rks = Management.GetAllPlayers().OrderBy(r => long.Parse(r.Mark)).ToArray(); var rkjson = JsonConvert.SerializeObject(rks); ply.SendHallData(rkjson, HallOrderType.推送排名信息); break; } } } catch (Exception ex) { Log.Print(ex.Message, ConsoleColor.Red); } } }
protected override async Task OnMessage(MessageEventArgs e) { using (StreamReader sr = new StreamReader(e.Data)) { string json = sr.ReadToEnd(); NetInfoBase bas = JsonConvert.DeserializeObject <NetInfoBase>(json); string data = bas.JsonData; if (this.OnGameData != null) { this.OnGameData(this, bas); } switch ((RoomOrderType)bas.OrderType) { case RoomOrderType.连接到房间: { var obj = JsonConvert.DeserializeAnonymousType(data, new { roomid = "", pid = "" }); string pid = obj.pid; string roomid = obj.roomid; //找到对应房间 GameRoom room = Management.GetRoom(roomid); if (room == null) { return; } //判断是否添加玩家到房间 Player p = Management.GetOnlinePlayer(pid); if (!room.HasPlayer(pid)) { room.AddPlayer(p); p.RoomWhich = room; } //绑定玩家到会话 this.BindPlayer = p; RoomBinding(p.RoomWhich); p.SetRoomWebSok(this); //创建房间实体类 NetRoom nr = new NetRoom(room); //广播房间信息 room.BroadCast(JsonConvert.SerializeObject(nr), RoomOrderType.回送房间信息); Log.Print("玩家" + this.BindPlayer.Name + "成功连接到房间"); break; } case RoomOrderType.玩家准备: { this.BindRoom.SetReady(this.BindPlayer); this.BindPlayer.LastSendRoomData("", RoomOrderType.玩家准备, this.BindPlayer.PlayerID); this.BindPlayer.NextSendRoomData("", RoomOrderType.玩家准备, this.BindPlayer.PlayerID); break; } case RoomOrderType.踢出玩家: { var pid = bas.Tag; this.BindRoom.BroadCast("", RoomOrderType.踢出玩家, pid); //将玩家从房间删除 this.BindRoom.RemovePlayer(pid); break; } } } }