Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using HacLib.Interface;
- using HacLib.Interface.Dependent;
- using HacLib.Models;
- using System.Linq;
- using System.Diagnostics;
- using HacLib.Models.Sys;
- using HacLib.Enums;
- namespace HacLib.Service
- {
- public class GameMachine : INotifierCliD, IGame
- {
- private const int MaxDelay = 5000;
- private IMapa Mapa;
- private ICrypto Crypter;
- private IGameLogic Logic;
- private IPicGenerator PicGenerator;
- private object Locker = new object();
- private CancellationTokenSource CancelTokenSource;
- private Dictionary<ClientIdentification, Player> Players;
- public GameMachine(ICrypto crypter, IMapa mapa, IPicGenerator picGenerator, IGameLogic gameLogic)
- {
- Players = new Dictionary<ClientIdentification, Player>();
- PicGenerator = picGenerator;
- Crypter = crypter;
- Logic = gameLogic;
- Mapa = mapa;
- }
- public event Action ClearEvent;
- public event Func<ClientIdentification, byte[], Task<bool>> SendInfoEvent;
- public event Func<ClientIdentification, Task<byte[]>> ReceiveInfoEvent;
- public async Task ConnectionClient(ClientIdentification cl, DateTime time)
- {
- byte[] boof = await ReceiveInfoEvent?.Invoke(cl);
- if (boof == null)
- return;
- try
- {
- string name = Crypter.DecodeStr(boof);
- lock (Locker)
- {
- Players.Add(cl, new Player()
- {
- Name = name,
- CurrentPosition = Mapa.StartPoint,
- PlayerCollor = ColorRGB.GetRandonColor(),
- LastPointer = PointerWNSE.Non
- });
- }
- }
- catch { }
- }
- #region !!!!
- public IMapa GetMapa() => Mapa;
- public Player[] GetPlayers()
- {
- lock (Locker)
- {
- return Players.Select(i => i.Value).ToArray();
- }
- }
- public void Restart()
- {
- CancelTokenSource?.Cancel(); // завершение метода start
- ClearEvent?.Invoke();
- }
- public async Task ShutdownClient(ClientIdentification cl, DateTime time)
- {
- lock (Locker)
- {
- Players.Remove(cl);
- }
- }
- #endregion
- public bool Start()
- {
- if (CancelTokenSource != null)
- return false;
- CancelTokenSource = new CancellationTokenSource();
- CancellationToken token = CancelTokenSource.Token;
- Task.Run(() =>
- {
- Stopwatch timer = new Stopwatch();
- for (; ; )
- {
- timer.Reset();
- timer.Start();
- lock (Locker)
- {
- Parallel.ForEach(Players, p =>
- {
- //try
- {
- if (p.Value.CurrentPosition == Mapa.FinishPoint)
- {
- p.Value.Win = true;
- p.Value.LastPointer = PointerWNSE.Non;
- return;
- }
- var validPointer = Logic.GetPointer(p.Value, Mapa);
- var pic = PicGenerator.GetPic(validPointer);
- var byteImg = Crypter.CodeImage(pic.Img);
- pic.Img.Dispose();
- var b = (bool)SendInfoEvent?.Invoke(p.Key, byteImg).Result;
- var byteResult = ReceiveInfoEvent?.Invoke(p.Key).Result;
- p.Value.SendingPic = pic.PicPath;
- if (!b || byteResult == null)
- {
- p.Value.LastPointer = PointerWNSE.Non;
- return;
- }
- var pointerResult = (PointerWNSE)Crypter.DecodeInt(byteResult);
- var pastP = p.Value.CurrentPosition;
- Logic.MovePlayer(pointerResult, p.Value, Mapa);
- }
- //catch (Exception e)
- //{
- //}
- });
- }
- #if memoptimize
- GC.Collect();
- #endif
- timer.Stop();
- int delay = timer.ElapsedMilliseconds > MaxDelay ? 0 : (int)(MaxDelay - timer.ElapsedMilliseconds);
- if (token.IsCancellationRequested)
- {
- CancelTokenSource.Dispose();
- CancelTokenSource = null;
- return;
- }
- Thread.Sleep(delay);
- }
- });
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement