예제 #1
0
 private void Reset(int ServerName)
 {
     SOL           = new List <ScreenObject>();
     IsTransfering = false;
     IsEntering    = false;
     SystemStatus  = BallStatus.DoesntHaveBall;
     Status        = BallStatus.DoesntHaveBall;
     if (Name == ServerName)
     {
         Server            = new SystemServer(2020 + Client.Port);
         Server.OnSetBool += (b) =>
         {
             if (b)
             {
                 SystemStatus = BallStatus.HasBall;
             }
         };
         //Server.OnSetPauseResume += (b) =>
         //{
         //    if (b) Pause.Set();
         //    else Pause.Reset();
         //    MainWindow.pause = b;
         //    OnSetPuaseResum?.Invoke(b);
         //};
         THis = new ScreenCondition
         {
             address  = null,
             Row      = 0,
             Col      = 0,
             HasBall  = false,
             IsServer = true,
             Top      = WallStatus.AcceleratorHard,
             Buttom   = WallStatus.AcceleratorHard,
             Left     = WallStatus.DeceleratorHard,
             Right    = WallStatus.DeceleratorHard,
         };
         OnSetWindowLocation?.Invoke(0, 0);
     }
     //else
     //  {
     Client = new TCPClientt(2020);
     Client.Connect();
     Client.OnMessageReciveClient += (mess) =>
     {
         if (Name != ServerName)
         {
             if (mess.Contains("relocate"))
             {
                 THis = new ScreenCondition(mess, out BallStatus bs);
                 Actor.SystemStatus = bs;
                 Mess = mess;
                 OnSetWindowLocation?.Invoke(THis.Row, THis.Col);
             }
             if (mess.Equals("ballInitiated"))
             {
                 SystemStatus = BallStatus.HasBall;
             }
         }
         if (mess.Contains("ServerClosed"))
         {
             Client.Disconnect();
             Thread.Sleep(20);
             if (mess.Contains(Name.ToString()))
             {
                 this.Reset(Name);
                 Server.Server.Start();
                 THis.IsServer = true;
                 Client.Reconnect(2020);
             }
             else
             {
                 Thread.Sleep(15);
                 Client.Reconnect(2020);
                 Client.Send(Name + ";IsConnected");
             }
         }
         if (mess.Equals("Pause"))
         {
             Pause.Reset();
             MainWindow.pause = false;
             OnSetPuaseResum?.Invoke(false);
         }
         if (mess.Equals("Resume"))
         {
             Pause.Set();
             MainWindow.pause = true;
             OnSetPuaseResum?.Invoke(true);
         }
         if (mess.Contains("BallReport"))
         {
             Console.WriteLine(mess + "   asClient");
             b             = new Ball(mess, Name);
             THis.HasBall  = true;
             Status        = BallStatus.HasBall;
             ActionFlag    = true;
             IsTransfering = true;
             //if (mess.Contains("ButtomRight"))
             CornerCross = false;
             Pause.Set();
             Action();
         }
         if (mess.Contains("relocate"))
         {
             THis.Update(mess);
             OnSetWindowLocation?.Invoke(THis.Row, THis.Col);
             //new ScreenCondition(mess,out BallStatus bs)
         }
     };
     if (Name != ServerName)
     {
         Client.Send(Name + ";IsConnected");
     }
     // }
     ActionFlag = false;
     Pause      = new ManualResetEvent(false);
 }
예제 #2
0
        public SystemServer(int port)
        {
            Server = new TCPServer(port);
            Server.Start();
            Server.OnMessageRecive += (sender, mess) =>
            {
                Console.WriteLine(mess);
                if (mess.Contains("IsConnected"))
                {
                    Add((sender as Socket).RemoteEndPoint as IPEndPoint);
                    foreach (var item in MatrixList.Values)
                    {
                        if (!item.IsServer)
                        {
                            Server.Send(item.address, item.ToString(Actor.SystemStatus));
                        }
                    }
                }
                if (mess.Contains("ClientRemoved"))
                {
                    var splitedmess = mess.Split(';');
                    Remove((sender as Socket).RemoteEndPoint as IPEndPoint);
                    foreach (var item in MatrixList.Values)
                    {
                        Console.WriteLine(item.ToString());
                        if (!item.IsServer)
                        {
                            Server.Send(item.address, item.ToString(Actor.SystemStatus));
                        }
                    }
                }
                if (mess.Contains("ballInitiated"))
                {
                    OnSetBool?.Invoke(true);
                    Server.SendAll("ballInitiated");
                }
                if (mess.Equals("Pause"))
                {
                    OnSetPauseResume?.Invoke(false);
                    Server.SendAll("Pause");
                }
                if (mess.Equals("Resume"))
                {
                    OnSetPauseResume?.Invoke(true);
                    Server.SendAll("Resume");
                }
                if (mess.Contains("BallReport"))
                {
                    Ball b = new Ball(mess, 0, out int coll, out int roww, out BallCrossingStatus bcs);
                    switch (bcs)
                    {
                    case BallCrossingStatus.Top:
                        coll--;
                        break;

                    case BallCrossingStatus.Left:
                        roww--;
                        break;

                    case BallCrossingStatus.Right:
                        roww++;
                        break;

                    case BallCrossingStatus.bottom:
                        coll++;
                        break;

                    case BallCrossingStatus.TopRight:
                        coll--;
                        roww++;
                        break;

                    case BallCrossingStatus.TopLeft:
                        coll--;
                        roww--;
                        break;

                    case BallCrossingStatus.ButtomRight:
                        coll++;
                        roww++;
                        break;

                    case BallCrossingStatus.ButtomLeft:
                        coll++;
                        roww--;
                        break;

                    default:
                        break;
                    }
                    ScreenCondition sc = MatrixList.Values.Where(x => x.Col == coll && x.Row == roww).First();
                    if (sc.IsServer)
                    {
                        Server.Send(Server.First, b.ToString(coll, roww, bcs));
                    }
                    else
                    {
                        Server.Send(sc.address, b.ToString(coll, roww, bcs));
                    }
                }
            };
            rnd           = new Random();
            Count         = 1;
            Matrixsize    = (int)Math.Ceiling(Math.Sqrt(Count));
            MatrixList    = new Dictionary <IPEndPoint, ScreenCondition>();
            ServerAddress = new IPEndPoint(IPAddress.Any, 1010);
            MatrixList.Add(ServerAddress, new ScreenCondition
            {
                HasBall  = false,
                Name     = 0,
                IsServer = true,
                Top      = WallStatus.AcceleratorHard,
                Right    = WallStatus.DeceleratorHard,
                Buttom   = WallStatus.AcceleratorHard,
                Left     = WallStatus.DeceleratorHard,
                Col      = 0,
                Row      = 0
            });
        }