Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.83 KB | None | 0 0
  1. ///<reference path="Place.ts"/>
  2. ///<reference path="main.ts"/>
  3. var Save = (function (_super) {
  4. __extends(Save, _super);
  5. // Constructor
  6. function Save(game) {
  7. _super.call(this, game);
  8. // The render area
  9. this.renderArea = new RenderArea();
  10. // The currently selected slot (slot1 by default)
  11. this.selectedSlot = "slot1";
  12. // The last local autosave minute we drew (used to refresh the page at the right time)
  13. this.lastLocalAutosaveMinute = null;
  14. // Should we show the file save warning?
  15. this.showFileSaveWarning = false;
  16. // The textarea content
  17. this.fileSaveTextareaContent = null;
  18. // Resize
  19. this.resize();
  20. // Create the slots array
  21. this.createSlotsArray();
  22. // Update
  23. this.update();
  24. }
  25. // getRenderArea()
  26. Save.prototype.getRenderArea = function () {
  27. return this.renderArea;
  28. };
  29. // willBeDisplayed()
  30. Save.prototype.willBeDisplayed = function () {
  31. // We resize (we must do this there and not in the constructor because the size depends on the translation)
  32. this.resize();
  33. // We add a callback : we will be updated every second (in order to show the correct time for the local autosave countdown)
  34. this.getGame().getOneSecondCallbackCollection().addCallback(this.oneSecondCallback.bind(this));
  35. };
  36. // Private methods
  37. Save.prototype.clickedAutosave = function () {
  38. // Save on the selected slot
  39. Saving.save(this.getGame(), MainLoadingType.LOCAL, this.selectedSlot);
  40. // Enable autosaving
  41. this.getGame().enableLocalAutosave(this.selectedSlot);
  42. // Re-create the slots array
  43. this.createSlotsArray();
  44. // Update
  45. this.update();
  46. this.getGame().updatePlace();
  47. };
  48. Save.prototype.clickedDisableAutosave = function () {
  49. // We disable auto saving
  50. this.getGame().disableLocalAutosave();
  51. // We update
  52. this.update();
  53. this.getGame().updatePlace();
  54. };
  55. Save.prototype.clickedFileLoad = function () {
  56. Main.reloadEverythingFromFile($(".saveFileLoadTextarea").val());
  57. };
  58. Save.prototype.clickedFileSave = function () {
  59. // Save some special variables by calling the save() methods of various objects
  60. this.getGame().save(); // Various variables owned by the game object
  61. this.getGame().getPlayer().save(); // The player
  62. // We now show the warning
  63. this.showFileSaveWarning = true;
  64. // Reset the textarea content
  65. this.fileSaveTextareaContent = "";
  66. // Write bools
  67. for (var str in Saving.getAllBools()) {
  68. if (this.fileSaveTextareaContent != "")
  69. this.fileSaveTextareaContent += ", "; // We add a comma if we're not adding the very first variable
  70. this.fileSaveTextareaContent += "bool " + str + "=" + Saving.boolToString(Saving.getAllBools()[str]);
  71. }
  72. // Write numbers
  73. for (var str in Saving.getAllNumbers()) {
  74. if (this.fileSaveTextareaContent != "")
  75. this.fileSaveTextareaContent += ", "; // We add a comma if we're not adding the very first variable
  76. this.fileSaveTextareaContent += "number " + str + "=" + Saving.numberToString(Saving.getAllNumbers()[str]);
  77. }
  78. // Write strings
  79. for (var str in Saving.getAllStrings()) {
  80. if (this.fileSaveTextareaContent != "")
  81. this.fileSaveTextareaContent += ", "; // We add a comma if we're not adding the very first variable
  82. this.fileSaveTextareaContent += "string " + str + "=" + Saving.getAllStrings()[str];
  83. }
  84. // Update
  85. this.update();
  86. this.getGame().updatePlace();
  87. };
  88. Save.prototype.clickedSave = function () {
  89. // Save on the selected slot
  90. Saving.save(this.getGame(), MainLoadingType.LOCAL, this.selectedSlot);
  91. // Re-create the slots array
  92. this.createSlotsArray();
  93. // Update
  94. this.update();
  95. this.getGame().updatePlace();
  96. };
  97. Save.prototype.createSlotsArray = function () {
  98. // Reset the array
  99. this.slotsArray = [];
  100. // Fill it
  101. for (var i = 1; i <= 5; i++) {
  102. this.slotsArray.push("slot" + i.toString());
  103. this.slotsArray.push("Slot " + i.toString() + " (" + LocalSaving.getSlotSummaryAsString("slot" + i.toString()) + ")");
  104. }
  105. };
  106. Save.prototype.drawGreen = function (text, x, y, translated) {
  107. if (translated === void 0) { translated = false; }
  108. this.renderArea.drawString(text, x, y, translated);
  109. this.renderArea.addColor(x, x + text.length, y, new Color(ColorType.SAVE_GREEN));
  110. };
  111. Save.prototype.drawLocalLoad = function (x, y) {
  112. // The y we will return (will remain 0 if there's no translation to show)
  113. var yAdd = 0;
  114. // Var we will use for link generation
  115. var link;
  116. // Title
  117. this.drawTitle("saveLocalLoadTitle", y + yAdd);
  118. // If we support local saving
  119. if (LocalSaving.supportsLocalSaving()) {
  120. // "You can load.."
  121. this.drawPoint("saveLocalLoadYouCan", x, y + yAdd + 2);
  122. if (Database.isTranslated())
  123. yAdd += 1;
  124. // The links
  125. for (var i = 1; i <= 5; i++) {
  126. link = "http://candybox2.net/?slot=" + i.toString();
  127. this.renderArea.addHtmlLink(x + 2, y + yAdd + 3 + i, link, link);
  128. this.renderArea.drawString("(slot " + i.toString() + ")", x + link.length + 4, y + yAdd + 3 + i);
  129. }
  130. // "Thanks to.."
  131. this.drawPoint("saveLocalLoadThanksTo", x, y + yAdd + 10);
  132. if (Database.isTranslated())
  133. yAdd += 1;
  134. }
  135. else {
  136. // Warning messages
  137. this.drawWarning(Database.getText("saveLocalSaveWarning0") + " (local storage and application cache)", x, y + yAdd + 2);
  138. this.drawWarning(Database.getText("saveLocalSaveWarning1"), x, y + yAdd + 3);
  139. this.drawWarning(Database.getTranslatedText("saveLocalSaveWarning0"), x, y + yAdd + 5, true);
  140. this.drawWarning(Database.getTranslatedText("saveLocalSaveWarning1"), x, y + yAdd + 6, true);
  141. }
  142. // Return yAdd
  143. return yAdd;
  144. };
  145. Save.prototype.drawLocalSave = function (x, y) {
  146. // The y we will return (will remain 0 if there's no translation to show)
  147. var yAdd = 0;
  148. // Title & why
  149. this.drawTitle("saveLocalSaveTitle", y + yAdd);
  150. this.drawPoint("saveLocalSaveWhy", x, y + yAdd + 2);
  151. if (Database.isTranslated())
  152. yAdd += 1;
  153. // If we support local saving
  154. if (LocalSaving.supportsLocalSaving()) {
  155. // Choose a slot text
  156. this.drawPoint("saveLocalSaveChooseSlot", x, y + yAdd + 4);
  157. // Slots list
  158. this.renderArea.addList(x + 5, x + 45, y + yAdd + 7, "saveLocalSaveSlotsList", new CallbackCollection(this.slotSelected.bind(this)), this.slotsArray);
  159. // Autosave enabled ?
  160. if (this.getGame().getLocalAutosaveEnabled()) {
  161. this.drawGreen(Database.getText("saveLocalSaveAutosaveEnabled"), x, y + yAdd + 9);
  162. if (Database.getTranslatedText("saveLocalSaveAutosaveEnabled") != "")
  163. this.drawGreen("(" + Database.getTranslatedText("saveLocalSaveAutosaveEnabled") + ")", x, y + yAdd + 10, true);
  164. this.drawGreen("Next save in " + Algo.pluralFormat(Math.ceil(this.getGame().getLocalAutosaveTime() / 60), " minute", " minutes") + " on slot " + this.getGame().getLocalAutosaveSlot().substr(4, 1) + ".", x, y + yAdd + 11);
  165. }
  166. // Separation lines
  167. this.renderArea.drawVerticalLine("|", x + 50, y + yAdd + 5, y + yAdd + 11);
  168. this.renderArea.drawHorizontalLine("-", x, x + 100, y + yAdd + 3);
  169. this.renderArea.drawHorizontalLine("-", x, x + 100, y + yAdd + 12);
  170. // Choose what to do text
  171. this.drawPoint("saveLocalSaveChooseWhatToDo", x + 51, y + yAdd + 4);
  172. // Add save button
  173. this.renderArea.addAsciiRealButton(Database.getText("saveLocalSaveSaveButton") + " on slot " + this.selectedSlot.substr(4, 1), x + 51, y + yAdd + 7, "saveLocalSaveSaveButton", Database.getTranslatedText("saveLocalSaveSaveButton"), true, -1, null, false);
  174. this.renderArea.addLinkCall(".saveLocalSaveSaveButton", new CallbackCollection(this.clickedSave.bind(this)));
  175. // If autosave is disabled or it's not enabled on the currently selected slot
  176. if (this.getGame().getLocalAutosaveEnabled() == false) {
  177. // Add autosave button
  178. this.renderArea.addAsciiRealButton(Database.getText("saveLocalSaveAutosaveButton") + " on slot " + this.selectedSlot.substr(4, 1), x + 51, y + yAdd + 10, "saveLocalSaveAutosaveButton", Database.getTranslatedText("saveLocalSaveAutosaveButton"), true, -1, null, false);
  179. this.renderArea.addLinkCall(".saveLocalSaveAutosaveButton", new CallbackCollection(this.clickedAutosave.bind(this)));
  180. }
  181. else {
  182. // Add disable autosave button
  183. this.renderArea.addAsciiRealButton(Database.getText("saveLocalSaveDisableAutosaveButton"), x + 51, y + yAdd + 10, "saveLocalSaveDisableAutosaveButton", Database.getTranslatedText("saveLocalSaveDisableAutosaveButton"), true, -1, null, false);
  184. this.renderArea.addLinkCall(".saveLocalSaveDisableAutosaveButton", new CallbackCollection(this.clickedDisableAutosave.bind(this)));
  185. }
  186. }
  187. else {
  188. // Warning messages
  189. this.drawWarning(Database.getText("saveLocalSaveWarning0") + " (local storage and application cache)", x, y + yAdd + 4);
  190. this.drawWarning(Database.getText("saveLocalSaveWarning1"), x, y + yAdd + 5);
  191. this.drawWarning(Database.getTranslatedText("saveLocalSaveWarning0"), x, y + yAdd + 7, true);
  192. this.drawWarning(Database.getTranslatedText("saveLocalSaveWarning1"), x, y + yAdd + 8, true);
  193. }
  194. // Return yAdd
  195. return yAdd;
  196. };
  197. Save.prototype.drawFileLoad = function (x, y) {
  198. // The y we will return (will remain 0 if there's no translation to show)
  199. var yAdd = 0;
  200. // Title
  201. this.drawTitle("saveFileLoadTitle", y + yAdd);
  202. // Instructions
  203. this.renderArea.drawString(Database.getText("saveFileLoadPaste"), x, y + yAdd + 2);
  204. this.renderArea.drawString(Database.getTranslatedText("saveFileLoadPaste"), x, y + yAdd + 3, true);
  205. // Add the text area
  206. this.renderArea.addTextarea(x + 2, y + yAdd + 5, 96, 6, "saveFileLoadTextarea");
  207. // Add the load button
  208. this.renderArea.addAsciiRealButton(Database.getText("saveFileLoadButton"), 48, y + yAdd + 13, "saveFileLoadButton", Database.getTranslatedText("saveFileLoadButton"), true);
  209. this.renderArea.addLinkCall(".saveFileLoadButton", new CallbackCollection(this.clickedFileLoad.bind(this)));
  210. // Return yAdd
  211. return yAdd;
  212. };
  213. Save.prototype.drawFileSave = function (x, y) {
  214. // The y we will return (will remain 0 if there's no translation to show)
  215. var yAdd = 0;
  216. // The title
  217. this.drawTitle("saveFileSaveTitle", y + yAdd);
  218. // The "why"
  219. this.renderArea.drawString(Database.getText("saveFileSaveWhy0"), x, y + yAdd + 2);
  220. this.renderArea.drawString(" - " + Database.getText("saveFileSaveWhy1"), x, y + yAdd + 3);
  221. this.renderArea.drawString(" - " + Database.getText("saveFileSaveWhy2"), x, y + yAdd + 4);
  222. this.renderArea.drawString(" - " + Database.getText("saveFileSaveWhy3"), x, y + yAdd + 5);
  223. this.renderArea.drawString(" " + Database.getText("saveFileSaveWhy4"), x, y + yAdd + 6);
  224. // The translated "why" (only if there's a translation)
  225. if (Database.isTranslated()) {
  226. this.renderArea.drawString(Database.getTranslatedText("saveFileSaveWhy0"), x, y + yAdd + 8, true);
  227. this.renderArea.drawString(" - " + Database.getTranslatedText("saveFileSaveWhy1"), x, y + yAdd + 9, true);
  228. this.renderArea.drawString(" - " + Database.getTranslatedText("saveFileSaveWhy2"), x, y + yAdd + 10, true);
  229. this.renderArea.drawString(" - " + Database.getTranslatedText("saveFileSaveWhy3"), x, y + yAdd + 11, true);
  230. this.renderArea.drawString(" " + Database.getTranslatedText("saveFileSaveWhy4"), x, y + yAdd + 12, true);
  231. yAdd += 6; // We increase yAdd by 6 because the translations took 6 lines
  232. }
  233. // Add the button
  234. this.renderArea.addAsciiRealButton(Database.getText("saveFileSaveButton"), 35, y + yAdd + 8, "saveFileSaveButton", Database.getTranslatedText("saveFileSaveButton"), true);
  235. this.renderArea.addLinkCall(".saveFileSaveButton", new CallbackCollection(this.clickedFileSave.bind(this)));
  236. // Add the text area
  237. this.renderArea.addTextarea(x + 2, y + yAdd + 11, 96, 6, "saveFileSaveTextarea", (this.fileSaveTextareaContent != null ? this.fileSaveTextareaContent : ""));
  238. // Should we show the warning?
  239. if (this.showFileSaveWarning) {
  240. this.drawWarning(Database.getText("saveFileSaveWarning"), x + 2, y + yAdd + 12);
  241. this.drawWarning(Database.getTranslatedText("saveFileSaveWarning"), x + 2, y + yAdd + 13, true);
  242. }
  243. // We return yAdd
  244. return yAdd;
  245. };
  246. Save.prototype.drawPoint = function (textName, x, y) {
  247. this.renderArea.drawString(Database.getText(textName), x, y);
  248. this.renderArea.drawString(Database.getTranslatedText(textName), x, y + 1, true);
  249. };
  250. Save.prototype.drawTitle = function (textName, y) {
  251. var x = 50 - Math.floor((Database.getText(textName).length / 2 + 1 + Database.getTranslatedText(textName).length / 2));
  252. this.renderArea.drawString(Database.getText(textName), x, y);
  253. this.renderArea.addBold(x, x + Database.getText(textName).length, y);
  254. this.renderArea.drawString(Database.getTranslatedText(textName), x + Database.getText(textName).length + 1, y, true);
  255. };
  256. Save.prototype.drawWarning = function (text, x, y, translated) {
  257. if (translated === void 0) { translated = false; }
  258. if (text != "") {
  259. this.renderArea.drawString(text, x, y, translated);
  260. this.renderArea.addColor(x, x + text.length, y, new Color(ColorType.SAVE_RED));
  261. }
  262. };
  263. Save.prototype.oneSecondCallback = function () {
  264. // If there's no last minute or it's different from the current minute
  265. if (this.lastLocalAutosaveMinute == null || this.lastLocalAutosaveMinute != Math.ceil(this.getGame().getLocalAutosaveTime() / 60)) {
  266. // We set the minute
  267. this.lastLocalAutosaveMinute = Math.ceil(this.getGame().getLocalAutosaveTime() / 60);
  268. // We update
  269. this.createSlotsArray();
  270. this.update();
  271. this.getGame().updatePlace();
  272. }
  273. };
  274. Save.prototype.resize = function () {
  275. // The size depends on if there's a translation or not
  276. if (Database.isTranslated())
  277. this.renderArea.resize(100, 84);
  278. else
  279. this.renderArea.resize(100, 74);
  280. };
  281. Save.prototype.selectRightSlot = function () {
  282. // We select the right slot
  283. $("#" + this.selectedSlot).prop('selected', true);
  284. };
  285. Save.prototype.slotSelected = function () {
  286. // Get the selected language id
  287. this.selectedSlot = $("#saveLocalSaveSlotsList").find(":selected").attr("id");
  288. // Update the ligthouse
  289. this.update();
  290. this.getGame().updatePlace();
  291. };
  292. Save.prototype.update = function () {
  293. var yPosition = 0; // The y position where we will add things. Can be incremented sometimes, depending on the user's language..
  294. // Erase everything
  295. this.renderArea.resetAllButSize();
  296. // Saving
  297. this.renderArea.drawArray(Database.getAscii("text/Saving"), 50 - Math.floor((Database.getAsciiWidth("text/Saving") / 2)), yPosition);
  298. yPosition += this.drawLocalSave(0, yPosition + 7);
  299. yPosition += this.drawFileSave(0, yPosition + 21);
  300. // Loading
  301. this.renderArea.drawArray(Database.getAscii("text/Loading"), 50 - Math.floor((Database.getAsciiWidth("text/Loading") / 2)), yPosition + 40);
  302. yPosition += this.drawLocalLoad(0, yPosition + 47);
  303. yPosition += this.drawFileLoad(0, yPosition + 59);
  304. // Add the link which will call the selectRightSlot method after the html dom is created
  305. this.renderArea.addLinkCallbackCollection(new CallbackCollection(this.selectRightSlot.bind(this)));
  306. };
  307. return Save;
  308. })(Place);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement