bootloader.js 13 KB

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