Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using HacLib.Interface;
  7. using HacLib.Interface.Dependent;
  8. using HacLib.Models;
  9. using System.Linq;
  10. using System.Diagnostics;
  11. using HacLib.Models.Sys;
  12. using HacLib.Enums;
  13.  
  14. namespace HacLib.Service
  15. {
  16.     public class GameMachine : INotifierCliD, IGame
  17.     {
  18.         private const int MaxDelay = 5000;
  19.  
  20.         private IMapa Mapa;
  21.         private ICrypto Crypter;
  22.         private IGameLogic Logic;
  23.         private IPicGenerator PicGenerator;
  24.         private object Locker = new object();
  25.         private CancellationTokenSource CancelTokenSource;
  26.         private Dictionary<ClientIdentification, Player> Players;
  27.  
  28.         public GameMachine(ICrypto crypter, IMapa mapa, IPicGenerator picGenerator, IGameLogic gameLogic)
  29.         {
  30.             Players = new Dictionary<ClientIdentification, Player>();
  31.             PicGenerator = picGenerator;
  32.             Crypter = crypter;
  33.             Logic = gameLogic;
  34.             Mapa = mapa;
  35.         }
  36.  
  37.         public event Action ClearEvent;
  38.         public event Func<ClientIdentification, byte[], Task<bool>> SendInfoEvent;
  39.         public event Func<ClientIdentification, Task<byte[]>> ReceiveInfoEvent;
  40.  
  41.         public async Task ConnectionClient(ClientIdentification cl, DateTime time)
  42.         {
  43.             byte[] boof = await ReceiveInfoEvent?.Invoke(cl);
  44.             if (boof == null)
  45.                 return;
  46.             try
  47.             {
  48.                 string name = Crypter.DecodeStr(boof);
  49.  
  50.                 lock (Locker)
  51.                 {
  52.                     Players.Add(cl, new Player()
  53.                     {
  54.                         Name = name,
  55.                         CurrentPosition = Mapa.StartPoint,
  56.                         PlayerCollor = ColorRGB.GetRandonColor(),
  57.                         LastPointer = PointerWNSE.Non
  58.                     });
  59.                 }
  60.             }
  61.             catch { }
  62.         }
  63.  
  64.         #region !!!!
  65.         public IMapa GetMapa() => Mapa;
  66.  
  67.         public Player[] GetPlayers()
  68.         {
  69.             lock (Locker)
  70.             {
  71.                 return Players.Select(i => i.Value).ToArray();
  72.             }
  73.         }
  74.  
  75.         public void Restart()
  76.         {
  77.             CancelTokenSource?.Cancel(); // завершение метода start
  78.             ClearEvent?.Invoke();
  79.         }
  80.  
  81.         public async Task ShutdownClient(ClientIdentification cl, DateTime time)
  82.         {
  83.             lock (Locker)
  84.             {
  85.                 Players.Remove(cl);            
  86.             }
  87.         }
  88.         #endregion
  89.  
  90.         public bool Start()
  91.         {
  92.             if (CancelTokenSource != null)
  93.                 return false;
  94.  
  95.             CancelTokenSource = new CancellationTokenSource();
  96.             CancellationToken token = CancelTokenSource.Token;
  97.  
  98.             Task.Run(() =>
  99.             {
  100.                 Stopwatch timer = new Stopwatch();
  101.                 for (; ; )
  102.                 {
  103.                     timer.Reset();
  104.                     timer.Start();
  105.  
  106.                     lock (Locker)
  107.                     {
  108.                         Parallel.ForEach(Players, p =>
  109.                         {
  110.                             //try
  111.                             {
  112.                                 if (p.Value.CurrentPosition == Mapa.FinishPoint)
  113.                                 {
  114.                                     p.Value.Win = true;
  115.                                     p.Value.LastPointer = PointerWNSE.Non;
  116.                                     return;
  117.                                 }
  118.  
  119.                                 var validPointer = Logic.GetPointer(p.Value, Mapa);
  120.                                 var pic = PicGenerator.GetPic(validPointer);
  121.                                 var byteImg = Crypter.CodeImage(pic.Img);
  122.                                 pic.Img.Dispose();
  123.  
  124.                                 var b = (bool)SendInfoEvent?.Invoke(p.Key, byteImg).Result;
  125.                                 var byteResult = ReceiveInfoEvent?.Invoke(p.Key).Result;
  126.  
  127.                                 p.Value.SendingPic = pic.PicPath;
  128.  
  129.                                 if (!b || byteResult == null)
  130.                                 {
  131.                                     p.Value.LastPointer = PointerWNSE.Non;
  132.                                     return;
  133.                                 }
  134.  
  135.                                 var pointerResult = (PointerWNSE)Crypter.DecodeInt(byteResult);
  136.                                 var pastP = p.Value.CurrentPosition;
  137.                                 Logic.MovePlayer(pointerResult, p.Value, Mapa);
  138.                             }
  139.                             //catch (Exception e)
  140.                             //{                                
  141.  
  142.                             //}
  143.                         });
  144.                     }
  145.  
  146. #if memoptimize
  147.                     GC.Collect();
  148. #endif
  149.  
  150.                     timer.Stop();
  151.                     int delay = timer.ElapsedMilliseconds > MaxDelay ? 0 : (int)(MaxDelay - timer.ElapsedMilliseconds);
  152.  
  153.                     if (token.IsCancellationRequested)
  154.                     {
  155.                         CancelTokenSource.Dispose();
  156.                         CancelTokenSource = null;
  157.                         return;
  158.                     }                    
  159.  
  160.                     Thread.Sleep(delay);
  161.                 }
  162.             });
  163.  
  164.             return true;
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement