using System.Collections.Generic; using System.Linq; using System.Threading; using Cysharp.Threading.Tasks; using Game.Player; using UnityEngine; namespace Game.Room { public interface IRoom { string roomName { get; } RoomData roomData { get; } RoomInfo roomInfo { get; } IReadOnlyList players { get; } RoomType roomType { get; } string roomId { get; } UniTask JoinAsync(IPlayer player, CancellationToken token); UniTask Quit(IPlayer player); List QuitAndClearAll(); void Dispose(); bool RefreshMoney(float jinBei); } [System.Serializable] public class Room : IRoom { private GameObject self; private RoomInfo _roomInfo; private RoomData _roomData; private string _roomName; public RoomType roomType => this._roomData.roomType; public string roomId => ((int)this._roomData.roomType).ToString(); public string roomName => this._roomName; public RoomData roomData => this._roomData; public RoomInfo roomInfo => this._roomInfo; public IReadOnlyList players => this._roomData.players; public Room(RoomType roomType, GameObject roomGo, float jinBei) { this._roomData = new RoomData(roomType, jinBei); _roomName = roomType.ToString(); // view this._roomInfo = roomGo.GetComponent(); this._roomInfo.SetRoom(this); this.self = roomGo; this._roomData.jinBeiCallback += this._roomInfo.UpdateNumber; } public void Dispose() { this._roomData.jinBeiCallback -= this._roomInfo.UpdateNumber; this._roomData.Reset(); GameObject.Destroy(this.self); this.self = null; this._roomInfo = null; this._roomData = null; this._roomName = null; } public async UniTask JoinAsync(IPlayer player, CancellationToken token) { if (this.players.Contains(player)) return false; // Debug.Log($"{player.playerName} join {roomType} !!"); bool res = await player.WaitMoveRoomAsync(this, token); this._roomData.AddPlayer(player); return true; } public async UniTask Quit(IPlayer player) { if (!this.players.Contains(player)) return false; // Debug.Log($"{player.playerName} quit {roomType} !!"); this._roomData.RemovePlayer(player); await UniTask.Yield(); return true; } public List QuitAndClearAll() { var list = new List(_roomData.players); this._roomData.Reset(); return list; } public bool RefreshMoney(float jinBei) { this._roomData.RefreshMoney(jinBei); return true; } } }