DialogueTrees.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. module DialogueTrees {
  2. // let trees : {[name : string] : DialogueTree} = {};
  3. //
  4. // export function addTree(tree : DialogueTree) {
  5. // trees[tree.id] = tree;
  6. // }
  7. /**
  8. * This prints information about usage of every DialogueTree available.
  9. * Note: Even though a DialogueTree is referenced, it *might* still not be accessible if the code leading to it is faulty.
  10. * This should be used to know whether or not a DialogueTree was "forgotten", since the IDE can only do this manually.
  11. */
  12. export function findUnusedTrees () {
  13. let allCode = document.getElementById("appCode").innerHTML;
  14. let useCount = {};
  15. let unused = [];
  16. for (let dialogueName in DialogueTrees) {
  17. if (dialogueName != "findUnusedTrees") {
  18. // The first mention will be the one defining it to exist, so we need at least two occurrences for it to be used.
  19. let count = occurrences(allCode, "DialogueTrees." + dialogueName, false) - 1;
  20. if (count > 0) {
  21. useCount[dialogueName] = count;
  22. } else {
  23. unused.push(dialogueName);
  24. }
  25. }
  26. }
  27. for (let dialogueName in useCount) {
  28. let times = useCount[dialogueName];
  29. console.debug("[DialogueUsage] " + dialogueName + " is referenced " + times + (times > 1 ? " times." : " time."));
  30. }
  31. for (let i = 0; i < unused.length; i++) {
  32. console.error("[DialogueUsage] " + unused[i] + " is never referenced and will not appear in-game.");
  33. }
  34. }
  35. /** Function that count occurrences of a substring in a string;
  36. * @param {String} string The string
  37. * @param {String} subString The sub string to search for
  38. * @param {Boolean} [allowOverlapping] Optional. (Default:false)
  39. *
  40. * @author Vitim.us https://gist.github.com/victornpb/7736865
  41. * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
  42. * @see http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
  43. */
  44. function occurrences(string, subString, allowOverlapping) {
  45. string += "";
  46. subString += "";
  47. if (subString.length <= 0) return (string.length + 1);
  48. var n = 0,
  49. pos = 0,
  50. step = allowOverlapping ? 1 : subString.length;
  51. while (true) {
  52. pos = string.indexOf(subString, pos);
  53. if (pos >= 0) {
  54. ++n;
  55. pos += step;
  56. } else break;
  57. }
  58. return n;
  59. }
  60. }