GameState.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.WebSockets;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace gameapi.Library
  8. {
  9. public class GameState
  10. {
  11. public static GameState instance;
  12. public static GameState GetInstance()
  13. {
  14. if (GameState.instance == null)
  15. {
  16. GameState.instance = new GameState();
  17. }
  18. return GameState.instance;
  19. }
  20. private Dictionary<string, Player> players;
  21. private Dictionary<Tuple<int, int>, List<int>> channels;
  22. public GameState()
  23. {
  24. this.players = new Dictionary<string, Player>();
  25. }
  26. public async Task EnterSector(Player player, Tuple<int, int> sector)
  27. {
  28. var leaveMessage = string.Format("leave {0}", player.GetKey());
  29. await this.MessageAllOtherPlayersInSector(player, leaveMessage);
  30. player.SetSector(sector);
  31. //TODO: get valid location in sector to enter
  32. player.SetPosition(5000, 5000);
  33. var joinMessage = string.Format("join {0}", player.GetShipData());
  34. await this.MessageAllOtherPlayersInSector(player, joinMessage);
  35. }
  36. public List<Player> GetAllPlayers()
  37. {
  38. List<Player> allPlayers = new List<Player>();
  39. allPlayers.AddRange(this.players.Values);
  40. return allPlayers;
  41. }
  42. public List<Player> GetSectorPopulation(Tuple<int, int> sector, Player exclude = null)
  43. {
  44. var playersInSector = new List<Player>();
  45. foreach (var kvp in this.players)
  46. {
  47. if (exclude != null && kvp.Key.Equals(exclude.GetKey()))
  48. {
  49. continue;
  50. }
  51. if (kvp.Value.GetSector().Equals(sector))
  52. {
  53. playersInSector.Add(kvp.Value);
  54. }
  55. }
  56. return playersInSector;
  57. }
  58. public async Task MessageAllPlayers(string message)
  59. {
  60. List<Task> messagesToSend = new List<Task>();
  61. foreach (var player in this.players)
  62. {
  63. messagesToSend.Add(this.MessagePlayer(player.Value, message));
  64. }
  65. await Task.WhenAll(messagesToSend);
  66. }
  67. public async Task MessageAllPlayersInSector(Player fromPlayer, string message)
  68. {
  69. List<Task> messagesToSend = new List<Task>();
  70. var playersInSector = this.GetSectorPopulation(fromPlayer.GetSector());
  71. await this.MessagePlayers(playersInSector, message);
  72. }
  73. public async Task MessageAllPlayersInSector(Tuple<int, int> sector, string message)
  74. {
  75. List<Task> messagesToSend = new List<Task>();
  76. var playersInSector = this.GetSectorPopulation(sector);
  77. await this.MessagePlayers(playersInSector, message);
  78. }
  79. public async Task MessageAllOtherPlayersInSector(Player fromPlayer, string message)
  80. {
  81. List<Task> messagesToSend = new List<Task>();
  82. var playersInSector = this.GetSectorPopulation(fromPlayer.GetSector(), fromPlayer);
  83. await this.MessagePlayers(playersInSector, message);
  84. }
  85. public async Task MessagePlayers(IEnumerable<Player> playersToMessage, string message)
  86. {
  87. List<Task> messagesToSend = new List<Task>();
  88. foreach (var player in playersToMessage)
  89. {
  90. messagesToSend.Add(this.MessagePlayer(player, message));
  91. }
  92. await Task.WhenAll(messagesToSend.ToArray());
  93. }
  94. public Player GetPlayerBySocket(WebSocket webSocket)
  95. {
  96. return this.players.GetValueOrDefault("" + webSocket.GetHashCode(), null);
  97. }
  98. public void AddPlayer(WebSocket socket)
  99. {
  100. var player = new Player(socket);
  101. this.players.Add("" + socket.GetHashCode(), player);
  102. }
  103. public async Task MessagePlayer(Player player, string message)
  104. {
  105. var connection = player.GetConnection();
  106. if (connection.State == WebSocketState.Open)
  107. {
  108. await connection.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message), 0, message.Length), WebSocketMessageType.Text, true, CancellationToken.None);
  109. }
  110. }
  111. internal IEnumerable<Player> GetPlayers()
  112. {
  113. return this.players.Values;
  114. }
  115. }
  116. }