Frame/Assets/Scripts/Room/Room.cs

105 lines
2.7 KiB
C#

using System.Collections.Generic;
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; }
List<IPlayer> players { get; }
RoomType roomType { get; }
UniTask<bool> JoinAsync(IPlayer player, CancellationToken token);
UniTask<bool> Quit(IPlayer player);
List<IPlayer> QuitAndClearAll();
// float ClearAll();
void Dispose();
bool InvestmentJinbei(IPlayer player, float jinbei);
}
class Room : IRoom
{
private GameObject self;
private RoomInfo _roomInfo;
private RoomData _roomData;
private string _roomName;
public RoomType roomType => this._roomData.roomType;
public string roomName => this._roomName;
public RoomData roomData => this._roomData;
public RoomInfo roomInfo => this._roomInfo;
public List<IPlayer> players => this._roomData.players;
public Room(RoomType roomType, GameObject roomGo)
{
this._roomData = new RoomData(roomType);
_roomName = roomType.ToString();
// view
this._roomInfo = roomGo.GetComponent<RoomInfo>();
this._roomInfo.SetRoom(this);
this.self = roomGo;
}
public async UniTask<bool> 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<bool> Quit(IPlayer player)
{
if (!this.players.Contains(player))
return false;
Debug.Log($"{player.playerName} quit {roomType} !!");
// player.SetRoom(null);
this._roomData.RemovePlayer(player);
await UniTask.Yield();
return true;
}
public List<IPlayer> QuitAndClearAll()
{
var list = new List<IPlayer>(_roomData.players);
this._roomData.players.Clear();
this._roomData.jinbei = 0;
return list;
}
// public float ClearAll()
// {
// // TODO:
//// EventManager.Instance.FireNow(this, new InputNameFinishEventArgs());
//// this.players.Clear();
// return this._roomData.jinbei;
// }
public void Dispose()
{
foreach (var player in this.players)
{
player.Dispose();
}
}
public bool InvestmentJinbei(IPlayer player, float jinbei)
{
if (!this.players.Contains(player))
return false;
this._roomData.jinbei += jinbei;
return true;
}
}