/// <summary> /// 进入选人 /// </summary> /// <param name="client"></param> private void OnEnter(MOBAClient client) { int playerID = playerCache.GetID(client); SelectRoom selectRoom = selectCache.Enter(playerID, client); if (selectRoom == null) { return; } //进入成功 //先给客户端发一个房间模型,再给房间内的客户端发一条消息:有人进入房间 Send(client, OperationCode.SelectCode, OpSelect.GetInfo, 0, "获取房间信息", JsonMapper.ToJson(selectRoom.RedTeamSelectModels.Values.ToArray()), JsonMapper.ToJson(selectRoom.BlueTeamSelectModels.Values.ToArray())); selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Enter, 0, "有玩家进入房间", client, playerID); //检测是否全部都进入房间 if (selectRoom.IsAllEnter) { //如果全部都进入房间就开启一个定时任务,通知玩家在60秒内准备,不准备房间就解散 selectRoom.StartSchedule(DateTime.UtcNow.AddSeconds(60), () => { //房间解散 if (!selectRoom.IsAllReady) { selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Cancel, 1, "有玩家60秒内未准备,解散当前房间", null); selectCache.DestorySelectRoom(selectRoom.ID); } }); //给所有人发送全部玩家进入房间的消息 selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Enter, 1, "全部玩家进入房间", null, playerID); } }
/// <summary> /// 玩家上线的处理 /// </summary> /// <param name="client"></param> private void OnPlayerOnline(MOBAClient client) { int accountID = accountCache.GetID(client); int playerID = playerCache.GetID(accountID); //防止重复在线 if (playerCache.IsOnLine(client)) { return; } //上线 playerCache.OnLine(client, playerID); //上线的时候,通知在线好友,显示在线状态 PlayerModel playerModel = playerCache.GetPlayerModel(playerID); if (playerModel != null) { foreach (int friendID in playerModel.FriendIdList) { if (!playerCache.IsOnLine(friendID)) //因为GetPlayerModel,GetClient是获取在线玩家的数据 { continue; } MOBAClient friendClient = playerCache.GetClient(friendID); Send(friendClient, OperationCode.PlayerCode, OpPlayer.FriendOnlineState, 0, "好友玩家上线", playerModel.Id); } } PlayerDto playerDto = ToDto(playerCache.GetPlayerModel(playerID)); //发送 Send(client, OperationCode.PlayerCode, OpPlayer.Online, 0, "上线成功", JsonMapper.ToJson(playerDto)); }
public void OnRequest(MOBAClient client, byte subCode, OperationRequest request) { switch (subCode) { case OpPlayer.GetPlayerInfo: OnGetInfo(client); break; case OpPlayer.CreatePlayer: OnCreatePlayer(client, request[0].ToString()); break; case OpPlayer.Online: OnPlayerOnline(client); break; case OpPlayer.AddFriend: OnAddFriend(client, request[0].ToString()); break; case OpPlayer.AddFriendToClient: OnAddFriendToClient(client, (bool)request[0], (int)request[1]); break; case OpPlayer.MatchStart: OnMatchStart(client, (int)request[0]); break; case OpPlayer.MatchStop: OnMatchStop(client, (int)request[0]); break; } }
/// <summary> /// 玩家确认选择(准备) /// </summary> /// <param name="client"></param> private void OnReady(MOBAClient client) { int playerID = playerCache.GetID(client); SelectRoom selectRoom = selectCache.Ready(playerID); if (selectRoom == null) { //返回准备失败给客户端 Send(client, OperationCode.SelectCode, OpSelect.Ready, -1, "准备失败"); return; } //通知房间所有人此玩家准备 selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Ready, 0, "有人准备了", null, playerID); //检测 是否全部人都准备,都准备就进入战斗 if (selectRoom.IsAllReady) { StartBattleAction(selectRoom.RedTeamSelectModels.Values.ToList(), selectRoom.BlueTeamSelectModels.Values.ToList()); //给客户端发送消息:准备战斗 切换场景 selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.StartBattle, 0, "准备进入战斗场景", null); //销毁当前房间 selectCache.DestorySelectRoom(selectRoom.ID); } }
/// <summary> /// 玩家离开匹配队列 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> /// <returns>离开是否成功</returns> public bool LeaveMatch(MOBAClient client, int playerID) { //安全检测 if (!roomIDs.ContainsKey(playerID)) { return(false); } int roomID = roomIDs[playerID]; MatchRoom room; //检测 (防止多线程造成不必要的错误) if (!Rooms.TryGetValue(roomID, out room)) { return(false); } room.Leave(client, playerID); if (room.RoomIsEmpty()) { //移除映射关系 roomIDs.Remove(playerID); Rooms.TryRemove(room.ID, out room); //移除定时任务 if (!room.Guid.Equals(Guid.NewGuid())) { room.Timer.RemoveAction(room.Guid); } //把房间入重用队列 rooms.Enqueue(room); } return(true); }
//删除在线客户端 public void OffLineClient(MOBAClient client) { if (clients.ContainsKey(client)) { clients.Remove(client); } }
public void OnDisConnect(MOBAClient client) { //下线 *** 这里要通过客户端获取到玩家账号ID,再用账号ID获取到玩家ID,否则会报空 int accountID = accountCache.GetID(client); int playerID = playerCache.GetID(accountID); battleCache.OffLine(client, playerID); }
/// <summary> /// 获取在线玩家ID /// </summary> /// <param name="client"></param> public int GetID(MOBAClient client) { if (_onLinePlayers.ContainsKey(client)) { return(_onLinePlayers[client]); } return(-1); }
/// <summary> /// 获取在线玩家数据 /// </summary> /// <param name="client"></param> /// <returns></returns> public PlayerModel GetPlayerModel(MOBAClient client) { if (_onLinePlayers.ContainsKey(client)) { return(playerModels[_onLinePlayers[client]]); } return(null); }
//添加在线客户端 public bool OnLineClient(string account, MOBAClient client) { if (IsOnLine(account)) { return(false); } clients[client] = account; return(true); }
/// <summary> /// 离开匹配的处理 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> private void OnMatchStop(MOBAClient client, int playerID) { bool result = matchCache.LeaveMatch(client, playerID); if (result) { Send(client, OperationCode.PlayerCode, OpPlayer.MatchStop, 0, "离开匹配成功"); } }
/// <summary> /// 下线 /// </summary> /// <param name="client"></param> public void OffLine(MOBAClient client) { if (IsOnLine(client)) { if (_onLinePlayers.ContainsKey(client)) { _onLinePlayers.Remove(client); } } }
/// <summary> /// 进入战斗 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> /// <returns></returns> public BattleRoom Enter(MOBAClient client, int playerID) { BattleRoom battleRoom = GetRoom(playerID); if (battleRoom == null) { return(null); } battleRoom.Enter(client); return(battleRoom); }
/// <summary> /// 创建角色的处理 /// </summary> /// <param name="client"></param> /// <param name="requestParameters"></param> private void OnCreatePlayer(MOBAClient client, string name) { int accountID = accountCache.GetID(client); if (playerCache.Has(accountID)) { return; } playerCache.Create(name, accountID); Send(client, OperationCode.PlayerCode, OpPlayer.CreatePlayer, 0, "创建成功"); }
/// <summary> /// 进入选人 /// </summary> /// <param name="playerID"></param> /// <param name="client"></param> public SelectRoom Enter(int playerID, MOBAClient client) { //获取房间 SelectRoom selectRoom = GetRoom(playerID); if (selectRoom == null) { return(null); } selectRoom.Enter(playerID, client); return(selectRoom); }
/// <summary> /// 玩家移动 /// </summary> /// <param name="client"></param> /// <param name="v1"></param> /// <param name="v2"></param> /// <param name="v3"></param> private void OnWalk(MOBAClient client, float x, float y, float z) { int playerID = playerCache.GetID(client); BattleRoom battleRoom = battleCache.GetRoom(playerID); if (battleRoom == null) { return; } //给每一个客户端发送谁移动到哪的信息 battleRoom.Broadcast(OperationCode.BattleCode, OpBattle.Walk, 0, "有玩家移动", null, playerID, x, y, z); }
/// <summary> /// 玩家选人 /// </summary> /// <param name="client"></param> private void OnSelect(MOBAClient client, int heroID) { int playerID = playerCache.GetID(client); SelectRoom selectRoom = selectCache.Select(playerID, heroID); if (selectRoom == null) { Send(client, OperationCode.SelectCode, OpSelect.Select, -1, "选择失败"); return; } //给房间里面的所有人发一条消息:谁选了什么英雄(playerID,heroID) selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Select, 0, "有人选择了英雄", null, playerID, heroID); }
public void OnRequest(MOBAClient client, byte subCode, OperationRequest request) { switch (subCode) { case OpBattle.Enter: OnEnter(client, (int)request[0]); break; case OpBattle.Walk: OnWalk(client, (float)request[0], (float)request[1], (float)request[2]); break; } }
/// <summary> /// 根据连接对象获取账号ID /// </summary> /// <param name="client"></param> /// <returns>有就返回,没有返回-1</returns> public int GetID(MOBAClient client) { if (!clients.ContainsKey(client)) { return(-1); } string account = clients[client]; if (!accountModels.ContainsKey(account)) { return(-1); } return(accountModels[account].Id); }
/// <summary> /// 玩家下线 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> public void OffLine(MOBAClient client, int playerID) { //获取房间 SelectRoom selectRoom = GetRoom(playerID); if (selectRoom == null) { return; } //离开房间 selectRoom.Leave(client); //给剩余的客户端发一个消息:有人退出房间,回到主界面 selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Cancel, 2, "有玩家退出", null); //销毁房间 DestorySelectRoom(selectRoom.ID); }
private void OnRegister(MOBAClient client, string account, string password) { //无效检测 if (account == null || password == null) { return; } //重复检测 if (accountCache.Has(account)) { Send(client, OperationCode.AccountCode, OpAccount.Register, -1, "账号已存在"); return; } accountCache.Add(account, password); Send(client, OperationCode.AccountCode, OpAccount.Register, 0, "注册成功"); }
public void OnRequest(MOBAClient client, byte subCode, OperationRequest request) { switch (subCode) { case OpAccount.Login: AccountDto dto = JsonMapper.ToObject <AccountDto>(request[0].ToString()); OnLogin(client, dto.Account, dto.Password); break; case OpAccount.Register: string account = request[0].ToString(); string password = request[1].ToString(); OnRegister(client, account, password); break; } }
private void OnChat(MOBAClient client, string text) { //给当前客户端所在的房间内的所有人发一条消息 PlayerModel playerModel = playerCache.GetPlayerModel(client); if (playerModel == null) { return; } SelectRoom selectRoom = selectCache.GetRoom(playerModel.Id); if (selectRoom != null) { string str = string.Format("{0}:{1}", playerModel.Name, text); selectRoom.Broadcast(OperationCode.SelectCode, OpSelect.Chat, 0, "有玩家发言了", null, str); } }
/// <summary> /// 玩家下线 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> public void OffLine(MOBAClient client, int playerID) { //获取房间 BattleRoom battleRoom = GetRoom(playerID); if (battleRoom == null) { return; } //离开房间 battleRoom.Leave(client); if (!battleRoom.IsAllLeave) { return; } //如果全部人都退出了 销毁房间 DestroyBattleRoom(battleRoom.ID); }
/// <summary> /// 进入战斗 /// </summary> /// <param name="client"></param> /// <param name="playerID"></param> private void OnEnter(MOBAClient client, int playerID) { BattleRoom battleRoom = battleCache.Enter(client, playerID); if (battleRoom == null) { return; } //首先要判断是否全部进入了 if (!battleRoom.IsAllEnter) { return; } //给每一个客户端发送战斗房间的信息 battleRoom.Broadcast(OperationCode.BattleCode, OpBattle.GetInfo, 0, "加载战斗场景数据", null, JsonMapper.ToJson(battleRoom.HeroModels), JsonMapper.ToJson(battleRoom.BuildModels)); }
/// <summary> /// 添加好友结果的处理 /// </summary> /// <param name="result"></param> private void OnAddFriendToClient(MOBAClient client, bool result, int requestID) { MOBAClient requestClient = playerCache.GetClient(requestID); if (result) { //同意了,保存数据 int playerID = playerCache.GetID(client); playerCache.AddFriend(playerID, requestID); Send(client, OperationCode.PlayerCode, OpPlayer.AddFriendToClient, 1, "添加成功", JsonMapper.ToJson(ToDto(playerCache.GetPlayerModel(client)))); Send(requestClient, OperationCode.PlayerCode, OpPlayer.AddFriendToClient, 1, "添加成功", JsonMapper.ToJson(ToDto(playerCache.GetPlayerModel(requestClient)))); } else { //拒绝了,就回传给原来的客户端,告诉他被拒绝了 Send(requestClient, OperationCode.PlayerCode, OpPlayer.AddFriendToClient, -1, "此玩家拒绝你的请求"); } }
/// <summary> /// 开始匹配的处理 /// </summary> /// <param name="client"></param> /// <param name="isSingle"></param> /// <param name="playerID"></param> private void OnMatchStart(MOBAClient client, int playerID) { //非法操作检测 if (playerCache.GetID(client) != playerID) { return; } MatchRoom room = matchCache.EnterMatch(client, playerID); Send(client, OperationCode.PlayerCode, OpPlayer.MatchStart, 0, "开始匹配成功"); //如果房间满了,就开始选人 if (room.RoomIsFull()) { //通知房间内所有人进入选人界面 room.Broadcast(OperationCode.PlayerCode, OpPlayer.MatchComplete, 1, "是否进入选人界面(10秒)", null); StartSelectAction(room.RedTeamIdList, room.BlueTeamIdList); //删除房间 matchCache.DeleteRoom(room); } }
/// <summary> /// 获取角色信息的处理 /// </summary> private void OnGetInfo(MOBAClient client) { int accountID = accountCache.GetID(client); if (accountID == -1) { Send(client, OperationCode.PlayerCode, OpPlayer.GetPlayerInfo, -1, "非法登录"); return; } else if (playerCache.Has(accountID)) { Send(client, OperationCode.PlayerCode, OpPlayer.GetPlayerInfo, 0, "角色存在"); return; } else { Send(client, OperationCode.PlayerCode, OpPlayer.GetPlayerInfo, -2, "角色不存在"); return; } }
public void OnRequest(MOBAClient client, byte subCode, OperationRequest request) { switch (subCode) { case OpSelect.Enter: OnEnter(client); break; case OpSelect.Select: OnSelect(client, (int)request[0]); break; case OpSelect.Ready: OnReady(client); break; case OpSelect.Chat: OnChat(client, request[0].ToString()); break; } }
/// <summary> /// 添加好友的处理 /// </summary> /// <param name="name"></param> private void OnAddFriend(MOBAClient client, string name) { //获取添加好友的数据模型 PlayerModel friendModel = playerCache.GetPlayerModel(name); //获取当前玩家的数据模型 PlayerModel playerModel = playerCache.GetPlayerModel(client); if (friendModel == null) { Send(client, OperationCode.PlayerCode, OpPlayer.AddFriend, -1, "没有此玩家"); return; } //如果添加的是自己则返回 if (playerCache.GetPlayerModel(client).Id.Equals(friendModel.Id)) { Send(client, OperationCode.PlayerCode, OpPlayer.AddFriend, -3, "不能添加自己为好友"); return; } //如果添加的是已经是好友的玩家 int playerID = playerCache.GetID(client); if (friendModel.FriendIdList.Contains(playerID)) { Send(client, OperationCode.PlayerCode, OpPlayer.AddFriend, -4, "此玩家已经是你的好友"); return; } //如果能获取到数据模型,先判断他是否在线 bool isOnline = playerCache.IsOnLine(friendModel.Id); //不在线 回传不在线 if (!isOnline) { Send(client, OperationCode.PlayerCode, OpPlayer.AddFriend, -1, "此玩家不在线"); } //在线 给模型对应的客户端发消息 MOBAClient friendClient = playerCache.GetClient(friendModel.Id); Send(friendClient, OperationCode.PlayerCode, OpPlayer.AddFriendToClient, 0, "是否添加好友?", JsonMapper.ToJson(ToDto(playerModel))); }