bootloader.js 9.7 KB

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