loadsave.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Save Object
  2. // General container for all variables for a game in progress
  3. function ASavedGame()
  4. {
  5. // The player
  6. this.player = "";
  7. // some details on each visitable place
  8. this.places = "";
  9. // Demon
  10. this.demon = "";
  11. // Smith
  12. this.smith = "";
  13. // Runaways
  14. this.runaways = "";
  15. }
  16. // Load Games
  17. function loadGame(cancel)
  18. {
  19. var cnt = 0;
  20. var id;
  21. var str;
  22. var sg;
  23. for (id = 1; id < 5; id++) {
  24. str = localStorage.getItem('clani' + id);
  25. sg = JSON.parse(str);
  26. if (sg !== undefined) cnt++;
  27. }
  28. if (cnt === 0) return 0;
  29. $("#output").html("<h1>Loading a game</h1><div id='loading'></div>");
  30. for (id = 1; id < 5; id++) {
  31. str = localStorage.getItem('clani' + id);
  32. if (str !== "" && str !== null) {
  33. sg = JSON.parse(str);
  34. if (sg !== undefined) {
  35. cnt++;
  36. var temp = JSON.parse(sg.player);
  37. $("#loading").append("<button myid='" + id + "' id='load_button_"+id+"' class='btn btn-woman push--right' title='" + temp.name + ", week " + temp.round + "'>Save Game "+id+"</button>");
  38. $("#load_button_"+id).click(
  39. function() { loadGameId($(this).attr("myid")); }
  40. );
  41. }
  42. }
  43. }
  44. $("#loading").append("<button id='load_button_import' class='btn btn-woman push--right' title='Import'>Import</button>");
  45. $("#load_button_import").click(
  46. function() {
  47. $("#loading").append("<br><textarea onclick='this.focus()' id='importtxt' cols='40' rows='10''></textarea>");
  48. $("#loading").append("<br><button id='load_export' class='btn btn-woman push--right' title='Load'>Load</button>");
  49. $("#load_export").click(
  50. function() {
  51. var str = $('textarea#importtxt').val();
  52. loadGameString(str);
  53. }
  54. );
  55. }
  56. );
  57. $("#loading").append("<button id='load_button_cancel' class='btn btn-woman push--right' title='Cancel'>Cancel</button>");
  58. $("#load_button_cancel").click(
  59. function() {
  60. eval(cancel);
  61. }
  62. );
  63. return cnt;
  64. }
  65. function loadGameId(id)
  66. {
  67. var str = localStorage.getItem('clani' + id);
  68. loadGameString(str, id);
  69. }
  70. function loadGameString(str, id)
  71. {
  72. function loadAvatar(temp){
  73. var ret = new Avatar(50,95,0,75,80);
  74. jQuery.extend(ret,temp);
  75. ret.upgradeSave();
  76. return ret;
  77. };
  78. if (str === undefined) {
  79. alert("Save not found.");
  80. return;
  81. }
  82. createRival(0);
  83. var sg = JSON.parse(str);
  84. var temp = JSON.parse(sg.player);
  85. sg.characters=JSON.parse(sg.characters);
  86. jQuery.extend(player, temp);
  87. var rawchars = sg.characters;
  88. console.log(rawchars);
  89. for(var i in sg.characters){
  90. console.log(sg.characters[i]);
  91. sg.characters[i] = loadAvatar(sg.characters[i]);
  92. }
  93. player = sg.characters[sg.player];
  94. demon = sg.characters[sg.demon];
  95. smith = sg.characters[sg.smith];
  96. if(sg.stories){
  97. for(var s in sg.stories){
  98. if(activeStories[s]){
  99. activeStories[s].deserialize(sg.stories[s]);
  100. }
  101. }
  102. }
  103. sg.runaways = JSON.parse(sg.runaways);
  104. console.log(sg.characters);
  105. for(var i in sg.characters){
  106. sg.characters[i].restoreRefs(sg.characters);
  107. }
  108. runaways = sg.runaways.map(function(i){return sg.characters[i];});
  109. places = { };
  110. if (sg.places !== undefined) {
  111. var tempplaces = JSON.parse(sg.places);
  112. $.each(tempplaces, function(index, place) {
  113. places[index] = tempplaces[index];
  114. if (isNaN(places[index])) places[index] = 0;
  115. });
  116. }
  117. /*
  118. if (sg.round !== undefined) player.round = sg.round;
  119. player.loadGame(temp);
  120. runaways = [];
  121. if (sg.runaways !== undefined && sg.runaways.length > 0) {
  122. var temprunaways = JSON.parse(sg.runaways);
  123. if (temprunaways !== "") {
  124. $.each(temprunaways, function(index, person) {
  125. runaways[index] = new Avatar(50, 95, 90, 75, 80);
  126. jQuery.extend(runaways[index], temprunaways[index]);
  127. runaways[index].upgradeSave();
  128. });
  129. }
  130. }
  131. createDemon();
  132. if (sg.demon !== undefined) {
  133. var tempdemon = JSON.parse(sg.characters[sg.demon]);
  134. jQuery.extend(demon, tempdemon);
  135. demon.loadGame(tempdemon);
  136. }
  137. createSmith();
  138. if (sg.smith !== undefined) {
  139. var tempsmith = JSON.parse(sg.characters[sg.smith]);
  140. jQuery.extend(smith, tempsmith);
  141. smith.loadGame(tempsmith);
  142. } */
  143. resetRival();
  144. rival.name = "";
  145. console.log(sg.leader);
  146. if(sg.leader===undefined){
  147. leader=player;
  148. }else{
  149. leader=sg.characters[sg.leader];
  150. }
  151. if(sg.thoth){
  152. thoth = sg.characters[sg.thoth];
  153. }else{
  154. var res = sg.characters.filter(x=>x.maleName=="Thoth");
  155. if(res.length>0){
  156. thoth=res[0];
  157. }else{
  158. thoth=player.women[0];//Just making a guess for older saves.
  159. }
  160. }
  161. if (id === undefined) alert("Imported Game, week " + player.round);
  162. else alert("Loaded Game " + id + ", week " + player.round);
  163. ShowCamp();
  164. }
  165. // Save Games
  166. function saveGame(cancel)
  167. {
  168. $("#output").html("<h1>Saving a game</h1><div id='saving'></div>");
  169. for (var id = 1; id < 5; id++) {
  170. $("#saving").append("<button myid='" + id + "' id='save_button_"+id+"' class='btn btn-woman push--right'>Save Game "+id+"</button>");
  171. $("#save_button_"+id).click(
  172. function() { saveGameId($(this).attr("myid")); }
  173. );
  174. }
  175. $("#saving").append("<button id='save_button_export' class='btn btn-woman push--right' title='Export'>Export</button>");
  176. $("#save_button_export").click(
  177. function() {
  178. $("#saving").append("<br><textarea onclick='this.select()' cols='40' rows='10'>" + saveString() + "</textarea>");
  179. }
  180. );
  181. $("#saving").append("<button id='save_button_cancel' class='btn btn-woman push--right' title='Cancel'>Cancel</button>");
  182. $("#save_button_cancel").click(
  183. function() {
  184. eval(cancel);
  185. }
  186. );
  187. }
  188. function saveString()
  189. {
  190. var sg = new ASavedGame();
  191. var charTable = [player,demon,smith];
  192. sg.player = 0;//JSON.stringify(JSON.decycle(player));
  193. sg.demon = 1;//JSON.stringify(JSON.decycle(demon));
  194. sg.smith = 2;//JSON.stringify(JSON.decycle(smith));
  195. sg.places = JSON.stringify(JSON.decycle(places));
  196. sg.thoth=-1;
  197. charTable=charTable.concat(player.women);
  198. if(leader!==player){
  199. if(!player.women.includes(leader)){
  200. charTable.push(leader);
  201. }
  202. for(var w in leader.women){
  203. if(!charTable.includes(leader.women[w])){
  204. charTable.push(leader.women[w]);
  205. }
  206. }
  207. }
  208. charTable=charTable.concat(runaways);
  209. for(var c in charTable){
  210. if((thoth!==undefined)&&(charTable[c]===thoth)){
  211. sg.thoth=c;
  212. }
  213. delete charTable[c].currentAction;//the cause of so many circular references. useless, delete
  214. charTable[c].replaceRefs(charTable);
  215. }
  216. /*if(charTable.length>3){
  217. console.log(JSON.stringify(charTable[4].women,2));
  218. }*/
  219. sg.runaways = JSON.stringify(runaways.map(function(r){return charTable.indexOf(r);}));//JSON.decycle(runaways));
  220. sg.leader=charTable.indexOf(leader);
  221. isCyclic(charTable);
  222. sg.characters = JSON.stringify(charTable);
  223. sg.stories = {};
  224. for(var s in activeStories){
  225. sg.stories[s]=activeStories[s].serialize();
  226. }
  227. var ret =JSON.stringify(JSON.decycle(sg));
  228. for(var c in charTable){//easier to do in place, but need to restore after so we can keep playing.
  229. charTable[c].restoreRefs(charTable);
  230. }
  231. return ret;
  232. }
  233. function saveGameId(id)
  234. {
  235. var str = saveString();
  236. localStorage.setItem('clani' + id, str);
  237. alert("Saved.");
  238. resetRival();
  239. rival.name = "";
  240. ShowCamp();
  241. }