-
Notifications
You must be signed in to change notification settings - Fork 2
/
TournamentViewModel.pas
120 lines (99 loc) · 2.71 KB
/
TournamentViewModel.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
unit TournamentViewModel;
interface
uses
Classes,
Knockoff.Observable;
type
TMatch = class
private
fHomeTeam: Observable<string>;
fAwayTeam: Observable<string>;
fHomeScore: Observable<Integer>;
fAwayScore: Observable<Integer>;
fWinner: Observable<string>;
public
constructor Create(const matchName: string;
const homeTeam: Observable<string>;
const awayTeam: Observable<string>);
procedure SaveScore;
property HomeTeam: Observable<string> read fHomeTeam;
property AwayTeam: Observable<string> read fAwayTeam;
property HomeScore: Observable<Integer> read fHomeScore;
property AwayScore: Observable<Integer> read fAwayScore;
property Winner: Observable<string> read fWinner;
end;
TTournamentViewModel = class(TComponent)
private
fSemiFinalOne: TMatch;
fSemiFinalTwo: TMatch;
fFinale: TMatch;
public
constructor Create; reintroduce;
destructor Destroy; override;
// temporary until methods also work in chained expressions
procedure SemiFinalOne_SaveScore;
procedure SemiFinalTwo_SaveScore;
procedure Finale_SaveScore;
property SemiFinalOne: TMatch read fSemiFinalOne;
property SemiFinalTwo: TMatch read fSemiFinalTwo;
property Finale: TMatch read fFinale;
end;
implementation
{ TTournamentViewModel }
constructor TTournamentViewModel.Create;
begin
inherited Create(nil);
fSemiFinalOne := TMatch.Create(
'semifinal one',
TObservable<string>.Create('Team 1'),
TObservable<string>.Create('Team 2'));
fSemiFinalTwo := TMatch.Create(
'semifinal two',
TObservable<string>.Create('Team 3'),
TObservable<string>.Create('Team 4'));
fFinale := TMatch.Create(
'final',
fSemiFinalOne.Winner,
fSemiFinalTwo.Winner);
end;
destructor TTournamentViewModel.Destroy;
begin
fFinale.Free;
fSemiFinalTwo.Free;
fSemiFinalOne.Free;
inherited;
end;
procedure TTournamentViewModel.Finale_SaveScore;
begin
fFinale.SaveScore;
end;
procedure TTournamentViewModel.SemiFinalOne_SaveScore;
begin
fSemiFinalOne.SaveScore;
end;
procedure TTournamentViewModel.SemiFinalTwo_SaveScore;
begin
fSemiFinalTwo.SaveScore;
end;
{ TMatch }
constructor TMatch.Create(const matchName: string;
const homeTeam: Observable<string>; const awayTeam: Observable<string>);
begin
fHomeTeam := homeTeam;
fAwayTeam := awayTeam;
fHomeScore := TObservable<Integer>.Create();
fAwayScore := TObservable<Integer>.Create();
fWinner := TObservable<string>.Create();
end;
procedure TMatch.SaveScore;
var
homeScore, awayScore: Integer;
begin
homeScore := fHomeScore;
awayScore := fAwayScore;
if homeScore > awayScore then
fWinner(fHomeTeam)
else if homeScore < awayScore then
fWinner(fAwayTeam);
end;
end.