예제 #1
0
        public void GameDataEvent(GameEventType eventType, Object evt)
        {
            GameEventTrigger  newStatusEvent = new GameEventTrigger(eventType, evt);
            CancellationToken cToken         = GameDataWorkerCTS.Token;

            GameDataQueue.Add(newStatusEvent, cToken);
        }
예제 #2
0
        private void GameDataWorkerThread()
        {
            GameDataWorkerState = State.Started;
            Log.Instance.Info("Game Data Worker Thread started");

            DateTime lastSuccessfulUpdate = DateTime.Today;

            CancellationToken cToken = GameDataWorkerCTS.Token;

            while (cToken.IsCancellationRequested == false)
            {
                // Update Matric state
                if (matricapi.IsConnected())
                {
                    Log.Instance.Info("Updating Matric state");
                    matricapi.UpdateStatus(currentStatus);
                    matricapi.UpdateTarget(currentTarget);
                }

                while (!GameDataQueue.IsCompleted)
                {
                    GameEventTrigger gameEventTrigger = new GameEventTrigger(GameEventType.Empty, null);
                    try {
                        gameEventTrigger = GameDataQueue.Take(cToken);
                    } catch (InvalidOperationException) { }

                    if (gameEventTrigger.GameEvent != GameEventType.Empty)
                    {
                        Log.Instance.Info("Game data event received");

                        if (gameEventTrigger.GameEvent == GameEventType.ServerStop)
                        {
                            Log.Instance.Info("Server stop event received");
                            break;
                        }
                        else if (gameEventTrigger.GameEvent == GameEventType.Status)
                        {
                            currentStatus = new StatusData();
                            currentStatus = gameEventTrigger.EventData as StatusData;
                            string statusJSON = JsonSerializer.Serialize(currentStatus);
                            Log.Instance.Info("Current State: {gamestate}", statusJSON);
                            if (PanelServerStarted)
                            {
                                GameDataUpdateController.SendStatusUpdate(currentStatus);
                            }
                        }
                        else if (gameEventTrigger.GameEvent == GameEventType.Target)
                        {
                            currentTarget = new ShipTargetedData();
                            currentTarget = gameEventTrigger.EventData as ShipTargetedData;
                            Log.Instance.Info("Current Target: {target}", JsonSerializer.Serialize(currentTarget));
                            if (PanelServerStarted)
                            {
                                GameDataUpdateController.SendTargetUpdate(currentTarget);
                            }
                        }
                        // Update Matric state
                        if (matricapi.IsConnected())
                        {
                            Log.Instance.Info("Updating Matric state");
                            matricapi.UpdateStatus(currentStatus);
                            matricapi.UpdateTarget(currentTarget);
                        }
                    }
                    Log.Instance.Info("Game Data Worker Thread waiting for new work");
                }
            }
            Log.Instance.Info("Game Data Worker Thread ending");
        }