bootloader.js 12 KB

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