Exemplo n.º 1
0
 public static void BroadCast(string m)
 {
     if (m == null)
     {
         return;
     }
     Client?.SendMessage(m).Wait();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Update. Determines what messages to send every some seconds
        /// 1. every one second will send train location
        /// 2. by default, every 10 seconds will send switch/signal status, this can be changed by in the menu of setting MPUpdateInterval
        /// 3. housekeeping (remove/add trains, remove players)
        /// 4. it will also capture key stroke of horn, panto, wiper, bell, headlight etc.
        /// </summary>
        public void Update(double newtime)
        {
            if (begineZeroTime == 0)
            {
                begineZeroTime = newtime - 10;
            }

            CheckPlayerTrainSpad();//over speed or pass a red light

            //server update train location of all
            if (IsDispatcher && newtime - lastMoveTime >= 1f)
            {
                MSGMove move = new MSGMove();
                if (Simulator.Instance.PlayerLocomotive.Train.TrainType != TrainType.Remote)
                {
                    move.AddNewItem(GetUserName(), Simulator.Instance.PlayerLocomotive.Train);
                }
                Notify(OnlineTrains.MoveTrains(move));
                MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust
                Train      t       = Simulator.Instance.PlayerLocomotive.Train;
                for (int iCar = 0; iCar < t.Cars.Count; iCar++)
                {
                    if (t.Cars[iCar] is MSTSDieselLocomotive)
                    {
                        exhaust.AddNewItem(GetUserName(), t, iCar);
                    }
                }
                // Broadcast also exhaust
                var exhaustMessage = OnlineTrains.ExhaustingLocos(exhaust);
                if (!string.IsNullOrEmpty(exhaustMessage))
                {
                    Notify(exhaustMessage);
                }

                lastMoveTime = lastSendTime = newtime;

#if INDIVIDUAL_CONTROL
                if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
                {
                    Server.BroadCast((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString());
                }
#endif
            }

            //server updates switch
            if (IsDispatcher && newtime - lastSwitchTime >= MPUpdateInterval)
            {
                lastSwitchTime = lastSendTime = newtime;
                var switchStatus = new MSGSwitchStatus();

                if (switchStatus.OKtoSend)
                {
                    BroadCast(switchStatus.ToString());
                }
                var signalStatus = new MSGSignalStatus();
                if (signalStatus.OKtoSend)
                {
                    BroadCast(signalStatus.ToString());
                }
            }

            //client updates itself
            if (Client != null && !IsDispatcher && newtime - lastMoveTime >= 1f)
            {
                Train      t       = Simulator.Instance.PlayerLocomotive.Train;
                MSGMove    move    = new MSGMove();
                MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust
                //if I am still controlling the train
                if (t.TrainType != TrainType.Remote)
                {
                    if (Math.Abs(t.SpeedMpS) > 0.001 || newtime - begineZeroTime < 5f || Math.Abs(t.LastReportedSpeed) > 0)
                    {
                        move.AddNewItem(GetUserName(), t);
                        for (int iCar = 0; iCar < t.Cars.Count; iCar++)
                        {
                            if (t.Cars[iCar] is MSTSDieselLocomotive)
                            {
                                exhaust.AddNewItem(GetUserName(), t, iCar);
                            }
                        }
                    }
                    else
                    {
                        if (Math.Abs(previousSpeed) > 0.001)
                        {
                            begineZeroTime = newtime;
                        }
                    }
                }
                //MoveUncoupledTrains(move); //if there are uncoupled trains
                //if there are messages to send
                if (move.OKtoSend())
                {
                    Client.SendMessage(move.ToString()).Wait();
                    if (exhaust.OKtoSend())
                    {
                        Client.SendMessage(exhaust.ToString()).Wait();
                    }
                    lastMoveTime = lastSendTime = newtime;
                }
                previousSpeed = t.SpeedMpS;

#if INDIVIDUAL_CONTROL
                if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
                {
                    Client.Send((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString());
                }
#endif
            }


            //need to send a keep-alive message if have not sent one to the server for the last 30 seconds
            if (Client != null && !IsDispatcher && newtime - lastSendTime >= 30f)
            {
                Notify((new MSGAlive(GetUserName())).ToString());
                lastSendTime = newtime;
            }

            //some players are removed
            //need to send a keep-alive message if have not sent one to the server for the last 30 seconds
            if (IsDispatcher && newtime - lastSyncTime >= 60f)
            {
                Notify((new MSGMessage("All", "TimeCheck", Simulator.Instance.ClockTime.ToString(System.Globalization.CultureInfo.InvariantCulture))).ToString());
                lastSyncTime = newtime;
            }
            RemovePlayer();

            //some players are disconnected more than 1 minute ago, will not care if they come back later
            CleanLostPlayers();

            //some trains are added/removed
            HandleTrainList();

            //some locos are added/removed
            if (IsDispatcher)
            {
                HandleLocoList();
            }

            AddPlayer(); //a new player joined? handle it

            /* will have this in the future so that helpers can also control
             *          //I am a helper, will see if I need to update throttle and dynamic brake
             *          if (Simulator.PlayerLocomotive.Train != null && Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
             *          {
             *
             *          }
             * */
        }