Guest User

Untitled

a guest
Jun 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. /**
  2. * @title : AutoBuild.js
  3. *
  4. * @author : alogwe
  5. *
  6. * @desc : This script is included when any script includes libs/common/Config.js and calls Config.init().
  7. * If enabled, loads a threaded helper script that will monitor changes in character level and
  8. * upon level up detection, it will spend skill and stat points based on a configurable
  9. * character build template file located in libs/config/Builds/*.
  10. *
  11. * Any skill and stat points obtained as quest rewards are currently
  12. * invisible to this script and must be spent manually.
  13. *
  14. * @todo : Make this file "libs/config/Builds/README.txt"
  15. */
  16. js_strict(true);
  17.  
  18. if (!isIncluded("common/Cubing.js")) { include("common/Cubing.js"); };
  19. if (!isIncluded("common/Prototypes.js")) { include("common/Prototypes.js"); };
  20. if (!isIncluded("common/Runewords.js")) { include("common/Runewords.js"); };
  21.  
  22. var AutoBuild = new function AutoBuild () {
  23.  
  24. if (Config.AutoBuild.DebugMode) { Config.AutoBuild.Verbose = true; }
  25.  
  26. var debug = !!Config.AutoBuild.DebugMode,
  27. verbose = !!Config.AutoBuild.Verbose,
  28. configUpdateLevel = 0;
  29.  
  30.  
  31. // Apply all Update functions from the build template in order from level 1 to me.charlvl.
  32. // By reapplying all of the changes to the Config object, we preserve
  33. // the state of the Config file without altering the saved char config.
  34. function applyConfigUpdates () {
  35. if (debug) { this.print("Updating Config from level "+configUpdateLevel+" to "+me.charlvl)}
  36. while (configUpdateLevel < me.charlvl) {
  37. configUpdateLevel += 1;
  38. AutoBuildTemplate[configUpdateLevel].Update.apply(Config); // TODO: Make sure this works
  39. }
  40. };
  41.  
  42.  
  43. function getBuildType () {
  44. var build = Config.AutoBuild.Template;
  45. if (!build) {
  46. this.print("Config.AutoBuild.Template is either 'false', or invalid ("+build+")");
  47. throw new Error("Invalid build template, read libs/config/Builds/README.txt for information");
  48. }
  49. return build;
  50. };
  51.  
  52.  
  53. function getCurrentScript () {
  54. return getScript(true).name.toLowerCase();
  55. };
  56.  
  57.  
  58. function getLogFilename () {
  59. var d = new Date();
  60. var dateString = d.getMonth()+"_"+d.getDate()+"_"+d.getFullYear();
  61. return "logs/AutoBuild."+me.realm+"."+me.charname+"."+dateString+".log";
  62. };
  63.  
  64.  
  65. function getTemplateFilename () {
  66. var classname = ["Amazon", "Sorceress", "Necromancer", "Paladin", "Barbarian", "Druid", "Assassin"][me.classid];
  67. var build = getBuildType();
  68. var template = "config/Builds/"+classname+"."+build+".js";
  69. return template.toLowerCase();
  70. };
  71.  
  72.  
  73. function initialize () {
  74. var currentScript = getCurrentScript();
  75. var template = getTemplateFilename();
  76. this.print("Including build template "+template+" into "+currentScript);
  77. if (!include(template)) {
  78. throw new Error("Failed to include template: "+template);
  79. }
  80.  
  81. // Only load() helper thread from default.dbj if it isn't loaded
  82. if (currentScript === "default.dbj" && !getScript("tools\\autobuildthread.js")) {
  83. load("tools/autobuildthread.js");
  84. }
  85.  
  86. // All threads except autobuildthread.js use this event listener
  87. // to update their thread-local Config object
  88. if (currentScript !== "tools\\autobuildthread.js") {
  89. addEventListener("scriptmsg", levelUpHandler);
  90. }
  91.  
  92. // Resynchronize our Config object with all past changes
  93. // made to it by AutoBuild system
  94. applyConfigUpdates();
  95. };
  96.  
  97.  
  98. function levelUpHandler (obj) {
  99. if (typeof obj === "object" && obj.hasOwnProperty("event") && obj["event"] === "level up") {
  100. applyConfigUpdates();
  101. }
  102. };
  103.  
  104.  
  105. function log (message) { FileTools.appendText(getLogFilename(), message+"\n"); };
  106.  
  107.  
  108. // Only print to console from autobuildthread.js,
  109. // but log from all scripts
  110. function myPrint () {
  111. var args = Array.prototype.slice.call(arguments);
  112. args.unshift("AutoBuild:");
  113. var result = args.join(" ");
  114. if (verbose) { print.call(this, result); }
  115. if (debug) { log.call(this, result); }
  116. };
  117.  
  118.  
  119. this.print = myPrint;
  120. this.initialize = initialize;
  121. this.applyConfigUpdates = applyConfigUpdates;
  122.  
  123. };
Add Comment
Please, Sign In to add comment