bootloader.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. function isSafari() {
  2. var ua = navigator.userAgent.toLowerCase();
  3. return ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1;
  4. }
  5. var svgNS = "http://www.w3.org/2000/svg";
  6. var color = ['#DE3F26','#606060','#48AF5E','#FFC800'];
  7. function parseTransformAttribute(aa) {
  8. var a = aa.split(' ').join('');
  9. var b={};
  10. for (var i in a = a.match(/(\w+\((\-?\d+\.?\d*,?)+\))+/g)) { var c = a[i].match(/[\w\.\-]+/g); b[c.shift()] = c; }
  11. return b; }
  12. function svg(html) { return new DOMParser().parseFromString(html, "text/xml").firstChild; }
  13. function removeChilds(e) { var last; while (last = e.lastChild) e.removeChild(last); };
  14. function setPlayerName(e, playerName) {
  15. var dx = 15;
  16. document.getElementById(e+"-Name").setAttribute("y",27);
  17. document.getElementById(e+"-Name").setAttribute("x",dx);
  18. document.getElementById(e+"-Name").textContent = playerName;
  19. document.getElementById(e+"-Pad").setAttribute('width', document.getElementById(e+"-Name").getBBox().width + 25); }
  20. function template_engine(html, data) {
  21. var re = /{([^}]+)?}/g, code = 'var r=[];', cursor = 0;
  22. var add = function(line,js) {
  23. js? (code += 'r.push(' + line + ');') :
  24. (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");' : ''); // "
  25. return add; }
  26. while(match = re.exec(html)) {
  27. add(html.slice(cursor, match.index))(match[1],true);
  28. cursor = match.index + match[0].length; }
  29. add(html.substr(cursor, html.length - cursor));
  30. code += 'return r.join("");';
  31. return new Function(code.replace(/[\r\t\n]/g, '')).apply(data); }
  32. function discarder(name) { return template_engine(localStorage.getItem("svg/Discarder.svg?q=" + $.timestamp), { name: name }); }
  33. function initDiscards() {
  34. [ {name:"Gabrielo-Discard", hand:"Player-Left-Hand"},
  35. {name:"Alina-Discard", hand:"Player-Right-Hand"},
  36. {name:"Mustafa-Discard", hand:"Player-Center-Hand"},
  37. {name:"You-Discard", hand:"Player-Me-Hand"} ].map(function(e) {
  38. document.getElementById(e.name).appendChild(svg(discarder(e.hand)));
  39. });
  40. }
  41. function PatchSVG()
  42. {
  43. $('Player-Statistics').hide();
  44. var onlineListOnClick = [
  45. "Online-Users",
  46. "Online-Users-Pad",
  47. "Online-Logo",
  48. "Users-Online-Message",
  49. "Users-Online-Number" ];
  50. onlineListOnClick.map(function(x) {
  51. document.getElementById(x).style.cursor = "pointer";
  52. document.getElementById(x).onclick = showOnlineList; });
  53. var rulesOnClick = [
  54. "Rules",
  55. "Rules-Text",
  56. "Rules-Rectangle" ];
  57. rulesOnClick.map(function(x) {
  58. document.getElementById(x).style.cursor = "pointer";
  59. document.getElementById(x).onclick = showRules; });
  60. Core(ControllerScope);
  61. Core(DragScope);
  62. Core(DropScope);
  63. Core(TimerScope);
  64. Core(PlayerScope);
  65. Core(OkeyApiProviderScope);
  66. Core(CardScope);
  67. Core(HandScope);
  68. Core(DeckScope);
  69. Core(RosterScope);
  70. initDiscards();
  71. initChat();
  72. initChatSample();
  73. initEditorsSafari();
  74. initPauseOverlay();
  75. if ((hours() < 6 || hours() >= 18)) {
  76. $("#Sky").attr({fill:"#0E4B69"});
  77. $("#City").attr({fill:"#3B5998"});
  78. } else
  79. {
  80. $("#Sky").attr({fill:"#EDF9FF"});
  81. $("#City").attr({fill:"#DFF1F4"});
  82. }
  83. // document.addEventListener('touchmove',function(e) {e.preventDefault();},false);
  84. $svg.attr({preserveAspectRatio:"xMidYMid meet",width:"100%",height:"100%"});
  85. }
  86. function initChatSample() {
  87. chatMessage("Chat","0","Maxim","Kakaranet:\nThis is in-game chat");
  88. barHover();
  89. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  90. barHoverOut();
  91. }
  92. function onPlayerInfo(evt) {
  93. ws.send(enc(tuple(atom('client'),
  94. tuple(atom('stats_action'),bin(document.user),atom('game_okey'))))); }
  95. function onPlayerInfoClose(evt) { document.getElementById('Player-Statistics').style.display = 'none'; }
  96. // Run
  97. $.load('Kakaranet-Scene.svg', function(x) {
  98. var name = "Refined";
  99. var slot = document.getElementById(name);
  100. if (slot == null) return;
  101. slot.parentNode.replaceChild(svg(x),slot);
  102. $.load("svg/Discarder.svg", function(h) {
  103. PatchSVG();
  104. StartApp();
  105. });
  106. });
  107. function showRules()
  108. {
  109. $.load("svg/Okey-Rules.svg", function(h) {
  110. var rules = document.getElementById("Okey-Rules");
  111. if (null == rules) {
  112. var rulesElement = svg(h);
  113. document.getElementById("Kakaranet-12-maxim").appendChild(rulesElement);
  114. rules = document.getElementById("Okey-Rules");
  115. rules.setAttribute('transform', 'translate(210,86)');
  116. rules.setAttribute('onclick', 'onRulesClose(evt)');
  117. }
  118. rules.style.display = 'block';
  119. });
  120. }
  121. function onRulesClose(evt) {
  122. document.getElementById('Okey-Rules').style.display = 'none';
  123. }
  124. function initEditorsSafari()
  125. {
  126. if (isSafari()) {
  127. $("#edit").css({position:"relative"});
  128. $("#onlineChatEdit").css({position:"relative"});
  129. $(window).on("resize", manualForeignObjectPositioning);
  130. $(window).on("orientationchange", manualForeignObjectPositioning);
  131. manualForeignObjectPositioning();
  132. }
  133. }
  134. function manualForeignObjectPositioning()
  135. {
  136. var svgWidth = $svg[0].viewBox.baseVal.width,
  137. svgHeight = $svg[0].viewBox.baseVal.height;
  138. var sizeX = svgWidth / innerWidth,
  139. sizeY = svgHeight / innerHeight,
  140. size = Math.max(sizeX, sizeY) || 1;
  141. var realX, realY, scale, shiftX, shiftY;
  142. realX = 1/size * svgWidth;
  143. realY = 1/size * svgHeight;
  144. if (sizeX < sizeY) {
  145. // console.log("Left and Right White Spaces. Do stretch the Width ");
  146. shiftY = 0;
  147. shiftX = (innerWidth - realX) / 2;
  148. } else {
  149. // console.log("Top and Bottom White Spaces. Do reorient, or adopt the Height.");
  150. shiftX = 0;
  151. shiftY = (innerHeight - realY) / 2;
  152. }
  153. $("#GameChatEditor").attr({x: 864 * realX / svgWidth + shiftX,
  154. y: 504 * realY / svgHeight + shiftY,
  155. width: 198 * realX / svgWidth,
  156. height: 120 * realY / svgHeight});
  157. $("#OnlineChatEditor").attr({x: 10 * realX / svgWidth + shiftX,
  158. y: 504 * realY / svgHeight + shiftY,
  159. width: 198 * realX / svgWidth,
  160. height: 120 * realY / svgHeight});
  161. }
  162. function hours() { return (new Date()).getHours(); }
  163. function initPauseOverlay() {
  164. var html = '<g xmlns="http://www.w3.org/2000/svg" id="overlay" style="display:none;">'+
  165. '<rect x="216" y="91" stroke-width="0" stroke="red" width="641" height="367" rx="6" fill="skyblue" opacity="0.8"></rect>'+
  166. '<g>'+
  167. '<text fill="white" font-family="Exo 2" y="280" x="-116" text-anchor="middle" dx="641" font-size="30pt"> Someone paused the game</text></g>'+
  168. '</g>';
  169. var page = document.getElementById("Kakaranet-12-maxim");
  170. var kakush = document.getElementById("Kakush");
  171. page.insertBefore(svg(html),kakush);
  172. }
  173. function showRevealHand(o) {
  174. var player = o.value[0][3][0].value[0][1].value,
  175. discard = o.value[0][3][1].value[0][1].value,
  176. deck = [];
  177. deck[0] = o.value[0][3][2].value[0][1][0],
  178. deck[1] = o.value[0][3][2].value[0][1][1];
  179. console.log(player);
  180. console.log(discard);
  181. console.log(deck);
  182. if (!document.getElementById("RevealDeck"))
  183. $.load("svg/Deck.svg", function(h) { $overlay.append(svg(h)); });
  184. $overlay.show();
  185. $overlay.find("text").text(player + " revealed ");
  186. }