svg.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. var transition = {pid: '', port: '8080' };
  2. // BERT Protocol
  3. var players = ["Gabrielo","Mustafa","Alina","Me"];
  4. function handle_web_socket(body) {
  5. // console.log(dec(body).value[0][2].value);
  6. switch (dec(body).value[0][2].value) {
  7. case 'okey_game_info':
  8. var a = dec(body).value[0][3][0].value[0][1];
  9. console.log("Players: " + a.length);
  10. for (var i=0;i<a.length;i++) {
  11. var c = a[i].value[0][3].value;
  12. var v = a[i].value[0][4].value;
  13. setPlayerName(players[i],c + " " + v);
  14. console.log("Player: " + c + " " + v);
  15. }
  16. break;
  17. case 'okey_game_started':
  18. var a = dec(body).value[0][3][0].value[0][1];
  19. // console.log("Cards: " + a.length);
  20. for (var i=1;i<=a.length;i++) {
  21. var c = a[i-1].value[0][1];
  22. var v = a[i-1].value[0][2];
  23. // console.log("Card: " + c + " " + v);
  24. place_card(i,rand(1,2),c,v);
  25. }
  26. break;
  27. case 'okey_game_player_state': /// these two messages should be combined
  28. var a = dec(body).value[0][3][3].value[0][1];
  29. // console.log("Cards: " + a.length);
  30. for (var i=1;i<=a.length;i++) {
  31. var c = a[i-1].value[0][1];
  32. var v = a[i-1].value[0][2];
  33. console.log("Card: " + c + " " + v);
  34. place_card(i,1,c,v);
  35. }
  36. break;
  37. case 'okey_tile_discarded':
  38. console.log(String(dec(body)));
  39. var player = dec(body).value[0][3][0].value[0][1];
  40. var tile = dec(body).value[0][3][1].value[0][1];
  41. if (player.value == document.user) {
  42. console.log("Discard: " + player.value);
  43. var p = findCardOnTable(tile.value[0][1],tile.value[0][2]);
  44. empty_card(p.x,p.y);
  45. }
  46. break;
  47. case 'okey_tile_taken':
  48. console.log(String(dec(body)));
  49. var player = dec(body).value[0][3][0].value[0][1];
  50. var taken = dec(body).value[0][3][2].value[0][1];
  51. if (taken.value != 'null' && player.value == document.user) {
  52. console.log("Taken: " + player.value);
  53. var c = taken.value[0][1];
  54. var v = taken.value[0][2];
  55. var pos = findPlace();
  56. console.log(pos);
  57. place_card(pos.x,pos.y,c,v);
  58. }
  59. break;
  60. default: console.log(String(dec(body)));
  61. }
  62. }
  63. // SVG DOM and Template Engine
  64. function template_engine(html, data) {
  65. var re = /{([^}]+)?}/g, code = 'var r=[];', cursor = 0;
  66. var add = function(line,js) {
  67. js? (code += 'r.push(' + line + ');') :
  68. (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");' : '');
  69. return add; }
  70. while(match = re.exec(html)) {
  71. add(html.slice(cursor, match.index))(match[1],true);
  72. cursor = match.index + match[0].length; }
  73. add(html.substr(cursor, html.length - cursor));
  74. code += 'return r.join("");';
  75. return new Function(code.replace(/[\r\t\n]/g, '')).apply(data); }
  76. function reload(file, name) { var slot = document.getElementById(name);
  77. slot.parentNode.replaceChild(svg(localStorage.getItem(file)),slot);}
  78. function reload_cont(cont,name,element) { if (null != cont) (cont)(); else reload(name,element); }
  79. function loadFile(name,cont,element) {
  80. if (localStorage.getItem(name) == null) {
  81. var client = new XMLHttpRequest();
  82. client.open('GET', name, true);
  83. client.onload = function() {
  84. localStorage.setItem(name,client.responseText);
  85. reload_cont(cont,name,element); }
  86. client.send(); }
  87. else reload_cont(cont,name,element); }
  88. function svg(html) {
  89. return new DOMParser().parseFromString(html, "text/xml").firstChild;
  90. }
  91. function svgBulk() {
  92. var res = [];
  93. var node = new DOMParser().parseFromString(html, "text/xml").firstChild;
  94. for (var i=0;i<node.childnode.length;i++) { var child = childNodes.item(i);
  95. if (child.tagName != undefined) res.push(child); } }
  96. // The Card
  97. var color = ['#CE290F','#3B5998','#48AF5E','#FFEC00'];
  98. var slotName = slotNameDef;
  99. function slotNameDef(x,y) { return "Slot-"+y+","+x; }
  100. function card(line,pos,col,v) {
  101. return template_engine(
  102. localStorage.getItem("templates/Card.svg"),
  103. { name: slotName(pos,line),
  104. suit: color[col-1],
  105. value: v,
  106. y: (line-1)*69,
  107. x: (pos-1)*42 }); }
  108. // Game Scene
  109. function setPlayerName(e, playerName) {
  110. var dx = (document.getElementById(e).attributes['fill'].value == "#FFFFFF") ? 65 : 20;
  111. document.getElementById(e).setAttribute("y",27);
  112. document.getElementById(e).setAttribute("x",dx);
  113. document.getElementById(e).textContent = playerName;
  114. document.getElementById(e+"-Pad").setAttribute('width',
  115. document.getElementById(e).getBBox().width + 45); }
  116. function place_card(x,y,c,v) {
  117. var slot = document.getElementById(slotName(x,y));
  118. slot.parentNode.replaceChild(svg(card(y,x,c,v)),slot); }
  119. function empty_card(x,y) { var slot = document.getElementById(slotName(x,y));
  120. var html = '<g xmlns="http://www.w3.org/2000/svg" id="'+slotName(x,y)+'"/>';
  121. slot.parentNode.replaceChild(svg(html),slot); }
  122. function rand(lo,hi) { return Math.floor((Math.random()*hi)+lo); }
  123. function loadScene() {
  124. reload("Kakaranet-7-Refined.svg", "Refined");
  125. for (var i=1;i<16;i++) { empty_card(i,2); empty_card(i,1); }
  126. drawSampleCards(); }
  127. function findPlace() {
  128. for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
  129. var e = document.getElementById(slotName(x,y));
  130. if (e.childElementCount == 0) {
  131. var pos = e.attributes['id'].value.split("-")[1].split(",");
  132. console.log("Free");
  133. console.log(pos[0]);
  134. console.log(pos[1]);
  135. return { y: pos[0], x: pos[1] }; } } }
  136. function findCardOnTable(c,v) {
  137. console.log("Find Card " + c + " " + v);
  138. for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
  139. var e = document.getElementById(slotName(x,y));
  140. if (e.childElementCount > 0) {
  141. var value = e.lastChild.textContent;
  142. var col = color.indexOf(e.lastChild.attributes['fill'].value) + 1;
  143. console.log(col + " " + value);
  144. if (c == col && v == value) return { 'x': x, 'y': y }; } }
  145. console.log("Card Not Found");
  146. return ""; }
  147. function loadAppend(file, animation, name) {
  148. loadFile(file, function() {
  149. var slot = document.getElementById(name);
  150. var r = template_engine(localStorage.getItem(file),{'name': animation});
  151. document.getElementById(name).appendChild(svg(r)); }); }
  152. function loadAnimationForButton(a, b) { return loadAppend('templates/ButtonAnimation.svg', a, b); }
  153. // TODO: Rollout Monadic Chain here
  154. loadFile('templates/Card.svg', function() {
  155. loadFile('Kakaranet-7-Refined.svg', function() {
  156. loadScene();
  157. var a = [{button: "Create", pathes: ["CreateShow", "CreateHide"]},
  158. {button: "Play", pathes: ["PlayShow", "PlayHide"]}];
  159. for (var y=0;y<a.length;y++) for (var x=0;x<a[y].pathes.length;x++)
  160. loadAnimationForButton(a[y].pathes[x],a[y].button);
  161. document.getElementById("Right-Menu").setAttribute('onclick', 'onRightMenu(evt)');
  162. document.getElementById("Play") .setAttribute('onclick', 'onRightMenuDown(evt)');
  163. document.getElementById("Create") .setAttribute('onclick', 'onRightMenuDown(evt)');
  164. // onRightMenuDown();
  165. });
  166. });
  167. function onRightMenu(evt) {
  168. localStorage.clear();
  169. ["PlayShow","CreateShow"].map(function (x) {
  170. document.getElementById(x+"-Motion").beginElement(); }); }
  171. function onRightMenuDown(evt) {
  172. ["PlayHide","CreateHide"].map(function (x) {
  173. document.getElementById(x+"-Motion").beginElement(); }); }
  174. // SVG Samples for svg.htm
  175. loadFile('templates/Mustafa-Persona.svg', null, "Mustafa-Persona-Sample");
  176. loadFile('templates/Mustafa-Selection.svg', null, "Mustafa-Selection-Sample");
  177. document.getElementById("MustafaSelection").addEventListener('click', function() {
  178. var style = document.getElementById("Mustafa-Selection-Sample").style;
  179. if (style.display == 'none') style.display = 'block';
  180. else style.display = 'none'; });
  181. function slotName1(x,y) { return "1Slot-"+y+","+x; }
  182. function drawSampleCards() {
  183. slotName = slotName1;
  184. for (var i=1;i<15;i++) { place_card(i,rand(1,2),rand(1,4),rand(1,13)); }
  185. slotName = slotNameDef; }