/// <summary> /// 摧毁指定房间 /// </summary> /// <param name="room"></param> public void DestroyRoom(int roomId) { BattleRoom room; if (!RoomDict.TryGetValue(roomId, out room)) { return; } // 移除玩家id和房间id的映射 foreach (DtoHero item in room.HerosArray) { PlayerRoomDict.Remove(item.Id); } // 移除房间id和房间的映射 RoomDict.Remove(roomId); // 清空房间内的数据 room.Clear(); // 回收 RoomQue.Enqueue(room); MobaServer.LogInfo("战斗房间销毁了"); }
/// <summary> /// 摧毁房间 /// </summary> /// <param name="roomId"></param> public void DestroyRoom(int roomId) { SelectRoom room; if (!RoomDict.TryGetValue(roomId, out room)) { return; } // 移除玩家id和房间id的映射 foreach (int item in room.TeamOneDict.Keys) { PlayerRoomDict.Remove(item); } foreach (int item in room.TeamTwoDict.Keys) { PlayerRoomDict.Remove(item); } // 移除房间id和房间的映射 RoomDict.Remove(roomId); // 清空房间内的数据 room.Clear(); // 回收 RoomQue.Enqueue(room); MobaServer.LogInfo("选人房间销毁了"); }
/// <summary> /// 创建战斗的房间 /// </summary> /// <param name="team1"></param> /// <param name="team2"></param> public void CreateRoom(List <DtoSelect> team1, List <DtoSelect> team2) { BattleRoom room = null; if (RoomQue.Count <= 0) { room = new BattleRoom(Index++, team1.Count + team2.Count); } else { room = RoomQue.Dequeue(); } // 初始化房间数据 room.Init(team1, team2); // 添加映射 foreach (DtoSelect item in team1) { PlayerRoomDict.Add(item.PlayerId, room.Id); } foreach (DtoSelect item in team2) { PlayerRoomDict.Add(item.PlayerId, room.Id); } RoomDict.Add(room.Id, room); }
/// <summary> /// 摧毁指定房间 /// </summary> /// <param name="room"></param> public void DestroyRoom(MatchRoom room) { // 移除玩家id和房间id的映射 foreach (int item in room.TeamOneIdList) { PlayerRoomDict.Remove(item); } foreach (int item in room.TeamTwoIdList) { PlayerRoomDict.Remove(item); } // 移除房间id和房间的映射 RoomDict.Remove(room.Id); // 清空房间信息 room.Clear(); // 回收 RoomQue.Enqueue(room); }
/// <summary> /// 玩家进入匹配队列 /// 这里只实现1v1的情况 /// </summary> /// <param name="playerId"></param> /// <param name="peer"></param> /// <returns>房间</returns> public MatchRoom StartMatch(MobaPeer peer, int playerId) { // 存在等待的房间 foreach (MatchRoom item in RoomDict.Values) { if (item.IsFull) { continue; } item.EnterRoom(peer, playerId); // 绑定玩家和房间的映射 PlayerRoomDict.Add(playerId, item.Id); return(item); } // 不存在等待的房间 MatchRoom room = null; // 有可复用的房间 if (RoomQue.Count > 0) { // 添加映射 room = RoomQue.Dequeue(); RoomDict.Add(room.Id, room); PlayerRoomDict.Add(playerId, room.Id); // 玩家进入房间 room.EnterRoom(peer, playerId); return(room); } // 没有可复用的房间 else { // 这里固定房间只进两个人 room = new MatchRoom(Index, 2); Index++; // 添加映射 RoomDict.Add(room.Id, room); PlayerRoomDict.Add(playerId, room.Id); // 玩家进入房间 room.EnterRoom(peer, playerId); return(room); } }
/// <summary> /// 开始选人 /// </summary> public void CreateRoom(List <int> team1, List <int> team2) { SelectRoom room; // 获取房间 if (RoomQue.Count <= 0) { room = new SelectRoom(Index++, team1.Count + team2.Count); } else { room = RoomQue.Dequeue(); } room.InitRoom(team1, team2); // 绑定玩家id和房间id foreach (int item in team1) { PlayerRoomDict.Add(item, room.Id); } foreach (int item in team2) { PlayerRoomDict.Add(item, room.Id); } // 绑定房间id和房间 RoomDict.Add(room.Id, room); // 创建完成 开启定时任务 通知玩家在10s之内进入房间 否则 房间自动销毁 room.StartSchedule(DateTime.UtcNow.AddSeconds(ServerConfig.SelectRoomTimeOff), () => { if (!room.IsAllEnter) { // 通知所有玩家房间被销毁了 room.Brocast(OperationCode.DestroySelect, null); // 销毁房间 DestroyRoom(room.Id); } }); }
/// <summary> /// 玩家离开匹配队列 /// </summary> /// <param name="peer"></param> /// <param name="playerId"></param> /// <returns>是否离开成功</returns> public bool StopMatch(MobaPeer peer, int playerId) { // 获取房间 MatchRoom room = GetRoomByPlayerId(playerId); if (room == null) { return(false); } room.LeaveRoom(peer, playerId); PlayerRoomDict.Remove(playerId); if (room.IsEmpty) { // 移除映射 RoomDict.Remove(room.Id); // 清理房间 room.Clear(); // 回收房间 RoomQue.Enqueue(room); } return(true); }