1
1

ContentMoleculeAnnoy.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ///<reference path="../ContentMolecule.ts"/>
  2. class ContentMoleculeAnnoy extends ContentMolecule {
  3. protected say : Say | ((attacker : any, target : any, markers : Array<ContentNounSimple>) => Say);
  4. protected saidTimes = 0;
  5. public constructor (description : Say | ((attacker : any, target : any, markers : Array<ContentNounSimple>) => Say)) {
  6. super();
  7. this.say = description;
  8. ContentMoleculeAnnoy.MOLECULES.push(this);
  9. }
  10. public getSay(specificMolecule : ContentMolecule) {
  11. this.saidTimes++;
  12. if (this.say instanceof Say) {
  13. return this.say;
  14. } else {
  15. let combatAtom = <ContentAtomAnnoy>(specificMolecule.getAtoms()[0]);
  16. return this.say(combatAtom.annoyer, combatAtom.target, combatAtom.markers.getNouns());
  17. }
  18. }
  19. public getWeight () {
  20. // TODO: Check if this implementation is good enough.
  21. let weight = -this.saidTimes;
  22. this.atoms.forEach(value => {
  23. weight += value.getAtomPriority()
  24. });
  25. return weight;
  26. }
  27. protected static MOLECULES = [];
  28. /**
  29. * Searches for Combat molecules that describe the specificMolecule given. Assumes Atoms are of the Combat variety. Assumes there is always a single Atom.
  30. * @param specificMolecule
  31. */
  32. public static getSay (specificMolecule : ContentMolecule) {
  33. // Randomize the array to prevent the same message from showing up every time
  34. let many : Array<ContentMoleculeAnnoy> = new Shuffler(ContentMoleculeAnnoy.MOLECULES).getShuffled();
  35. // Order the array placing higher priority messages on top, but messages with same priority are still randomized
  36. many.sort((a, b) => {
  37. return b.getWeight() - a.getWeight()
  38. });
  39. let matches = specificMolecule.findMatches(many);
  40. if (matches.length == 0) {
  41. let atom = <ContentAtomAnnoy> specificMolecule.getAtoms()[0];
  42. Elements.CurrentTurnHandler.printAsError(
  43. new Say(
  44. "Unable to find match for this annoying passage. Attacker: ", atom.annoyer,
  45. ". Target: ", atom.target,
  46. ". Markers: ", ...(atom.markers.getNouns()),
  47. ". Please report for fixing."
  48. )
  49. );
  50. return new Say();
  51. } else {
  52. let finalSay = new Say();
  53. matches.forEach((idx : number) => {
  54. finalSay.add(ContentMoleculeAnnoy.MOLECULES[idx].getSay(specificMolecule));
  55. finalSay.add(" ");
  56. });
  57. return finalSay;
  58. }
  59. }
  60. }