Say.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /// <reference path="../../World/Classes/Rulebook.ts" />
  2. /// <reference path="../../World/Classes/Rule.ts" />
  3. /// <reference path="Say/OneOf.ts" />
  4. /// <reference path="Say/SayImage.ts" />
  5. interface Printable {
  6. getPrintedName () : string;
  7. }
  8. interface PrintableElement {
  9. getPrintedElement () : Array<Element>;
  10. }
  11. interface SayNoun {
  12. say : Say;
  13. noun : any;
  14. }
  15. class SayableObject {}
  16. class Say {
  17. // TODO: Separate own sequence from processing queue. This way a Say with functions/other says can be reutilized with fresh values.
  18. public sequence : Array <Object> = [];
  19. public skipbreaks : boolean = false;
  20. public static LINE_BREAK : Object = new SayableObject();
  21. public static CONDITIONAL_LINE_BREAK : Object = new SayableObject();
  22. public static PARAGRAPH_BREAK : Object = new SayableObject();
  23. public static RUN_PARAGRAPH : Object = new SayableObject();
  24. public static RUN_PARAGRAPH_OFF : Object = new SayableObject();
  25. public static CENTERED : Object = new SayableObject();
  26. public static b : Object = new SayableObject();
  27. public static DO_PARAGRAPH_BREAK = new SayableObject();
  28. public static DO_LINE_BREAK = new SayableObject();
  29. public static COCK = new SayableObject();
  30. public static PUSSY = new SayableObject();
  31. private centered : boolean = false;
  32. public static Mention (target : Thing, uppercase = true) : Array<any> {
  33. if (target == WorldState.player) {
  34. return [new SayYou(uppercase)];
  35. } else {
  36. return [new SayThe(uppercase), target];
  37. }
  38. }
  39. public static YouThem (target : Thing, uppercase = true) {
  40. return Say.Mention(target, uppercase);
  41. }
  42. public static YourTheir (target : Thing, uppercase = true) {
  43. if (target == WorldState.player) {
  44. return [new SayYour(uppercase)];
  45. } else {
  46. return [new SayHisHersIts(target, uppercase)];
  47. }
  48. }
  49. public static Speak (speaker : Thing | string, ...message : Array<any>) {
  50. if (typeof speaker == "string") {
  51. return [new SayBold(speaker, ": "), ...message];
  52. } else {
  53. return [new SayBold(...Say.YouThem(speaker), ": "), ...message];
  54. }
  55. }
  56. public constructor (...objs) {
  57. this.add(...objs);
  58. }
  59. public add (...objs : Array<Say | OneOf | Object | Printable | string | number | String | ((say : Say) => string)>) {
  60. for (let i = 0; i < objs.length; i++) {
  61. if (Array.isArray(objs[i])) {
  62. this.sequence.push(...(<Array<any>> objs[i]));
  63. } else {
  64. this.sequence.push(objs[i]);
  65. }
  66. }
  67. return this;
  68. }
  69. public remove (...objs) {
  70. for (let i = 0; i < objs.length; i++) {
  71. let index = this.sequence.indexOf(objs[i]);
  72. if (index >= 0) {
  73. this.sequence.splice(index, 1);
  74. }
  75. }
  76. }
  77. public isEmpty () {
  78. return this.sequence.length < 1;
  79. }
  80. public paragraphs : Array<Array<Element | Text>>;
  81. public currentParagraph : Array<Element | Text>;
  82. public sequenceRunner : number;
  83. // TODO: Create a single function to get the element of anything
  84. public async getTextOf (index : number, seq : any) : Promise<string> {
  85. let elements = await this.getElementFor(index, seq);
  86. let div = document.createElement("div");
  87. for (let i = 0; i < elements.length; i++) {
  88. if (typeof elements[i] != "number") {
  89. div.appendChild(elements[i]);
  90. }
  91. }
  92. return div.innerText;
  93. }
  94. public doLineBreak (noDouble : boolean) {
  95. if (this.currentParagraph.length > 0 && !this.skipbreaks) {
  96. if (noDouble) {
  97. let lastElement = this.currentParagraph[this.currentParagraph.length - 1];
  98. if (lastElement != undefined && lastElement instanceof Element && lastElement.tagName == "BR") {
  99. return;
  100. }
  101. }
  102. let br = document.createElement("br");
  103. br.classList.add("linebreak");
  104. let ti = document.createElement("span");
  105. ti.classList.add("textIndenter");
  106. this.currentParagraph.push(br, ti);
  107. }
  108. }
  109. public doParagraphBreak () {
  110. if (this.currentParagraph.length > 0 && !this.skipbreaks) {
  111. this.paragraphs.push(this.currentParagraph);
  112. this.currentParagraph = [];
  113. }
  114. }
  115. public async getParagraphs () : Promise<Array<Array<Element|Text>>> {
  116. this.paragraphs = [];
  117. this.currentParagraph = [];
  118. this.skipbreaks = false;
  119. for (this.sequenceRunner = 0; this.sequenceRunner < this.sequence.length; this.sequenceRunner++) {
  120. let seq = this.sequence[this.sequenceRunner];
  121. if (seq instanceof OneOf) {
  122. seq = seq.getOne();
  123. }
  124. if (seq == Say.CENTERED) {
  125. this.setCentered(true);
  126. } else if (seq == Say.b) {
  127. let boldObjects = [];
  128. for (let i = this.sequenceRunner + 1; i < this.sequence.length; i++) {
  129. let candidate = this.sequenceRunner[i];
  130. if (candidate == Say.b) {
  131. this.sequence.splice(i, 1);
  132. break;
  133. } else {
  134. boldObjects.push(this.sequence.splice(i, 1));
  135. }
  136. }
  137. if (boldObjects.length > 0) {
  138. let bold = new SayBold(...boldObjects);
  139. this.sequence.splice(this.sequenceRunner + 1, 0, bold);
  140. }
  141. } else if (seq == Say.COCK) {
  142. if (HumanoidPenis != undefined) {
  143. let cock = HumanoidPenis.getSynonym();
  144. this.currentParagraph.push(document.createTextNode(cock))
  145. }
  146. } else if (seq == Say.PUSSY) {
  147. if (HumanoidVagina != undefined) {
  148. let vagina = HumanoidVagina.getSynonym();
  149. this.currentParagraph.push(document.createTextNode(vagina))
  150. }
  151. } else if (seq == Say.LINE_BREAK || seq == Say.CONDITIONAL_LINE_BREAK) {
  152. this.doLineBreak(seq == Say.CONDITIONAL_LINE_BREAK);
  153. } else if (seq == Say.PARAGRAPH_BREAK) {
  154. this.doParagraphBreak();
  155. } else if (seq == Say.RUN_PARAGRAPH) {
  156. this.skipbreaks = true;
  157. } else if (seq == Say.RUN_PARAGRAPH_OFF) {
  158. this.skipbreaks = false;
  159. } else if (typeof seq == "function") {
  160. let fObj = (<(s: Say) => any> seq)(this);
  161. if (Array.isArray(fObj)) {
  162. for (let k = fObj.length - 1; k >= 0; k--) {
  163. this.sequence.splice(this.sequenceRunner + 1, 0, fObj[k]);
  164. }
  165. } else if (fObj != undefined) {
  166. this.sequence.splice(this.sequenceRunner + 1, 0, fObj);
  167. }
  168. this.sequence.splice(this.sequenceRunner, 1);
  169. this.sequenceRunner--;
  170. } else if (seq.constructor == this.constructor) {
  171. for (let k = (<Say> seq).sequence.length - 1; k >= 0; k--) {
  172. this.sequence.splice(this.sequenceRunner + 1, 0, (<Say> seq).sequence[k]);
  173. }
  174. this.sequence.splice(this.sequenceRunner, 1);
  175. this.sequenceRunner--;
  176. } else if (seq != undefined) {
  177. let elements = await this.getElementFor(this.sequenceRunner, seq);
  178. for (let i = 0; i < elements.length; i++) {
  179. if (elements[i] === Say.DO_LINE_BREAK) {
  180. this.doLineBreak(false);
  181. } else if (elements[i] === Say.DO_PARAGRAPH_BREAK) {
  182. this.doParagraphBreak();
  183. } else {
  184. this.currentParagraph.push(elements[i]);
  185. }
  186. }
  187. }
  188. }
  189. // TODO: Remove line break + text indenter if they are the last in the say
  190. if (this.currentParagraph.length > 0) {
  191. this.paragraphs.push(this.currentParagraph);
  192. }
  193. return this.paragraphs;
  194. }
  195. /**
  196. * Lord Have mercy, I wish to never have to debug this piece of god.
  197. * @param {number} index
  198. * @param {Say | OneOf | Object | Printable | string | number | ((say: Say) => string) | ((say: Say) => Promise<string>) | Element | Text} obj
  199. * @returns {Promise<Array<Element | Text>>}
  200. */
  201. public async getElementFor (index : number, obj : Say | OneOf | Object | Printable | string | number | String | ((say : Say) => string) | ((say : Say) => Promise<string>) | Element | Text) : Promise<Array<Element|Text>> {
  202. if (obj instanceof Promise) {
  203. obj = await obj;
  204. }
  205. if (typeof obj == "string" || obj instanceof String) {
  206. return [document.createTextNode(<string> obj)];
  207. } else if (typeof obj == "number" || obj instanceof Number) {
  208. return [document.createTextNode((parseFloat((<number> obj).toFixed(2))/1).toString())];
  209. } else if (typeof obj == "function") {
  210. let elements = await this.getElementFor(-1, (<any> obj)(this));
  211. return elements;
  212. } else if (obj instanceof SayImage) {
  213. return [obj.getImageElement()];
  214. } else if (obj instanceof SayLeftRight) {
  215. return (await obj.getPureElements());
  216. } else if (obj instanceof Say) {
  217. let elements = await obj.getPureElements(this);
  218. return elements;
  219. } else if (this.isProperElement(obj)) {
  220. return [<Element> obj];
  221. } else if (obj instanceof Object) {
  222. let elements = await this.printName(obj);
  223. return elements;
  224. }
  225. }
  226. public async getPureElements (say? : Say) : Promise<Array<Element | Text>> {
  227. let paragraphs = await this.getParagraphs();
  228. return paragraphs.length == 1 ? paragraphs[0] : Array.prototype.concat.apply([], paragraphs);
  229. }
  230. public setCentered (bool : boolean) {
  231. this.centered = bool;
  232. }
  233. public async getHTML (tagName : string, classList : Array<string>, singleParagraph? : boolean) : Promise<Array<HTMLElement>> {
  234. let paragraphs = await this.getParagraphs();
  235. // Reduce to single paragraph
  236. if (singleParagraph == true && paragraphs.length > 1) {
  237. paragraphs = [Array.prototype.concat.apply([], paragraphs)];
  238. }
  239. let elements = [];
  240. for (let i = 0, paragraph = paragraphs[i]; paragraph != undefined; paragraph = paragraphs[++i]) {
  241. let parent = <HTMLElement> document.createElement(tagName);
  242. if (classList.length > 0) {
  243. parent.classList.add(...classList);
  244. }
  245. for (let k = 0, ele = paragraph[k]; ele!= undefined; ele = paragraph[++k]) {
  246. parent.appendChild(ele);
  247. }
  248. elements.push(parent);
  249. if (this.centered) {
  250. parent.classList.add("centered");
  251. }
  252. }
  253. return elements;
  254. }
  255. public getHTMLContent () : Promise<Array<HTMLElement>> {
  256. return this.getHTML("p", ["content"]);
  257. }
  258. public isProperElement (o) : boolean {
  259. return (
  260. typeof Node === "object" ? o instanceof Node :
  261. o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName==="string"
  262. ) || (
  263. typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
  264. o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName==="string"
  265. );
  266. }
  267. public static beforePrinting = new Rulebook<any>("Before printing the name of something");
  268. public static printing = new Rulebook<any>("Printing the name of something");
  269. public static afterPrinting = new Rulebook<any>("After printing the name of something");
  270. public currentNoun : any;
  271. public currentNounElements : Array<Element | Text>;
  272. public async printName (thing : any) : Promise<Array<Element | Text>> {
  273. this.currentNoun = thing;
  274. this.currentNounElements = [];
  275. let before = Say.beforePrinting.execute({noun : this});
  276. await before;
  277. let print = Say.printing.execute({noun : this});
  278. await print;
  279. let after = Say.afterPrinting.execute({noun : this});
  280. await after;
  281. return this.currentNounElements;
  282. }
  283. public static hisHersIts (target : Thing, startOfSentence? : boolean) {
  284. return new SayHisHersIts(target);
  285. // let result : String;
  286. // if (target == WorldState.player) {
  287. // result = "your";
  288. // } else if (target instanceof Person) {
  289. // // TODO: Figure out whether target is male or female
  290. // result = "his";
  291. // } else {
  292. // result = "its";
  293. // }
  294. // if (startOfSentence == true) {
  295. // result = result.charAt(0).toUpperCase() + result.substr(1, result.length -1);
  296. // }
  297. // return result;
  298. }
  299. }
  300. Say.printing.addRule(new Rule({
  301. name : "Printing the name of a Printable Element",
  302. firstPriority : Rule.PRIORITY_LOW,
  303. code : (rulebook : RulebookRunner<any>) => {
  304. let say = <Say> rulebook.noun;
  305. if ((<any> say.currentNoun).getPrintedElement) {
  306. say.currentNounElements.push(...(<PrintableElement> say.currentNoun).getPrintedElement());
  307. return true; // We only want to print something once, so return true to stop others from running
  308. }
  309. }
  310. }));
  311. Say.printing.addRule(new Rule({
  312. name : "Printing the name of a Printable",
  313. firstPriority : Rule.PRIORITY_LOW,
  314. code : (rulebook : RulebookRunner<any>) => {
  315. let say = <Say> rulebook.noun;
  316. if ((<any> say.currentNoun).getPrintedName) {
  317. let thingEle = document.createTextNode(
  318. (<Printable> say.currentNoun).getPrintedName()
  319. );
  320. say.currentNounElements.push(thingEle);
  321. return true; // We only want to print something once, so return true to stop others from running
  322. }
  323. }
  324. }));
  325. Say.printing.addRule(new Rule({
  326. name : "Printing the name of an unknown object",
  327. firstPriority : Rule.PRIORITY_LOWEST,
  328. priority : Rule.PRIORITY_LOWEST,
  329. code : (rulebook : RulebookRunner<any>) => {
  330. let say = <Say> rulebook.noun;
  331. if ((<any> say.currentNoun).getPrintedName) {
  332. say.currentNounElements.push(
  333. (document.createTextNode((<Object> say.currentNoun).toString()))
  334. );
  335. return true; // We only want to print something once, so return true to stop others from running
  336. }
  337. }
  338. }));
  339. // var msg = new Say ("Hello! Welcome to The Obelisk! This is a game with ", johnTheOgre, " so you must be careful!");
  340. //
  341. // var otherSay = new Say (msg, "Will have to learn how to handle dots.");