-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFPL_Fixtures.pas
326 lines (280 loc) · 11.9 KB
/
FPL_Fixtures.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
unit FPL_Fixtures;
interface
uses
SuperObject, Spring.Collections;
type
TFPLFixtureStat = class
private
FHomeAway: char;
Felement: integer;
Fvalue: integer;
protected
// This overloaded constructor only allows this class to be instantiated from within this unit
constructor Create(const aOK: string = 'OK'); overload;
public
// This class cannot be instantiated from outside of this unit
constructor Create; overload; deprecated 'DO NOT USE!';
property HomeAway: char read FHomeAway;
property element: integer read Felement;
property value: integer read Fvalue;
end;
TFPLFixture = class
private
Fgoals_scored: IList<TFPLFixtureStat>;
Fassists: IList<TFPLFixtureStat>;
Fown_goals: IList<TFPLFixtureStat>;
Fpenalties_saved: IList<TFPLFixtureStat>;
Fpenalties_missed: IList<TFPLFixtureStat>;
Fyellow_cards: IList<TFPLFixtureStat>;
Fred_cards: IList<TFPLFixtureStat>;
Fsaves: IList<TFPLFixtureStat>;
Fbonus: IList<TFPLFixtureStat>;
Fbps: IList<TFPLFixtureStat>;
private
Fid: integer;
Fteam_h_difficulty: integer;
Fpulse_id: integer;
Ffinished: boolean;
Fcode: integer;
Fkickoff_time: TDateTime;
Fminutes: integer;
Fteam_h_score: integer;
Fteam_a_difficulty: integer;
Fstarted: boolean;
Fprovisional_start_time: boolean;
Fteam_h: integer;
Fteam_a_score: integer;
Fevent: integer;
Fteam_a: integer;
Ffinished_provisional: boolean;
function getGoalsScored: IReadOnlyList<TFPLFixtureStat>;
function getAssists: IReadOnlyList<TFPLFixtureStat>;
function getBonus: IReadOnlyList<TFPLFixtureStat>;
function getBPS: IReadOnlyList<TFPLFixtureStat>;
function getOwnGoals: IReadOnlyList<TFPLFixtureStat>;
function getPenaltiesMissed: IReadOnlyList<TFPLFixtureStat>;
function getPenaltiesSaved: IReadOnlyList<TFPLFixtureStat>;
function getRedCards: IReadOnlyList<TFPLFixtureStat>;
function getSaves: IReadOnlyList<TFPLFixtureStat>;
function getYellowCards: IReadOnlyList<TFPLFixtureStat>;
protected
// This overloaded constructor only allows this class to be instantiated from within this unit
constructor Create(const aOK: string = 'OK'); overload;
public
// This class cannot be instantiated from outside of this unit
constructor Create; overload; deprecated 'DO NOT USE!';
property id: integer read Fid;
property team_h_score: integer read Fteam_h_score;
property pulse_id: integer read Fpulse_id;
property minutes: integer read Fminutes;
property kickoff_time: TDateTime read Fkickoff_time;
property finished: boolean read Ffinished;
property provisional_start_time: boolean read Fprovisional_start_time;
property team_a_score: integer read Fteam_a_score;
property event: integer read Fevent;
property started: boolean read Fstarted;
property team_a_difficulty: integer read Fteam_a_difficulty;
property team_h_difficulty: integer read Fteam_h_difficulty;
property code: integer read Fcode;
property team_h: integer read Fteam_h;
property team_a: integer read Fteam_a;
property finished_provisional: boolean read Ffinished_provisional;
property goals_scored: IReadOnlyList<TFPLFixtureStat> read getGoalsScored;
property assists: IReadOnlyList<TFPLFixtureStat> read getAssists;
property own_goals: IReadOnlyList<TFPLFixtureStat> read getOwnGoals;
property penalties_saved: IReadOnlyList<TFPLFixtureStat> read getPenaltiesSaved;
property penalties_missed: IReadOnlyList<TFPLFixtureStat> read getPenaltiesMissed;
property yellow_cards: IReadOnlyList<TFPLFixtureStat> read getYellowCards;
property red_cards: IReadOnlyList<TFPLFixtureStat> read getRedCards;
property saves: IReadOnlyList<TFPLFixtureStat> read getSaves;
property bonus: IReadOnlyList<TFPLFixtureStat> read getBonus;
property bps: IReadOnlyList<TFPLFixtureStat> read getBPS;
end;
IFPLFixtures = interface
function build(aSO: ISuperObject): boolean;
function getFixtures: IReadOnlyList<TFPLFixture>;
property Fixtures: IReadOnlyList<TFPLFixture> read getFixtures;
end;
function FPLFixtures: IFPLFixtures;
implementation
uses
System.SysUtils, XSBuiltIns;
type
TFPLFixtures = class(TInterfacedObject, IFPLFixtures)
strict private
FFixtures: IList<TFPLFixture>;
private
function getFixtures: IReadOnlyList<TFPLFixture>;
public
constructor Create;
destructor Destroy; override;
function build(aSO: ISuperObject): boolean;
property Fixtures: IReadOnlyList<TFPLFixture> read getFixtures;
end;
var gInstance: IFPLFixtures;
function FPLFixtures: IFPLFixtures;
begin
case gInstance = NIL of TRUE: gInstance := TFPLFixtures.Create; end;
result := gInstance;
end;
{ TFPLFixtures }
function TFPLFixtures.build(aSO: ISuperObject): boolean;
var
so: ISuperObject;
rec: TFPLFixture;
recStat: TFPLFixtureStat;
statIDs: string;
procedure buildStatIDs;
// align IDs on 16-char boundaries
begin
statIDs := format('%15s%16s%16s%16s%16s%16s%16s%16s%16s%16s%16s',
[' ', 'goals_scored','assists','own_goals','penalties_saved','penalties_missed','yellow_cards','red_cards','saves','bonus','bps']);
end;
procedure processStat(const aIdentifier: string);
// statNum := pos(aIdentifier, statIDs) div 16;
begin
case pos(aIdentifier, statIDs) = 0 of TRUE: raise EProgrammerNotFound.Create('Fixture processStat: stat id = ' + aIdentifier); end;
case aIdentifier = 'goals_scored' of TRUE: rec.Fgoals_scored.Add(recStat); end;
case aIdentifier = 'assists' of TRUE: rec.Fassists.Add(recStat); end;
case aIdentifier = 'own_goals' of TRUE: rec.Fown_goals.Add(recStat); end;
case aIdentifier = 'penalties_saved' of TRUE: rec.Fpenalties_saved.Add(recStat); end;
case aIdentifier = 'penalties_missed' of TRUE: rec.Fpenalties_missed.Add(recStat); end;
case aIdentifier = 'yellow_cards' of TRUE: rec.Fyellow_cards.Add(recStat); end;
case aIdentifier = 'red_cards' of TRUE: rec.Fred_cards.Add(recStat); end;
case aIdentifier = 'saves' of TRUE: rec.Fsaves.Add(recStat); end;
case aIdentifier = 'bonus' of TRUE: rec.Fbonus.Add(recStat); end;
case aIdentifier = 'bps' of TRUE: rec.Fbps.Add(recStat); end;
end;
begin
FFixtures.Clear;
buildStatIDs;
for so in aSO do begin
for var fixture in so do begin
rec := TFPLFixture.Create('OK');
rec.Fid := fixture.I['id'];
rec.Fteam_h_difficulty := fixture.I['team_h_difficulty'];
rec.Fpulse_id := fixture.I['pulse_id'];
rec.Ffinished := fixture.B['finished'];
rec.Fcode := fixture.I['code'];
rec.Fkickoff_time := XMLTimeToDateTime(fixture.S['kickoff_time'], FALSE);
rec.Fminutes := fixture.I['minutes'];
rec.Fteam_h_score := fixture.I['team_h_score'];
rec.Fteam_a_difficulty := fixture.I['team_a_difficulty'];
rec.Fstarted := fixture.B['started'];
rec.Fprovisional_start_time := fixture.B['provisional_start_time'];
rec.Fteam_h := fixture.I['team_h'];
rec.Fteam_a_score := fixture.I['team_a_score'];
rec.Fevent := fixture.I['event'];
rec.Fteam_a := fixture.I['team_a'];
rec.Ffinished_provisional := fixture.B['finished_provisional'];
for var vStat in fixture.O['stats'] do begin
case trim(vstat.S['identifier']) = '' of TRUE: CONTINUE; end; // upcoming fixtures have a present but empty stats array
for var vHome in vstat['h'] do begin
recStat := TFPLFixtureStat.Create('OK');
recStat.FHomeAway := 'H';
recStat.Felement := vHome.I['element'];
recStat.Fvalue := vHome.I['value'];
processStat(vstat.S['identifier']);
end;
for var vAway in vStat['a'] do begin
recStat := TFPLFixtureStat.Create('OK');
recStat.FHomeAway := 'A';
recStat.Felement := vAway.I['element'];
recStat.Fvalue := vAway.I['value'];
processStat(vstat.S['identifier']);
end;
end;
FFixtures.Add(rec);
end;
end;
end;
constructor TFPLFixtures.Create;
begin
inherited;
FFixtures := TCollections.CreateObjectList<TFPLFixture>(TRUE);
end;
destructor TFPLFixtures.Destroy;
begin
// nowt to do
inherited;
end;
function TFPLFixtures.getFixtures: IReadOnlyList<TFPLFixture>;
begin
result := FFixtures.AsReadOnlyList;
end;
{ TFPLFixture }
constructor TFPLFixture.Create(const aOK: string);
begin
inherited Create;
Fgoals_scored := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fassists := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fown_goals := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fpenalties_saved := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fpenalties_missed := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fyellow_cards := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fred_cards := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fsaves := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fbonus := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
Fbps := TCollections.CreateObjectList<TFPLFixtureStat>(TRUE);
end;
constructor TFPLFixture.Create;
// This class cannot be instantiated from outside of this unit
begin
raise EProgrammerNotFound.Create('DO NOT USE THIS CONSTRUCTOR!');
end;
function TFPLFixture.getAssists: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fassists.AsReadOnlyList;
end;
function TFPLFixture.getBonus: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fbonus.AsReadOnlyList;
end;
function TFPLFixture.getBPS: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fbps.AsReadOnlyList;
end;
function TFPLFixture.getGoalsScored: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fgoals_scored.AsReadOnlyList;
end;
function TFPLFixture.getOwnGoals: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fown_goals.AsReadOnlyList;
end;
function TFPLFixture.getPenaltiesMissed: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fpenalties_missed.AsReadOnlyList;
end;
function TFPLFixture.getPenaltiesSaved: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fpenalties_saved.AsReadOnlyList;
end;
function TFPLFixture.getRedCards: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fred_cards.AsReadOnlyList;
end;
function TFPLFixture.getSaves: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fsaves.AsReadOnlyList;
end;
function TFPLFixture.getYellowCards: IReadOnlyList<TFPLFixtureStat>;
begin
result := Fyellow_cards.AsReadOnlyList;
end;
{ TFPLFixtureStat }
constructor TFPLFixtureStat.Create(const aOK: string);
// This overloaded constructor only allows this class to be instantiated from within this unit
begin
inherited Create;
end;
constructor TFPLFixtureStat.Create;
// This class cannot be instantiated from outside of this unit
begin
raise EProgrammerNotFound.Create('DO NOT USE THIS CONSTRUCTOR!');
end;
initialization
gInstance := NIL;
finalization
gInstance := NIL;
end.