Nations Your turn Notifier

Notify Nations in your turn (mabiweb.com)

  1. // ==UserScript==
  2. // @name Nations Your turn Notifier
  3. // @namespace tequila_j-script
  4. // @version 0.1.1
  5. // @description Notify Nations in your turn (mabiweb.com)
  6. // @match http://www.mabiweb.com/modules.php?name=GM_Nations*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
  8. // @grant GM_addStyle
  9. // @run-at document-ready
  10. // ==/UserScript==
  11.  
  12. // request permission on page load
  13.  
  14. document.addEventListener('DOMContentLoaded', function () {
  15. if (Notification.permission !== "granted")
  16. Notification.requestPermission();
  17. });
  18.  
  19. GM_addStyle( `
  20. div.playerboardinfo {
  21. position: absolute;
  22. top: 10px;
  23. left: 1150px;
  24. }
  25.  
  26. div.playerboardinfo dl {
  27. }
  28.  
  29. div.playerboardinfo dt {
  30. margin-bottom: 4px;
  31. padding-left: 10px;
  32. }
  33.  
  34. div.playerboardinfo dd {
  35. margin: 0px;
  36. }
  37.  
  38. div.playerboardinfo ul {
  39. list-style-type: disc;
  40. margin: 0px;
  41. padding:0px;
  42. }
  43.  
  44. div.playerboardinfo ul li {
  45. display: inline-block;
  46. width: 40px;
  47. height: 30px;
  48. margin: 0px 5px;
  49. }
  50.  
  51. div.playerboardinfo li p {
  52. margin: 0px;
  53. width: 20px;
  54. height: 20px;
  55. display: inline-block;
  56. }
  57.  
  58. div.playerboardinfo li p.desc {
  59. background-repeat: no-repeat;
  60. background-position: center;
  61. background-size: 100% 100%;
  62. }
  63.  
  64. div.playerboardinfo li.food p.desc {
  65. background-image: url(modules/GM_Nations/images/Token_Food.png);
  66. }
  67.  
  68. div.playerboardinfo li.stone p.desc {
  69. background-image: url(modules/GM_Nations/images/Token_Stone.png);
  70. }
  71.  
  72. div.playerboardinfo li.gold p.desc {
  73. background-image: url(modules/GM_Nations/images/Token_Gold.png);
  74. }
  75.  
  76. div.playerboardinfo li.point p.desc {
  77. background-image: url(modules/GM_Nations/images/Token_VP.png);
  78. }
  79.  
  80. div.playerboardinfo li.spare p.desc {
  81. opacity: 0.5;
  82. }
  83.  
  84. div.playerboardinfo li.worker-Blue p.desc {
  85. background-image: url(modules/GM_Nations/images/Meeple_Blue.png);
  86. }
  87.  
  88. div.playerboardinfo li.worker-Yellow p.desc {
  89. background-image: url(modules/GM_Nations/images/Meeple_Yellow.png);
  90. }
  91.  
  92. div.playerboardinfo li.worker-Red p.desc {
  93. background-image: url(modules/GM_Nations/images/Meeple_Red.png);
  94. }
  95.  
  96. div.playerboardinfo li.worker-Green p.desc {
  97. background-image: url(modules/GM_Nations/images/Meeple_Green.png);
  98. }
  99.  
  100. div.playerboardinfo li.worker-Purple p.desc {
  101. background-image: url(modules/GM_Nations/images/Meeple_Purple.png);
  102. }
  103.  
  104. div.playerboardinfo li.worker-Orange p.desc {
  105. background-image: url(modules/GM_Nations/images/Meeple_Orange.png);
  106. }
  107.  
  108. div.playerboardinfo li.worker-Gray p.desc {
  109. background-image: url(modules/GM_Nations/images/Meeple_Gray.png);
  110. }
  111.  
  112. div.playerboardinfo li.worker-Pink p.desc {
  113. background-image: url(modules/GM_Nations/images/Meeple_Pink.png);
  114. }
  115.  
  116. div.playerboardinfo li.worker-Cyan p.desc {
  117. background-image: url(modules/GM_Nations/images/Meeple_Cyan.png);
  118. }
  119.  
  120.  
  121.  
  122.  
  123. `);
  124.  
  125.  
  126. //console.log = function() {};
  127.  
  128. (function() {
  129.  
  130. 'use strict';
  131. /*jshint multistr: true */
  132.  
  133. //avoid double loading
  134. if (window.location.href.indexOf('show_message') > 0) return;
  135.  
  136. // Game finished
  137. if ($("#nations-gameheader:contains('Game fini')").length > 0) return;
  138.  
  139. //finds username
  140. var $userNameBox = $("a[href='modules.php?name=Your_Account&op=logout'").parent();
  141. var userName = $userNameBox.text().replace(/.*welcome /i,"").replace(/!.*/,"");
  142. console.log("Username:" + userName + ".");
  143.  
  144. var currentUser = $("#nations-gameheader").children("img").next("b").text();
  145. console.log("Current user:"+ currentUser + ".");
  146.  
  147. var gameUrl = "http://www.mabiweb.com/modules.php?name=GM_Nations&g_id="+ urlParam("g_id") + "&op=view_game_reset";
  148. console.log(gameUrl);
  149.  
  150. var numberOfPlayers = $("#nations-gameheader").children("img").length;
  151. console.log("Number of players:" + numberOfPlayers);
  152.  
  153. var $playerboards = $("#placeholder > div");
  154. var $playernameboxes = $("#placeholder > ul.tab > li");
  155.  
  156. var playercolors = $("#nations-gameheader").children("img").map(function() {
  157. var ptoken = $(this).attr('src');
  158. var color = ptoken.replace(/.*Disc_/,"").replace(/\.png/,"");
  159. return color;}).get();
  160.  
  161.  
  162. var $newPlayerBoxes = $("<div class='playerboardinfo'/>");
  163. $("div#nations-board").append($newPlayerBoxes);
  164. for (var i = 0; i < numberOfPlayers; i++) {
  165. var $playerDiv = $($playerboards[i]);
  166. var playerName = $($playernameboxes[i]).text();
  167. var playerColor = playercolors[i];
  168. console.log("player: " + playerName);
  169. var resources = $playerDiv.find(".resource_text").map(function() {
  170. return $(this).text();
  171. }).get();
  172. var spareWorkFinder = $playerDiv.find(".resource_text:first").prev();
  173. if (spareWorkFinder.hasClass('outline_text')) {
  174. resources.push(spareWorkFinder.text())
  175. } else {
  176. resources.push(0);
  177. }
  178. $newPlayerBoxes.append(buildPlayerAF(playerName,playerColor,resources));
  179. }
  180.  
  181. function buildPlayerAF(playerName, playercolor, resources) {
  182. var playerTable = "<dl><dt>" + playerName + "</dt>"
  183. + "<dd><ul>"
  184. + "<li class='food'><p class='desc'></p><p class='qdt'>" + resources[0] + "</p></li>"
  185. + "<li class='stone'><p class='desc'></p><p class='qdt'>" + resources[1] + "</p></li>"
  186. + "<li class='gold'><p class='desc'></p><p class='qdt'>" + resources[2] + "</p></li>"
  187. + "<li class='point'><p class='desc'></p><p class='qdt'>" + resources[3] + "</p></li>"
  188. + "<li class='worker-"+playercolor+"'><p class='desc'></p><p class='qdt'>" + resources[4] + "/" + resources[5] + "</p></li>"
  189. + "</ul></dd></dl>"
  190. ;
  191.  
  192. return $(playerTable);
  193. }
  194.  
  195. window.setCheckTurn = function(callbackFunc, timing) {
  196. var variableInterval = {
  197. config: timing,
  198. callback: callbackFunc,
  199. stopped: false,
  200. runLoop: function() {
  201. if (variableInterval.stopped) return;
  202. var result = variableInterval.callback.call(variableInterval);
  203. if (typeof result == 'number') {
  204. if (result === 0) return;
  205. variableInterval.interval = result;
  206. };
  207. variableInterval.loop();
  208. },
  209. stop: function() {
  210. this.stopped = true;
  211. console.log("Auto refresh stopped");
  212. window.clearTimeout(this.timeout);
  213. },
  214. start: function() {
  215. this.stopped = false;
  216. console.log("Auto refresh started");
  217. return this.loop();
  218. },
  219. loop: function() {
  220. this.timeout = window.setTimeout(this.runLoop, this.getInterval());
  221. return this;
  222. },
  223. incrementInterval: function(multiply) {
  224. if (multiply !== undefined) multiply = 1;
  225. var alic = this.getInterval() + this.config.step*multiply;
  226. if (alic > this.config.max) {
  227. this.setInterval(this.config.max);
  228. console.log("Interval already set to maximum:" + this.getInterval());
  229. } else {
  230. this.setInterval(this.getInterval() + this.config.step);
  231. console.log("Interval increased to:" + this.getInterval());
  232. }
  233. return this;
  234. },
  235. resetInterval: function() {
  236. this.setInterval(this.config.start);
  237. console.log("Interval reset to:" + this.getInterval());
  238. },
  239. getInterval: function() {
  240. if (sessionStorage.getItem("currentTime") === null) {
  241. sessionStorage.setItem("currentTime", this.config.start);
  242. }
  243. return Number(sessionStorage.getItem("currentTime"));
  244. },
  245. setInterval: function(val) {
  246. sessionStorage.setItem("currentTime", val);
  247. }
  248.  
  249. }
  250. return variableInterval;
  251. };
  252.  
  253.  
  254. var notificationTimeout = {
  255. start: 10000,
  256. max: 120000,
  257. step: 5000,
  258. current: 10000
  259. };
  260.  
  261.  
  262. function urlParam(param) {
  263. var vars = {};
  264. window.location.href.replace( location.hash, '' ).replace(
  265. /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
  266. function( m, key, value ) { // callback
  267. vars[key] = value !== undefined ? value : '';
  268. }
  269. );
  270.  
  271. if ( param ) {
  272. return vars[param] ? vars[param] : null;
  273. }
  274. return vars;
  275. }
  276.  
  277.  
  278. var honkSound = new Audio("https://www.soundjay.com/misc/sounds/briefcase-lock-4.mp3");
  279.  
  280.  
  281.  
  282.  
  283. var isDefaultTitle = true;
  284. var origTitle = document.title;
  285. var yourTurnTitle = origTitle + " - Your turn!";
  286.  
  287. function blinkTitle() {
  288. document.title = isDefaultTitle ? origTitle : yourTurnTitle;
  289. isDefaultTitle = !isDefaultTitle;
  290. }
  291.  
  292.  
  293.  
  294.  
  295. function notify(message) {
  296. if (!Notification) {
  297. console.log('Desktop notifications are not available for your browser.');
  298. return;
  299. }
  300.  
  301. if (Notification.permission !== "granted")
  302. Notification.requestPermission();
  303.  
  304. else {
  305. var notification = new Notification('Mabiweb: it is your turn!', {
  306. icon: 'http://www.boiteajeux.net/jeux/kan/img/sandra_1.png',
  307. body: message,
  308. requireInteraction: true
  309. });
  310. notification.onclick = function () {
  311. window.focus();
  312. };
  313. }
  314. honkSound.play();
  315. var titleNotification = setInterval(blinkTitle, 1500);
  316.  
  317. }
  318.  
  319.  
  320.  
  321. function updatePage() {
  322. turnNotifier.incrementInterval(numberOfPlayers-1);
  323. window.location.href = gameUrl;
  324.  
  325. }
  326.  
  327. var turnNotifier = setCheckTurn(function() {
  328. updatePage();
  329. }, notificationTimeout
  330. );
  331.  
  332. var isMyTurn = function() {
  333. return userName == currentUser;
  334. };
  335.  
  336. function startNotification() {
  337. console.log("Is my turn?" + isMyTurn());
  338.  
  339. if (! isMyTurn()) {
  340. console.log("Next reload:" + turnNotifier.getInterval());
  341. turnNotifier.start();
  342. } else {
  343. turnNotifier.resetInterval(); //ugly hack for double reload
  344. notify("Table " + urlParam("g_id"));
  345. }
  346. }
  347.  
  348. startNotification();
  349. })() ;//end of script
  350.  
  351.