2023-03-06 10:06:43 +04:00
|
|
|
/*
|
|
|
|
|
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Ragon.Client;
|
|
|
|
|
|
|
|
|
|
public sealed class RagonPlayerCache
|
|
|
|
|
{
|
2023-06-27 23:41:30 +03:00
|
|
|
private readonly List<RagonPlayer> _players = new();
|
|
|
|
|
private readonly Dictionary<string, RagonPlayer> _playersById = new();
|
|
|
|
|
private readonly Dictionary<ushort, RagonPlayer> _playersByConnection = new();
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-06-27 23:41:30 +03:00
|
|
|
public IReadOnlyList<RagonPlayer> Players => _players;
|
2023-03-06 10:06:43 +04:00
|
|
|
public RagonPlayer Owner { get; private set; }
|
2023-06-27 23:41:30 +03:00
|
|
|
public RagonPlayer Local { get; private set; }
|
2023-03-06 10:06:43 +04:00
|
|
|
public bool IsRoomOwner => _ownerId == _localId;
|
|
|
|
|
|
|
|
|
|
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId];
|
|
|
|
|
public RagonPlayer? GetPlayerByPeer(ushort peerId) => _playersByConnection[peerId];
|
|
|
|
|
|
|
|
|
|
private string _ownerId;
|
|
|
|
|
private string _localId;
|
|
|
|
|
|
|
|
|
|
public void SetOwnerAndLocal(string ownerId, string localId)
|
|
|
|
|
{
|
|
|
|
|
_ownerId = ownerId;
|
|
|
|
|
_localId = localId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddPlayer(ushort peerId, string playerId, string playerName)
|
|
|
|
|
{
|
|
|
|
|
if (_playersById.ContainsKey(playerId))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var isOwner = playerId == _ownerId;
|
|
|
|
|
var isLocal = playerId == _localId;
|
|
|
|
|
|
|
|
|
|
RagonLog.Trace($"Added player {peerId}|{playerId}|{playerName} IsOwner: {isOwner} isLocal: {isLocal}");
|
|
|
|
|
|
|
|
|
|
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
|
|
|
|
|
|
2023-05-07 12:46:39 +03:00
|
|
|
if (player.IsLocal)
|
2023-06-27 23:41:30 +03:00
|
|
|
Local = player;
|
2023-03-06 10:06:43 +04:00
|
|
|
|
|
|
|
|
if (player.IsRoomOwner)
|
|
|
|
|
Owner = player;
|
|
|
|
|
|
|
|
|
|
_players.Add(player);
|
|
|
|
|
_playersById.Add(player.Id, player);
|
|
|
|
|
_playersByConnection.Add(player.PeerId, player);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemovePlayer(string playerId)
|
|
|
|
|
{
|
|
|
|
|
if (_playersById.Remove(playerId, out var player))
|
|
|
|
|
{
|
|
|
|
|
_players.Remove(player);
|
|
|
|
|
_playersByConnection.Remove(player.PeerId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 07:47:57 +03:00
|
|
|
public void OnOwnershipChanged(ushort playerPeerId)
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
foreach (var player in _players)
|
|
|
|
|
{
|
2023-07-01 07:47:57 +03:00
|
|
|
if (player.PeerId == playerPeerId)
|
|
|
|
|
{
|
2023-03-06 10:06:43 +04:00
|
|
|
Owner = player;
|
2023-07-01 07:47:57 +03:00
|
|
|
Owner.IsRoomOwner = true;
|
|
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Cleanup()
|
|
|
|
|
{
|
|
|
|
|
_players.Clear();
|
|
|
|
|
_playersByConnection.Clear();
|
|
|
|
|
_playersById.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|