-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import Player from '@civ-clone/core-player/Player'; | ||
import PlayerAction from '@civ-clone/core-player/PlayerAction'; | ||
import PriorityValue from '@civ-clone/core-rule/Priority'; | ||
import Routine from '../Routine'; | ||
import Rule from '@civ-clone/core-rule/Rule'; | ||
export declare class Priority extends Rule<[Player, Routine], PriorityValue> {} | ||
import Strategy from '../Strategy'; | ||
export declare class Priority extends Rule< | ||
[PlayerAction, Strategy], | ||
PriorityValue | ||
> {} | ||
export default Priority; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import Player from '@civ-clone/core-player/Player'; | ||
import PlayerAction from '@civ-clone/core-player/PlayerAction'; | ||
import PriorityValue from '@civ-clone/core-rule/Priority'; | ||
import Routine from '../Routine'; | ||
import Rule from '@civ-clone/core-rule/Rule'; | ||
import Strategy from '../Strategy'; | ||
|
||
export class Priority extends Rule<[Player, Routine], PriorityValue> {} | ||
export class Priority extends Rule<[PlayerAction, Strategy], PriorityValue> {} | ||
|
||
export default Priority; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import Player from '@civ-clone/core-player/Player'; | ||
import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry'; | ||
import PlayerAction from '@civ-clone/core-player/PlayerAction'; | ||
import Priority from '@civ-clone/core-rule/Priority'; | ||
import Routine from './Routine'; | ||
export interface IStrategy { | ||
attempt(action: PlayerAction): boolean; | ||
priority(player: Player): Priority; | ||
priority(action: PlayerAction): Priority; | ||
} | ||
export declare class Strategy implements IStrategy { | ||
#private; | ||
constructor(...items: Routine[]); | ||
constructor(ruleRegistry?: RuleRegistry); | ||
/** | ||
* Checks to see if the `action` can be handled, returns `true` if it is, `false` otherwise. | ||
*/ | ||
attempt(action: PlayerAction): boolean; | ||
priority(player: Player): Priority; | ||
priority(action: PlayerAction): Priority; | ||
protected ruleRegistry(): RuleRegistry; | ||
} | ||
export default Strategy; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
import Player from '@civ-clone/core-player/Player'; | ||
import { | ||
RuleRegistry, | ||
instance as ruleRegistryInstance, | ||
} from '@civ-clone/core-rule/RuleRegistry'; | ||
import PlayerAction from '@civ-clone/core-player/PlayerAction'; | ||
import Priority from '@civ-clone/core-rule/Priority'; | ||
import Routine from './Routine'; | ||
import PriorityRule from './Rules/Priority'; | ||
|
||
export interface IStrategy { | ||
attempt(action: PlayerAction): boolean; | ||
priority(player: Player): Priority; | ||
priority(action: PlayerAction): Priority; | ||
} | ||
|
||
export class Strategy implements IStrategy { | ||
#routines: Routine[] = []; | ||
#ruleRegistry: RuleRegistry; | ||
|
||
constructor(...items: Routine[]) { | ||
items.forEach((item) => this.#routines.push(item)); | ||
constructor(ruleRegistry: RuleRegistry = ruleRegistryInstance) { | ||
this.#ruleRegistry = ruleRegistry; | ||
} | ||
|
||
/** | ||
* Checks to see if the `action` can be handled, returns `true` if it is, `false` otherwise. | ||
*/ | ||
attempt(action: PlayerAction): boolean { | ||
return this.#routines | ||
.sort( | ||
(a, b) => | ||
a.priority(action.player()).value() - | ||
b.priority(action.player()).value() | ||
) | ||
.some((routine) => routine.attempt(action)); | ||
throw new Error('This must be overwritten in the implementor.'); | ||
} | ||
|
||
priority(player: Player): Priority { | ||
priority(action: PlayerAction): Priority { | ||
return new Priority( | ||
// This takes the highest priority (lowest value) from all the applicable `PriorityRule`s | ||
Math.min( | ||
...this.#routines.map((routine) => routine.priority(player).value()), | ||
...this.#ruleRegistry | ||
.process(PriorityRule, action, this) | ||
.map((priority) => priority.value()), | ||
Infinity | ||
) | ||
); | ||
} | ||
|
||
protected ruleRegistry(): RuleRegistry { | ||
return this.#ruleRegistry; | ||
} | ||
} | ||
|
||
export default Strategy; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.