static GameRoom() { _instance = new GameRoom(); var roomStruct = new MemoryCacheStruct<RoomData>(); var roomList = new ShareCacheStruct<RoomInfo>().FindAll(false); foreach (var roomInfo in roomList) { var roomData = new RoomData() { RoomId = roomInfo.Id }; string key = roomData.RoomId.ToString(); roomStruct.TryAdd(key, roomData); } }
/// <summary> /// 选择桌位,找不到桌子自动新开一个 /// </summary> /// <param name="user"></param> /// <param name="roomData"></param> /// <param name="roomInfo"></param> /// <returns></returns> public TableData SelectTable(GameUser user, RoomData roomData, RoomInfo roomInfo) { TableData useTable = null; roomData.Tables.Foreach((key, table) => { if (SelectPosition(user, roomData, table)) { useTable = table; //退出Foreach return false; } return true; }); if (roomData.TablePool.Count == 0) { //初始桌数 int minTableCount = ConfigEnvSet.GetInt("Game.Table.MinTableCount", 10); var pokers = PokerList; TableData tableData = null; for (int i = 0; i < minTableCount; i++) { int tableId = roomData.NewTableId; tableData = new TableData(roomData.RoomId, tableId, roomInfo.PlayerNum, roomInfo.AnteNum, roomInfo.MultipleNum, DoTableTimer, roomInfo.CardPackNum); roomData.TablePool.Enqueue(tableData); CreateCardData(tableData, pokers); } } if (useTable == null && roomData.TablePool.TryDequeue(out useTable)) { SetTablePosition(roomData.RoomId, useTable, useTable.Positions[0], user); roomData.Tables.Add(useTable.TableId, useTable); } if (useTable != null && !useTable.IsTimerStarted) { int periodAiStart = ConfigEnvSet.GetInt("Game.Table.AIIntoTime", 5000); useTable.ReStartTimer(periodAiStart); } return useTable; }
/// <summary> /// 找桌位,已经进入过保留原位置 /// </summary> /// <param name="user"></param> /// <param name="roomData"></param> /// <param name="table"></param> /// <returns></returns> public bool SelectPosition(GameUser user, RoomData roomData, TableData table) { var userPos = Array.Find(table.Positions, m => m.UserId == user.UserId); if (userPos != null) { //已经在桌上 SetTablePosition(roomData.RoomId, table, userPos, user); return true; } if (!table.IsSettlemented && table.BackCardData.Count > 0) { //已经发牌 return false; } if (table.IsSettlemented && table.BackCardData.Count > 0) { DoComplatedSettlement(table); return false; } foreach (var pos in table.Positions) { lock (table) { if (pos.UserId == 0) { SetTablePosition(roomData.RoomId, table, pos, user); return true; } } } return false; }