// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { context.Response.StatusCode = 500; context.Response.ContentType = "application/json; charset=utf-8"; var exception = context.Features.Get <IExceptionHandlerPathFeature>().Error; var newError = new ApiErrorDetail(exception); if (ApiErrors.Count > 50) { Startup.ApiErrors.Remove(Startup.ApiErrors.LastOrDefault()); } ApiErrors.Insert(0, newError); CloudScriptResponse statusResponse = new CloudScriptResponse() { ResponseCode = ResponseCode.Error, Method = context.Request.Path, ErrorMessage = exception.Message }; statusResponse.Params.Add("InstanceId", Startup.InstanceId); string output = JsonConvert.SerializeObject(statusResponse); await context.Response.WriteAsync(output); }); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public override async Task <CloudScriptResponse> ExecuteAsync(CloudScriptRequest request, string[] urlArguments) { DateTime benginRequest = DateTime.UtcNow; if (IsClientValidRequest(request)) { var cloudData = new PlayFabFileDetails(MetaSettings.MetaDataStateFileName); if (await PlayFabApiHandler.GetPlayerTitleData(request.UserId, new List <PlayFabFileDetails>() { cloudData })) { MetaDataState metaDataState = MetaDataState.FromJson <MetaDataState>(cloudData.DataAsString); cloudData.DataAsString = metaDataState.ToJson(); if (await PlayFabApiHandler.UploadPlayerTitleData(request.UserId, new List <PlayFabFileDetails>() { cloudData })) { var response = new CloudScriptResponse() { ResponseCode = ResponseCode.Success }; return(response); } } } return(new CloudScriptResponse() { ResponseCode = ResponseCode.Error }); }
public override async Task <CloudScriptResponse> ExecuteAsync(CloudScriptRequest request, string[] urlArguments) { DateTime benginRequest = DateTime.UtcNow; if (IsClientValidRequest(request)) { var cloudData = new PlayFabFileDetails(MetaSettings.MetaDataStateFileName); if (await PlayFabApiHandler.GetPlayerTitleData(request.UserId, new List <PlayFabFileDetails>() { cloudData })) { MetaDataState metaDataState = null; //if file does not exist yet, create default for content, otherwise perfom Login Activies. if (cloudData.ExistOnServer) { metaDataState = MetaDataState.FromJson <MetaDataState>(cloudData.DataAsString); } else { metaDataState = new MetaDataState(); metaDataState.Consumables.AddConsumable(Consumable.GetByName <Consumable>(MetaSettings.HardCurrencyId), 100); metaDataState.Consumables.AddConsumable(Consumable.GetByName <Consumable>(MetaSettings.SoftCurrencyId), 3000); metaDataState.Consumables.AddConsumable(Consumable.GetByName <Consumable>(MetaSettings.EnergyId), 72); metaDataState.CreationDate = DateTime.UtcNow; var playerProfile = await PlayFabApiHandler.GetPlayerProfileInfo(request.UserId); if (playerProfile != null && playerProfile.Locations != null && playerProfile.Locations.LastOrDefault() != null) { switch (playerProfile.Locations.LastOrDefault().ContinentCode) { case PlayFab.ServerModels.ContinentCode.AF: case PlayFab.ServerModels.ContinentCode.AN: case PlayFab.ServerModels.ContinentCode.EU: metaDataState.MetaTimeZone = MetaTimeZone.EU; break; case PlayFab.ServerModels.ContinentCode.NA: case PlayFab.ServerModels.ContinentCode.SA: metaDataState.MetaTimeZone = MetaTimeZone.NA; break; case PlayFab.ServerModels.ContinentCode.AS: case PlayFab.ServerModels.ContinentCode.OC: metaDataState.MetaTimeZone = MetaTimeZone.ASIA; break; } metaDataState.CountryCode = playerProfile.Locations.LastOrDefault().CountryCode.ToString(); } metaDataState.ApplyDailyReset(); } if (metaDataState.NextDailyReset == DateTime.MinValue) { metaDataState.NextDailyReset = MetaSettings.GetNextDailyReset(metaDataState.MetaTimeZone, DateTime.UtcNow); } metaDataState.SyncLoginCalendar(); metaDataState.ServerDateTime = DateTime.UtcNow; PlayerStatus.AddEnergyToPlayer(metaDataState); if (DateTime.UtcNow > metaDataState.NextDailyReset) { metaDataState.ApplyDailyReset(); } if (request.CloudScriptMethod.Params.ContainsKey("DisplayName")) { metaDataState.ProfileName = request.CloudScriptMethod.Params["DisplayName"].ToString(); } cloudData.DataAsString = metaDataState.ToJson(); if (metaDataState.CountryCode == "CN") { return(new CloudScriptResponse() { ResponseCode = ResponseCode.Error }); } if (await PlayFabApiHandler.UploadPlayerTitleData(request.UserId, new List <PlayFabFileDetails>() { cloudData })) { var response = new CloudScriptResponse() { ResponseCode = ResponseCode.Success }; return(response); } } } return(new CloudScriptResponse() { ResponseCode = ResponseCode.Error }); }
public async Task <string> Get(string method, string param1, string param2, string param3, string param4) { //await PlayFabApiHandler.ValidateEntityToken(); //Response.ContentType = "application/json; charset=utf-8"; string result = string.Empty; bool isStackCall = param1 != null && param1.ToLower() == "stack"; switch (method) { case "": case "Status": CloudScriptResponse statusResponse = new CloudScriptResponse() { ResponseCode = ResponseCode.Success, Method = method }; statusResponse.Params.Add("DataVersion", DataLayer.Instance.GetTable <DataVersion>().First().Version.ToString()); statusResponse.Params.Add("Environment", PlayFabSettings.PlayFabEnvironment); statusResponse.Params.Add("UptimeMinutes", Convert.ToUInt32((DateTime.UtcNow - Startup.UpTimeStart).TotalMinutes).ToString()); statusResponse.Params.Add("InstanceId", Startup.InstanceId); return(JsonConvert.SerializeObject(statusResponse)); case "ResetPlayerData": CloudScriptResponse resetDataResponse; //TODO: Implement ResetPlayerData resetDataResponse = new CloudScriptResponse() { ResponseCode = ResponseCode.Error, ErrorMessage = string.Format("Reset data for user {0} failed.", param1), Method = method }; result = JsonConvert.SerializeObject(resetDataResponse); break; case "AppVersion": result = Startup.AppVersion; break; case "Debug": result = JsonConvert.SerializeObject(Startup.ApiErrors); break; default: CloudScriptResponse response = null; if (ApiController.ApiMethods.ContainsKey(method)) { var apiMethod = ApiController.ApiMethods[method]; string jsonRequest; using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) { jsonRequest = await reader.ReadToEndAsync(); } if (isStackCall) { CloudScriptRequestStack requests = JsonConvert.DeserializeObject <CloudScriptRequestStack>(jsonRequest); response = await apiMethod.ExecuteStackAsync(requests); } else { CloudScriptRequest request = JsonConvert.DeserializeObject <CloudScriptRequest>(jsonRequest); response = await apiMethod.ExecuteAsync(request, new string[] { param1, param2, param3, param4 }); } } else { response = new CloudScriptResponse() { ResponseCode = ResponseCode.ProtocolError, ErrorMessage = "Invalid method.", Method = method }; } result = JsonConvert.SerializeObject(response); break; } return(result); }