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.

Game.cs 3.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Grpc.Core;
  2. using Protobuf;
  3. using System.Threading;
  4. using Timothy.FrameRateTask;
  5. using System;
  6. using System.Net.Http.Headers;
  7. namespace Server
  8. {
  9. public class Game
  10. {
  11. private const int gameTime = 3000;
  12. public int GameTime => gameTime;
  13. private MessageToClient gameInfo = new();
  14. private object gameInfoLock = new();
  15. private int isGaming = 0;
  16. public bool IsGaming
  17. {
  18. get => Interlocked.CompareExchange(ref isGaming, 0, 0) != 0;
  19. set => Interlocked.Exchange(ref isGaming, value ? 1 : 0);
  20. }
  21. public MessageToClient GetCopiedGameInfo()
  22. {
  23. lock (gameInfoLock)
  24. {
  25. return gameInfo.Clone();
  26. }
  27. }
  28. public void AddPlayer(PlayerMsg player)
  29. {
  30. lock (gameInfoLock)
  31. {
  32. if (player.PlayerType == PlayerType.NullPlayerType)
  33. return;
  34. if (player.PlayerType == PlayerType.HumanPlayer)
  35. {
  36. gameInfo.HumanMessage.Add(new MessageOfHuman()
  37. {
  38. PlayerId = player.PlayerId
  39. });
  40. return;
  41. }
  42. if (player.PlayerType == PlayerType.ButcherPlayer)
  43. {
  44. gameInfo.ButcherMessage.Add(new MessageOfButcher()
  45. {
  46. PlayerId = player.PlayerId
  47. });
  48. return;
  49. }
  50. }
  51. }
  52. public void Move()
  53. {
  54. }
  55. public SemaphoreSlim StartGame()
  56. {
  57. IsGaming = true;
  58. var waitHandle = new SemaphoreSlim(0);
  59. new Thread
  60. (
  61. () =>
  62. {
  63. new FrameRateTaskExecutor<int>
  64. (
  65. () => IsGaming,
  66. () =>
  67. {
  68. lock (gameInfo)
  69. {
  70. for (int i = 0; i < gameInfo.HumanMessage.Count; i++)
  71. {
  72. if (gameInfo.HumanMessage[i] != null)
  73. {
  74. gameInfo.HumanMessage[i].X++;
  75. gameInfo.HumanMessage[i].Y--;
  76. }
  77. }
  78. for (int i = 0; i < gameInfo.ButcherMessage.Count; i++)
  79. {
  80. if (gameInfo.ButcherMessage[i] != null)
  81. {
  82. gameInfo.ButcherMessage[i].X--;
  83. gameInfo.ButcherMessage[i].Y++;
  84. }
  85. }
  86. }
  87. },
  88. 100,
  89. () =>
  90. {
  91. IsGaming = false;
  92. waitHandle.Release();
  93. return 0;
  94. },
  95. gameTime
  96. ).Start();
  97. }
  98. )
  99. { IsBackground = true }.Start();
  100. return waitHandle;
  101. }
  102. }
  103. }