bootloader.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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,check) {
  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] || !check) {
  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].value,
  23. total = results[i].value[0][3].value;
  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: utf8decode(name) + " " + 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) {
  84. document.getElementById(x).onclick = function() {
  85. switchLanguage(); translateScene(); } });
  86. var rulesOnClick = [
  87. "Rules",
  88. "Rules-Text",
  89. "Rules-Rectangle" ];
  90. rulesOnClick.map(function(x) {
  91. document.getElementById(x).style.cursor = "pointer";
  92. document.getElementById(x).onclick = showRules; });
  93. Core(ControllerScope);
  94. Core(DragScope);
  95. Core(DropScope);
  96. Core(TimerScope);
  97. Core(PlayerScope);
  98. Core(OkeyApiProviderScope);
  99. Core(CardScope);
  100. Core(HandScope);
  101. Core(DeckScope);
  102. Core(RosterScope);
  103. if (currentLanguage() != "en") $("#Flag-en").hide();
  104. translateScene();
  105. initDiscards();
  106. initChat();
  107. initChatSample();
  108. initPauseOverlay();
  109. initEditorsSafari();
  110. if ((hours() < 6 || hours() >= 18)) {
  111. $("#Sky").attr({fill:"#0E4B69"});
  112. $("#City").attr({fill:"#3B5998"});
  113. $("body")[0].style.background = "#0E4B69";
  114. } else
  115. {
  116. $("#Sky").attr({fill:"#EDF9FF"});
  117. $("#City").attr({fill:"#DFF1F4"});
  118. $("body")[0].style.background= "#EDF9FF";
  119. }
  120. document.addEventListener('touchmove',function(e) {e.preventDefault();},false);
  121. $svg.attr({preserveAspectRatio:"xMidYMid meet",width:"100%",height:"100%"});
  122. }
  123. function sendSawOkey()
  124. {
  125. ws.send(enc(tuple(atom('client'),tuple(atom('game_action'),scope.gameId,
  126. atom('okey_i_saw_okey'),[]))));
  127. }
  128. function initChatSample() {
  129. chatMessage("Chat","0","Maxim","Kakaranet:\n"+i18n("GameChat"));
  130. barHover();
  131. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  132. barHoverOut();
  133. }
  134. function onPlayerInfo(evt) {
  135. ws.send(enc(tuple(atom('client'),
  136. tuple(atom('stats_action'),bin(document.user),atom('game_okey'))))); }
  137. function onPlayerInfoClose(evt) { document.getElementById('Player-Statistics').style.display = 'none'; }
  138. // Run
  139. function appRun() {
  140. $.load('Kakaranet-Scene.svg', function(x) {
  141. var name = "Refined";
  142. var slot = document.getElementById(name);
  143. if (slot == null) return;
  144. slot.parentNode.replaceChild(svg(x),slot);
  145. $.load("svg/Discarder.svg", function(h) {
  146. PatchSVG();
  147. StartApp();
  148. });
  149. });
  150. }
  151. function showRules()
  152. {
  153. $.load("svg/Okey-Rules.svg", function(h) {
  154. var rules = document.getElementById("Okey-Rules");
  155. if (null == rules) {
  156. var rulesElement = svg(h);
  157. document.getElementById("Kakaranet-Scene").appendChild(rulesElement);
  158. rules = document.getElementById("Okey-Rules");
  159. rules.setAttribute('transform', 'translate(210,86)');
  160. rules.setAttribute('onclick', 'onRulesClose(evt)');
  161. }
  162. translateScene();
  163. rules.style.display = 'block';
  164. });
  165. }
  166. function displayPlayerInfo(fun)
  167. {
  168. $.load("svg/Player-Statistics.svg", function(h) {
  169. var rules = document.getElementById("Player-Statistics");
  170. if (null == rules) {
  171. var rulesElement = svg(h);
  172. document.getElementById("Kakaranet-Scene").appendChild(rulesElement);
  173. rules = document.getElementById("Player-Statistics");
  174. rules.setAttribute('transform', 'translate(210,86)');
  175. rules.setAttribute('onclick', 'onPlayerInfoClose(evt)');
  176. }
  177. rules.style.display = 'block';
  178. fun();
  179. });
  180. }
  181. function requestPlayerInfo()
  182. {
  183. ws.send(enc(tuple(atom('client'),
  184. tuple(atom('stats_action'),bin(document.user),atom('game_okey')))));
  185. }
  186. function onPlayerInfoClose(evt) {
  187. document.getElementById('Player-Statistics').style.display = 'none';
  188. }
  189. function onRulesClose(evt) {
  190. document.getElementById('Okey-Rules').style.display = 'none';
  191. }
  192. function initEditorsSafari()
  193. {
  194. if (isSafari())
  195. {
  196. $("#edit").css({position:"relative"});
  197. $("#onlineChatEdit").css({position:"relative"});
  198. $(window).on("resize", manualForeignObjectPositioning);
  199. $(window).on("orientationchange", manualForeignObjectPositioning);
  200. manualForeignObjectPositioning();
  201. }
  202. }
  203. function hours() { return (new Date()).getHours(); }
  204. function initPauseOverlay() {
  205. var html = '<g xmlns="http://www.w3.org/2000/svg" id="overlay" style="display:none;">'+
  206. '<rect x="216" y="91" stroke-width="0" stroke="red" width="641" height="367" rx="6" fill="skyblue" opacity="0.9"></rect>'+
  207. '<g>'+
  208. '<text id="Overlay-Results" fill="white" font-family="Exo 2" font-size="20pt"></text>'+
  209. '<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>'+
  210. '</g>';
  211. var page = document.getElementById("Kakaranet-Scene");
  212. var kakush = document.getElementById("Kakush");
  213. page.insertBefore(svg(html),kakush);
  214. $("#overlay").on("click", hideOverlay);
  215. }
  216. function showRoundEnd(e)
  217. {
  218. $overlay.show();
  219. var reason = dec(e.raw).value[0][3][1].value[0][1].value;
  220. var gameres = dec(e.raw).value[0][3][2].value[0][1];
  221. $("#Overlay-Results").empty();
  222. for (var i=0;i<gameres.length;i++) { gameresultRow(400,130,i,gameres); }
  223. if (reason == "tashes_out") {
  224. $("#Overlay-Text").text("Tashes out");
  225. $("#RevealDeckRoot").hide();
  226. }
  227. }
  228. function hideOverlay()
  229. {
  230. $overlay.hide();
  231. $("#Overlay-Results").empty();
  232. if (scope.ended) scope.deck.fill([]);
  233. }
  234. function denyWrongReveal() {
  235. $overlay.show();
  236. $("#RevealDeck").hide();
  237. $("#Overlay-Text").text(i18n("WrongReveal"));
  238. }
  239. function showRevealHand(o) {
  240. var player = o.value[0][3][0].value[0][1].value,
  241. discard = o.value[0][3][1].value[0][1].value,
  242. deck = o.value[0][3][2].value[0][1];
  243. $.load("svg/Deck.svg", function(h) {
  244. $reveal_deck = $("#RevealDeck");
  245. if (null == $reveal_deck.$el) {
  246. $overlay.append(svg(h));
  247. $reveal_deck = $("#RevealDeck");
  248. $("#RevealDeckRoot").on("click", hideOverlay);
  249. }
  250. $reveal_deck.each(function(card){ card.$el && card.$el.remove(); });
  251. for (var i=0;i<2;i++)
  252. for (var j=0;j<15;j++) {
  253. var bin = deck[i][j];
  254. if (null != bin.value[0][2]) {
  255. var card = new scope.Card({
  256. color: scope.CARD_COLORS[bin.value[0][1]-1],
  257. value: bin.value[0][2] });
  258. card.$el.attr('transform', 'translate(' + (5+j*42) + ' ' + (10+i*62) + ')');
  259. $reveal_deck.append(card.$el[0]);
  260. }
  261. }
  262. });
  263. $overlay.show();
  264. $("#Overlay-Text").text(utf8decode(player) + " " + i18n("Revealed"));
  265. }
  266. scope.playersPositions =
  267. [
  268. [ "Me", "Right", "Center", "Left" ],
  269. [ "Left", "Me", "Right", "Center" ],
  270. [ "Center", "Left", "Me", "Right" ],
  271. [ "Right", "Center", "Left", "Me" ]
  272. ];
  273. globalShiftX = 0;
  274. globalRightPosition = 857;
  275. appRun();
  276. if (adaptiveDesign())
  277. {
  278. $(window).on("orientationchange", relayout);
  279. $(window).on("resize", relayout);
  280. }
  281. function manualForeignObjectPositioning()
  282. {
  283. var svgWidth = $svg[0].viewBox.baseVal.width,
  284. svgHeight = $svg[0].viewBox.baseVal.height;
  285. var sizeX = svgWidth / innerWidth,
  286. sizeY = svgHeight / innerHeight,
  287. size = Math.max(sizeX, sizeY) || 1;
  288. var realX, realY, scale, shiftX, shiftY;
  289. realX = 1/size * svgWidth;
  290. realY = 1/size * svgHeight;
  291. var barwidth=0,rightPosition=866,globalShiftX=0;
  292. if (sizeX < sizeY) {
  293. // console.log("Left and Right White Spaces. Do stretch the Width ");
  294. shiftY = 0;
  295. shiftX = (innerWidth - realX) / 2 * sizeX;
  296. globalShiftX = adaptiveDesign() ? (shiftX > 30 ? shiftX - 20 : 0) : 0;
  297. barwidth = scope.fixedChatBars ? 0 : globalShiftX;
  298. rightPosition = adaptiveDesign() ? (svgWidth+globalShiftX-(barwidth+206)) : 866 ;
  299. } else {
  300. // console.log("Top and Bottom White Spaces. Do reorient, or adopt the Height.");
  301. shiftX = 0;
  302. shiftY = (innerHeight - realY) / 2;
  303. }
  304. $("#GameChatEditor").attr({x: (rightPosition * realY / svgHeight + shiftX / sizeX),
  305. y: 504 * realY / svgHeight + shiftY,
  306. width: 196 * realX / svgWidth,
  307. height: 120 * realY / svgHeight});
  308. $("#OnlineChatEditor").attr({x: 10 * realX / svgWidth + shiftX / sizeX -
  309. globalShiftX * realX / svgWidth,
  310. y: 504 * realY / svgHeight + shiftY,
  311. width: 196 * realX / svgWidth,
  312. height: 120 * realY / svgHeight});
  313. }
  314. function relayout()
  315. {
  316. var svgWidth = $svg[0].viewBox.baseVal.width,
  317. svgHeight = $svg[0].viewBox.baseVal.height;
  318. var sizeX = svgWidth / innerWidth,
  319. sizeY = svgHeight / innerHeight,
  320. size = Math.max(sizeX, sizeY) || 1;
  321. var realX, realY, scale, shiftX, shiftY;
  322. realX = 1/size * svgWidth;
  323. realY = 1/size * svgHeight;
  324. if (sizeX < sizeY) {
  325. //console.log("Left and Right White Spaces. Do stretch the Width "+svgWidth);
  326. shiftY = 0;
  327. shiftX = (innerWidth - realX) / 2 * sizeX;
  328. globalShiftX = shiftX > 30 ? shiftX - 20 : 0;
  329. var barwidth = scope.fixedChatBars ? 0 : globalShiftX;
  330. var rightPosition = svgWidth+globalShiftX-(barwidth+213);
  331. globalRightPosition = rightPosition;
  332. // $("#Chat") .attr({x:0,transform:"translate("+(rightPosition)+" 0)",width:barwidth+206});
  333. $("#Right-Bar") .attr({x:0,transform:"translate("+(rightPosition)+" 0)",width:barwidth+216});
  334. $("#Clip-Path-Right").attr({x:0,transform:"translate("+(rightPosition)+" 0)",width:barwidth+216});
  335. $("#Online-Users").attr({transform:"translate("+(-globalShiftX+10)+" 20)"});
  336. $("#Facebook-Login").attr({transform:"translate("+(rightPosition+(barwidth+216)-150)+" 20)"});
  337. // $("#Online-List").attr({x:0,transform:"translate("+(-globalShiftX)+" 100)",width:barwidth+206});
  338. $("#Left-Bar") .attr({x:0,transform:"translate("+(-globalShiftX)+" 0)", width:barwidth+216});
  339. $("#Clip-Path-Left") .attr({x:0,transform:"translate("+(-globalShiftX)+" 100)",width:barwidth+216});
  340. $("#Clip-Path-Left-Chat").attr({x:0,transform:"translate("+(-globalShiftX)+" 0)",width:barwidth+216});
  341. onlineHover();
  342. mouseWheelHandler({'detail':0,'wheelDelta':0});
  343. onlineHoverOut();
  344. barHover();
  345. mouseWheelHandler({'detail':0,'wheelDelta':0});
  346. barHoverOut();
  347. if (!isSafari()) {
  348. $("#GameChatEditor").attr({x: 10+rightPosition});
  349. $("#OnlineChatEditor").attr({x: 10-globalShiftX});
  350. }
  351. } else {
  352. // console.log("Top and Bottom White Spaces. Do reorient, or adopt the Height.");
  353. shiftX = 0;
  354. shiftY = (innerHeight - realY) / 2;
  355. // do nothing with Vertical Layout
  356. // possible changing orientation
  357. }
  358. }