svg.js 10 KB

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