-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
uScores.pas
116 lines (100 loc) · 2.37 KB
/
uScores.pas
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
unit uScores;
interface
const
CNbMaxScore = 50;
type
TScore = record
pseudo: string;
score: cardinal;
level: cardinal;
end;
TScoresList = array of TScore;
procedure ScoreAdd(pseudo: string; score: cardinal; level: cardinal;
savescorelist: boolean = true);
function getScoresList: TScoresList;
implementation
uses
system.Types, system.IOUtils, system.sysutils, uCommonTools;
var
ScoresList: TScoresList;
function getScoreFilePath: string;
begin
result := tpath.Combine(getFolderName, 'scores.dat');
end;
procedure Save;
var
i: integer;
f: textfile;
begin
assignfile(f, getScoreFilePath);
{$I-}
Rewrite(f);
{$I+}
for i := 0 to length(ScoresList) - 1 do
if (ScoresList[i].score > 0) then
writeln(f, ScoresList[i].pseudo, tabulator, ScoresList[i].score,
tabulator, ScoresList[i].level);
closefile(f);
end;
procedure Load;
var
f: textfile;
FileName: string;
ch: string;
tab: tarray<string>;
begin
setlength(ScoresList, 0);
FileName := getScoreFilePath;
if tfile.Exists(FileName) then
begin
assignfile(f, FileName);
{$I-}
Reset(f);
{$I+}
while not eof(f) do
begin
readln(f, ch);
tab := ch.Split([tabulator]);
if length(tab) = 3 then
ScoreAdd(tab[0], StrToUInt64(tab[1]), StrToUInt64(tab[2]), false);
end; // TODO : verifier UInt64 et Cardinal
closefile(f);
end;
end;
procedure ScoreAdd(pseudo: string; score: cardinal; level: cardinal;
savescorelist: boolean = true);
var
i, i_NewScore: integer;
newscore: TScore;
begin
if (not pseudo.Trim.isempty) and (not pseudo.Contains(tabulator)) and
(score > 0) then
begin
newscore.pseudo := pseudo;
newscore.score := score;
newscore.level := level;
if length(ScoresList) < CNbMaxScore then
setlength(ScoresList, length(ScoresList) + 1);
i := 0;
while (i <= length(ScoresList) - 2) and
(ScoresList[i].score > newscore.score) do
inc(i);
i_NewScore := i;
i := length(ScoresList) - 1;
while (i > i_NewScore) do
begin
ScoresList[i] := ScoresList[i - 1];
dec(i);
end;
ScoresList[i_NewScore] := newscore;
if savescorelist then
Save;
end;
end;
function getScoresList: TScoresList;
begin
result := ScoresList;
end;
initialization
Load;
end.