123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Net.WebSockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace gameapi.Library
- {
- public class GameState
- {
- public static GameState instance;
- public static GameState GetInstance()
- {
- if (GameState.instance == null)
- {
- GameState.instance = new GameState();
- }
- return GameState.instance;
- }
- private Dictionary<string, Player> players;
- private Dictionary<Tuple<int, int>, List<int>> channels;
- public GameState()
- {
- this.players = new Dictionary<string, Player>();
- }
- public async Task EnterSector(Player player, Tuple<int, int> sector)
- {
- var leaveMessage = string.Format("leave {0}", player.GetKey());
- await this.MessageAllOtherPlayersInSector(player, leaveMessage);
- player.SetSector(sector);
- //TODO: get valid location in sector to enter
- player.SetPosition(5000, 5000);
- var joinMessage = string.Format("join {0}", player.GetShipData());
- await this.MessageAllOtherPlayersInSector(player, joinMessage);
- }
- public List<Player> GetAllPlayers()
- {
- List<Player> allPlayers = new List<Player>();
- allPlayers.AddRange(this.players.Values);
- return allPlayers;
- }
- public List<Player> GetSectorPopulation(Tuple<int, int> sector, Player exclude = null)
- {
- var playersInSector = new List<Player>();
- foreach (var kvp in this.players)
- {
- if (exclude != null && kvp.Key.Equals(exclude.GetKey()))
- {
- continue;
- }
- if (kvp.Value.GetSector().Equals(sector))
- {
- playersInSector.Add(kvp.Value);
- }
- }
- return playersInSector;
- }
- public async Task MessageAllPlayers(string message)
- {
- List<Task> messagesToSend = new List<Task>();
- foreach (var player in this.players)
- {
- messagesToSend.Add(this.MessagePlayer(player.Value, message));
- }
- await Task.WhenAll(messagesToSend);
- }
- public async Task MessageAllPlayersInSector(Player fromPlayer, string message)
- {
- List<Task> messagesToSend = new List<Task>();
- var playersInSector = this.GetSectorPopulation(fromPlayer.GetSector());
- await this.MessagePlayers(playersInSector, message);
- }
- public async Task MessageAllPlayersInSector(Tuple<int, int> sector, string message)
- {
- List<Task> messagesToSend = new List<Task>();
- var playersInSector = this.GetSectorPopulation(sector);
- await this.MessagePlayers(playersInSector, message);
- }
- public async Task MessageAllOtherPlayersInSector(Player fromPlayer, string message)
- {
- List<Task> messagesToSend = new List<Task>();
- var playersInSector = this.GetSectorPopulation(fromPlayer.GetSector(), fromPlayer);
- await this.MessagePlayers(playersInSector, message);
- }
- public async Task MessagePlayers(IEnumerable<Player> playersToMessage, string message)
- {
- List<Task> messagesToSend = new List<Task>();
- foreach (var player in playersToMessage)
- {
- messagesToSend.Add(this.MessagePlayer(player, message));
- }
- await Task.WhenAll(messagesToSend.ToArray());
- }
- public Player GetPlayerBySocket(WebSocket webSocket)
- {
- return this.players.GetValueOrDefault("" + webSocket.GetHashCode(), null);
- }
- public void AddPlayer(WebSocket socket)
- {
- var player = new Player(socket);
- this.players.Add("" + socket.GetHashCode(), player);
- }
- public async Task MessagePlayer(Player player, string message)
- {
- var connection = player.GetConnection();
- if (connection.State == WebSocketState.Open)
- {
- await connection.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message), 0, message.Length), WebSocketMessageType.Text, true, CancellationToken.None);
- }
- }
- internal IEnumerable<Player> GetPlayers()
- {
- return this.players.Values;
- }
- }
- }
|