/// <summary> /// Get all player's names and scores from the leaderboard /// </summary> /// <param name="gameMode">Game Mode (leaderboard name)</param> /// <returns>All leaderbaord records for this mode</returns> public static async UniTask <GmLeaderboardModel> GetAllScores(string gameMode) { GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/leaderboards/get/{GmGameServicesClient.GameId}/{gameMode}"; UnityWebRequest uwr = UnityWebRequest.Get(url); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmLeaderboardGetAllScoresEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, Leaderboard = null }); return(null); } string response = uwr.downloadHandler.text; var leaderboard = new GmLeaderboardModel(gameMode, response); Events.Raise(new GmLeaderboardGetAllScoresEvent { Success = true, Leaderboard = leaderboard }); return(leaderboard); }
/// <summary> /// Change the gamer's name in all existing leaderboard records for this game (all modes) /// </summary> /// <remarks> /// Raises GmLeaderboardNameChangeEvent on success or failure /// </remarks> /// <param name="gamerId">Gamer ID</param> /// <param name="gamerName">Gamer Name</param> public static async UniTask SetGamerNameAsync(string gamerId, string gamerName) { gamerName = CleanGamerName(gamerName); GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/leaderboards/setname/{GmGameServicesClient.GameId}/{gamerId}/{gamerName}"; UnityWebRequest uwr = UnityWebRequest.Post(url, ""); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmLeaderboardNameChangeEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, GamerId = gamerId, GamerName = gamerName }); } else { Events.Raise(new GmLeaderboardNameChangeEvent { Success = true, GamerId = gamerId, GamerName = gamerName }); } }
/// <summary> /// Attempt login /// </summary> /// <param name="GamerTag">GamerTag or Email Address</param> /// <param name="password">password</param> /// <returns>Gamer details</returns> public static async UniTask <GmGamerTagModel> Login(string GamerTag, string password) { GmGameServicesClient.CheckApiKey(); GamerTag = GamerTag.Trim(); string url = $"{GmGameServicesClient.BaseUrl}/Gamer/login/{GmGameServicesClient.GameId}"; WWWForm form = new WWWForm(); form.AddField("GamerTag", GamerTag); form.AddField("password", password); UnityWebRequest uwr = UnityWebRequest.Post(url, form); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmLoginEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, Gamer = null }); return(null); } string responseText = uwr.downloadHandler.text; var responseObject = JsonConvert.DeserializeObject <GmGamerResponse>(responseText); if (responseObject == null || responseObject.Gamer == null) { Events.Raise(new GmLoginEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 2, Gamer = null }); return(null); } var evnt = new GmLoginEvent { Success = responseObject.Success, Gamer = responseObject.Gamer }; Events.Raise(evnt); return(responseObject.Gamer); }
/// <summary> /// Forgot Password /// </summary> /// <param name="gamerTagOrEmail">GamerTag or Email Address</param> /// <returns>True if account found and code sent</returns> public static async UniTask <bool> ForgotPassword(string gamerTagOrEmail) { GmGameServicesClient.CheckApiKey(); gamerTagOrEmail = gamerTagOrEmail.Trim(); string url = $"{GmGameServicesClient.BaseUrl}/Gamer/forgotpassword/{GmGameServicesClient.GameId}"; WWWForm form = new WWWForm(); form.AddField("gamerTagOrEmail", gamerTagOrEmail); UnityWebRequest uwr = UnityWebRequest.Post(url, form); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmForgotPasswordEvent { GamerTagOrEmail = gamerTagOrEmail, Success = false, ErrorMessage = uwr.error, ErrorCode = 1 }); return(false); } string responseText = uwr.downloadHandler.text; var responseObject = JsonConvert.DeserializeObject <BaseResponse>(responseText); if (responseObject == null) { Events.Raise(new GmForgotPasswordEvent { GamerTagOrEmail = gamerTagOrEmail, Success = false, ErrorMessage = uwr.error, ErrorCode = 2 }); return(false); } var evnt = new GmForgotPasswordEvent { GamerTagOrEmail = gamerTagOrEmail, Success = responseObject.Success }; Events.Raise(evnt); return(responseObject.Success); }
/// <summary> /// Verify email account as the 2nd part of registration /// </summary> /// <param name="gamerId">Gamer ID</param> /// <param name="code">Email code to check</param> /// <returns></returns> public static async Task <GmGamerTagModel> RegisterVerify(string gamerId, string code) { GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/gamer/verifyemail/{GmGameServicesClient.GameId}"; WWWForm form = new WWWForm(); form.AddField("gamerId", gamerId); form.AddField("emailVerificationCode", code); UnityWebRequest uwr = UnityWebRequest.Post(url, form); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmRegisterVerifyEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, Gamer = null }); return(null); } string responseText = uwr.downloadHandler.text; var responseObject = JsonConvert.DeserializeObject <GmGamerResponse>(responseText); if (responseObject == null) { Events.Raise(new GmRegisterVerifyEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 2, Gamer = null }); return(null); } var evnt = new GmRegisterVerifyEvent { Success = responseObject.Success, Gamer = responseObject.Gamer }; Events.Raise(evnt); return(responseObject.Gamer); }
/// <summary> /// Save the player's name and score to the leaderboard /// </summary> /// <param name="gameMode">Game Mode (leaderboard name)</param> /// <param name="gamerId">Gamer ID</param> /// <param name="gamerName">Gamer Name</param> /// <param name="score">Score</param> /// <returns>Gamer's leaderbaord record</returns> public static async UniTask <GmLeaderboardRecord> SetScoreAsync(string gameMode, string gamerId, string gamerName, int score) { gamerName = CleanGamerName(gamerName); if (gamerName == "") { return(null); } GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/leaderboards/set/{GmGameServicesClient.GameId}/{gameMode}/{gamerId}/{gamerName}/{score}"; UnityWebRequest uwr = UnityWebRequest.Post(url, ""); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmLeaderboardSetScoreEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, LeaderboardRecord = null }); return(null); } string response = uwr.downloadHandler.text; var leaderboardRecord = new GmLeaderboardRecord(response); Events.Raise(new GmLeaderboardSetScoreEvent { Success = true, LeaderboardRecord = leaderboardRecord }); return(leaderboardRecord); }
/// <summary> /// Delete a gamer file /// </summary> /// <param name="gamerId">Gamer ID</param> /// <param name="fileName">File Name</param> /// <returns>True if deleted</returns> public static async UniTask <bool> DeleteGamerFile(string gamerId, string fileName) { GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/gamercontent/delete/{GmGameServicesClient.GameId}/{gamerId}/{fileName}"; fileName = CleanFileName(fileName); UnityWebRequest uwr = UnityWebRequest.Get(url); uwr.method = "DELETE"; uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmGamerContentDeleteEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, GamerId = gamerId, FileName = fileName }); return(false); } string responseText = uwr.downloadHandler.text; var response = JsonConvert.DeserializeObject <GmGamerContentFileDeleteResponse>(responseText); Events.Raise(new GmGamerContentDeleteEvent { Success = response.Success, FileName = response.FileName }); return(true); }
/// <summary> /// Get the lsit of files for a gamer /// </summary> /// <param name="gamerId">Gamer ID</param> /// <returns>List of file infos</returns> public static async UniTask <List <GmGamerContentFileInfo> > GetGamerFileList(string gamerId) { GmGameServicesClient.CheckApiKey(); string url = $"{GmGameServicesClient.BaseUrl}/gamercontent/list/{GmGameServicesClient.GameId}/{gamerId}"; UnityWebRequest uwr = UnityWebRequest.Get(url); uwr.useHttpContinue = true; uwr.redirectLimit = 2; uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmGamerContentListEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, FileInfos = null }); return(null); } string responseText = uwr.downloadHandler.text; var response = JsonConvert.DeserializeObject <GmGamerContentListResponse>(responseText); var files = response.Files; Events.Raise(new GmGamerContentListEvent { Success = response.Success, FileInfos = files }); return(files); }
/// <summary> /// Register phase 1 /// </summary> /// <param name="gamerTag">GamerTag</param> /// <param name="email">Email Address</param> /// <param name="realName">Real Name (optional)</param> /// <param name="password">Password</param> /// <returns>Temporary gamerId, or null</returns> public static async Task <GmGamerTagModel> Register(string gamerTag, string email, string realName, string password) { GmGameServicesClient.CheckApiKey(); gamerTag = gamerTag.Trim(); string url = $"{GmGameServicesClient.BaseUrl}/gamer/register/{GmGameServicesClient.GameId}"; WWWForm form = new WWWForm(); form.AddField("email", email); form.AddField("gamerTag", gamerTag); form.AddField("password", password); form.AddField("realName", realName); UnityWebRequest uwr = UnityWebRequest.Post(url, form); uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); await uwr.SendWebRequest().AsObservable(); if (uwr.isNetworkError) { Events.Raise(new GmRegisterEvent { GamerId = null, Email = null, Success = false, ErrorMessage = uwr.error, ErrorCode = 1 }); return(null); } string responseText = uwr.downloadHandler.text; var responseObject = JsonConvert.DeserializeObject <GmGamerResponse>(responseText); if (responseObject == null) { Events.Raise(new GmRegisterEvent { GamerId = null, Email = null, Success = false, ErrorMessage = uwr.error, ErrorCode = responseObject.ErrorCode }); return(null); } var evnt = new GmRegisterEvent { ErrorCode = responseObject.ErrorCode, ErrorMessage = responseObject.Error, GamerId = (responseObject.Gamer != null) ? responseObject.Gamer.GamerId : null, Email = (responseObject.Gamer != null) ? responseObject.Gamer.Email : null, Success = responseObject.Success }; Events.Raise(evnt); return(responseObject.Gamer); }
/// <summary> /// Upload a file /// </summary> /// <param name="gamerId">User identifier - game can use deivce id or GamerTag id</param> /// <param name="fileName">File Name (slashes not allowed)</param> /// <param name="fileBytes">Content of File being uploaded</param> /// <param name="metadata">Additional data to store with the file</param> /// <param name="fileType">File Type used to set mime type</param> /// <param name="progressCallback">Progress callback used to update UI</param> /// <returns>Json details of success or failure/</returns> public static async UniTask <GmGamerContentFileInfo> SaveFileAsync( string gamerId, string fileName, byte[] fileBytes, Dictionary <string, string> metadata, GmGamerContentFileType fileType = GmGamerContentFileType.autoFromFileName, Func <float, bool> progressCallback = null ) { GmGameServicesClient.CheckApiKey(); // Remove forbidden characters from the file name fileName = CleanFileName(fileName); // Mime type string mimeType; if (fileType == GmGamerContentFileType.autoFromFileName) { fileType = GetFileTimeFromFileName(fileName); } mimeType = MimeTypes[fileType]; // Convert the metadata to json string metadataJson = FormatMetadataAsJson(metadata); // Build the request string url = $"{GmGameServicesClient.BaseUrl}/gamercontent/save/{GmGameServicesClient.GameId}/{gamerId}"; List <IMultipartFormSection> form = new List <IMultipartFormSection> { new MultipartFormFileSection("file", fileBytes, fileName, mimeType), new MultipartFormDataSection("metadata", metadataJson) }; var uwr = UnityWebRequest.Post(url, form); uwr.useHttpContinue = true; uwr.redirectLimit = 2; uwr.SetRequestHeader("Authentication", GmGameServicesClient.GameApiKey); // Wait for completion var result = await uwr.SendWebRequest() .ConfigureAwait(new Progress <float>((progress) => { if (progressCallback != null) { // Don't let progress to go 100 if it's not done if (progress > 0.99f && (uwr.downloadProgress < 1 || !uwr.isDone)) { progress = 0.99f; } progressCallback.Invoke(progress); } })); if (result.isNetworkError) { Events.Raise(new GmGamerContentSaveEvent { Success = false, ErrorMessage = uwr.error, ErrorCode = 1, FileInfo = null }); return(null); } string responseText = result.downloadHandler.text; var fileInfoResponse = JsonConvert.DeserializeObject <GmGamerContentFileInfoResponse>(responseText); var fileInfo = fileInfoResponse.FileInfo; if (fileInfo == null) { Debug.Log("Response didnt deserialize"); } var evnt = new GmGamerContentSaveEvent { Success = fileInfoResponse.Success, FileInfo = fileInfo }; Events.Raise(evnt); return(fileInfo); }