bootloader.js 11 KB

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