bootloader.js 10 KB

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