[go: up one dir, main page]

Menu

[r48]: / trunk / core / gameservice.pas  Maximize  Restore  History

Download this file

357 lines (319 with data), 8.7 kB

  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
unit GameService;
{$MODE ObjFpc}
{$H+}
interface
uses
Classes, SysUtils,
(* project units *)
Model, ServiceInterfaces, Helpers;
type
TGameBase = class(TObject)
private
function IsItemExistant(ItemId: Integer): Boolean;
function IsItemNonExistant(ItemId: Integer): Boolean;
function IsAttributeLower(ItemId, Value: Integer): Boolean;
function IsAttributeHigher(ItemId, Value: Integer): Boolean;
function IsAttributeEqual(ItemId, Value: Integer): Boolean;
procedure IncAttribute(AttributeId: Integer; Value: String);
procedure DecAttribute(AttributeId: Integer; Value: String);
procedure ResetAttribute(AttributeId: Integer);
procedure EnableItem(ItemId: Integer);
procedure DisableItem(ItemId: Integer);
procedure SwitchItem(ItemId: Integer);
protected
Dao: IReadonlyDao;
protected
FAttributes: TAttributeList;
FAvailableExits: TPageExitList;
FCurrentPage: TPage;
FGameTitle: String;
FItems: TItemList;
protected
function AreExitConditionsSatisfied(PageExit: TPageExit): Boolean;
procedure ExecuteAction(Action: TAction);
procedure ExecuteActionsOfPage(PageId: Integer);
function IsConditionSatisfied(Condition: TPageExitCondition): Boolean;
procedure CheckExits(ExitList: TPageExitList);
function GetValue(const Key: String): String;
procedure InitAttributeValues;
procedure LoadExits(PageId: Integer);
procedure LoadGameData;
procedure LoadStartPage;
procedure LoadPage(PageId: Integer);
public
destructor Destroy; override;
end;
TGameDependencies = record
Dao: IReadonlyDao;
end;
TGame = class(TGameBase)
public
constructor Create(Dependencies: TGameDependencies);
public
procedure InitGame;
procedure UseExit(ExitId: Integer);
public
function AvailableExits: TPageExitList;
function CurrentPage: TPage;
function GameTitle: String;
end;
implementation
function TGameBase.IsItemExistant(ItemId: Integer): Boolean;
var
Item: TItem;
begin
Result := False;
Item := FItems.FindById(ItemId);
if Assigned(Item) then
Result := Item.IsPresent;
end;
function TGameBase.IsItemNonExistant(ItemId: Integer): Boolean;
var
Item: TItem;
begin
Result := False;
Item := FItems.FindById(ItemId);
if Assigned(Item) then
Result := not Item.IsPresent;
end;
function TGameBase.IsAttributeLower(ItemId, Value: Integer): Boolean;
var
Attribute: TAttribute;
begin
Result := False;
Attribute := FAttributes.FindById(ItemId);
if Assigned(Attribute) then
Result := Attribute.Value < Value;
end;
function TGameBase.IsAttributeHigher(ItemId, Value: Integer): Boolean;
var
Attribute: TAttribute;
begin
Result := False;
Attribute := FAttributes.FindById(ItemId);
if Assigned(Attribute) then
Result := Attribute.Value > Value;
end;
function TGameBase.IsAttributeEqual(ItemId, Value: Integer): Boolean;
var
Attribute: TAttribute;
begin
Result := False;
Attribute := FAttributes.FindById(ItemId);
if Assigned(Attribute) then
Result := Attribute.Value = Value;
end;
procedure TGameBase.IncAttribute(AttributeId: Integer; Value: String);
var
Attribute: TAttribute;
begin
Attribute := FAttributes.FindById(AttributeId);
if Assigned(Attribute) then
Attribute.Value := Attribute.Value + Helpers.CalculateValue(Attribute.Value, Value);
end;
procedure TGameBase.DecAttribute(AttributeId: Integer; Value: String);
var
Attribute: TAttribute;
begin
Attribute := FAttributes.FindById(AttributeId);
if Assigned(Attribute) then
Attribute.Value := Attribute.Value - Helpers.CalculateValue(Attribute.Value, Value);
end;
procedure TGameBase.ResetAttribute(AttributeId: Integer);
var
Attribute: TAttribute;
begin
Attribute := FAttributes.FindById(AttributeId);
if Assigned(Attribute) then
Attribute.Value := StrToInt(Attribute.InitialValue);
end;
procedure TGameBase.EnableItem(ItemId: Integer);
var
Item: TItem;
begin
Item := FItems.FindById(ItemId);
Item.IsPresent := True;
end;
procedure TGameBase.DisableItem(ItemId: Integer);
var
Item: TItem;
begin
Item := FItems.FindById(ItemId);
Item.IsPresent := False;
end;
procedure TGameBase.SwitchItem(ItemId: Integer);
var
Item: TItem;
begin
Item := FItems.FindById(ItemId);
Item.IsPresent := not Item.IsPresent;
end;
function TGameBase.AreExitConditionsSatisfied(PageExit: TPageExit): Boolean;
var
ConditionsList: TPageExitConditionList;
o: TPageExitCondition;
begin
Result := True;
if PageExit.IsConditional then begin
ConditionsList := Dao.GetPageExitConditionsByPageExitId(PageExit.Id);
for o in ConditionsList do begin
if not IsConditionSatisfied(o) then Result := False;
end;
end;
end;
procedure TGameBase.ExecuteAction(Action: TAction);
begin
case Action.ActionType of
ACTIONTYPE_INC_ATTRIBUTE:
IncAttribute(Action.ActionObjectId, Action.ActionValue);
ACTIONTYPE_DEC_ATTRIBUTE:
DecAttribute(Action.ActionObjectId, Action.ActionValue);
ACTIONTYPE_RESET_ATTRIBUTE:
ResetAttribute(Action.ActionObjectId);
ACTIONTYPE_ENABLE_ITEM:
EnableItem(Action.ActionObjectId);
ACTIONTYPE_DISABLE_ITEM:
DisableItem(Action.ActionObjectId);
ACTIONTYPE_SWITCH_ITEM:
SwitchItem(Action.ActionObjectId);
end;
end;
procedure TGameBase.ExecuteActionsOfPage(PageId: Integer);
var
Actions: TActionList;
o: TAction;
begin
try
Actions := Dao.GetActionsByPageId(PageId);
for o in Actions do begin
ExecuteAction(o);
end;
finally
FreeAndNil(Actions);
end;
end;
function TGameBase.IsConditionSatisfied(Condition: TPageExitCondition): Boolean;
begin
case Condition.ConditionType of
CONDITIONTYPE_ITEM_EXISTANT:
Result := IsItemExistant(Condition.ConditionObjectId);
CONDITIONTYPE_ITEM_NONEXISTANT:
Result := IsItemNonExistant(Condition.ConditionObjectId);
CONDITIONTYPE_ATTRIBUTE_HIGHER:
Result := IsAttributeHigher(Condition.ConditionObjectId, Condition.ConditionValue);
CONDITIONTYPE_ATTRIBUTE_LOWER:
Result := IsAttributeLower(Condition.ConditionObjectId, Condition.ConditionValue);
CONDITIONTYPE_ATTRIBUTE_EQUAL:
Result := IsAttributeEqual(Condition.ConditionObjectId, Condition.ConditionValue);
else
Result := False;
end;
end;
procedure TGameBase.CheckExits(ExitList: TPageExitList);
var
o: TPageExit;
DeletionList: TPageExitList;
begin
try
DeletionList := TPageExitList.Create(False);
for o in ExitList do
if not AreExitConditionsSatisfied(o) then DeletionList.Add(o);
for o in DeletionList do
ExitList.Remove(o);
finally
FreeAndNil(DeletionList);
end;
end;
function TGameBase.GetValue(const Key: String): String;
var
Setting: TSetting;
begin
Result := EmptyStr;
try
Setting := Dao.GetSetting(Key);
if Assigned(Setting) then Result := Setting.Value;
finally
FreeAndNil(Setting);
end;
end;
procedure TGameBase.InitAttributeValues;
var
o: TAttribute;
begin
for o in FAttributes do begin
o.Value := Helpers.CalculateValue(0, o.InitialValue);
o.InitialValue := IntToStr(o.Value);
end;
end;
procedure TGameBase.LoadExits(PageId: Integer);
var
NewExits: TPageExitList;
begin
NewExits := Dao.GetPageExistsByPageId(PageId);
FAvailableExits.Clear;
FAvailableExits.Assign(NewExits);
end;
procedure TGameBase.LoadGameData;
begin
FAttributes := Dao.GetAllAttributes;
InitAttributeValues;
FItems := Dao.GetAllItems;
FGameTitle := GetValue('GameTitle');
end;
procedure TGameBase.LoadStartPage;
var
StartPageId: Integer;
begin
StartPageId := StrToIntDef(GetValue('StartPage'), 1);;
LoadPage(StartPageId);
end;
procedure TGameBase.LoadPage(PageId: Integer);
var
NewPage: TPage;
begin
NewPage := Dao.GetPage(PageId);
if Assigned(NewPage) then begin
LoadExits(PageId);
ExecuteActionsOfPage(PageId);
end;
end;
destructor TGameBase.Destroy;
begin
FreeAndNil(FItems);
FreeAndNil(FAttributes);
FreeAndNil(FAvailableExits);
FreeAndNil(FCurrentPage);
inherited Destroy;
end;
constructor TGame.Create(Dependencies: TGameDependencies);
begin
inherited Create;
Dao := Dependencies.Dao;
end;
procedure TGame.InitGame;
begin
LoadGameData;
LoadStartPage;
end;
procedure TGame.UseExit(ExitId: Integer);
var
Exit: TPageExit;
begin
Exit := FAvailableExits.FindById(ExitId);
if Assigned(Exit) then begin
LoadPage(Exit.PageId);
end;
end;
function TGame.CurrentPage: TPage;
begin
Result := FCurrentPage;
end;
function TGame.GameTitle: String;
begin
Result := FGameTitle;
end;
function TGame.AvailableExits: TPageExitList;
begin
Result := FAvailableExits;
end;
end.