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 <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"); } }