Beispiel #1
0
        public static async Task StopGame()
        {
            try
            {
                Game CurrentGame = await GameRepository.GetCurrentGameAsync();

                if (CurrentGame == null)
                {
                    // Er is geen game bezig
                    throw new ApplicationException("NO_GAME");
                }
                else
                {
                    // Er is al een game bezig, stuur stop methode
                    await IoTHubHelper.StopGameMethod();

                    // Send game_stop w/ no data to frontend
                    MqttMessage message = new MqttMessage("game_stop", "");

                    // Serialize
                    string mqttBody = JsonConvert.SerializeObject(message);

                    // Send to topic /neocage
                    MqttHelper.SendMessage("/neocage", mqttBody);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        public static async Task <Game> StartGame(Gamemode gamemode)
        {
            try
            {
                // Haal de huidige game op
                Game CurrentGame = await GameRepository.GetCurrentGameAsync();

                // Is er al een game?
                if (CurrentGame == null)
                {
                    DateTime timeStarted = DateTime.Now;
                    timeStarted = timeStarted.AddSeconds(10);

                    // Er is nog geen game bezig, game aanmaken
                    CurrentGame = new Game()
                    {
                        Id          = Guid.NewGuid(),
                        GamemodeId  = gamemode.Id,
                        Gamemode    = gamemode.Name,
                        TimeStarted = timeStarted,
                        Duration    = gamemode.Duration,
                        Score       = 0
                    };

                    // Game details naar device sturen in start methode
                    await IoTHubHelper.StartGameMethod(CurrentGame);

                    // Game started sturen over MQTT
                    // type "game_start", payload is the game info
                    string      gamePayload = JsonConvert.SerializeObject(CurrentGame);
                    MqttMessage message     = new MqttMessage("game_start", gamePayload);

                    // Serialize
                    string mqttBody = JsonConvert.SerializeObject(message);

                    // Send to topic /neocage
                    MqttHelper.SendMessage("/neocage", mqttBody);

                    return(CurrentGame);
                }
                else
                {
                    // Er is al een game bezig
                    throw new ApplicationException("GAME_BUSY");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #3
0
        public static void GameUpdated(Game game)
        {
            try
            {
                // Received a game update, send it to the end user

                // type "game_update", payload is the game info
                string      gamePayload = JsonConvert.SerializeObject(game);
                MqttMessage message     = new MqttMessage("game_update", gamePayload);

                // Serialize
                string mqttBody = JsonConvert.SerializeObject(message);

                // Send to topic /neocage
                MqttHelper.SendMessage("/neocage", mqttBody);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #4
0
        public static async Task GameEnded(Game game)
        {
            try
            {
                // Received a game update, the game has ended, send it to the end user

                // type "game_end", payload is the game info
                string      gamePayload = JsonConvert.SerializeObject(game);
                MqttMessage message     = new MqttMessage("game_end", gamePayload);

                // Serialize
                string mqttBody = JsonConvert.SerializeObject(message);

                // Send to topic /neocage
                MqttHelper.SendMessage("/neocage", mqttBody);

                // Save game to table storage
                await GameRepository.SaveGameAsync(game);
            }
            catch (Exception ex) {
                throw ex;
            }
        }