-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardAlignmentHelper.cs
520 lines (454 loc) · 20 KB
/
CardAlignmentHelper.cs
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Handelabra.Sentinels.Engine.Controller;
using Handelabra.Sentinels.Engine.Model;
using Handelabra;
namespace Jp.SOTMUtilities
{
// Enum representing card 'alignment' - hero/villain/environment.
// Also can represent nonhero/nonvillain/nonenvironment.
// Used for card text that says something like "the hero card..." or "the nonenvironment target..."
//
// The values are deliberately set up so that the bottom bit is on for the 'non' case.
// It's not a full bitflags thing.
public enum CardAlignment
{
Hero = 0,
Nonhero = 1,
Environment = 2,
Nonenvironment = 3,
Villain = 4,
Nonvillain = 5
};
// Enum representing card 'target-ness' - either a target, a non-target, or just a 'card'.
// Used for card text that says something like "the villain target..." (target) or
// "the environment card..." (either) or "an environment non-target" (nontarget)
public enum CardTarget
{
Either,
Target,
Nontarget
};
public class CardAlignmentHelperStorage
{
protected CardAlignmentHelperStorage()
{ }
protected CardAlignmentHelperStorage(CardAlignmentHelperStorage other)
{
CopyFrom(other);
}
public void CopyFrom(CardAlignmentHelperStorage other)
{
turntaker = other.turntaker;
card = other.card;
controller = other.controller;
alignment = other.alignment;
target = other.target;
character = other.character;
isCard = other.isCard;
expectedKeywords = other.expectedKeywords;
unwantedKeywords = other.unwantedKeywords;
}
// Indicates whether or not the Card/TurnTaker 'helper' was constructed on meets the requirements specified.
internal bool ConvertToBool()
{
if (alignment == CardAlignment.Villain && controller == null)
{
throw new InvalidOperationException("CardAlignmentHelper with villain alignment without controller converted to bool (Missing AccordingTo?)");
}
if (alignment == CardAlignment.Hero && controller == null)
{
throw new InvalidOperationException("CardAlignmentHelper with hero alignment without controller converted to bool (Missing AccordingTo?)");
}
if (expectedKeywords.Count() > 0 && controller == null)
{
throw new InvalidOperationException("CardAlignmentHelper with expected keywords but without controller converted to bool (Missing AccordingTo?)");
}
if (unwantedKeywords.Count() > 0 && controller == null)
{
throw new InvalidOperationException("CardAlignmentHelper with unwanted keywords but without controller converted to bool (Missing AccordingTo?)");
}
if (card != null)
{
if (isCard == false)
{
return false;
}
bool hasAlignment = true;
if (alignment.HasValue)
{
var baseAlignment = (CardAlignment)(((int)alignment.Value) & ~1);
switch (baseAlignment)
{
case CardAlignment.Hero:
if (target == CardTarget.Target)
{
var result = controller.GameController.AskOnlyCardControllersIfIsHeroTarget(card, controller.GetCardSource());
var kind = card.TargetKind ?? card.Kind;
var isHeroDefault = kind == DeckDefinition.DeckKind.Hero;
hasAlignment = result.GetValueOrDefault(isHeroDefault);
}
else { hasAlignment = controller.GameController.AskCardControllersIfIsHero(card, controller.GetCardSource()); }
break;
case CardAlignment.Villain:
if (target == CardTarget.Target)
{
var result = controller.GameController.AskOnlyCardControllersIfIsVillainTarget(card, controller.GetCardSource());
var kind = card.TargetKind ?? card.Kind;
var isVillainDefault = kind == DeckDefinition.DeckKind.Villain ||
kind == DeckDefinition.DeckKind.VillainTeam;
hasAlignment = result.GetValueOrDefault(isVillainDefault);
}
else { hasAlignment = controller.GameController.AskCardControllersIfIsVillain(card, controller.GetCardSource()); }
break;
case CardAlignment.Environment:
if (target == CardTarget.Target)
{
hasAlignment = (card.TargetKind ?? card.Kind) == DeckDefinition.DeckKind.Environment;
}
else { hasAlignment = card.IsEnvironment; }
break;
default:
hasAlignment = false;
break;
}
var isNonCase = (((int)alignment.Value) & 1) > 0;
if (isNonCase)
{
hasAlignment = !hasAlignment;
}
}
switch (target)
{
case CardTarget.Target: hasAlignment = hasAlignment && card.IsTarget; break;
case CardTarget.Nontarget: hasAlignment = hasAlignment && !card.IsTarget; break;
case CardTarget.Either:
default: break;
}
foreach (string keyword in expectedKeywords)
{
if (!controller.GameController.DoesCardContainKeyword(card, keyword))
{
return false;
}
}
foreach (string keyword in unwantedKeywords)
{
if (controller.GameController.DoesCardContainKeyword(card, keyword))
{
return false;
}
}
return hasAlignment && (character == null || character.Value == card.IsCharacter);
}
if (turntaker != null)
{
if (isCard == true)
{
return false;
}
if (target == CardTarget.Target)
{
return false;
}
if (expectedKeywords.Count() > 0 || unwantedKeywords.Count() > 0)
{
return false;
}
if (alignment.HasValue)
{
// CardAlignment enum is deliberately set up so that the bottom bit is 'non-'.
var baseAlignment = (CardAlignment)(((int)alignment) & ~1);
bool hasBaseAlignment = false;
switch (baseAlignment)
{
case CardAlignment.Hero:
hasBaseAlignment = controller.IsHero(turntaker);
break;
case CardAlignment.Villain:
hasBaseAlignment = controller.IsVillain(turntaker);
break;
case CardAlignment.Environment:
hasBaseAlignment = turntaker.IsEnvironment;
break;
default:
break;
}
var isNonCase = (((int)alignment) & 1) > 0;
if (isNonCase)
{
hasBaseAlignment = !hasBaseAlignment;
}
return hasBaseAlignment;
}
return true;
}
throw new InvalidOperationException("CardAlignmentHelper without card or turntaker converted to bool");
}
protected TurnTaker turntaker;
protected Card card;
protected CardController controller;
protected CardAlignment? alignment;
protected CardTarget target = CardTarget.Either;
protected bool? character = null;
protected bool? isCard = null;
protected List<string> expectedKeywords = new List<string>();
protected List<string> unwantedKeywords = new List<string>();
}
// Builder class for testing card/turntaker properties.
//
// Can indicate whether or not a card is:
// - A hero/villain/environment/nonhero/nonvillain/nonenvironment
// - A target/nontarget
// - A character/noncharacter
// - Has specific keywords
// - Doesn't have specific keywords
//
// Can indicate whether or not a turntaker is:
// - A hero/villain/environment/nonhero/nonvillain/nonenvironment
//
// This takes into account cards that have one alignment as a card
// and a different alignment as a target (e.g. Heroic Infinitor).
//
// It takes into account cards that change villain-ness of other cards.
//
// It takes into account cards that add keywords to other cards.
//
// If target-ness or keywords are specified turntakers always return false.
// This is mostly to support .Is() on DamageSources.
//
// If checking for villain-ness or keywords a CardController must be provided
// via .AccordingTo(). That's the CardController that wants to know - it's
// necessary for villain-ness so we can call AskCardControllersIfIsVillain.
// It's necessary for keyword-ness because we can't otherwise get to GameController
// to call DoesCardContainKeyword
public class CardAlignmentHelperBase<NoChangeType, NeedControllerType, ControllerSuppliedType> : CardAlignmentHelperStorage
where NoChangeType : CardAlignmentHelperStorage, new()
where NeedControllerType : CardAlignmentHelperStorage, new()
where ControllerSuppliedType : CardAlignmentHelperStorage, new()
{
// Isn't a TurnTaker.
public NoChangeType Card()
{
isCard = true;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is sourced from a TurnTaker. Excludes cards.
public NoChangeType Noncard()
{
isCard = false;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically a target. Excludes TurnTakers as well as non-target cards.
public NoChangeType Target()
{
target = CardTarget.Target;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically not a target. Either a TurnTaker or a non-target card.
public NoChangeType NonTarget()
{
target = CardTarget.Nontarget;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically a character card. Excludes TurnTakers.
public NoChangeType Character()
{
character = true;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically a non-character card. Excludes TurnTakers.
public NoChangeType Noncharacter()
{
character = false;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically Hero.
// If Target() is specified, a Hero target (uses targetKind). Otherwise, a Hero card or TurnTaker.
public NeedControllerType Hero()
{
alignment = CardAlignment.Hero;
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Is specifically Environment.
// If Target() is specified, an Environment target (uses targetKind). Otherwise an Environment card or TurnTaker.
public NoChangeType Environment()
{
alignment = CardAlignment.Environment;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically Villain.
// If Target() is specified, a Villain target (uses targetKind). Otherwise a Villain card or TurnTaker.
//
// Requires that a controller is passed via AccordingTo or the parameter to Is; that's the controller
// that wants to know whether a card is a villain. It gets passed to AskCardControllersIfIsVillain.
public NeedControllerType Villain()
{
alignment = CardAlignment.Villain;
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Is specifically non-hero.
// If Target() is specified, a non-hero target (uses targetKind). Otherwise a non-hero card or TurnTaker.
//
// Note that this doesn't call AskCardControllersIfIsVillain; this is consistent with Handleabra's usage.
// That might be because they only use that mechanism for Maze of Mirrors, which wouldn't affect this
// usage. It's not clear what the 'correct' behaviour would be.
public NeedControllerType NonHero()
{
alignment = CardAlignment.Nonhero;
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Is specifically non-environment.
// If Target() is specified, a non-environment target (uses targetKind). Otherwise a non-environment card or TurnTaker.
//
// Note that this doesn't call AskCardControllersIfIsVillain; this is consistent with Handleabra's usage.
// That might be because they only use that mechanism for Maze of Mirrors, which wouldn't affect this
// usage. It's not clear what the 'correct' behaviour would be.
public NoChangeType NonEnvironment()
{
alignment = CardAlignment.Nonenvironment;
var ret = new NoChangeType();
ret.CopyFrom(this);
return ret;
}
// Is specifically non-villain.
// If Target() is specified, a non-villain target (uses targetKind).
//
// Requires that a controller is passed via AccordingTo or the parameter to Is; that's the controller
// that wants to know whether a card is a villain. It gets passed to AskCardControllersIfIsVillain.
public NeedControllerType NonVillain()
{
alignment = CardAlignment.Nonvillain;
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Adds a keyword that the card must have (e.g., Ongoing).
// Excludes TurnTakers (as they don't have keywords).
//
// A CardController must be specified with AccordingTo or Is because of implementation limitations.
public NeedControllerType WithKeyword(string keyword)
{
expectedKeywords.Add(keyword);
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Adds a keyword that the card musn't have (i.e. "non-Citizen")
// Excludes TurnTakers.
//
// A CardController must be specified with AccordingTo or Is because of implementation limitations.
public NeedControllerType WithoutKeyword(string keyword)
{
unwantedKeywords.Add(keyword);
var ret = new NeedControllerType();
ret.CopyFrom(this);
return ret;
}
// Requires that the card is ongoing.
//
// Syntactic sugar for WithKeyword("ongoing")
public NeedControllerType Ongoing()
{
return WithKeyword("ongoing");
}
// Requires that the card is equipment.
//
// Syntactic sugar for WithKeyword("equipment")
public NeedControllerType Equipment()
{
return WithKeyword("equipment");
}
// Specifies a CardController that is doing the asking.
// This is necessary for Villain, NonVillain, WithKeyword and WithoutKeyword.
//
// For the villain checks this is because of the AskCardControllersIfIsVillain function,
// which needs to know which controller is doing the asking. This is used in the base game to implement
// Maze of Mirrors and probably won't work for any other use, but for the sake of future-proofing it's
// handled here.
//
// For the keyword checks it's just because we need to get a GameController somehow and we only have
// a Card; it's an implementation issue.
public ControllerSuppliedType AccordingTo(CardController _controller)
{
if (_controller == null)
throw new NullReferenceException("Null CardController supplied to AccordingTo()");
controller = _controller;
var ret = new ControllerSuppliedType();
ret.CopyFrom(this);
return ret;
}
};
public class CardAlignmentHelperWithController : CardAlignmentHelperBase<CardAlignmentHelperWithController, CardAlignmentHelperWithController, CardAlignmentHelperWithController>
{
public CardAlignmentHelperWithController() { }
internal CardAlignmentHelperWithController(Card _card, CardController _controller)
{
card = _card;
controller = _controller;
if (controller == null)
throw new NullReferenceException("Null CardController supplied to Is()");
}
internal CardAlignmentHelperWithController(TurnTaker _turntaker, CardController _controller)
{
turntaker = _turntaker;
controller = _controller;
if (controller == null)
throw new NullReferenceException("Null CardController supplied to Is()");
}
// Indicates whether or not the Card/TurnTaker 'helper' was constructed on meets the requirements specified.
public static implicit operator bool(CardAlignmentHelperWithController helper)
{
return helper.ConvertToBool();
}
};
public class CardAlignmentHelperNeedsController : CardAlignmentHelperBase<CardAlignmentHelperNeedsController, CardAlignmentHelperNeedsController, CardAlignmentHelperWithController>
{
// You have used a method on CardAlignmentHelper that requires you to provide a card controller with AccordingTo
[Obsolete("You have used a method on CardAlignmentHelper that requires providing a CardController with AccordingTo, but have not provided a controller", true)]
public static implicit operator bool(CardAlignmentHelperNeedsController helper)
{
throw new InvalidOperationException("Called unimplemented CardAlignmentHelperNeedsController.operator bool");
}
};
public class CardAlignmentHelperImpl : CardAlignmentHelperBase<CardAlignmentHelperImpl, CardAlignmentHelperNeedsController, CardAlignmentHelperWithController>
{
public CardAlignmentHelperImpl() { }
internal CardAlignmentHelperImpl(Card _card)
{
card = _card;
}
internal CardAlignmentHelperImpl(TurnTaker _turntaker)
{
turntaker = _turntaker;
}
// Indicates whether or not the Card/TurnTaker 'helper' was constructed on meets the requirements specified.
public static implicit operator bool(CardAlignmentHelperImpl helper)
{
return helper.ConvertToBool();
}
};
}