not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.9 KiB

  1. function Game(activity,stage,xocolor,Fraction,doc,abacuspalette,custompalette,datastore){
  2. this.palette = null;
  3. this.custompalette = null;
  4. this.abacus = null;
  5. //rods top bottom factor base
  6. this.customarr = [15,1,4,5,10];
  7. this.abacustype = null;
  8. //Custom Abacus
  9. this.updateCustom = function(rods,top,bottom,factor,base){
  10. this.customarr[0] = rods;
  11. this.customarr[1] = top;
  12. this.customarr[2] = bottom;
  13. this.customarr[3] = factor;
  14. this.customarr[4] = base;
  15. }
  16. this.Custom = function(stage,xocolor){
  17. this.abacustype = 10;
  18. var c = this.customarr;
  19. this.abacus = new StandardAbacus(stage,c[0],c[1],c[3],c[2],c[4],xocolor);
  20. this.abacus.init();
  21. }
  22. //Inits for other abaci
  23. this.Decimal = function(stage,xocolor){
  24. this.abacustype = 0;
  25. this.abacus = new OneColumnAbacus(stage,15,10,10,xocolor);
  26. this.abacus.init();
  27. }
  28. this.Soroban = function(stage,xocolor){
  29. this.abacustype = 1;
  30. this.abacus = new StandardAbacus(stage,15,1,5,4,10,xocolor,8);
  31. this.abacus.init();
  32. }
  33. this.Suanpan = function(stage,xocolor){
  34. this.abacustype = 2;
  35. this.abacus = new StandardAbacus(stage,15,2,5,5,10,xocolor);
  36. this.abacus.init();
  37. }
  38. this.Nepohualtzintzin = function(stage,xocolor){
  39. this.abacustype = 3;
  40. this.abacus = new StandardAbacus(stage,13,3,5,4,20,xocolor);
  41. this.abacus.init();
  42. }
  43. this.Hexadecimal = function(stage,xocolor){
  44. this.abacustype = 4;
  45. this.abacus = new StandardAbacus(stage,15,1,8,7,16,xocolor);
  46. this.abacus.init();
  47. }
  48. this.Binary = function(stage,xocolor){
  49. this.abacustype = 5;
  50. this.abacus = new OneColumnAbacus(stage,15,1,2,xocolor);
  51. this.abacus.init();
  52. }
  53. this.Schety = function(stage,xocolor){
  54. this.abacustype = 6;
  55. this.abacus = new OneColumnAbacus(stage,15,10,10,xocolor,null,true);
  56. this.abacus.init();
  57. }
  58. this.Fractions = function(stage,xocolor){
  59. this.abacustype = 7;
  60. this.abacus = new OneColumnAbacus(stage,15,12,10,xocolor,null,false,true);
  61. this.abacus.init();
  62. }
  63. this.Caacupé = function(stage,xocolor){
  64. this.abacustype = 8;
  65. this.abacus = new OneColumnAbacus(stage,15,12,10,xocolor,null,false,false,true);
  66. this.abacus.init();
  67. }
  68. this.Rods = function(stage,xocolor){
  69. this.abacustype = 9;
  70. this.abacus = new OneColumnAbacus(stage,10,12,10,xocolor,null,false,false,false,true);
  71. this.abacus.init();
  72. }
  73. //Copy
  74. this.copy = function(){
  75. var text = "";
  76. console.log(this.abacus);
  77. if (this.abacus!=null){
  78. if (this.abacus.answertext!=null){
  79. if (this.abacus.answertext.text.length>0){
  80. text = this.abacus.answertext.text;
  81. if (text.indexOf('=') > -1){
  82. text = text.substring(text.indexOf('=')+2);
  83. }
  84. }
  85. }
  86. }
  87. function copyToClipboard(t) {
  88. var copyText = document.getElementById('copytext');
  89. copyText.value = t;
  90. copyText.select();
  91. try {
  92. document.execCommand('copy');
  93. } catch (err) {
  94. window.prompt("Copy to clipboard: ", t);
  95. }
  96. }
  97. copyToClipboard(text);
  98. }
  99. //Resize/clear
  100. this.resize = function(){
  101. var d = this.makeData();
  102. stage.removeAllChildren();
  103. this.customarr = d.customarr;
  104. this.initAbacus(d.mode);
  105. this.abacus.restore(d.abacusinuse);
  106. this.abacus.restoreTri(d.trix);
  107. }
  108. this.clear = function(){
  109. stage.removeAllChildren();
  110. this.initAbacus(this.abacustype);
  111. }
  112. //Save related things
  113. this.makeData = function(){
  114. var arr = {};
  115. arr.mode = this.abacustype;
  116. arr.customarr = this.customarr;
  117. arr.abacusinuse = this.abacus.save();
  118. arr.trix = this.abacus.saveTri();
  119. console.log("arr");
  120. console.log(arr);
  121. return arr;
  122. }
  123. this.stop = function(restart){
  124. if (restart === undefined) restart=false;
  125. var arr = this.makeData();
  126. console.log(arr);
  127. var js = JSON.stringify(arr);
  128. activity.getDatastoreObject().setDataAsText(js);
  129. if (restart == true){
  130. activity.getDatastoreObject().save(function(){
  131. location.reload();
  132. });
  133. } else {
  134. activity.getDatastoreObject().save(function(){
  135. activity.close();
  136. });
  137. }
  138. }
  139. //Load related things
  140. this.init = function(){
  141. console.log("init");
  142. console.log(activity.getDatastoreObject());
  143. activity.getDatastoreObject().getMetadata(this.init_canaccessdatastore.bind(this));
  144. }
  145. this.init_canaccessdatastore = function(error,mdata){
  146. console.log("datastore check");
  147. var d = new Date().getTime();
  148. if (Math.abs(d-mdata.creation_time)<2000){
  149. console.log("Time too short");
  150. this.initActivity(false,[]);
  151. } else {
  152. activity.getDatastoreObject().loadAsText(this.init_getdatastore.bind(this));
  153. }
  154. }
  155. this.init_getdatastore = function(error,metadata,data){
  156. if (error==null&&data!=null){
  157. data = JSON.parse(data);
  158. this.initActivity(true,data);
  159. } else {
  160. this.initActivity(false,[]);
  161. }
  162. }
  163. //Init related things
  164. this.initAbacus = function(abacus){
  165. stage.removeAllChildren();
  166. switch(abacus) {
  167. case 0:
  168. this.Decimal(stage,xocolor);
  169. break;
  170. case 1:
  171. this.Soroban(stage,xocolor);
  172. break;
  173. case 2:
  174. this.Suanpan(stage,xocolor);
  175. break;
  176. case 3:
  177. this.Nepohualtzintzin(stage,xocolor);
  178. break;
  179. case 4:
  180. this.Hexadecimal(stage,xocolor);
  181. break;
  182. case 5:
  183. this.Binary(stage,xocolor);
  184. break;
  185. case 6:
  186. this.Schety(stage,xocolor);
  187. break;
  188. case 7:
  189. this.Fractions(stage,xocolor);
  190. break;
  191. case 8:
  192. this.Caacupé(stage,xocolor);
  193. break;
  194. case 9:
  195. this.Rods(stage,xocolor);
  196. break;
  197. case 10:
  198. this.Custom(stage,xocolor);
  199. break;
  200. }
  201. }
  202. this.initActivity = function(isdata,data){
  203. console.log(isdata);
  204. console.log(data);
  205. window.Fraction = Fraction;
  206. this.palette = new abacuspalette.AbacusPalette(this,doc.getElementById('abacus-button'),undefined);
  207. this.custompalette = new custompalette.CustomPalette(this,doc.getElementById('settings-button'),undefined);
  208. //var a = new StandardAbacus(stage,15,2,5,5,10,xocolor);
  209. if (isdata){
  210. this.customarr = data.customarr;
  211. this.initAbacus(data.mode);
  212. this.abacus.restore(data.abacusinuse);
  213. this.abacus.restoreTri(data.trix);
  214. } else {
  215. this.Suanpan(stage,xocolor);
  216. }
  217. this.palette.setUsed();
  218. }
  219. }