/// <summary> /// 指定IDのオブジェクトを取得を行います。 /// </summary> /// <returns>結果</returns> public static IObservable <T> GetAsyncAsStream <T>(this NCMBQuery <T> query, string objectId) where T : NCMBObject { return(Observable.Create <T>(observer => { var gate = new object(); var isDisposed = false; query.GetAsync(objectId, (objects, error) => { lock (gate) { if (isDisposed) { return; } if (error == null) { observer.OnNext(objects); observer.OnCompleted(); } else { observer.OnError(error); } } }); return Disposable.Create(() => { lock (gate) { isDisposed = true; } }); })); }
/// <summary> /// 1回のゲームの結果をサーバーに保存する /// </summary> /// <param name="objectId">PlayerPrefs("AccountToken")</param> /// <param name="scoreThisTime">今回の獲得スコア</param> /// <param name="goalCountToEnemyThisTime">今回の敵ゴール数</param> /// <param name="onUpdate">結果の保存終了時の処理</param> /// <returns></returns> public IEnumerator UpdatePlayerDataOnFinishOneGame(string objectId, int scoreThisTime, int goalCountToEnemyThisTime, Action onUpdate) { NCMBQuery <NCMBObject> query = new NCMBQuery <NCMBObject>("User"); NCMBObject result = null; NCMBException error = null; query.GetAsync(objectId, (NCMBObject _result, NCMBException _error) => { result = _result; error = _error; }); //resultもしくはerrorが入るまで待機 yield return(new WaitWhile(() => result == null && error == null)); //後続処理 if (error == null) { result["TotalScore"] = scoreThisTime + int.Parse(result["TotalScore"].ToString()); result["TotalPlayCount"] = int.Parse(result["TotalPlayCount"].ToString()) + 1; result["TotalGoalToEnemyCount"] = goalCountToEnemyThisTime + int.Parse(result["TotalGoalToEnemyCount"].ToString()); if (int.Parse(result["HighScore"].ToString()) < scoreThisTime) { result["HighScore"] = scoreThisTime; } bool isComplete = false; result.SaveAsync(e => { if (e != null) { Debug.Log(e); return; } else { isComplete = true; } }); yield return(new WaitUntil(() => isComplete)); onUpdate(); } else { Debug.Log(error); } }
/// <summary> /// 指定IDのオブジェクトを取得を行います。 /// </summary> public static Task <T> GetTaskAsync <T>(this NCMBQuery <T> query, string objectId) where T : NCMBObject { var tcs = new TaskCompletionSource <T>(); query.GetAsync(objectId, (obj, error) => { if (error != null) { tcs.SetException(error); } else { tcs.SetResult(obj); } }); return(tcs.Task); }
/// <summary> /// 端末ごとに保存されているobjectIdによって、アカウントデータを取得する /// </summary> /// <param name="objectId">端末ごとにPlayerPrefs("AccountToken")によって保存されたId</param> /// <param name="action">dataを取ってきた際の処理</param> /// <returns></returns> public IEnumerator GetUserData(string objectId, Action <NetworkUserData> action) { NCMBQuery <NCMBObject> query = new NCMBQuery <NCMBObject>("User"); NCMBObject result = null; NCMBException error = null; query.GetAsync(objectId, (NCMBObject _result, NCMBException _error) => { result = _result; error = _error; }); //resultもしくはerrorが入るまで待機 yield return(new WaitWhile(() => result == null && error == null)); //後続処理 if (error == null) { action(new NetworkUserData(result)); } }
/// <summary> /// PlayerNameの変更 /// </summary> /// <param name="playerNameAfterChange"></param> /// <param name="objectId">PlayerPrefs("AccountToken")</param> /// <param name="onUpdate"></param> /// <returns></returns> public IEnumerator UpdatePlayerName(string playerNameAfterChange, string objectId, Action onUpdate) { NCMBQuery <NCMBObject> query = new NCMBQuery <NCMBObject>("User"); NCMBObject result = null; NCMBException error = null; query.GetAsync(objectId, (NCMBObject _result, NCMBException _error) => { result = _result; error = _error; }); //resultもしくはerrorが入るまで待機 yield return(new WaitWhile(() => result == null && error == null)); //後続処理 if (error == null) { result["PlayerName"] = playerNameAfterChange; result.Save(); //非同期通信にしてもいいかも onUpdate(); } }