Ejemplo n.º 1
0
        public static async Task <dynamic> UnlockHighSkillContent(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            /* Create the function execution's context through the request */
            var context = await FunctionPlayerPlayStreamContext <dynamic> .Create(req);

            var args = context.FunctionArgument;

            var playerStatUpdatedEvent = PlayFabSimpleJson.DeserializeObject <dynamic>(context.PlayStreamEventEnvelope.EventData);

            var request = new UpdateUserInternalDataRequest
            {
                PlayFabId = context.CurrentPlayerId,
                Data      = new Dictionary <string, string>
                {
                    { "HighSkillContent", "true" },
                    { "XPAtHighSkillUnlock", playerStatUpdatedEvent["StatisticValue"].ToString() }
                }
            };

            /* Use the ApiSettings and AuthenticationContext provided to the function as context for making API calls. */
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);

            /* Execute the Server API request */
            var updateUserDataResponse = await serverApi.UpdateUserInternalDataAsync(request);

            log.LogInformation($"Unlocked HighSkillContent for {context.PlayerProfile.DisplayName}");

            return(new
            {
                profile = context.PlayerProfile
            });
        }
Ejemplo n.º 2
0
        public static async Task <dynamic> LevelCompleted(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            /* Create the function execution's context through the request */
            var context = await FunctionContext <dynamic> .Create(req);

            var args = context.FunctionArgument;

            var level          = args["levelName"];
            var monstersKilled = (int)args["monstersKilled"];

            var updateUserInternalDataRequest = new UpdateUserInternalDataRequest
            {
                PlayFabId = context.CurrentPlayerId,
                Data      = new Dictionary <string, string>
                {
                    { "lastLevelCompleted", level }
                }
            };

            /* Use the ApiSettings and AuthenticationContext provided to the function as context for making API calls. */
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);
            /* Execute the Server API request */
            var updateUserDataResult = await serverApi.UpdateUserInternalDataAsync(updateUserInternalDataRequest);

            log.LogDebug($"Set lastLevelCompleted for player {context.CurrentPlayerId} to {level}");

            var updateStatRequest = new UpdatePlayerStatisticsRequest
            {
                PlayFabId  = context.CurrentPlayerId,
                Statistics = new List <StatisticUpdate>
                {
                    new StatisticUpdate
                    {
                        StatisticName = "level_monster_kills",
                        Value         = monstersKilled
                    }
                }
            };

            /* Execute the server API request */
            var updateStatResult = await serverApi.UpdatePlayerStatisticsAsync(updateStatRequest);

            log.LogDebug($"Updated level_monster_kills stat for player {context.CurrentPlayerId} to {monstersKilled}");

            return(new
            {
                updateStatResult.Result
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This is a helper function that verifies that the player's move wasn't made
        /// too quickly following their previous move, according to the rules of the game.
        /// If the move is valid, then it updates the player's statistics and profile data.
        /// This function is called from the "UpdatePlayerMove" handler above and also is
        /// triggered by the "RoomEventRaised" Photon room event in the Webhook handler
        /// below.
        ///
        /// For this example, the script defines the cooldown period (playerMoveCooldownInSeconds)
        /// as 15 seconds.A recommended approach for values like this would be to create them in Title
        /// Data, so that they can be queries in the script with a call to GetTitleData
        /// (https://api.playfab.com/Documentation/Server/method/GetTitleData). This would allow you to
        /// make adjustments to these values over time, without having to edit, test, and roll out an
        /// updated script.
        /// </summary>
        /// <param name="playerMove">The player's move object</param>
        /// <param name="currentPlayerId">The player's PlayFab ID</param>
        /// <param name="log">The logger object to log to</param>
        /// <returns>True if the player's move was valid, false otherwise</returns>
        private static async Task <bool> ProcessPlayerMove(PlayFabServerInstanceAPI serverApi, dynamic playerMove, string currentPlayerId, ILogger log)
        {
            var now = DateTime.Now;
            var playerMoveCooldownInSeconds = -15;

            var userInternalDataRequest = new GetUserDataRequest
            {
                PlayFabId = currentPlayerId,
                Keys      = new List <string>
                {
                    "last_move_timestamp"
                }
            };

            var playerDataResponse = await serverApi.GetUserInternalDataAsync(userInternalDataRequest);

            var playerData = playerDataResponse.Result.Data;
            var lastMoveTimeStampSetting = playerData["last_move_timestamp"];

            if (lastMoveTimeStampSetting != null)
            {
                var lastMoveTime = DateTime.Parse(lastMoveTimeStampSetting.Value);
                var timeSinceLastMoveInSeconds = (now - lastMoveTime) / 1000;
                log.LogDebug($"lastMoveTime: {lastMoveTime} now: {now} timeSinceLastMoveInSeconds: {timeSinceLastMoveInSeconds}");

                if (timeSinceLastMoveInSeconds.TotalSeconds < playerMoveCooldownInSeconds)
                {
                    log.LogError($"Invalid move - time since last move: {timeSinceLastMoveInSeconds}s less than minimum of {playerMoveCooldownInSeconds}s.");
                    return(false);
                }
            }

            var getStatsRequest = new GetPlayerStatisticsRequest
            {
                PlayFabId = currentPlayerId
            };

            var playerStats = (await serverApi.GetPlayerStatisticsAsync(getStatsRequest)).Result.Statistics;
            var movesMade   = 0;

            for (var i = 0; i < playerStats.Count; i++)
            {
                if (string.IsNullOrEmpty(playerStats[i].StatisticName))
                {
                    movesMade = playerStats[i].Value;
                }
            }
            movesMade += 1;
            var updateStatsRequest = new UpdatePlayerStatisticsRequest
            {
                PlayFabId  = currentPlayerId,
                Statistics = new List <StatisticUpdate>
                {
                    new StatisticUpdate
                    {
                        StatisticName = "movesMade",
                        Value         = movesMade
                    }
                }
            };

            await serverApi.UpdatePlayerStatisticsAsync(updateStatsRequest);

            await serverApi.UpdateUserInternalDataAsync(new UpdateUserInternalDataRequest
            {
                PlayFabId = currentPlayerId,
                Data      = new Dictionary <string, string>
                {
                    { "last_move_timestamp", DateTime.Now.ToUniversalTime().ToString() },
                    { "last_move", PlayFabSimpleJson.SerializeObject(playerMove) }
                }
            });

            return(true);
        }