/// <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 (matchCache.IsMatching(userId) == false) { return; } MatchRoom room = matchCache.GetRoom(userId); room.Ready(userId); room.Brocast(OpCode.MATCH, MatchCode.READY_BRO, userId); //检测: 是否所有玩家都准备了 if (room.IsAllReady()) { //开始战斗 //TODO //通知房间内玩家 要进行战斗了 给客户端群发消息 room.Brocast(OpCode.MATCH, MatchCode.START_BRO, null); //销毁房间 matchCache.Destroy(room); } }); }
/// <summary> /// 准备 /// </summary> /// <param name="client"></param> private void Ready(ClientPeer client) { SingleExecute.Instance.Execute(() => { //安全校验 if (!userCache.IsOnLine(client)) { return; } int userId = userCache.GetIdByClient(client); if (matchCache.IsMatching(userId) == false) { return; } //玩家准备list添加 MatchRoom room = matchCache.GetRoom(userId); room.Ready(userId); room.Brocast(OpCode.MATCH, MatchCode.READY_BRO, userId); //每准备一个 判断一下是否全部准备 if (room.IsReady()) { //开始进入战斗 //客户端群发进入战斗 startFight(room.GetIdList()); room.Brocast(OpCode.MATCH, MatchCode.START_BRO, null); //销毁准备房间 matchCache.Destroy(room); } }); }
/// <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 (matchCache.IsMatching(userId) == false) { return; } //一定要注意安全的验证 MatchRoom room = matchCache.GetRoom(userId); room.Ready(userId); //之前忘记了 &&&……%……&¥&¥&%#%#&¥&……%& room.Brocast(OpCode.MATCH, MatchCode.READY_BRO, userId); //检测:是否所有玩家都准备了 if (room.IsAllReady()) { //开始战斗 startFight(room.GetUIdList()); //通知房间内的玩家 要进行战斗了 给客户端群发消息 room.Brocast(OpCode.MATCH, MatchCode.START_BRO, null); //销毁房间 matchCache.Destroy(room); } } ); }
/// <summary> /// 发送消息 /// </summary> /// <param name="client"></param> /// <param name="socketMsg"></param> private void ChatRequest(ClientPeer client, SocketMsg socketMsg) { if (!userCache.IsOnline(client)) { return; } int userId = userCache.GetClientUserId(client); ChatDto chatDto = new ChatDto { UserId = userId, Type = (int)socketMsg.value }; //向当前匹配房间所有玩家广播消息 if (matchCache.IsMatching(userId)) { MatchRoom room = matchCache.GetRoom(userId); socketMsg.OpCode = MsgType.Chat; socketMsg.SubCode = ChatCode.Result; socketMsg.value = chatDto; room.Brocast(socketMsg); } else if (true) { } }
/// <summary> /// 进入 /// </summary> /// <param name="client"></param> private void enter(ClientPeer client) { SingleExecute.Instance.Execute(delegate() { if (!userCache.IsOnline(client)) { return; } int userId = userCache.GetId(client); //如果用户已经在匹配房间等待了 那就无视 if (matchCache.IsMatching(userId)) { //client.Send(OpCode.MATCH, MatchCode.ENTER_SRES, -1);//重复加入 return; } //正常进入 MatchRoom room = matchCache.Enter(userId, client); //广播给房间内除了当前客户端的其他用户,有新玩家加入了 参数:新进入的玩家的用户id 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); room.Brocast(OpCode.MATCH, MatchCode.ENTER_BRO, userDto, client); //返回给当前客户端 给他房间的数据模型 MatchRoomDto dto = makeRoomDto(room); client.Send(OpCode.MATCH, MatchCode.ENTER_SRES, dto); Console.WriteLine("有玩家进入匹配房间"); }); }
private void ChatRequest(ClientPeer client, int chatType) { //接收到的是类型 //返回什么 ? 所以创建一个数据传输模型DTO //谁发的? //需要一个userId 所以获取userCache if (userCache.IsOnLine(client) == false) { return; } int userId = userCache.GetIdByClient(client); ChatDto chatDto = new ChatDto(userId, chatType); //发给谁? //通过matchCache 获取房间 通过房间告诉其他玩家(挨个告诉 全体广播) if (matchCache.IsMatching(userId)) { MatchRoom mRoom = matchCache.GetRoom(userId); mRoom.Brocast(OpCode.CHAT, ChatCode.SRES, chatDto); } else if (false) { //在战斗房间内 //TODO } }
public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer) { MobaServer.LogInfo("处理开始匹配的请求"); // 检测id是否正确 int playerId = (int)request.Parameters[(byte)ParameterCode.PlayerId]; if (UserManager.GetPlayer(peer.Username).Identification != playerId) { MobaServer.LogWarn("非法操作"); return; } MatchRoom room = Caches.Match.StartMatch(peer, playerId); // 如果房间满了 开始选人 if (room.IsFull) { // 创建选人的房间 Caches.Select.CreateRoom(room.TeamOneIdList, room.TeamTwoIdList); // 通知所有客户端进入选人的房间 room.Brocast(OperationCode.StartMatch, null); // 摧毁房间 Caches.Match.DestroyRoom(room); } }
/// <summary> /// 用户开始匹配 /// </summary> /// <param name="client"></param> private void StartMatch(ClientPeer client) { SingleExecute.Instance.Execute(() => { int userId = user.GetId(client); if (match.IsMatching(userId)) { return; } MatchRoom room = match.Enter(userId, client); //构造一个自身信息UserDto ui需要更新什么信息就构造什么信息 UserModel model = user.GetModelById(userId); UserDto userDto = new UserDto(); userDto.Set("", model.id, model.name, model.beens, model.winCount, model.loseCount, model.runCount, model.lv, model.exp); //对房间内其他玩家进行广播 新用户 加入了房间 room.Brocast(OpCode.MATCHROOM, MatchRoomCode.STARTMATCH_BRO, userDto, client); //将匹配到的房间号给玩家 var roomDto = MakeRoomDto(room); client.StartSend(OpCode.MATCHROOM, MatchRoomCode.STARTMATCH_SRES, roomDto); Console.WriteLine(string.Format("玩家 : {0} 匹配到房间 :{1}", userDto.name, room.id)); //int index = -1; //for (int i = 0; i < roomDto.uIdList.Count; i++) //{ // Console.WriteLine(roomDto.uIdList[i]); // if (roomDto.uIdList[i] == userDto.id) // { // index = i + 1; // } //} //Console.WriteLine(string.Format("有{1}个人。第{0}个进来的",index,roomDto.uIdList.Count)); }); }
private void chatRequest(ClientPeer client, int chatType) { //接收到的是 聊天类型 //返回的是什么? if (userCache.IsOnline(client) == false) { return; } int userId = userCache.GetId(client); //谁? 发送者的id userID //发了什么? 聊天的类型 chatType ChatDto dto = new ChatDto(userId, chatType); //给谁? 房间内的每一个玩家 if (matchCache.IsMatching(userId)) { MatchRoom mRoom = matchCache.GetRoom(userId); mRoom.Brocast(OpCode.CHAT, ChatCode.SRES, dto); } else if (false) { //在这里检测战斗房间 //TODO } }
public void Chat(ClientPeer client, int type) { SingleExecute.Instance.Execute(() => { if (!user.IsOnLine(client)) { return; } int userId = user.GetId(client); //匹配场景 if (match.IsMatching(userId)) { MatchRoom mr = match.GetRoom(userId); ChatDto dto = new ChatDto(userId, type); mr.Brocast(OpCode.CHAT, ChatCode.CHAT_SRES, dto); } else if (fight.IsFighting(userId)) { //战斗场景 FightRoom mr = fight.GetRoom(userId); ChatDto dto = new ChatDto(userId, type); //fight.(OpCode.CHAT, ChatCode.CHAT_SRES, dto); Brocast(mr, OpCode.CHAT, ChatCode.CHAT_SRES, dto); } }); }
/// <summary> /// 用户离开匹配 /// </summary> /// <param name="client"></param> private void CancelMatch(ClientPeer client) { SingleExecute.Instance.Execute(() => { if (!user.IsOnLine(client)) { //不在线 return; } int userId = user.GetId(client); //用户没有匹配 if (!match.IsMatching(userId)) { return;//非法操作 不能离开 } //正常离开 MatchRoom room = match.Leave(userId); if (!room.IsEmpty()) { room.Brocast(OpCode.MATCHROOM, MatchRoomCode.CANCELMATCH_BRO, userId, client); } Console.WriteLine(string.Format("玩家 : {0} 取消进入房间 :{1}", user.GetModelByClient(client).name, room.id)); }); }
/// <summary> /// 进入 /// </summary> void Enter(ClientPeer client) { SingleExecute.Instance.Execute ( delegate() { if (!userCache.IsOnLine(client)) { return; } int userID = userCache.GetIdByClient(client); //判断用户是否已经在用户匹配房间 if (matchCache.IsMatching(userID)) { client.Send(OpCode.MATCH, MatchCode.ENTER_SRES, -1); //重复加入 return; } //正常进入 MatchRoom room = matchCache.Enter(userID, client); //广播给所有用户 有玩家进入 //参数,新进入的玩家ID 不用广播 UserModel model = userCache.GetModelByUserId(userID); UserDto userDto = new UserDto(model.id, model.name, model.been, model.lv, model.exp, model.winCount, model.loseCount, model.runCount); room.Brocast(OpCode.MATCH, MatchCode.ENTER_BRO, userDto, client); //返回给当前client 房间的数据模型 MatchRoomDto dto = GetMatchRoomDto(room); //给MatchRoomDto中的uidUserDtoDic<int ,userDto> 进行赋值 //那么需要获取UserModel 房间之中有多个用户 client.Send(OpCode.MATCH, MatchCode.ENTER_SRES, dto); } ); }
/// <summary> /// 离开 /// </summary> /// <param name="client"></param> private void leave(ClientPeer client) { SingleExecute.Instance.Execute( delegate() { int userId = userCache.GetId(client); //用户没有匹配 不能退出 非法操作 if (matchCache.IsMatching(userId) == false) { // client.Send(OpCode.MATCH, MatchCode.LEAVE_SRES, -1); return; } MatchRoom room = matchCache.GetRoom(userId); if (room.UIdClientDict.Keys.Count > 1) { //正常离开 room = matchCache.Leave(userId); //广播给房间所有人 有人离开了 参数:离开的用户id room.Brocast(OpCode.MATCH, MatchCode.LEAVE_BRO, userId); } else { matchCache.Destroy(room); client.Send(OpCode.MATCH, MatchCode.BACK_BRO, null); return; } Console.WriteLine("有玩家退出匹配房间"); }); }
/// <summary> /// 用户准备 /// </summary> /// <param name="client"></param> private void Ready(ClientPeer client) { SingleExecute.Instance.Execute(() => { if (!user.IsOnLine(client)) { //不在线 return; } int userId = user.GetId(client); if (!match.IsMatching(userId)) { return;//非法操作 不能准备 } MatchRoom room = match.GetRoom(userId); room.Ready(userId); //广播消息 准备了 为什么要给自己发,确保服务器收到准备请求 回复消息后 将准备按钮隐藏 room.Brocast(OpCode.MATCHROOM, MatchRoomCode.READY_BRO, userId); Console.WriteLine(string.Format("玩家 : {0} 在房间 :{1} 准备了", user.GetModelByClient(client).name, room.id)); //是否所有玩家都准备了 if (room.IsAllUserReady()) { Console.WriteLine(String.Format("房间 :{0} 开始了战斗。。。", room.id)); //开始战斗 调用FightHandler 的开始方法 委托 if (FightDelegate != null) { FightDelegate.Invoke(room.uidList); } //通知房间内的玩家 要进行战斗了 群发消息 room.Brocast(OpCode.MATCHROOM, MatchRoomCode.START_BRO, null); match.Destroy(room); } }); }
/// <summary> /// 进入 /// </summary> /// <param name="client"></param> private void enter(ClientPeer client) { SingleExecute.Instance.Execute( delegate() { int userId = userCache.GetId(client); //正常进入 MatchRoom room = matchCache.Enter(userId, client); // 返回给当前客户端 给他房间的数据模型 MatchRoomDto dto = makeRoomDto(room); client.Send(OpCode.MATCH, MatchCode.ENTER_SRES, dto); //广播给房间内所有的用户,有玩家进入了 //参数:新进入玩家的用户id 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); // 返回给当前客户端 给他房间的数据模型 room.Brocast(OpCode.MATCH, MatchCode.ENTER_BRO, userDto, client); Console.WriteLine("有玩家进入匹配房间"); }); }
/// <summary> /// 开始匹配 /// </summary> private void OnStartMatch(MobaClient client, int playerId) { if (playerCache.GetId(client) != playerId) { return; } MatchRoom room = matchCache.StartMatch(client, playerId); Send(client, OpCode.PlayerCode, OpPlayer.StartMatch, 0, "开始匹配"); //如果房间满了就开始选人 if (room.IsFull == true) { //开始选人 StartSelectEvent(room.Team1IdList, room.Team2IdList); //发起是否进入选人请求 room.Brocast(OpCode.PlayerCode, OpPlayer.MatchComplete, 0, "是否进入选人界面(10s内)"); matchCache.DestroyRoom(room); } }
private void chatRequest(ClientPeer client, int chatType) { if (userCache.IsOnline(client) == false) { return; } int userId = userCache.GetId(client); ChatDto chatDto = new ChatDto(userId, chatType); if (matchCache.IsMatching(userId)) { MatchRoom mRoom = matchCache.GetRoom(userId); mRoom.Brocast(OpCode.CHAT, ChatCode.SERS, chatDto); Console.WriteLine("快捷喊话:" + chatDto.ChatType); } else if (fightCache.IsFighting(userId)) { FightRoom fightRoom = fightCache.GetRoomByUId(userId); fightRoom.Brocast(OpCode.CHAT, ChatCode.SERS, chatDto); Console.WriteLine("快捷喊话:" + chatDto.ChatType); } }
/// <summary> /// 离开 /// </summary> /// <param name="client"></param> private void Leave(ClientPeer client) { SingleExecute.Instance.Execute ( delegate() { if (!userCache.IsOnLine(client)) { return; } int userId = userCache.GetIdByClient(client); //用户 否 匹配 if (matchCache.IsMatching(userId) == false) { return; } //正常操作 MatchRoom room = matchCache.Leave(userId); //广播给房间内 room.Brocast(OpCode.MATCH, MatchCode.LEAVE_BRO, userId, client); } ); }
private void chatRequest(ClientPeer client, int chatType) { if (userCache.IsOnline(client) == false) { return; } int userId = userCache.GetId(client); //获取发送者的id userId //发送 ChatDto dto = new ChatDto(userId, chatType); //广播给房间内的玩家 if (matchCache.IsMatching(userId)) { MatchRoom mRoom = matchCache.GetRoom(userId); mRoom.Brocast(OpCode.CHAT, ChatCode.SRES, dto); } if (fightCache.IsFighting(userId)) { FightRoom fRoom = fightCache.GetRoomByUId(userId); fRoom.Brocast(OpCode.CHAT, ChatCode.SRES, dto, client); } }
/// <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 (matchCache.IsMatching(userId) == false) { //client.Send(OpCode.MATCH, MatchCode.LEAVE_SRES, -1); return; } //正常离开 MatchRoom room = matchCache.Leave(userId); //广播给房间内所有人 有人离开了 参数:离开的用户id room.Brocast(OpCode.MATCH, MatchCode.LEAVE_BRO, userId); Console.WriteLine("有玩家离开匹配房间"); }); }