|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Grpc.Core;
- using Protobuf;
- using System.Threading;
- using Timothy.FrameRateTask;
- using System;
- using System.Net.Http.Headers;
-
- namespace Server
- {
- public class Game
- {
- private const int gameTime = 3000;
- public int GameTime => gameTime;
-
- private MessageToClient gameInfo = new();
- private object gameInfoLock = new();
- private int isGaming = 0;
- public bool IsGaming
- {
- get => Interlocked.CompareExchange(ref isGaming, 0, 0) != 0;
- set => Interlocked.Exchange(ref isGaming, value ? 1 : 0);
- }
-
- public MessageToClient GetCopiedGameInfo()
- {
- lock (gameInfoLock)
- {
- return gameInfo.Clone();
- }
- }
- public void AddPlayer(PlayerMsg player)
- {
- lock (gameInfoLock)
- {
- if (player.PlayerType == PlayerType.NullPlayerType)
- return;
- if (player.PlayerType == PlayerType.HumanPlayer)
- {
- gameInfo.HumanMessage.Add(new MessageOfHuman()
- {
- PlayerId = player.PlayerId
- });
- return;
- }
- if (player.PlayerType == PlayerType.ButcherPlayer)
- {
- gameInfo.ButcherMessage.Add(new MessageOfButcher()
- {
- PlayerId = player.PlayerId
- });
- return;
- }
- }
- }
-
- public void Move()
- {
- }
-
- public SemaphoreSlim StartGame()
- {
- IsGaming = true;
- var waitHandle = new SemaphoreSlim(0);
-
- new Thread
- (
- () =>
- {
- new FrameRateTaskExecutor<int>
- (
- () => IsGaming,
- () =>
- {
- lock (gameInfo)
- {
- for (int i = 0; i < gameInfo.HumanMessage.Count; i++)
- {
- if (gameInfo.HumanMessage[i] != null)
- {
- gameInfo.HumanMessage[i].X++;
- gameInfo.HumanMessage[i].Y--;
- }
- }
- for (int i = 0; i < gameInfo.ButcherMessage.Count; i++)
- {
- if (gameInfo.ButcherMessage[i] != null)
- {
- gameInfo.ButcherMessage[i].X--;
- gameInfo.ButcherMessage[i].Y++;
- }
- }
- }
- },
- 100,
- () =>
- {
- IsGaming = false;
- waitHandle.Release();
- return 0;
- },
- gameTime
- ).Start();
- }
- )
- { IsBackground = true }.Start();
- return waitHandle;
- }
- }
- }
|