DC_time_updated

Display DC's date on top of the screen, or on your deck!

  1. // ==UserScript==
  2. // @name DC_time_updated
  3. // @author Ianouf, Ladoria
  4. // @version 0.8
  5. // @grant none
  6. // @description Display DC's date on top of the screen, or on your deck!
  7. // @match http://www.dreadcast.net/Main
  8. // @copyright 2015+, Ianouf & Ladoria
  9. // @namespace InGame
  10. // ==/UserScript==
  11. var server_date = undefined;
  12. var old_server_date = undefined;
  13. var handled_seconds = 0;
  14. var first_sync = true;
  15. var cumputing_skill_need = 40;
  16. var debugDeck = true;
  17. $(document).ready( function() {
  18. //Affichage de la date
  19. $('#bandeau ul.menus').eq(0)
  20. .prepend('<li id="affichageDateDC" class="couleur5" ></li>'
  21. +'<li class="separator"></li>'
  22. +'<li id="affichageDate" class="couleur5" ></li>'
  23. +'<li class="separator"></li>');
  24. $('head').append('<style>/*DC time updated stylesheet*/.custom_command.important_data { font-weight: bold;}.custom_command.red_info { color: red;}.custom_command.green_info { color: green;}.custom_command.orange_info { color: orange;}</style>');
  25. // Display DC's server date
  26. function handle_DC_date() {
  27. if(undefined === server_date)
  28. return;
  29. // server_date's refreshing all 3s, need to handle seconds between refreshs.
  30. if(server_date.getTime() != old_server_date.getTime()) {
  31. old_server_date = server_date;
  32. handled_seconds = 0;
  33. }
  34. else
  35. handled_seconds++;
  36. var server_seconds = server_date.getSeconds() + handled_seconds;
  37. // seconds handled.
  38. // display hour & DC's date
  39. date_to_display = new Date(server_date.getTime());
  40. date_to_display.setSeconds(server_seconds);
  41. $('#affichageDate').html(date_to_display.toLocaleString());
  42. $('#affichageDateDC').html(get_DC_date(server_date));
  43. }
  44. // input : Date
  45. function get_DC_date(date) {
  46. var server_day = date.getDate();
  47. var server_month = date.getMonth() + 1; // 0-11
  48. var server_year = date.getYear() - 100;
  49. var dc_hep = Math.floor(server_day / 7) + 1; //heptade
  50. var dc_day = (server_day % 7); //jour de l'heptade
  51. var dc_year = 70 + (server_year * 12) + server_month; //année, basé sur le fait que janvier 2000 est l'an 70 de DC.
  52. //le jour 0 est plutot le dernier jour de l'heptade précédente!
  53. if (dc_day === 0) {
  54. dc_hep--;
  55. dc_day=7;
  56. }
  57. return dc_day+'/'+dc_year+'.'+dc_hep;
  58. }
  59. // input format : ddmmyyyy
  60. // throw dummy exception
  61. function cast_date(text) {
  62. var day = text.substring(0,2);
  63. var month = text.substring(2,4) - 1; // 0-11
  64. var year = text.substring(4);
  65. if(isNaN(day) || isNaN(month) || isNaN(year)
  66. || day > 31 || month > 11 || year.length != 4)
  67. throw true;
  68. return new Date(year, month, day, 0, 0, 0, 0);
  69. }
  70. // input format : x/xxx.x whatever those x can mean
  71. // throw dummy exception
  72. function get_date_form_DC(date) {
  73. if(/^[0-9]{1}\/[0-9]{3}\.[0-9]{1}$/.test(date)) {
  74. var dc_day = date.substring(0,1);
  75. var dc_year = date.substring(2, 5);
  76. var dc_hep = date.substring(6);
  77. if(!isNaN(dc_day) && dc_day > 0 && dc_day < 8
  78. && !isNaN(dc_year) && dc_year >= 0 && dc_year < 1000
  79. && !isNaN(dc_hep) && dc_year > 0 && dc_hep < 6) {
  80. dc_day = parseInt(dc_day);
  81. dc_year = parseInt(dc_year);
  82. dc_hep = parseInt(dc_hep);
  83. var day =( (dc_hep-1)*7 ) + dc_day;
  84. var month = ( dc_year%12 ) +2;
  85. var year = Math.floor(dc_year / 12)+1994;
  86. if(month>12){
  87. month = month%12;
  88. year++;
  89. }
  90. day = ('0' + day).slice(-2);
  91. month = ('0' + month).slice(-2);
  92. year = ('000' + year).slice(-4);
  93. return day + month + year;
  94. }
  95. }
  96. throw true;
  97. }
  98.  
  99. // Date command line 'Object'
  100. var CommandLine_DC_time = function (command_line) {
  101. this.command_line = command_line;
  102. this.argument = '';
  103. this.parameter = '';
  104. if(/\-/gi.test(command_line)) { // argument given
  105. this.argument = command_line.trim().split("-")[1];
  106. if(/ /gi.test(this.argument)) { // parameter given
  107. this.argument = command_line.trim().split("-")[1].split(" ")[0];
  108. this.parameter = command_line.trim().split("-")[1].split(" ")[1];
  109. }
  110. }
  111. this.enabledArguments = ['h', // display all date's and secondes.
  112. 'd', // display converted standard date to DC's date
  113. 't', // display converted DC's date to standard date
  114. 'a']; // display the manual
  115. // processing the command
  116. // input : Deck object
  117. this.execute = function(deck) {
  118. var deckLines = new Array();
  119. if(false === CommandLine_DC_time.check_character_skill()) {
  120. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command", "Votre niveau en informatique est trop faible pour réussir cette commande"]));
  121. return;
  122. }
  123. else switch (this.argument) {
  124. case '' : // display DC's server date
  125. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command", get_DC_date(server_date)]));
  126. break;
  127. case 'h' : // display all date's and secondes.
  128. // put separators
  129. var deckLine = $('#affichageDateDC').html()+' '+$('#affichageDate').html();
  130. deckLine = deckLine.replace(/ /g, ' | ');
  131. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command", deckLine]));
  132. break;
  133. case 'd' : // display converted standard date to DC's date
  134. var date;
  135. try {
  136. date = get_DC_date(cast_date(this.parameter));
  137. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command", date]));
  138. }
  139. catch (e) {
  140. deckLines.push(Deck.getHTMLElementLineTab(
  141. ["span", "custom_command", "Date ancienne invalide ("],
  142. ["span", "custom_command red_info", "jj"],
  143. ["span", "custom_command green_info", "mm"],
  144. ["span", "custom_command orange_info", "aaaa"],
  145. ["span", "custom_command", ")"]));
  146. deckLines.push(Deck.getHTMLElementLineTab(
  147. ["span", "custom_command", "- "],
  148. ["span", "custom_command red_info", "jj"],
  149. ["span", "custom_command"," : 00 -> 31, "],
  150. ["span", "custom_command red_info", "Jour"]));
  151. deckLines.push(Deck.getHTMLElementLineTab(
  152. ["span", "custom_command", "- "],
  153. ["span", "custom_command green_info", "mm"],
  154. ["span", "custom_command"," : 00 -> 12, "],
  155. ["span", "custom_command green_info", "Mois"]));
  156. deckLines.push(Deck.getHTMLElementLineTab(
  157. ["span", "custom_command", "- "],
  158. ["span", "custom_command orange_info", "aaaa"],
  159. ["span", "custom_command"," : 0000 -> 9999, "],
  160. ["span", "custom_command orange_info", "Année"]));
  161. }
  162. break;
  163. case 't' : // display converted DC's date to standard date
  164. var date;
  165. try {
  166. date = get_date_form_DC(this.parameter);
  167. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command", date]));
  168. }
  169. catch(e) {
  170. deckLines.push(Deck.getHTMLElementLineTab(
  171. ["span", "custom_command", "Point Temporel Impérial invalide ("],
  172. ["span", "custom_command red_info", "x"],
  173. ["span", "custom_command green_info", "/xxx"],
  174. ["span", "custom_command orange_info", ".x"],
  175. ["span", "custom_command", ")"]));
  176. deckLines.push(Deck.getHTMLElementLineTab(
  177. ["span", "custom_command", "- "],
  178. ["span", "custom_command red_info", "x"],
  179. ["span", "custom_command"," : 1 -> 7, "],
  180. ["span", "custom_command red_info", "Jour"]));
  181. deckLines.push(Deck.getHTMLElementLineTab(
  182. ["span", "custom_command", "- "],
  183. ["span", "custom_command green_info", "/xxx"],
  184. ["span", "custom_command"," : 000 -> 999, "],
  185. ["span", "custom_command green_info", "Année"]));
  186. deckLines.push(Deck.getHTMLElementLineTab(
  187. ["span", "custom_command", "- "],
  188. ["span", "custom_command orange_info", ".x"],
  189. ["span", "custom_command"," : 1 -> 5, "],
  190. ["span", "custom_command orange_info", "Heptade"]));
  191. }
  192. break;
  193. default : // display the manual ("a" argument)
  194. deckLines = CommandLine_DC_time.getManualLines();
  195. break;
  196. }
  197. deck.putResultsLines(deckLines);
  198. };
  199. };
  200. CommandLine_DC_time.cumputing_skill_need = cumputing_skill_need;
  201. CommandLine_DC_time.check_character_skill = function() {
  202. if (CommandLine_DC_time.cumputing_skill_need <= $('.stat_6_entier').first().html())
  203. return true;
  204. return false;
  205. };
  206.  
  207. CommandLine_DC_time.getManualLines = function() {
  208. manualLines = new Array();
  209. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  210. ["span", "custom_command couleur_jaune important_data","date: "]);
  211. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  212. ["span", "custom_command","Affiche la date ou en convertit les différents formats"]);
  213. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  214. ["span", "custom_command","Arguments facultatifs : "]);
  215.  
  216. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  217. ["span", "custom_command", "- "],
  218. ["span", "custom_command couleur_jaune", "d "],
  219. ["span", "custom_command red_info", "jj"],
  220. ["span", "custom_command green_info", "mm"],
  221. ["span", "custom_command orange_info", "aaaa"],
  222. ["span", "custom_command", " : Convertir une "],
  223. ["span", "custom_command couleur_jaune", "d"],
  224. ["span", "custom_command", "-ate ancienne"]);
  225. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  226. ["span", "custom_command", "- "],
  227. ["span", "custom_command couleur_jaune", "t "],
  228. ["span", "custom_command red_info", "x"],
  229. ["span", "custom_command green_info", "/xxx"],
  230. ["span", "custom_command orange_info", ".x"],
  231. ["span", "custom_command", " : Convertir un Point "],
  232. ["span", "custom_command couleur_jaune", "T"],
  233. ["span", "custom_command", "-emporel Impérial"]);
  234. manualLines[manualLines.length] = Deck.getHTMLElementLineTab(
  235. ["span", "custom_command", "- "],
  236. ["span", "custom_command couleur_jaune", "h "],
  237. ["span", "custom_command", " : Affiche tous les formats de date, ainsi que l'"],
  238. ["span", "custom_command couleur_jaune", "h"],
  239. ["span", "custom_command", "-eure courante"]);
  240. return manualLines;
  241. };
  242. // Help date command line 'Object'
  243. var CommandLine_DC_time_help = function () {
  244. // processing the command
  245. // input : Deck object
  246. this.execute = function(deck) {
  247. deck.putResultsLines(CommandLine_DC_time.getManualLines());
  248. };
  249. };
  250. // Help date command line 'Object'
  251. var CommandLine_help_updated = function () {
  252. // processing the command
  253. // input : Deck object
  254. this.execute = function(deck) {
  255. var deckLines = new Array();
  256. deckLines.push(Deck.getHTMLElementLineTab(["span", "custom_command important_data", "date"]));
  257. deck.putResultsLines(deckLines, 'toOldResults');
  258. };
  259. };
  260.  
  261. // Deck 'Object'
  262. var Deck = function (id) {
  263. this.id = id;
  264.  
  265. // Use it with result of Deck.getHTMLElementLineTab() (see below)
  266. // input : lines[], 'mode'
  267. // optional : mode. Whatever's given, append to old results line.
  268. this.putResultsLines = function (lines, mode) {
  269. mode = (undefined === mode) ? 'zone_ecrit' : 'ligne_resultat_fixed';
  270. for(var i = 0; i < lines.length; i++) {
  271. var resultsDiv = document.createElement('div');
  272. resultsDiv.className = "ligne_resultat_fixed";
  273. for(var j = 0; j < lines[i].length; j++) {
  274. var domElement = document.createElement(lines[i][j].type);
  275. domElement.className = lines[i][j]['class'];
  276. domElement.appendChild(document.createTextNode(lines[i][j].text));
  277. resultsDiv.appendChild(domElement);
  278. }
  279.  
  280. $("#" + this.id + " ." + mode).append(resultsDiv);
  281. }
  282. };
  283. };
  284.  
  285. // Start to fuck the DOM
  286. // DO NOT USE, NEVER, I WARNED YOU FOOL
  287. Deck.getHtmlElementTab = function(type, elementClass, text) {
  288. var anElement = new Array();
  289. anElement.type = type;
  290. anElement["class"] = elementClass;
  291. anElement.text = text;
  292. return anElement;
  293. };
  294.  
  295. // input : [ [type_of_dom_element, class, text_node], ... ]
  296. Deck.getHTMLElementLineTab = function() {
  297. var lineTab = new Array();
  298. for(var i = 0; i < arguments.length; i++) {
  299. lineTab[lineTab.length] = Deck.getHtmlElementTab(arguments[i][0],arguments[i][1],arguments[i][2]);
  300. }
  301. return lineTab;
  302. };
  303. // Dom fucked
  304. $(document).ajaxComplete( function(a,b,c) {
  305. // Get and store the server date
  306. server_date = new Date(b.getResponseHeader('Date'));
  307. if(first_sync)
  308. old_server_date = server_date;
  309. first_sync = false;
  310.  
  311. // Handle custom deck command
  312. if(/Command/.test(c.url)) {
  313. var deckId = 'db_deck_' + c.data.match(/[0-9]*$/)[0];
  314. var commandLine_text = $('#' + deckId + ' .ligne_ecrite_fixed').last().find('input').val();
  315. var deck = new Deck(deckId);
  316. var commandLine;
  317. // Handle Date command
  318. if(/^date/gi.test(commandLine_text)) {
  319. // Bind Command and Deck objects
  320. commandLine = new CommandLine_DC_time(commandLine_text);
  321. commandLine.execute(deck);
  322. }
  323. // Handle help Date command
  324. else if(/^help date/gi.test(commandLine_text)) {
  325. // Bind Command and Deck objects
  326. commandLine = new CommandLine_DC_time_help();
  327. commandLine.execute(deck);
  328. }
  329. // Handle help Date command
  330. else if(/^help$/gi.test(commandLine_text)) {
  331. // Bind Command and Deck objects
  332. commandLine = new CommandLine_help_updated();
  333. commandLine.execute(deck);
  334. }
  335. }
  336. });
  337. handle_DC_date();
  338. setInterval(function() { handle_DC_date(); }, 1000);
  339. });
  340. console.log('DC - Time Updated started');