/// <summary> /// 准备 /// </summary> /// <param name="client"></param> private void ready(ClientPeer client) { SingleExecute.Instance.Execute( () => { if (userCache.IsOnline(client) == false) { return; } int userId = userCache.GetId(client); if (fightCache.IsInRoom(userId) == false) { return; } //一定要注意安全的验证 _21MutiFightRoom room = fightCache.GetRoom(userId); room.Ready(userId); //之前忘记了 &&&……%……&¥&¥&%#%#&¥&……%& room.Brocast(OpCode._21Multi, _21MultiCode.READY_BRO, userId); //检测:是否所有玩家都准备了 //if (room.IsAllReady()) //{ // //通知房间内的玩家 要进行战斗了 给客户端群发消息 // room.Brocast(OpCode._21Multi, _21MultiCode.START_BRO, null); // Console.WriteLine("要开始战斗了"); // Thread.Sleep(1000); // //开始战斗 // StartFight(userId, client, room); //} } ); }
/// <summary> /// 匹配 /// </summary> /// <param name="client"></param> public void match(ClientPeer client) { SingleExecute.Instance.Execute( delegate() { if (!userCache.IsOnline(client)) { return; } int userId = userCache.GetId(client); //if (userId != id) // return; //如果用户已经在匹配房间等待了 那就无视 if (fightCache.IsInRoom(userId)) { client.Send(OpCode._21Multi, _21MultiCode.MATCH_SRES, -1); //重复匹配 return; } //正常匹配 _21MutiFightRoom room = fightCache.Match(userId, client); //广播给房间内除了当前客户端的其他用户,有新玩家计入了 参数:新进入的玩家的用户id #region 构造一个UserDto Dto就是针对UI定义的 UI需要什么我们就加什么字段 UserModel model = userCache.GetModelById(userId); UserDto userDto = new UserDto(model.Id, model.Name, model.Been, model.WinCount, model.LoseCount, model.RunCount, model.Lv, model.Exp); int position = room.getPosition(userId); MultiEnterDto edto = new MultiEnterDto(userDto, position); #endregion room.Brocast(OpCode._21Multi, _21MultiCode.ENTER_BRO, edto, client); MutiRoomDto dto = makeRoomDto(room); client.Send(OpCode._21Multi, _21MultiCode.MATCH_SRES, dto); Console.WriteLine("有玩家进行了匹配"); } ); }
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { SingleExecute.Instance.Execute( delegate() { foreach (int roomId in fightCache.GetAllWaitingRoomId()) { _21MutiFightRoom room = fightCache.GetRoomByRoomId(roomId); if (room.IsAllReady()) { if (room.isStart) { if (room.countDownTime == 0) { room.Brocast(OpCode._21Multi, _21MultiCode.START_BRO, null); StartFight(roomId); } room.countDownTime -= 100; } else { room.isStart = true; room.countDownTime = 4000; brocast(room, OpCode._21Multi, _21MultiCode.COUNT_DOWN_BRO, null, null); } } else { if (room.isStart) { brocast(room, OpCode._21Multi, _21MultiCode.STOP_COUNT_DOWN_BRO, null, null); } room.isStart = false; room.countDownTime = 0; } } }); }
/// <summary> /// 离开 /// </summary> /// <param name="client"></param> private void leave(ClientPeer client) { SingleExecute.Instance.Execute( delegate() { if (!userCache.IsOnline(client)) { return; } int userId = userCache.GetId(client); //用户没有匹配 不能退出 非法操作 if (fightCache.IsInRoom(userId) == false) { //client.Send(OpCode.MATCH, MatchCode.LEAVE_SRES, -1); return; } //在战斗的时候离开 if (fightCache.IsInPlaying(userId)) { _21MutiFightRoom room = fightCache.GetRoom(userId); //加入到离开链表里 room.LeaveUIdList.Add(userId); //广播一下离开的用户 room.Brocast(OpCode._21Multi, _21MultiCode.LEAVE_BRO, userId); //如果在处理的时候离开了 换下一个 if (userId == room.playingUserId) { int nextUid = room.GetNextUId(userId); room.SetPlayingUId(nextUid); //没有下一个玩家了 if (nextUid == -1) { //room.SetPlayingUId(-1); //todo 庄家的操作 gameOver(room); } else { //和刚开始差不多 if (room.is21ByUserId(nextUid)) //21点 直接赢了 { //记录一下这组牌 room.SaveListByUserId(nextUid, 1, false); int position = room.getPosition(nextUid); OverHandDto ohdto = new OverHandDto(nextUid, position, 1); brocast(room, OpCode._21Multi, _21MultiCode.OVER_HAND_BRO, ohdto, null); //该下一个玩家了 turnPlayer(nextUid, position, room); } if (room.isCanSplitByUserId(nextUid)) //能够分牌 { TurnHandDto thdto = new TurnHandDto(true, nextUid); //client.Send(OpCode._21Multi, _21MultiCode.TURN_HS_BRO, thdto); brocast(room, OpCode._21Multi, _21MultiCode.TURN_HS_BRO, thdto, null); } else { TurnHandDto thdto = new TurnHandDto(false, nextUid); //client.Send(OpCode._21Multi, _21MultiCode.TURN_HS_BRO, thdto); brocast(room, OpCode._21Multi, _21MultiCode.TURN_HS_BRO, thdto, null); } } } } //未战斗的时候离开 else { //正常离开 _21MutiFightRoom room = fightCache.Leave(userId); //广播给房间内所有人 有人离开了 参数:离开的用户id room.Brocast(OpCode._21Multi, _21MultiCode.LEAVE_BRO, userId); } Console.WriteLine("有玩家离开房间"); }); }