bootloader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var svgNS = "http://www.w3.org/2000/svg";
  2. var color = ['#DE3F26','#606060','#48AF5E','#FFC800'];
  3. var slotName = slotNameDef;
  4. function parseTransformAttribute(aa) {
  5. var a = aa.split(' ').join('');
  6. var b={};
  7. for (var i in a = a.match(/(\w+\((\-?\d+\.?\d*,?)+\))+/g)) { var c = a[i].match(/[\w\.\-]+/g); b[c.shift()] = c; }
  8. return b; }
  9. function svg(html) { return new DOMParser().parseFromString(html, "text/xml").firstChild; }
  10. function removeChilds(e) { var last; while (last = e.lastChild) e.removeChild(last); };
  11. function slotNameDef(x,y) { return "Slot-"+y+","+x; }
  12. function loadAnimationForButton(a, b) { return loadAppend('svg/ButtonAnimation.svg', a, b); }
  13. function setPlayerName(e, playerName) {
  14. var dx = 15;
  15. document.getElementById(e+"-Name").setAttribute("y",27);
  16. document.getElementById(e+"-Name").setAttribute("x",dx);
  17. document.getElementById(e+"-Name").textContent = playerName;
  18. document.getElementById(e+"-Pad").setAttribute('width', document.getElementById(e).getBBox().width + 15); }
  19. function template_engine(html, data) {
  20. var re = /{([^}]+)?}/g, code = 'var r=[];', cursor = 0;
  21. var add = function(line,js) {
  22. js? (code += 'r.push(' + line + ');') :
  23. (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");' : ''); // "
  24. return add; }
  25. while(match = re.exec(html)) {
  26. add(html.slice(cursor, match.index))(match[1],true);
  27. cursor = match.index + match[0].length; }
  28. add(html.substr(cursor, html.length - cursor));
  29. code += 'return r.join("");';
  30. return new Function(code.replace(/[\r\t\n]/g, '')).apply(data); }
  31. function loadAppend(file, animation, name) {
  32. loadFile(file, function() {
  33. var slot = document.getElementById(name);
  34. var r = template_engine(localStorage.getItem(file),{'name': animation});
  35. document.getElementById(name).appendChild(svg(r)); }); }
  36. function discarder(name) { return template_engine(localStorage.getItem("svg/Discarder.svg"), { name: name }); }
  37. function initDiscards() {
  38. [ {name:"Gabrielo-Discard", hand:"Player-Left-Hand"},
  39. {name:"Alina-Discard", hand:"Player-Right-Hand"},
  40. {name:"Mustafa-Discard", hand:"Player-Center-Hand"},
  41. {name:"You-Discard", hand:"Player-Me-Hand"} ].map(function(e) {
  42. document.getElementById(e.name).firstElementChild.remove();
  43. document.getElementById(e.name).appendChild(svg(discarder(e.hand))); });
  44. }
  45. function card(line,pos,col,v) {
  46. return template_engine(
  47. localStorage.getItem("svg/Card.svg"),
  48. { name: slotName(pos,line),
  49. suit: color[col-1],
  50. value: v,
  51. y: (line-1)*69,
  52. x: (pos-1)*42 }); }
  53. function reload(file, name2) { var name = name2==null?file:name2;
  54. var slot = document.getElementById(name);
  55. if (slot == null) return;
  56. slot.parentNode.replaceChild(svg(localStorage.getItem(file)),slot);}
  57. function reload_cont(cont,name,element) { reload(name,element); if (null != cont) (cont)(); }
  58. function loadFile(name,cont,element) {
  59. if (localStorage.getItem(name) == null) {
  60. var client = new XMLHttpRequest();
  61. client.open('GET', name, true);
  62. client.onload = function() {
  63. localStorage.setItem(name,client.responseText);
  64. reload_cont(cont,name,element);
  65. }
  66. client.send(); }
  67. else reload_cont(cont,name,element); }
  68. function PatchSVG()
  69. {
  70. // document.getElementById('Page-1').addEventListener("mousewheel", mouseWheelHandler, false);
  71. // Setup Clipping ViewPorts
  72. var clipPath1 = svg('<clipPath id="myClip1"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Left" x="0" y="0" width="216" height="400"/></clipPath>');
  73. var clipPath2 = svg('<clipPath id="myClip2"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Right" x="0" y="0" width="216" height="400"/></clipPath>');
  74. var clipPath3 = svg('<clipPath id="myClip3"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Left-Chat" x="0" y="0" width="216" height="400"/></clipPath>');
  75. document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
  76. document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
  77. document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
  78. // document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
  79. // document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
  80. // document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
  81. document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
  82. document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
  83. document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
  84. // document.getElementById('Player-Statistics').style.display = 'none';
  85. document.getElementById("Right-Bar").setAttribute("fill","skyblue");
  86. document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
  87. document.getElementById("Right-Bar").onmouseover = barHover;
  88. document.getElementById("Right-Bar").onmouseout = barHoverOut;
  89. document.getElementById("Left-Bar").onmouseover = onlineHover;
  90. document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
  91. document.getElementById("Player-Left").style.display = 'block';
  92. // HTML editors
  93. /*
  94. document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
  95. document.getElementById('onlineChatEdit').onkeydown = chatEditor;
  96. document.getElementById("onlineChatEdit").style.display = 'none';
  97. document.getElementById('edit').setAttribute("contentEditable","true");
  98. document.getElementById('edit').onkeydown = chatEditor;
  99. document.getElementById('edit').setAttribute("xmlns:data","Chat");
  100. document.getElementById("edit").style.display = '';
  101. */
  102. // showOnlineList ctor
  103. var onlineListOnClick = [
  104. "Online-Users",
  105. "Online-Users-Pad",
  106. "Online-Logo",
  107. "Users-Online-Message",
  108. "Users-Online-Number" ];
  109. // onlineListOnClick.map(function(x) {
  110. // console.log(x);
  111. // document.getElementById(x).onclick = showOnlineList; });
  112. initDiscards();
  113. Core(ControllerScope);
  114. Core(TimerScope);
  115. Core(PlayerScope);
  116. Core(OkeyApiProviderScope);
  117. Core(DragScope);
  118. Core(DropScope);
  119. Core(CardScope);
  120. Core(HandScope);
  121. Core(DeckScope);
  122. $svg.attr({preserveAspectRatio:"xMidYMin meet",class:"svg"});
  123. }
  124. function onPlayerInfo(evt) {
  125. ws.send(enc(tuple(atom('client'),
  126. tuple(atom('stats_action'),bin(document.user),atom('game_okey'))))); }
  127. function onPlayerInfoClose(evt) { document.getElementById('Player-Statistics').style.display = 'none'; }
  128. // Run
  129. loadFile('Kakaranet-Scene.svg', function() {
  130. loadFile("svg/Discarder.svg", function() {
  131. PatchSVG(),
  132. StartApp();
  133. });
  134. }, "Refined");