Rule.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /// <reference path="../Settings.ts" />
  2. interface RuleOptions<T> {
  3. name : string;
  4. firstPriority? : number;
  5. priority? : number;
  6. code : (runner? : RulebookRunner<T>) => (Promise<any> | any);
  7. conditions? : (runner? : RulebookRunner<T>) => (boolean);
  8. }
  9. class Rule {
  10. private _priority : number = 0;
  11. public firstPriority : number = 0;
  12. public name : string;
  13. private code : Function;
  14. private createdWhere : Error;
  15. private conditions : (rulebook? : RulebookRunner<any>) => (boolean);
  16. public constructor (options : RuleOptions<any>) {
  17. this.priority = options.priority != undefined ? options.priority : Rule.PRIORITY_MEDIUM;
  18. this.firstPriority = options.firstPriority != undefined ? options.firstPriority : Rule.PRIORITY_MEDIUM;
  19. this.name = options.name;
  20. this.code = options.code;
  21. this.createdWhere = (new Error());
  22. this.conditions = options.conditions != undefined ? options.conditions : () => { return true; };
  23. }
  24. public async execute (rulebook? : RulebookRunner<any>) : Promise<any> {
  25. if (!this.conditions(rulebook)) {
  26. return;
  27. }
  28. console.debug(Rulebook.getIndentation() + "[RULE] " + this.name);
  29. Settings.hardDebug && console.debug(this.name, this.createdWhere);
  30. Rulebook.increaseIndentation(this);
  31. rulebook.rule = this;
  32. let result = this.code(rulebook);
  33. // Was the function async?
  34. if (result instanceof Promise) {
  35. result = await result;
  36. }
  37. if (result != undefined) {
  38. console.debug(Rulebook.getIndentation() + "Result:", result);
  39. }
  40. Rulebook.decreaseIndentation();
  41. return result;
  42. }
  43. get priority(): number {
  44. return this._priority;
  45. }
  46. set priority(value: number) {
  47. this._priority = value;
  48. }
  49. public compareTo (b : Rule) {
  50. var a = this;
  51. // Higher priority goes first as this is a rolling array
  52. if (b.firstPriority < a.firstPriority) return -1;
  53. if (a.firstPriority < b.firstPriority) return 1;
  54. if (b.priority < a.priority) return -1;
  55. if (a.priority < b.priority) return 1;
  56. return 0;
  57. }
  58. public static PRIORITY_HIGHEST : number = 20;
  59. public static PRIORITY_HIGH : number = 15;
  60. public static PRIORITY_MEDIUM : number = 10;
  61. public static PRIORITY_LOW : number = 5;
  62. public static PRIORITY_LOWEST : number = 0;
  63. }
  64. // var goblinJumpingRule = new Rule({
  65. // name : "Goblin Jumping Rule",
  66. // code : async (rule : Rule, ruleResolver : Function) => {
  67. // console.debug("weee");
  68. // }
  69. // });
  70. //
  71. // goblinJumpingRule.execute();
  72. /**
  73. var goblinJumpingRule = new Rule({name : "Goblin Jumping Rule", code : (function () {
  74. return function () {
  75. // this is now the Rule and has access to this.currentRulebook
  76. // If there is no need for own module, it's okay to use a simple function
  77. // IF YOUR FUNCTION SHOULD NOT COMPLETE AUTOMATICALLY, IT NEEDS TO RETURN FALSE
  78. // FUNCTIONS THAT RETURN ANYTHING ELSE WILL AUTO COMPLETE AT THE END OF THEIR CODE
  79. // If your rule does not auto-complete, then it must complete manually, or the game will STALL.
  80. // If a rule must return either "true" or "false" to the rulebook, you need to set the rule's result that way:
  81. // this.result = true (or false)
  82. };
  83. })()});
  84. */