-
Notifications
You must be signed in to change notification settings - Fork 0
/
RankingManager.cs
197 lines (181 loc) · 7.1 KB
/
RankingManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using NCMB;
using TMPro;
using UnityEngine;
namespace UnityCustomRankingTemplate.Scripts
{
using static RankingUtils;
public class RankingManager : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _statusText;
[SerializeField] private RankingRecord _rankingRecord;
[SerializeField] private Transform _rankingContentsPanel;
[SerializeField, Range(0, 500)] private float _scrollPadding = 100.0f;
private readonly List<RankingRecord> _records = new List<RankingRecord>();
private string _uniqueUserId = "";
private void Start()
{
// 初回起動時にPlayerPrefsを初期化
InitPlayerPrefs();
}
/// <summary>
/// 初回起動時にユーザデータを初期化する
/// </summary>
private void InitPlayerPrefs()
{
var uniqueId = "invalid id";
// 初回起動時のみ初期化
if (!PlayerPrefs.HasKey(UniqueUserIdKey))
{
// ユーザデータと紐づけるための一意ID
Guid guid = Guid.NewGuid();
uniqueId = guid.ToString();
PlayerPrefs.SetString(UniqueUserIdKey, uniqueId);
// デフォルトのユーザ名をセット
PlayerPrefs.SetString(ClientUserNameKey, DefaultUserName);
PlayerPrefs.Save();
}
else
{
uniqueId = PlayerPrefs.GetString(UniqueUserIdKey);
}
// 一意IDのキャッシュ
_uniqueUserId = uniqueId;
}
/// <summary>
/// ローカルデータ、データベースの名前を更新する
/// </summary>
public void ChangeName(string newName)
{
// ローカルデータ上のユーザ名を更新
PlayerPrefs.SetString(ClientUserNameKey, newName);
PlayerPrefs.Save();
// データベース上のユーザ名を更新
NCMBQuery<NCMBObject> query = new NCMBQuery<NCMBObject>(NCMBStorageKey);
// 一意IDに紐づいたデータを検索
query.WhereEqualTo(UniqueUserIdKey, _uniqueUserId);
query.FindAsync((List<NCMBObject> objList, NCMBException e) =>
{
// 取得に成功
if (e == null)
{
// 既にスコアデータが存在している場合のみ変更
if (objList.Count != 0)
{
objList[0][UserNameKey] = newName;
objList[0].SaveAsync();
}
}
else
{
Debug.LogWarning($"ユーザ名の変更に失敗しました: {e}");
}
});
}
/// <summary>
/// ランキングの送信
/// </summary>
public void SendRanking(int score)
{
NCMBQuery<NCMBObject> query = new NCMBQuery<NCMBObject>(NCMBStorageKey);
var userName = PlayerPrefs.GetString(ClientUserNameKey);
// 一意IDに紐づいたデータを検索
query.WhereEqualTo(UniqueUserIdKey, _uniqueUserId);
query.FindAsync((List<NCMBObject> objList, NCMBException e) =>
{
// 取得に成功
if (e == null)
{
// 未登録のデータ
if (objList.Count == 0)
{
NCMBObject obj = new NCMBObject(NCMBStorageKey);
obj[UniqueUserIdKey] = _uniqueUserId;
obj[UserNameKey] = userName;
obj[HighScoreKey] = score;
obj.SaveAsync();
}
// ハイスコア更新の場合
// スコアを更新
else if (Convert.ToInt32(objList[0][HighScoreKey]) < score)
{
objList[0][HighScoreKey] = score;
objList[0].SaveAsync();
}
}
else
{
Debug.LogWarning($"ランキングの送信に失敗しました: {e}");
}
});
}
/// <summary>
/// ランキングの取得
/// </summary>
public void FetchRanking()
{
// ランキングリストの初期化
// 接続中を表示
var rectTrans = _rankingContentsPanel.gameObject.GetComponent<RectTransform>();
ClearRecords();
_statusText.text = "Loading ...";
// データの取得
NCMBQuery<NCMBObject> query = new NCMBQuery<NCMBObject>(NCMBStorageKey);
query.OrderByDescending(HighScoreKey);
query.Limit = MaxRecordNum;
query.FindAsync((List<NCMBObject> objList, NCMBException e) =>
{
// 取得に成功
if (e == null)
{
// データが存在しない場合
if (objList.Count == 0)
{
_statusText.text = "No Data";
return;
}
var rank = 1;
// 取得したデータのリスト
for (int i = 0; i < objList.Count; i++)
{
// スコア(順位)が一緒の場合
if (i == 0 || objList[i][HighScoreKey].ToString() == objList[i - 1][HighScoreKey].ToString())
{
}
else
{
rank++;
}
// ランキングレコードの表示
var record = Instantiate(_rankingRecord, _rankingContentsPanel);
rectTrans.sizeDelta =
new Vector2(rectTrans.sizeDelta.x, rectTrans.sizeDelta.y + _scrollPadding);
// 自分のスコアかの判定
bool isMyRecord = objList[i][UniqueUserIdKey].ToString() == _uniqueUserId;
record.SetRecord(objList[i][UserNameKey].ToString(), Convert.ToInt32(objList[i][HighScoreKey]),
rank, isMyRecord);
_records.Add(record);
_statusText.text = "";
}
}
else
{
_statusText.text = "Failed";
Debug.LogWarning($"ランキングの取得に失敗しました: {e}");
}
});
}
/// <summary>
/// レコードのクリア
/// </summary>
private void ClearRecords()
{
foreach (var t in _records)
{
Destroy(t.gameObject);
}
_records.Clear();
}
}
}