Frame/Assets/Scripts/Dinosaurs/Room/Room.cs

131 lines
3.9 KiB
C#
Raw Normal View History

2024-04-06 11:59:18 +08:00
using System.Collections.Generic;
2024-04-10 16:18:32 +08:00
using System.Linq;
2024-04-08 18:20:55 +08:00
using System.Threading;
using Cysharp.Threading.Tasks;
2024-04-06 11:59:18 +08:00
using Game.Player;
using UnityEngine;
namespace Game.Room
2024-04-06 11:59:18 +08:00
{
public interface IRoom
{
string roomName { get; }
RoomData roomData { get; }
RoomInfo roomInfo { get; }
IReadOnlyList<IPlayer> players { get; }
RoomType roomType { get; }
2024-04-06 11:59:18 +08:00
UniTask<bool> JoinAsync(IPlayer player, CancellationToken token);
UniTask<bool> Quit(IPlayer player);
2024-04-10 16:18:32 +08:00
void SetIsCanReturnJinBei(bool isCan);
List<IPlayer> QuitAndClearAll();
2024-04-10 16:18:32 +08:00
void Dispose();
2024-04-06 11:59:18 +08:00
bool InvestmentJinBei(IPlayer player, float jinbei);
}
2024-04-06 11:59:18 +08:00
[System.Serializable]
public class Room : IRoom
{
private GameObject self;
private RoomInfo _roomInfo;
private RoomData _roomData;
private string _roomName;
private bool _isCanReturnJinBei;
2024-04-06 11:59:18 +08:00
public RoomType roomType => this._roomData.roomType;
2024-04-09 13:22:52 +08:00
public string roomName => this._roomName;
2024-04-09 13:22:52 +08:00
public RoomData roomData => this._roomData;
public RoomInfo roomInfo => this._roomInfo;
2024-04-06 11:59:18 +08:00
public IReadOnlyList<IPlayer> players => this._roomData.players;
2024-04-06 11:59:18 +08:00
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;
this._roomData.jinBeiCallback += this._roomInfo.UpdateNumber;
}
2024-04-10 16:18:32 +08:00
public void Dispose()
{
this._roomData.jinBeiCallback -= this._roomInfo.UpdateNumber;
this._roomData.Reset();
2024-04-10 16:18:32 +08:00
GameObject.Destroy(this.self);
this.self = null;
this._roomInfo = null;
this._roomData = null;
this._roomName = null;
}
2024-04-06 11:59:18 +08:00
public async UniTask<bool> JoinAsync(IPlayer player, CancellationToken token)
{
if (this.players.Contains(player))
return false;
2024-04-11 14:44:39 +08:00
// Debug.Log($"{player.playerName} join {roomType} !!");
bool res = await player.WaitMoveRoomAsync(this, token);
this._roomData.AddPlayer(player);
return true;
}
2024-04-06 11:59:18 +08:00
public async UniTask<bool> Quit(IPlayer player)
{
if (!this.players.Contains(player))
return false;
Debug.Log($"{player.playerName} quit {roomType} !!");
2024-04-10 16:18:32 +08:00
var f = this._roomData.RemovePlayer(player);
EventManager.Instance.FireNow(this, new ReturnPlayerJinBeiEventArgs(player, f));
2024-04-11 14:44:39 +08:00
await UniTask.Yield();
return true;
}
2024-04-06 11:59:18 +08:00
public void SetIsCanReturnJinBei(bool isCan)
{
this._isCanReturnJinBei = isCan;
}
public List<IPlayer> QuitAndClearAll()
2024-04-10 16:18:32 +08:00
{
if (this._isCanReturnJinBei)
{
// 归还金贝
var dictionary = this._roomData.ReturnPlayerAndJinBei();
foreach (var player in dictionary.Keys)
{
Debug.Log($"return player {player.playerName} jinBei : {dictionary[player]}");
EventManager.Instance.FireNow(this, new ReturnPlayerJinBeiEventArgs(player, dictionary[player]));
}
}
2024-04-10 16:18:32 +08:00
var list = new List<IPlayer>(_roomData.players);
this._roomData.Reset();
return list;
}
2024-04-06 11:59:18 +08:00
public bool InvestmentJinBei(IPlayer player, float jinBei)
2024-04-08 18:20:55 +08:00
{
if (!this.players.Contains(player))
{
Debug.LogWarning($"{roomType} dont have {player.playerName}");
return false;
}
2024-04-06 11:59:18 +08:00
this._roomData.AddJinBei(player, jinBei);
return true;
}
2024-04-06 11:59:18 +08:00
}
}