You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlaybackServer.cs 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using Protobuf;
  2. using Playback;
  3. using System;
  4. using System.Threading;
  5. using Timothy.FrameRateTask;
  6. using Gaming;
  7. using Grpc.Core;
  8. namespace Server
  9. {
  10. class PlaybackServer : AvailableService.AvailableServiceBase
  11. {
  12. protected readonly ArgumentOptions options;
  13. private int[,] teamScore;
  14. private Dictionary<long, (SemaphoreSlim, SemaphoreSlim)> semaDict = new();
  15. private object semaDictLock = new();
  16. private MessageToClient? currentGameInfo = new();
  17. private uint spectatorMinPlayerID = 2023;
  18. private List<uint> spectatorList = new List<uint>();
  19. public int TeamCount => options.TeamCount;
  20. private MessageWriter? mwr = null;
  21. private bool IsGaming { get; set; }
  22. private int[] finalScore;
  23. public int[] FinalScore
  24. {
  25. get
  26. {
  27. return finalScore;
  28. }
  29. }
  30. public PlaybackServer(ArgumentOptions options)
  31. {
  32. this.options = options;
  33. IsGaming = true;
  34. teamScore = new int[0, 0];
  35. finalScore = new int[0];
  36. }
  37. public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context)
  38. {
  39. if (request.PlayerId >= spectatorMinPlayerID)
  40. {
  41. // 观战模式
  42. uint tp = (uint)request.PlayerId;
  43. if (!spectatorList.Contains(tp))
  44. {
  45. spectatorList.Add(tp);
  46. Console.WriteLine("A new spectator comes to watch this game.");
  47. var temp = (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1));
  48. lock (semaDictLock)
  49. {
  50. semaDict.Add(request.PlayerId, temp);
  51. }
  52. }
  53. do
  54. {
  55. semaDict[request.PlayerId].Item1.Wait();
  56. try
  57. {
  58. if (currentGameInfo != null)
  59. {
  60. await responseStream.WriteAsync(currentGameInfo);
  61. //Console.WriteLine("Send!");
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. //Console.WriteLine(ex);
  67. }
  68. finally
  69. {
  70. semaDict[request.PlayerId].Item2.Release();
  71. }
  72. } while (IsGaming == true);
  73. return;
  74. }
  75. }
  76. public void ReportGame(MessageToClient? msg)
  77. {
  78. currentGameInfo = msg;
  79. foreach (var kvp in semaDict)
  80. {
  81. kvp.Value.Item1.Release();
  82. }
  83. foreach (var kvp in semaDict)
  84. {
  85. kvp.Value.Item2.Wait();
  86. }
  87. }
  88. public void WaitForGame()
  89. {
  90. try
  91. {
  92. if (options.ResultOnly)
  93. {
  94. using (MessageReader mr = new MessageReader(options.FileName))
  95. {
  96. Console.WriteLine("Parsing playback file...");
  97. teamScore = new int[mr.teamCount, mr.playerCount];
  98. finalScore = new int[mr.teamCount];
  99. int infoNo = 0;
  100. object cursorLock = new object();
  101. var initialTop = Console.CursorTop;
  102. var initialLeft = Console.CursorLeft;
  103. while (true)
  104. {
  105. MessageToClient? msg = null;
  106. for (int i = 0; i < mr.teamCount; ++i)
  107. {
  108. for (int j = 0; j < mr.playerCount; ++j)
  109. {
  110. msg = mr.ReadOne();
  111. if (msg == null)
  112. {
  113. Console.WriteLine("The game doesn't come to an end because of timing up!");
  114. IsGaming = false;
  115. goto endParse;
  116. }
  117. lock (cursorLock)
  118. {
  119. var curTop = Console.CursorTop;
  120. var curLeft = Console.CursorLeft;
  121. Console.SetCursorPosition(initialLeft, initialTop);
  122. Console.WriteLine($"Parsing messages... Current message number: {infoNo}");
  123. Console.SetCursorPosition(curLeft, curTop);
  124. }
  125. if (msg != null)
  126. {
  127. //teamScore[i] = msg.TeamScore;
  128. }
  129. }
  130. }
  131. ++infoNo;
  132. if (msg == null)
  133. {
  134. Console.WriteLine("No game information in this file!");
  135. goto endParse;
  136. }
  137. if (msg.GameState == GameState.GameEnd)
  138. {
  139. Console.WriteLine("Game over normally!");
  140. finalScore[0] = msg.AllMessage.StudentScore;
  141. finalScore[1] = msg.AllMessage.TrickerScore;
  142. goto endParse;
  143. }
  144. }
  145. endParse:
  146. Console.WriteLine($"Successfully parsed {infoNo} informations!");
  147. }
  148. }
  149. else
  150. {
  151. long timeInterval = GameServer.SendMessageToClientIntervalInMilliseconds;
  152. if (options.PlaybackSpeed != 1.0)
  153. {
  154. options.PlaybackSpeed = Math.Max(0.25, Math.Min(4.0, options.PlaybackSpeed));
  155. timeInterval = (int)Math.Round(timeInterval / options.PlaybackSpeed);
  156. }
  157. using (MessageReader mr = new MessageReader(options.FileName))
  158. {
  159. teamScore = new int[mr.teamCount, mr.playerCount];
  160. finalScore = new int[mr.teamCount];
  161. int infoNo = 0;
  162. object cursorLock = new object();
  163. var msgCurTop = Console.CursorTop;
  164. var msgCurLeft = Console.CursorLeft;
  165. var frt = new FrameRateTaskExecutor<int>
  166. (
  167. loopCondition: () => true,
  168. loopToDo: () =>
  169. {
  170. MessageToClient? msg = null;
  171. msg = mr.ReadOne();
  172. if (msg == null)
  173. {
  174. Console.WriteLine("The game doesn't come to an end because of timing up!");
  175. IsGaming = false;
  176. ReportGame(msg);
  177. return false;
  178. }
  179. ReportGame(msg);
  180. lock (cursorLock)
  181. {
  182. var curTop = Console.CursorTop;
  183. var curLeft = Console.CursorLeft;
  184. Console.SetCursorPosition(msgCurLeft, msgCurTop);
  185. Console.WriteLine($"Sending messages... Current message number: {infoNo}.");
  186. Console.SetCursorPosition(curLeft, curTop);
  187. }
  188. if (msg != null)
  189. {
  190. foreach (var item in msg.ObjMessage)
  191. {
  192. if (item.StudentMessage != null)
  193. teamScore[0, item.StudentMessage.PlayerId] = item.StudentMessage.Score;
  194. if (item.TrickerMessage != null)
  195. teamScore[1, item.TrickerMessage.PlayerId - options.MaxStudentCount] = item.TrickerMessage.Score; // 这里默认 Tricker 的 PlayerId 从 MaxStudentCount = 4 开始
  196. }
  197. }
  198. ++infoNo;
  199. if (msg == null)
  200. {
  201. Console.WriteLine("No game information in this file!");
  202. IsGaming = false;
  203. ReportGame(msg);
  204. return false;
  205. }
  206. if (msg.GameState == GameState.GameEnd)
  207. {
  208. Console.WriteLine("Game over normally!");
  209. IsGaming = false;
  210. finalScore[0] = msg.AllMessage.StudentScore;
  211. finalScore[1] = msg.AllMessage.TrickerScore;
  212. ReportGame(msg);
  213. return false;
  214. }
  215. return true;
  216. },
  217. timeInterval: timeInterval,
  218. finallyReturn: () => 0
  219. )
  220. { AllowTimeExceed = true, MaxTolerantTimeExceedCount = 5 };
  221. Console.WriteLine("The server is well prepared! Please MAKE SURE that you have opened all the clients to watch the game!");
  222. Console.WriteLine("If ALL clients have opened, press any key to start.");
  223. Console.ReadKey();
  224. new Thread
  225. (
  226. () =>
  227. {
  228. var rateCurTop = Console.CursorTop;
  229. var rateCurLeft = Console.CursorLeft;
  230. lock (cursorLock)
  231. {
  232. rateCurTop = Console.CursorTop;
  233. rateCurLeft = Console.CursorLeft;
  234. Console.WriteLine($"Send message to clients frame rate: {frt.FrameRate}");
  235. }
  236. while (!frt.Finished)
  237. {
  238. lock (cursorLock)
  239. {
  240. var curTop = Console.CursorTop;
  241. var curLeft = Console.CursorLeft;
  242. Console.SetCursorPosition(rateCurLeft, rateCurTop);
  243. Console.WriteLine($"Send message to clients frame rate: {frt.FrameRate}");
  244. Console.SetCursorPosition(curLeft, curTop);
  245. }
  246. Thread.Sleep(1000);
  247. }
  248. }
  249. )
  250. { IsBackground = true }.Start();
  251. lock (cursorLock)
  252. {
  253. msgCurLeft = Console.CursorLeft;
  254. msgCurTop = Console.CursorTop;
  255. Console.WriteLine("Sending messages...");
  256. }
  257. frt.Start();
  258. }
  259. }
  260. }
  261. finally
  262. {
  263. teamScore ??= new int[0, 0];
  264. }
  265. }
  266. }
  267. }