/
ScoreBoard.cs
210 lines (163 loc) · 6.68 KB
/
ScoreBoard.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
198
199
200
201
202
203
204
205
206
207
208
209
210
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class ScoreBoard : MonoBehaviour
{
[SerializeField]
private SaveData saveData;
[SerializeField]
private GameObject gameOverScreen;
[SerializeField]
private Text playerRankText;
[SerializeField]
private Text pageNumberText;
[SerializeField]
private int scoresPerPage = 10;
private int pageNumber = 1;
private int maxPageNumber;
private bool isTimeData;
private string savedScores;
private string[] dataLines;
private Text namesFieldTextField;
private Text scoresTextField;
void Awake()
{
namesFieldTextField = gameOverScreen.transform.Find("NamesField").GetComponent<Text>();
scoresTextField = gameOverScreen.transform.Find("ScoresField").GetComponent<Text>();
}
private void MakeABoard(List<string> _score, bool _isTimeData) {
//make the textfield clear so we can put a new score in them
namesFieldTextField.text = "";
scoresTextField.text = "";
isTimeData = _isTimeData;
int startRank = (pageNumber * scoresPerPage) - scoresPerPage + 1;
if (_isTimeData)
MakeTimeBoard(_score, startRank);
else
MakeScoreBoard(_score, startRank);
}
private void MakeScoreBoard(List<string> _score, int _startRank) {
//the rank where i show at which place this score is in de leaderboards
int rank = _startRank;
foreach (string text in _score) {
string[] myStr2 = text.Split('_');
if (saveData != null && myStr2[0] == saveData.PlayerName)
namesFieldTextField.text += rank.ToString() + ". " + myStr2[0] + " <--- \n";
else
namesFieldTextField.text += rank.ToString() + ". " + myStr2[0] + "\n";
scoresTextField.text += myStr2[1] + "\n";
rank++;
}
}
private void MakeTimeBoard(List<string> _score, int _startRank)
{
//the rank where i show at which place this score is in de leaderboard
int rank = _startRank;
foreach (string text in _score)
{
//split the names and time in a string array
string[] namesAndTimes = text.Split('_');
//add all the names to the scoreboard
if(saveData != null && namesAndTimes[0] == saveData.PlayerName)
namesFieldTextField.text += rank.ToString() + ". " + namesAndTimes[0] + " <--- \n";
else
namesFieldTextField.text += rank.ToString() + ". " + namesAndTimes[0] + "\n";
//make lists for each time type
List<char> min = new List<char>();
List<char> sec = new List<char>();
List<char> frac = new List<char>();
//make a char array that we store the time in
char[] timeArray = namesAndTimes[1].ToCharArray();
//put the right values in the right list
for (int i = timeArray.Length - 1; i >= 0; i--)
{
if (i > timeArray.Length - 3)
frac.Add(timeArray[i]);//fractions
else if (i > timeArray.Length - 5)
sec.Add(timeArray[i]);//seconds
else
min.Add(timeArray[i]);//minutes
}
//reverse each list because when they are not added in the right order
min.Reverse();
sec.Reverse();
frac.Reverse();
string time = "";
if (new string(min.ToArray()) == "")
time = string.Format("{0:00}:{1:00}:{2:00}", 0.ToString(), new string(sec.ToArray()), new string(frac.ToArray()));
else
time = string.Format("{0:00}:{1:00}:{2:00}", new string(min.ToArray()), new string(sec.ToArray()), new string(frac.ToArray()));
scoresTextField.text += time + "\n";
rank++;
}
}
public void GetPlayerRanking(string _scoreType)
{
//only do this if saveData is not null,
//if it is null, it means we are in the highscore scene and not the game scene
if (saveData != null)
{
for (int i = 0; i < dataLines.Length; i++) {
string[] splitLines = dataLines[i].Split('_');
//if the name (first part of the line array) is the same as the saved name, it is our score
if (splitLines[0] == saveData.PlayerName) {
playerRankText.text = (i + 1).ToString();
SetPageNumber(Mathf.CeilToInt(((float)i + 1) / scoresPerPage));
break;
}
}
}
}
public List<string> CutLines(string _uncutScore)
{
//split all the lines in a array
dataLines = _uncutScore.Trim().Split('\n');
//calculate the max page number
maxPageNumber = Mathf.CeilToInt(dataLines.Length / (float)scoresPerPage);
//make a new list where we will store the selected scores in
List<string> cuttedLines = new List<string>();
for (int i = (pageNumber * scoresPerPage) - scoresPerPage; i < (pageNumber * scoresPerPage); i++)
{
//if the index (i) isnt higher then the total lines, and not lower then zero
if (i < dataLines.Length && i >= 0)
cuttedLines.Add(dataLines[i]);
}
//return the results
return cuttedLines;
}
public void ScrollPageNumber(int _change)
{
//change the page number
pageNumber += _change;
//check if it isnt out of range
if (pageNumber > maxPageNumber)
pageNumber = 1;
else if (pageNumber < 1)
pageNumber = maxPageNumber;
pageNumberText.text = pageNumber.ToString();
//make the new page, with the same string only differently cut.
MakeABoard(CutLines(savedScores), isTimeData);
}
public void SetPageNumber(int _newPageNumber)
{
//change the page number
pageNumber = _newPageNumber;
pageNumberText.text = pageNumber.ToString();
//make the new page, with the same string only differently cut.
MakeABoard(CutLines(savedScores), isTimeData);
}
public void NewScoreBoard(string _score, string _scoreType)
{
//reset the page
pageNumber = 1;
//update the ui
pageNumberText.text = pageNumber.ToString();
//save the current loaded scores in savedScores
savedScores = _score;
//if the dataType is Time, make a special board.
if (_scoreType == "time")
MakeABoard(CutLines(_score), true);
else //else make a normal board
MakeABoard(CutLines(_score), false);
}
}