bootloader.js 11 KB

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