public static async Task <string> WorldPublishRequest(string worldId) { // Make sure the world is owned, i.e. it has the "__#" format. if (worldId != "__WORLD") { UIHandler.AddNotification(UIAlertType.Error, "Invalid World", "You can only publish your own world. Access it in the Main Menu.", 300); return("fail"); } // Make sure we have the world loaded: if (Systems.handler.worldContent == null || Systems.handler.worldContent.worldId != worldId) { UIHandler.AddNotification(UIAlertType.Error, "Invalid World", "The world did not have a valid ID, or was not loaded correctly.", 300); return("fail"); } UIHandler.AddNotification(UIAlertType.Warning, "Publishing World", "Please wait while we attempt to publish your world...", 180); // All checks have passed. Attempt to publish the world. try { // Attach Login Cookies WebHandler.AttachLoginCookiesToRequest(GameValues.CreoAPI); // Run the World Post StringContent content = new StringContent(JsonConvert.SerializeObject(Systems.handler.worldContent.data), Encoding.UTF8, "application/json"); var response = await Systems.httpClient.PostAsync(GameValues.CreoAPI + "world/" + worldId, content); string responseStr = await response.Content.ReadAsStringAsync(); WorldAPIFormat json = JsonConvert.DeserializeObject <WorldAPIFormat>(responseStr); if (json.success == false) { if (json.reason is string) { UIHandler.AddNotification(UIAlertType.Error, "Unable to Publish", json.reason, 300); } return("fail"); } else { WebHandler.ResponseMessage = ""; } UIHandler.AddNotification(UIAlertType.Success, "World Published", "World Code: " + json.worldId, 3000); return(responseStr); } catch (Exception ex) { return("fail"); } }
public static async Task <bool> LoginRequest(string username, string password) { var data = new { username, password }; // All checks have passed. Request the Level. try { // Run the Login Request Uri uri = new Uri(GameValues.CreoAPI + "login"); HttpResponseMessage response = await WebHandler.PostJSON(uri, data); if (response == null || response.IsSuccessStatusCode == false) { return(false); } // Extract the contents from the response. string contents = await response.Content.ReadAsStringAsync(); LoginFormat json = JsonConvert.DeserializeObject <LoginFormat>(contents); if (json.success == false) { if (json.reason is string) { WebHandler.ResponseMessage = json.reason; } return(false); } else { WebHandler.ResponseMessage = ""; } // Save Login Cookies Dictionary <string, string> cookies = GetCookiesFromResponse(response); WebHandler.SaveLoginCookies(cookies); // Verify the Cookies Were Sent return(WebHandler.IsLoggedIn()); } catch (Exception ex) { return(false); } }
public static async Task <bool> RegisterRequest(string email, string username, string password) { var data = new { email, username, password }; // All checks have passed. Request the Level. try { // Run the Login Request Uri uri = new Uri(GameValues.CreoAPI + "register"); HttpResponseMessage response = await WebHandler.PostJSON(uri, data); if (response == null || response.IsSuccessStatusCode == false) { WebHandler.ResponseMessage = "No Response was provided."; return(false); } // Extract the contents from the response. string contents = await response.Content.ReadAsStringAsync(); RegisterFormat json = JsonConvert.DeserializeObject <RegisterFormat>(contents); if (json.success == false) { if (json.reason is string) { WebHandler.ResponseMessage = json.reason; } return(false); } else { WebHandler.ResponseMessage = ""; } return(true); } catch (Exception ex) { return(false); } }
public static async Task <string> LevelPublishRequestAsync(string levelId) { // Must be logged in: if (!WebHandler.IsLoggedIn()) { return("fail"); } // Make sure we have the level loaded: if (Systems.handler.levelContent == null || Systems.handler.levelContent.levelId != levelId) { UIHandler.AddNotification(UIAlertType.Error, "Level Not Loaded", "The level did not load correctly. Unable to publish.", 300); return("fail"); } // Make sure the level is owned, i.e. it has the "__#" format. if (levelId.IndexOf("__") != 0) { UIHandler.AddNotification(UIAlertType.Error, "Invalid Access", "You can only publish levels that are stored on the \"My Levels\" screen.", 300); return("fail"); } string testId = levelId.Replace("__", ""); bool success = byte.TryParse(testId, out byte levelNum); if (!success) { UIHandler.AddNotification(UIAlertType.Error, "Invalid Format", "The level file was formatted incorrectly. Cannot publish.", 300); return("fail"); } if (levelNum > GameValues.MaxLevelsAllowedPerUser) { UIHandler.AddNotification(UIAlertType.Error, "Exceeds Allowance", "Can only publish up to " + GameValues.MaxLevelsAllowedPerUser + " levels. This level (#" + levelNum + ") is invalid.", 300); return("fail"); } UIHandler.AddNotification(UIAlertType.Warning, "Publishing Level", "Please wait while we attempt to publish your level...", 180); // All checks have passed. Attempt to publish the level. try { // Attach Login Cookies WebHandler.AttachLoginCookiesToRequest(GameValues.CreoAPI); // Run the Level Post StringContent content = new StringContent(JsonConvert.SerializeObject(Systems.handler.levelContent.data), Encoding.UTF8, "application/json"); var response = await Systems.httpClient.PostAsync(GameValues.CreoAPI + "level/" + levelNum, content); string responseStr = await response.Content.ReadAsStringAsync(); LevelAPIFormat json = JsonConvert.DeserializeObject <LevelAPIFormat>(responseStr); if (json.success == false) { if (json.reason is string) { UIHandler.AddNotification(UIAlertType.Error, "Unable to Publish", json.reason, 300); } return("fail"); } else { WebHandler.ResponseMessage = ""; } UIHandler.AddNotification(UIAlertType.Success, "Level Published", "Level Code: " + json.levelId, 3000); return(responseStr); } catch (Exception ex) { return("fail"); } }