예제 #1
0
        public static async Task <int> GetGamesPlayed(string playFabId, PlayFabApiSettings apiSettings, PlayFabAuthenticationContext authenticationContext)
        {
            var request = new GetUserDataRequest
            {
                PlayFabId = playFabId,
                Keys      = new List <string> {
                    Constants.GAMES_PLAYED_KEY
                }
            };

            var serverApi = new PlayFabServerInstanceAPI(apiSettings, authenticationContext);

            var result = await serverApi.GetUserDataAsync(request);

            if (result.Error != null)
            {
                throw new Exception($"An error occured while fetching the number of games played: Error: {result.Error.GenerateErrorReport()}");
            }

            var resultData = result.Result.Data;

            if (resultData.Count > 0 && resultData.TryGetValue(Constants.GAMES_PLAYED_KEY, out var gamesPlayedRecord))
            {
                return(int.Parse(gamesPlayedRecord.Value));
            }

            // Set the number of games played to be 0 since the record doesn't exist
            await SetGamesPlayed(0, playFabId, apiSettings, authenticationContext);

            return(0);
        }
    // ユーザーデータを取得する
    public static async Task <UserDataInfo> GetUserDataAsync(FunctionExecutionContext <dynamic> context)
    {
        var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);

        var result = await serverApi.GetUserDataAsync(new GetUserDataRequest()
        {
            PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
        });

        // ユーザーデータのパラム名とそのデータのJsonの辞書にしてユーザーデータを取得
        var userData = UserDataUtil.GetUserData(result.Result.Data);

        return(userData);
    }
예제 #3
0
        public static async Task <TicTacToeState> GetCurrentGameState(string playFabId, PlayFabApiSettings apiSettings, PlayFabAuthenticationContext authenticationContext)
        {
            var request = new GetUserDataRequest()
            {
                PlayFabId = playFabId,
                Keys      = new List <string>()
                {
                    Constants.GAME_CURRENT_STATE_KEY
                }
            };

            var serverApi = new PlayFabServerInstanceAPI(apiSettings, authenticationContext);

            var result = await serverApi.GetUserDataAsync(request);

            if (result.Error != null)
            {
                throw new Exception($"An error occurred while fetching the current game state: Error: {result.Error.GenerateErrorReport()}");
            }

            var resultData = result.Result.Data;

            // Current state found
            if (resultData.Count > 0 && resultData.TryGetValue(Constants.GAME_CURRENT_STATE_KEY, out var currentGameStateRecord))
            {
                return(PlayFabSimpleJson.DeserializeObject <TicTacToeState>(currentGameStateRecord.Value));
            }
            // Current game record does not exist and so must be created
            else
            {
                var newState = new TicTacToeState()
                {
                    Data = new int[9]
                };
                await UpdateCurrentGameState(newState, playFabId, apiSettings, authenticationContext);

                return(newState);
            }
        }