Пример #1
0
        private static async Task <string> SavePlayerRoleAsync(PlayerRole playerRole, ILogger log, ExecutionContext context)
        {
            CloudStorageAccount storageAccount1 = GetCloudStorageAccount(log, context);
            var tableClient1 = storageAccount1.CreateCloudTableClient();
            var table1       = tableClient1.GetTableReference(PLAYER_ROLES_TABLE_NAME);
            await table1.CreateIfNotExistsAsync();

            TableResult tr = await table1.ExecuteAsync(TableOperation.InsertOrReplace(playerRole));

            return(playerRole.GameId);
        }
Пример #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{game_id?}/{action1?}")] HttpRequest req,
            string game_id,
            string action1,
            ILogger log, ExecutionContext context)
        {
            try
            {
                string GameHostName = "DefaultHost";

                if (req.Method == "GET")
                {
                    if (game_id == null || game_id == "")
                    {
                        string filePath     = chapterHtml[0];
                        string fileMimeType = "text/html";

                        string htmlFilePath = Path.Combine(context.FunctionAppDirectory, filePath);
                        var    content1     = File.ReadAllText(htmlFilePath);

                        var cr = new ContentResult()
                        {
                            Content     = content1,
                            ContentType = fileMimeType,
                        };

                        return(cr);
                    }

                    string action = req.Query["action"];
                    if (action == "GET_POSITION")
                    {
                        string playerName = req.Cookies["PlayerName"];

                        var gameState = await LoadGameStateAsync(game_id, log, context);

                        JObject jsonObj = JObject.FromObject(gameState);
                        jsonObj.Add("PlayerName", playerName);

                        var playerRoles = await LoadPlayerRolesAsync(game_id, log, context);

                        jsonObj.Add("RolesDistribution", JArray.FromObject(playerRoles));

                        string content1 = JsonConvert.SerializeObject(jsonObj);
                        var    cr1      = new ContentResult()
                        {
                            Content     = content1,
                            ContentType = "application/json",
                        };

                        return(cr1);
                    }
                    else
                    {
                        string filePath     = "";
                        string fileMimeType = "";

                        if (files.Contains(game_id))
                        {
                            filePath     = game_id;
                            fileMimeType = "text/css";
                        }
                        else
                        {
                            var gameState = await LoadGameStateAsync(game_id, log, context);

                            filePath     = chapterHtml[gameState.GameChapter];
                            fileMimeType = "text/html";
                        }

                        string htmlFilePath = Path.Combine(context.FunctionAppDirectory, filePath);
                        var    content1     = File.ReadAllText(htmlFilePath);

                        var cr = new ContentResult()
                        {
                            Content     = content1,
                            ContentType = fileMimeType,
                        };

                        return(cr);
                    }
                }
                else if (req.Method == "POST")
                {
                    GameState gameState = null;
                    bool      logon     = false;
                    if (game_id == null || game_id == "")
                    {
                        logon = true;
                        if (req.Form.ContainsKey("game_id") && req.Form["game_id"] != "")
                        {
                            game_id = req.Form["game_id"]; // joined the game
                        }
                        else
                        {
                            game_id = GenerateId(); // game creation
                        }
                    }

                    gameState = (await LoadGameStateAsync(game_id, log, context)) ?? new GameState(game_id, GameHostName);
                    if (req.Form.ContainsKey("chapter"))
                    {
                        gameState.GameChapter = Int32.Parse(req.Form["chapter"]);
                    }

                    string playerName = req.Cookies["PlayerName"];
                    if (playerName == "" || playerName == null)
                    {
                        if (req.Form.ContainsKey("player_name"))
                        {
                            playerName = req.Form["player_name"];
                            SetCookie(req, "PlayerName", playerName, DateTimeOffset.Now.AddDays(1));
                        }
                        else
                        {
                            throw new UnauthorizedAccessException("Player name must be specified");
                        }
                    }

                    if (gameState.GameChapter == 1)
                    { // player chosen a role
                        // TODO: need to distingusih READY and ROLE_SELECT - in particular with regards the result
                        string role = "";

                        if (req.Form.ContainsKey("role"))
                        {
                            role = req.Form["role"];
                        }

                        var playerRole = new PlayerRole(game_id, playerName, role);
                        await SavePlayerRoleAsync(playerRole, log, context);
                    }
                    else if (gameState.GameChapter == 2)
                    { // progress in chapter changed OR chapter changed
                        if (req.Form.ContainsKey("position"))
                        {
                            gameState.Position = Int32.Parse(req.Form["position"]);
                        }
                    }

                    await SaveGameStateAsync(gameState, log, context); // not always make sense - so far on game create and position update

                    // create or join
                    if (logon)
                    {
                        RedirectResult rr = new RedirectResult(URL + game_id);
                        return(rr);
                    }
                    else
                    {
                        return(new OkObjectResult("OK"));
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
                return(new OkObjectResult(e.Message));
            }
        }