// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Spludlow.Tetris
{
///
/// Thread Safe Wrapper for the Players dictionary used by the server
///
public class TetrisPlayers
{
private Dictionary Players = new Dictionary();
private int LastPlayerId = 0;
public TetrisPlayers()
{
}
public void Add(TetrisPlayer player)
{
lock (this.Players)
{
player.ClientId = ++LastPlayerId;
this.Players.Add(player.ClientId, player);
}
}
public TetrisPlayer Remove(int playerId)
{
TetrisPlayer player = null;
lock (this.Players)
{
if (this.Players.ContainsKey(playerId) == true)
{
player = this.Players[playerId];
this.Players.Remove(playerId);
}
}
return player;
}
public TetrisPlayer[] CurrentPlayers()
{
TetrisPlayer[] players;
lock (this.Players)
{
players = new TetrisPlayer[this.Players.Keys.Count];
int index = 0;
foreach (int clientId in this.Players.Keys)
players[index++] = this.Players[clientId];
}
return players;
}
public TetrisPlayer Get(int playerId)
{
lock (this.Players)
{
if (this.Players.ContainsKey(playerId) == true)
return this.Players[playerId];
}
return null;
}
public int TargetNext(int currentTargetPlayerId)
{
List keyList;
lock (this.Players)
{
keyList = new List(this.Players.Keys);
}
int index = -1;
if (currentTargetPlayerId != 0 && keyList.Contains(currentTargetPlayerId) == true)
index = keyList.IndexOf(currentTargetPlayerId);
++index;
if (index >= keyList.Count)
return 0;
return keyList[index];
}
}
}