bootloader.js 11 KB

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