-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGameData.pas
202 lines (166 loc) · 4.91 KB
/
OGameData.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
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
unit OGameData;
interface
uses
html, parser, classes, sysutils;
type
TGameUniverse = class
public
name: string;
urlName: string; // needed if uniname and urlCheckName different
galaxyCount: integer;
solsysCount: integer;
gameSpeedFactor: Single;
tfFleetFactor: Single;
tfDefFactor: Single;
redesign_rules: boolean;
constructor Create(const name: string; const xml: THTMLElement);
destructor Destroy; override;
end;
TGameSite = class
private
fGameUniverseList: TList;
procedure doGameUniverseTag(const xml: THTMLElement);
function getUniverse(index: integer): TGameUniverse;
public
name: string;
index: integer; // deprecated, needed only for backward compatibility
property UniverseList[index: integer]: TGameUniverse read getUniverse;
function Count(): integer;
constructor Create(xml: THTMLElement);
destructor Destroy; override;
end;
TGameData = class
private
fGameSitesList: TList;
procedure loadfromXML(xmlroot: THTMLElement);
function getGameSite(index: integer): TGameSite;
public
property GameSites[index: integer]: TGameSite read getGameSite;
function Count(): integer;
constructor Create(filename: string);
destructor Destroy; override;
end;
function StrEmptyDef(value, default: string): string;
implementation
uses StrUtils;
function StrEmptyDef(value, default: string): string;
begin
if value = '' then
Result := default
else
Result := value;
end;
function TGameData.Count: integer;
begin
Result := fGameSitesList.Count;
end;
constructor TGameData.Create(filename: string);
var xmlroot: THTMLElement;
text: TStringList;
begin
fGameSitesList := TList.Create;
text := TStringList.Create();
xmlroot := THTMLElement.Create(nil, 'root');
try
text.LoadFromFile(filename);
xmlroot.ParseHTMLCode(text.Text);
loadfromXML(xmlroot);
finally
xmlroot.Free;
text.Free;
end;
end;
destructor TGameData.Destroy;
begin
while Count > 0 do
begin
GameSites[0].Free;
fGameSitesList.Delete(0);
end;
fGameSitesList.Free;
inherited;
end;
function TGameData.getGameSite(index: integer): TGameSite;
begin
Result := TGameSite(fGameSitesList[index]);
end;
procedure TGameData.loadfromXML(xmlroot: THTMLElement);
var xmlgames: THTMLElement;
i, count: integer;
newGS: TGameSite;
begin
xmlgames := xmlroot.FindChildTagPath_e('?xml/data/game');
count := xmlgames.ChildNameCount('site');
for i := 0 to count - 1 do
begin
newGS := TGameSite.Create(xmlgames.FindChildTag('site', i));
fGameSitesList.Add(newGS);
end;
end;
{ TGameSite }
function TGameSite.Count: integer;
begin
Result := fGameUniverseList.Count;
end;
constructor TGameSite.Create(xml: THTMLElement);
var i, count: integer;
begin
fGameUniverseList := TList.Create;
name := xml.AttributeValue['name'];
index := StrToInt(xml.AttributeValue['index']);
count := xml.ChildNameCount('uni');
for i := 0 to count - 1 do
begin
doGameUniverseTag(xml.FindChildTag('uni', i));
end;
end;
destructor TGameSite.Destroy;
begin
while Count > 0 do
begin
UniverseList[0].Free;
fGameUniverseList.Delete(0);
end;
fGameUniverseList.Free;
inherited;
end;
procedure TGameSite.doGameUniverseTag(const xml: THTMLElement);
var uname, symbol: string;
start, ende, i: integer;
newU: TGameUniverse;
begin
uname := xml.AttributeValue['name'];
symbol := xml.AttributeValue['symbol'];
start := xml.AttributesByName['index'].AsInteger(1);
ende := xml.AttributesByName['to'].AsInteger(1);
for i := start to ende do
begin
newU := TGameUniverse.Create(
StringReplace(uname, symbol,
inttostr(i), [rfReplaceAll]),
xml);
fGameUniverseList.Add(newU);
end;
end;
function TGameSite.getUniverse(index: integer): TGameUniverse;
begin
Result := TGameUniverse(fGameUniverseList[index]);
end;
{ TGameUniverse }
constructor TGameUniverse.Create(const name: string; const xml: THTMLElement);
begin
self.name := name;
// now load universe data:
urlname := StrEmptyDef (xml.AttributeValue['urlname'] , name);
galaxyCount := StrToIntDef (xml.AttributeValue['galaxycount'] , 9);
solsysCount := StrToIntDef (xml.AttributeValue['solsyscount'] , 499);
gameSpeedFactor := StrToFloatDef(xml.AttributeValue['gamespeedfactor'], 1.0);
tfFleetFactor := StrToFloatDef(xml.AttributeValue['tffleetfactor'] , 0.3);
tfDefFactor := StrToFloatDef(xml.AttributeValue['tfdeffactor'] , 0.0);
redesign_rules := StrToBoolDef (xml.AttributeValue['redesign'] , true);
end;
destructor TGameUniverse.Destroy;
begin
inherited;
end;
end.