Capitan

Aide à la recherche de cachettes de Capitan

  1. // ==UserScript==
  2. // @author TilK, Dabihul, Rouletabille
  3. // @description Aide à la recherche de cachettes de Capitan
  4. // @grant GM_getValue
  5. // @grant GM_setValue
  6. // @grant GM_deleteValue
  7. // @grant GM_listValues
  8. // @include */mountyhall/View/TresorHistory*
  9. // @include */mountyhall/MH_Play/Play_a_TrouverCachette2*
  10. // @include */mountyhall/MH_Play/Play_equipement.php*
  11. // @include */mountyhall/MH_Taniere/TanierePJ_o_Stock.php*
  12. // @include */mountyhall/MH_Play/Play_a_ActionResult.php*
  13. // @exclude *mh2.mh.raistlin.fr*
  14. // @exclude *mzdev.mh.raistlin.fr*
  15. // @name Capitan
  16. // @version 8.8.19
  17. // @namespace https://greasyfork.org/users/70018
  18. // ==/UserScript==
  19.  
  20. /****************************************************************
  21. * Aide à la recherche de cachettes de Capitan *
  22. * Développé par Mini TilK (n°36216) *
  23. * mini@tilk.info *
  24. *****************************************************************
  25. * Adapté pour GreaseMonkey par Dabihul (n°79738) *
  26. * Roule : ajout https 07/08/2016 *
  27. *****************************************************************
  28. * Pour utiliser la recherche, allez des les infos *
  29. * de la carte de la cachette du capitan *
  30. * Une fois ceci fait, *
  31. * toutes les recherches seront sauvergardées *
  32. * Et dans le détail de la carte vous verrez *
  33. * Le nombre de cachettes possibles et leur position *
  34. ****************************************************************/
  35.  
  36. /*
  37. Roule 06/02/2024 V8.8.14
  38. Remise en route
  39. Roule 01/05/2023 V8.8.13
  40. Adaptation modif de présentation MH
  41. Roule 27/11/2021 V8.8.12
  42. Fix résultat pas remis à zéro quand on affiche une 2e fois
  43. Roule 03/01/2021 V8.8.10
  44. Réécriture de la recherche des solutions
  45. Roule 26/11/2020 V8.8.09
  46. Fix suppression d'essai
  47. Roule 16/10/2020 V8.8.08
  48. Adaptation à des modifications MH
  49. Roule 07/10/2020 V8.8.07
  50. Adaptation à des modifications MH
  51. disciple 17/06/2020 V8.8.06
  52. Correction ± #2
  53. Roule 05/08/2018 V8.8.04
  54. Saut de version suite à une erreur de numérotation
  55. Correction ± (Tu es => Vous êtes)
  56. Roule 05/08/2018 V8.2.03
  57. Passage en objet pour assurer l'indépendance par rapport aux autres scripts
  58. Blindage de la détection sous/hors GreaseMonkey ou ViolentMonkey
  59. Roule 05/08/2018 V8.1.67
  60. Correction ±
  61. Roule 04/08/2018 V8.1.66
  62. Utilisation hors GM
  63. Roule 25/08/2017 V8.1.61
  64. Réactivation de la gestion des signes (+/-)
  65. Roule 23/11/2016 V8.1.6
  66. Adaptation à l'affichage en popup dans les tanières (méthode toujours très discutable par setInterval)
  67. Roule 09/12/2016 V8.1.5
  68. Nouvelle méthode de migration des essais V1.0 par copier/coller de tout pref.js
  69. Roule 23/11/2016 V8.1.4
  70. Adaptation à l'affichage en popup du détail d'un équipement (méthode très discutable par setInterval)
  71. Roule 14/10/2016 V8.1.3
  72. simplification de l'entête GM (include)
  73. passage à greasyfork
  74. Roule 24 à 26/08/2016 V8.1.2
  75. Ajout outils de récupération des recherches pré Greasemonkey
  76. Roule 15/08/2016 V8.1.1
  77. Ajout demande d'avis sur la position courante sur le site Psyko-Chasseurs
  78. Quelques corrections de calcul (on avait du NaN)
  79. Roule 08 à 10/08/2016
  80. recopie ici de this.appendButton() version MZ
  81. Ajout liste des essais et possibilité d'en supprimer
  82. Ajout lien vers Psyko Chasseurs
  83. Adaptation aux IDs dans la page de résultat d'une recherche de cachette (et plus besoin de stocker le numéro de carte)
  84. */
  85.  
  86. /* 05/08/2018 passage en objet
  87. avantages
  88. indépendance du nomage par rapport aux autres scripts (il y avait effectivement un souci aléatoire sans doute lié à une collision de nom)
  89. inconvénient
  90. syntaxe plus compliquée (utilisation de this, déclaration pas naturelle des fonctions
  91. complexité pour l'utilisation des callback (par exemple setIntervale)
  92. */
  93.  
  94. var oCAPITAN_MH_ROULE;
  95. if (oCAPITAN_MH_ROULE instanceof Object) {
  96. window.console.log("script capitan déjà chargé"); // ça arrive dans le cas de l'affichage des détails d'une carte en popup
  97. } else {
  98. oCAPITAN_MH_ROULE = {
  99. bDebug: false,
  100. appendButton: function(paren,value,onClick) {
  101. var input = document.createElement('input');
  102. input.type = 'button';
  103. input.className = 'mh_form_submit';
  104. input.value = value;
  105. input.onmouseover = function(){this.style.cursor='pointer';};
  106. if (onClick) input.onclick = onClick;
  107. paren.appendChild(input);
  108. return input;
  109. },
  110.  
  111. /* Ajout des éléments manquants de libs */
  112. isPage: function(url) {
  113. return window.location.href.indexOf(url)!=-1;
  114. },
  115.  
  116. insertTitle: function(next,txt) {
  117. var div = document.createElement('div');
  118. div.className = 'titre2';
  119. this.appendText(div,txt);
  120. this.insertBefore(next,div);
  121. },
  122.  
  123. insertBefore: function(next,el) {
  124. next.parentNode.this.insertBefore(el,next);
  125. },
  126.  
  127. // Roule 08/08/2016 ajout cssClass
  128. appendTr: function(tbody, cssClass) {
  129. var tr = document.createElement('tr');
  130. tbody.appendChild(tr);
  131. if (cssClass) tr.className = cssClass;
  132. return tr;
  133. },
  134.  
  135. appendTd: function(tr) {
  136. var td = document.createElement('td');
  137. if(tr) { tr.appendChild(td); }
  138. return td;
  139. },
  140.  
  141. appendText: function(paren,text,bold) {
  142. if(bold) {
  143. var b = document.createElement('b');
  144. b.appendChild(document.createTextNode(text));
  145. paren.appendChild(b);
  146. }
  147. else
  148. paren.appendChild(document.createTextNode(text));
  149. },
  150.  
  151. appendTdText: function(tr,text,bold) {
  152. var td = this.appendTd(tr);
  153. this.appendText(td,text,bold);
  154. return td;
  155. },
  156. /* */
  157.  
  158. sortNumber: function(a,b) {
  159. return b-a;
  160. },
  161.  
  162. removeTab: function(tab, i) {
  163. var newTab = new Array();
  164. for(var j=0;j<i;j++)
  165. {
  166. newTab.push(tab[j]);
  167. }
  168. for(var j=i+1;j<tab.length;j++)
  169. {
  170. newTab.push(tab[j]);
  171. }
  172. return newTab;
  173. },
  174. cache: new Array(),
  175.  
  176. tabToString: function(tab) {
  177. var string = tab[0];
  178. for(var i=1;i<tab.length;i++)
  179. string+=";"+tab[i];
  180. return string;
  181. },
  182.  
  183. extractPosition: function(nombre, indice) {
  184. if((nombre+"").length<=indice)
  185. return "%";
  186. indice = (nombre+"").length - 1 - indice;
  187. return (nombre+"").substring(indice,indice+1);
  188. },
  189.  
  190. comparePos: function(x,y,n,x1,y1,n1) {
  191. x = Math.abs(x);
  192. y = Math.abs(y);
  193. n = Math.abs(n);
  194. x1 = Math.abs(x1);
  195. y1 = Math.abs(y1);
  196. n1 = Math.abs(n1);
  197. var nbGood=0;
  198. for(var i=0;i<(x+"").length;i++)
  199. if(this.extractPosition(x,i)==this.extractPosition(x1,i))
  200. nbGood++;
  201. for(var i=0;i<(y+"").length;i++)
  202. if(this.extractPosition(y,i)==this.extractPosition(y1,i))
  203. nbGood++;
  204. for(var i=0;i<(n+"").length;i++)
  205. if(this.extractPosition(n,i)==this.extractPosition(n1,i))
  206. nbGood++;
  207. return nbGood;
  208. },
  209.  
  210. signe: function(x) {
  211. if(x<0)
  212. return -1;
  213. return 1;
  214. },
  215.  
  216. getPosFromArray: function(liste,begin,length) {
  217. var pos="";
  218. for(var i=begin;i<begin+length;i++)
  219. pos+=""+liste[i];
  220. return parseInt(pos, 10);
  221. },
  222.  
  223. toggleTableau: function() {
  224. var tbody = this.parentNode.parentNode.parentNode.childNodes[1];
  225.  
  226. tbody.setAttribute('style', !tbody.getAttribute('style') || tbody.getAttribute('style') == '' ? 'display:none;' : '');
  227. },
  228.  
  229. createCase: function(titre,table,width) {
  230. if(width==null)
  231. width="120";
  232. var tr = this.appendTr(table, 'mh_tdpage');
  233.  
  234. var td = this.appendTdText(tr, titre, true);
  235. td.setAttribute('class', 'mh_tdpage');
  236. td.setAttribute('width', width);
  237. td.setAttribute('align', 'center');
  238.  
  239. return td;
  240. },
  241.  
  242. showXYN: function(tabXYN, signes) {
  243. var sx = '±';
  244. var sy = '±';
  245. if (signes) {
  246. sx = '+';
  247. sy = '+';
  248. if (signes[0] < 0) sx = '-';
  249. if (signes[1] < 0) sy = '-';
  250. }
  251. return "X = " + sx + Math.abs(tabXYN[0]) + ", Y = " + sy + Math.abs(tabXYN[1]) + ", N = -" + Math.abs(tabXYN[2]);
  252. },
  253.  
  254. is200: function(tabXYN) { // vrai si au moins une coord >= 200
  255. if (Math.abs(tabXYN[0]) >= 200) return true;
  256. if (Math.abs(tabXYN[1]) >= 200) return true;
  257. if (Math.abs(tabXYN[2]) >= 200) return true;
  258. return false;
  259. },
  260.  
  261. gbody: null,
  262.  
  263. generateTable: function(listeSolutions, signes) {
  264. var table = document.createElement('table');
  265. table.setAttribute('class', 'mh_tdborder');
  266. table.setAttribute('border', '0');
  267. table.setAttribute('cellspacing', '1');
  268. table.setAttribute('cellpadding', '4');
  269. table.setAttribute('style', 'width: 400px;');
  270. table.setAttribute('align', 'center');
  271.  
  272. if(listeSolutions.length==1)
  273. {
  274. var thead = document.createElement('thead');
  275. var tr = this.appendTr(thead, 'mh_tdtitre');
  276. var td = this.appendTdText(tr, "Position de la cachette : " + this.showXYN(listeSolutions[0], signes), true);
  277. td.setAttribute('align', 'center');
  278. table.appendChild(thead);
  279. return table;
  280. }
  281. else if(listeSolutions.length==0)
  282. {
  283. var thead = document.createElement('thead');
  284. var tr = this.appendTr(thead, 'mh_tdtitre');
  285. var td = this.appendTdText(tr, "Aucune solution trouvée.", true);
  286. td.setAttribute('align', 'center');
  287. table.appendChild(thead);
  288. return table;
  289. }
  290.  
  291. var thead = document.createElement('thead');
  292. var tr = this.appendTr(thead, 'mh_tdtitre');
  293. var td = this.appendTdText(tr, "Il y a "+listeSolutions.length+" positions possibles", true);
  294. td.setAttribute('align', 'center');
  295. table.appendChild(thead);
  296.  
  297. this.gbody = document.createElement('tbody');
  298. table.appendChild(this.gbody);
  299.  
  300. var bExist200 = false;
  301. for (var i = 0; i < listeSolutions.length; i++) {
  302. if (this.is200(listeSolutions[i]))
  303. bExist200 = true;
  304. else
  305. this.createCase(this.showXYN(listeSolutions[i], signes),this.gbody,400);
  306. }
  307. if (bExist200) {
  308. this.createCase("Les suivantes sont peu probables car trop en dehors du Hall",this.gbody,400);
  309. for (var i = 0; i < listeSolutions.length; i++) {
  310. if (this.is200(listeSolutions[i]))
  311. this.createCase(this.showXYN(listeSolutions[i], signes),this.gbody,400);
  312. }
  313. }
  314.  
  315. td.addEventListener("click", this.toggleTableau, true);
  316. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  317. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  318. this.gbody.setAttribute('style', 'display:none;');
  319.  
  320. return table;
  321. },
  322.  
  323. gListeSolutions: new Array(), // tableau de tableaux des 3 coord
  324. gEssais: new Array(), // tableau d'objets de type oEssai
  325. oMort: null, // objet de type oEssai sans "c"
  326.  
  327. oEssai: function(x, y, n, c) { // déclaration d'objet méthode "function"
  328. if (y == undefined) { // initialisation à partir d'une chaine séparée par ";"
  329. let t = x.split(";");
  330. this.x = parseInt(t[0], 10);
  331. this.y = parseInt(t[1], 10);
  332. this.n = parseInt(t[2], 10);
  333. this.c = parseInt(t[3], 10);
  334. } else {
  335. this.x = x;
  336. this.y = y;
  337. this.n = n;
  338. this.c = c;
  339. }
  340.  
  341. this.xAbs = Math.abs(this.x);
  342. this.yAbs = Math.abs(this.y);
  343. this.nAbs = Math.abs(this.n);
  344. this.xText = this.xAbs + '';
  345. if (this.xText.length < 2) this.xText = '0' + this.xText;
  346. this.yText = this.yAbs + '';
  347. if (this.yText.length < 2) this.yText = '0' + this.yText;
  348. this.nText = this.nAbs + '';
  349. if (this.nText.length < 2) this.nText = '0' + this.nText;
  350.  
  351. this.nbMatchesOne = function (t1, t2) {
  352. t1 = '' + parseInt(t1, 10); // virer le zéro à gauche. MH n'en tient pas compte quand il compte le nombre de match
  353. t2 = '' + parseInt(t2, 10);
  354. var nRet = 0;
  355. var l1 = t1.length;
  356. var l2 = t2.length;
  357. for (var i = 0; i < l1 && i < l2; i++)
  358. if (t1.substring(l1 - (i+1), l1 - i) == t2.substring(l2 - (i+1), l2 - i)) nRet++;
  359. return nRet;
  360. };
  361.  
  362. this.isCompatible = function(tabCoord) { // vérifie si c'est compatible avec les coord passées en argument sous forme de tableau de chaines
  363. var nMatches = this.nbMatchesOne(this.xText, tabCoord[0]);
  364. nMatches += this.nbMatchesOne(this.yText, tabCoord[1]);
  365. nMatches += this.nbMatchesOne(this.nText, tabCoord[2]);
  366. return nMatches == this.c
  367. };
  368.  
  369. this.forPsychoChasseur = function() { // rend le bout de texte à mettre dans l'URL vers l'outil des Psycho Chasseurs
  370. return this.xAbs + '+' + this.yAbs + '+' + this.nAbs + '+' + this.c;
  371. };
  372.  
  373. this.nbChiffre = function() { // rend le nombre de chiffres (une coord à 1 chiffe en donne 2, le "0" et le chiffre des unités)
  374. return this.xText.length + this.yText.length + this.nText.length;
  375. };
  376.  
  377. this.tabOccurenceChiffre = function() { // le nombre d'occurrence de chaque chiffre (0 à 9) dans les coord
  378. var tabRet = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  379. this.addOccurenceChiffre(tabRet, this.xText);
  380. this.addOccurenceChiffre(tabRet, this.yText);
  381. this.addOccurenceChiffre(tabRet, this.nText);
  382. return tabRet;
  383. };
  384.  
  385. this.addOccurenceChiffre = function(t, s) {
  386. var l = s.length;
  387. for (var i = 0; i < l; i++) {
  388. var c = s.substring(i, i+1);
  389. var n = parseInt(c, 10);
  390. if (!isNaN(n)) t[n]++;
  391. }
  392. };
  393. },
  394.  
  395. calculeSolution2: function() { // calcule les solutions à partir des propriétés de oMort et gEssai
  396. var oContexte = {
  397. // nombre de chiffres (une coord à 1 chiffe en donne 2, le "0" et le chiffre des unités) dans les coord de la mort du Capitan
  398. nbChiffre: this.oMort.nbChiffre(),
  399. // nombre d'occurrence de chaque chiffre (0 à 9) dans les coord de la mort du Capitan
  400. tabOccurrenceChiffre: this.oMort.tabOccurenceChiffre(),
  401. // Si on a plus de 6 chiffres, il y a des coordonnées à 3 chiffres, leur nombre sera calculé plus loin
  402. nCoord3: undefined,
  403. // On n'a encore traité aucune coordonnée
  404. nCoordEnCours: 0, // 0:x, 1:y, 2:n
  405. // les chaines des coord en cours de construction
  406. tabCoord: ['', '', ''],
  407. };
  408. oContexte.nCoord3 = oContexte.nbChiffre - 6;
  409. if (this.bDebug) window.console.log("CAPITAN calculeSolution2 contexte initial=" + JSON.stringify(oContexte));
  410.  
  411. // On lance le balayage récursif des possibilités
  412. this.gListeSolutions = new Array();
  413. this.calculeSolutionRecursifCoord(oContexte);
  414. if (this.bDebug) window.console.log("CAPITAN calculeSolution2 résultat=" + JSON.stringify(this.gListeSolutions));
  415. },
  416.  
  417. calculeSolutionRecursifCoord: function(oContexte) { // balayage récursif des solutions, balayage coordonnée (x, y ou n)
  418. if (oContexte.nCoord3 > 0) { // lancer le test sur une coord à 3 chiffres
  419. var newContexte = Object.assign({}, oContexte); // clone car on modifie le contexte
  420. newContexte.nCoord3--;
  421. this.calculeSolutionRecursifDigit(newContexte, 3);
  422. }
  423. if (oContexte.nCoord3 <= (2- oContexte.nCoordEnCours)) { // pas de test à 2 chiffres si toutes les coord restantes doivent être à 3 chiffres
  424. this.calculeSolutionRecursifDigit(oContexte, 2);
  425. }
  426. },
  427.  
  428. calculeSolutionRecursifDigit: function(oContexte, nChiffreThisCoord) { // balayage récursif des solutions, balayages des suites de chiffres possibles
  429. var thisCoord = '';
  430. var newContexte = Object.assign({}, oContexte); // clone car on modifie le contexte
  431. newContexte.tabCoord = oContexte.tabCoord.slice(); // clone (le clone ci-dessus est un "shallow" clone)
  432. for (var i = 0; i <= 9; i++) { // boucle sur les chiffres possibles à cette position
  433. if (oContexte.tabOccurrenceChiffre[i] == 0) continue; // chiffre non disponible
  434. newContexte.tabCoord[oContexte.nCoordEnCours] = oContexte.tabCoord[oContexte.nCoordEnCours] + i;
  435. newContexte.tabOccurrenceChiffre = oContexte.tabOccurrenceChiffre.slice(); // clone car on modifie ce tableau
  436. newContexte.tabOccurrenceChiffre[i]--;
  437. if (nChiffreThisCoord > 1) { // continuer à tirer des chiffres pour cette coord
  438. this.calculeSolutionRecursifDigit(newContexte, nChiffreThisCoord-1);
  439. continue;
  440. }
  441. // on a fini avec cette coord
  442. if (oContexte.nCoordEnCours != 2) {
  443. // continuer sur la coord suivante
  444. newContexte.nCoordEnCours = oContexte.nCoordEnCours + 1;
  445. this.calculeSolutionRecursifCoord(newContexte);
  446. continue;
  447. }
  448. // ici, on a tiré tous les chiffres des 3 coordonnées, on teste si ces coord sont compatibles avec les essais
  449. var isCompatible = true;
  450. for (oEssai of this.gEssais) {
  451. if (!oEssai.isCompatible(newContexte.tabCoord)) {
  452. isCompatible = false;
  453. break;
  454. }
  455. }
  456. if (this.bDebug && newContexte.tabCoord[0] == '03' && newContexte.tabCoord[1] == '80') {
  457. let sCause = '';
  458. if (!isCompatible) {
  459. sCause = ' bad ' + oEssai.forPsychoChasseur() + ' nMatches=';
  460. sCause += oEssai.nbMatchesOne(oEssai.xText, newContexte.tabCoord[0]);
  461. sCause += ' ' + oEssai.nbMatchesOne(oEssai.yText, newContexte.tabCoord[1]);
  462. sCause += ' ' + oEssai.nbMatchesOne(oEssai.nText, newContexte.tabCoord[2]);
  463. }
  464. window.console.log('CAPITAN calculeSolutionRecursifDigit, teste ' + newContexte.tabCoord.join("; ") + ', isCompatible=' + isCompatible + sCause);
  465. }
  466. if (isCompatible) {
  467. this.gListeSolutions.push(newContexte.tabCoord.slice()); // slice pour cloner le tableau
  468. }
  469. }
  470. },
  471.  
  472. afficheInfoCarte: function(idCarte) {
  473. var originalPosText = this.CAPITAN_getValue("capitan."+idCarte+".position");
  474. if (originalPosText === undefined) {
  475. let msg = "La recherche a été enregistrée. Mais vous n'avez pas encore affiché le détail de la carte "
  476. + idCarte + " et le « script du Capitan » ne connait pas la position de la mort du Capitan. Il ne peut pas vous en dire plus. Allez dans «  EQUIPEMENT » et affichez cette carte.";
  477. window.console.log('afficheInfoCarte_log: ' + msg);
  478. this.afficheMsg(msg, 'red');
  479. return;
  480. }
  481. var originalPos = originalPosText.split(";");
  482. if(originalPos.length!=3) {
  483. msg = 'Text non reconnu : ' + originalPosText;
  484. window.console.log('afficheInfoCarte_log: ' + msg);
  485. this.afficheMsg(msg, 'red');
  486. return;
  487. }
  488. this.oMort = new this.oEssai(originalPosText);
  489. this.gEssais = new Array();
  490. var i = 0;
  491. var essaiText;
  492. while((essaiText = this.CAPITAN_getValue("capitan."+idCarte+".essai."+i)) != null)
  493. {
  494. //this.gEssais.push(this.CAPITAN_getValue("capitan."+idCarte+".essai."+i).split(";"));
  495. this.gEssais.push(new this.oEssai(essaiText));
  496. i++;
  497. }
  498. if(this.CAPITAN_getValue("capitan."+idCarte+".this.signe") !=null)
  499. {
  500. var signes = this.CAPITAN_getValue("capitan."+idCarte+".this.signe").split(";");
  501. if (this.bDebug) window.console.log('CAPITAN afficheInfoCarte_log signes=' + JSON.stringify(signes));
  502. }
  503. else
  504. {
  505. if (this.bDebug) window.console.log('CAPITAN afficheInfoCarte_log pas de signe ' + originalPos[0] + ',' + originalPos[1] + ',' + originalPos[2]);
  506. }
  507. this.calculeSolution2();
  508. return this.generateTable(this.gListeSolutions, signes);
  509. },
  510.  
  511. getRepartitionFromCase: function(tx, ty, tn, listeSolutions) {
  512. // Roule 15/08/2016 plus que dubitatif sur ce calcul de Size, je modifie l'algorithme
  513. //var size = (";"+Math.abs(listeSolutions[0][0])+Math.abs(listeSolutions[0][0])+Math.abs(listeSolutions[0][0])).length-1;
  514. var repartition = new Array();
  515. //for(var i=0;i<size;i++)
  516. // repartition.push(0);
  517. for(var i=0;i<listeSolutions.length;i++)
  518. {
  519. var nbGood = this.comparePos(listeSolutions[i][0],listeSolutions[i][1],listeSolutions[i][2],tx,ty,tn);
  520. for (var j = repartition.length; j <= nbGood; j++) repartition.push(0); // Roule 15/08/2016 compléter le tableau selon le besoin
  521. repartition[nbGood]++;
  522. }
  523. repartition.sort(this.sortNumber);
  524. return repartition;
  525. },
  526.  
  527. getMeanPositionNumber: function(repartition,nbSolutions) {
  528. var result=0;
  529. for(var i=0;i<repartition.length;i++)
  530. {
  531. result+=repartition[i]*repartition[i];
  532. }
  533. return result/nbSolutions;
  534. },
  535.  
  536. // Roule 08/08/2016 passage numTroll en paramètre
  537. // Roule 15/08/2016 passage position courante en paramètre (tableau des 3 valeurs)
  538. newRecherche: function(listeSolutions, currentPos) {
  539. if(listeSolutions.length<=1)
  540. return null;
  541. // Roule 15/08/2016 plus que dubitatif sur ce calcul de Size, j'utilise repartition.length
  542. //var size = (";"+Math.abs(listeSolutions[0][0])+Math.abs(listeSolutions[0][0])+Math.abs(listeSolutions[0][0])).length-1;
  543. var repartition = this.getRepartitionFromCase(currentPos[0], currentPos[1], currentPos[2], listeSolutions);
  544. var size = repartition.length;
  545. if (this.bDebug) window.console.log('newRecherche_log: this.newRecherche_log, repartition=' + JSON.stringify(repartition));
  546.  
  547. var table = document.createElement('table');
  548. table.setAttribute('class', 'mh_tdborder');
  549. table.setAttribute('border', '0');
  550. table.setAttribute('cellspacing', '1');
  551. table.setAttribute('cellpadding', '4');
  552. table.setAttribute('style', 'width: 400px;');
  553. table.setAttribute('align', 'center');
  554.  
  555. var nbNotZero = 0;
  556. for(var i=0;i<size;i++)
  557. {
  558. if(repartition[i]!=0)
  559. nbNotZero++;
  560. }
  561. var string = "Il y a une utilité de faire une recherche en X = "+currentPos[0]+" Y = "+currentPos[1]+" N = "+currentPos[2];
  562. if(nbNotZero<=1)
  563. {
  564. //
  565. var minsolution = listeSolutions.length;
  566. var newpos = "";
  567. var isNotN = true;
  568. for(var dx=-1;dx<=1;dx++)
  569. for(var dy=-1;dy<=1;dy++)
  570. for(var dn=0;dn!=-3;dn=(dn==0?1:dn-2))
  571. {
  572. if(dx==0 && dy==0 && dn==0)
  573. continue;
  574. var tmprepartition = this.getRepartitionFromCase(currentPos[0]+dx, currentPos[1]+dy, currentPos[2]+dn, listeSolutions);
  575. var tmpmeanscore = this.getMeanPositionNumber(tmprepartition,listeSolutions.length);
  576. if(((dn==0 || !isNotN) && minsolution>=tmpmeanscore) || (dn!=0 && isNotN && tmpmeanscore<=2*minsolution/3))
  577. {
  578. minsolution = tmpmeanscore;
  579. repartition = tmprepartition;
  580. newpos = "X = "+(currentPos[0]+dx)+" Y = "+(currentPos[1]+dy)+" N = "+(currentPos[2]+dn);
  581. isNotN = (dn==0);
  582. }
  583. }
  584. if(minsolution == listeSolutions.length)
  585. {
  586. var thead = document.createElement('thead');
  587. var tr = this.appendTr(thead, 'mh_tdtitre');
  588. var td = this.appendTdText(tr, "Il n'y a aucune utilité de faire une recherche en X = "+currentPos[0]+" Y = "+currentPos[1]+" N = "+currentPos[2], true);
  589. td.setAttribute('align', 'center');
  590. table.appendChild(thead);
  591. return table;
  592. }
  593. string = "Conseil : allez faire une recherche en "+newpos;
  594. }
  595.  
  596. var thead = document.createElement('thead');
  597. var tr = this.appendTr(thead, 'mh_tdtitre');
  598. var td = this.appendTdText(tr,string, true);
  599. td.setAttribute('align', 'center');
  600. table.appendChild(thead);
  601. var tbody = document.createElement('tbody');
  602. table.appendChild(tbody);
  603. for(var i=0;i<size;i++)
  604. {
  605. if(i==size-1)
  606. {
  607. if(repartition[i]!=0)
  608. this.createCase(Math.round(100*repartition[i]/listeSolutions.length)+"% de chance d'éliminer "+(listeSolutions.length-repartition[i])+" positions possibles",tbody,400);
  609. }
  610. else
  611. {
  612. var n=1;
  613. while((i+n)<size && repartition[i]==repartition[i+n])
  614. n++;
  615. if(repartition[i]!=0)
  616. this.createCase(Math.round(100*n*repartition[i]/listeSolutions.length)+"% de chance d'éliminer "+(listeSolutions.length-repartition[i])+" positions possibles",tbody,400);
  617. i+=n-1;
  618. }
  619. }
  620.  
  621. td.addEventListener("click", this.toggleTableau, true);
  622. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  623. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  624. tbody.setAttribute('style', 'display:none;');
  625. return table;
  626. },
  627.  
  628. // Roule 08/08/2016 récupération numéro de Troll dans la page HTML
  629. getNumTroll: function() {
  630. /* 07/10/2020, l'info n'est plus dans la page, on se replie sur le localStorage
  631. var infoObjet = document.evaluate("//td[@class = 'mh_tdtitre']/text()[contains(.,'Propriétaire actuel')]",
  632. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  633. if (this.bDebug) {
  634. if (infoObjet) {
  635. window.console.log('CAPITAN getNumTroll: infoObjet.nodeValue=' + infoObjet.nodeValue);
  636. } else {
  637. window.console.log('CAPITAN getNumTroll: infoObjet est nul');
  638. }
  639. }
  640. var numTroll = 0;
  641. if (infoObjet)
  642. {
  643. // infoObjet.nodeValue=Propriétaire actuel : TROLL - 91305. Rouletabille
  644. var m = infoObjet.nodeValue.match(/troll[ -]*(\d*)[. +]/i);
  645. if (m) {
  646. if (this.bDebug) window.console.log('CAPITAN getNumTroll: m=' + m);
  647. if (m.length > 1) numTroll = parseInt(m[1]);
  648. }
  649. }
  650. */
  651. numTroll = parseInt(window.localStorage['NUM_TROLL']);
  652. if (this.bDebug) window.console.log('CAPITAN getNumTroll: numTroll=' + numTroll);
  653. return numTroll;
  654. },
  655.  
  656. getIDCarte: function() {
  657. var infoObjet = document.evaluate("//h2[@class = 'titre2']/text()[contains(.,'Carte de la Cachette')]",
  658. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  659. // si échec, essayer avec l'ancienne méthode
  660. if (!infoObjet) infoObjet = document.evaluate("//td[@class = 'titre2']/text()[contains(.,'Carte de la Cachette')]",
  661. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  662. // si échec, essayer avec l'ancienne méthode
  663. if (!infoObjet) infoObjet = document.evaluate("//div[@class = 'titre2']/text()[contains(.,'Carte de la Cachette')]",
  664. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  665. var idCarte = 0;
  666. if(infoObjet)
  667. {
  668. idCarte = parseInt(infoObjet.nodeValue.replace('[', ''));
  669. }
  670. return idCarte;
  671. },
  672.  
  673. analyseObjectCallback: function() { // appelle analyseObject dans le contexte de l'objet
  674. oCAPITAN_MH_ROULE.analyseObject();
  675. },
  676.  
  677. analyseObject: function() {
  678. var eSpacer = document.getElementById('spacerMZCapitan');
  679. if (eSpacer) return; // déjà affiché
  680. var numTroll = this.getNumTroll(); // Roule 08/08/2016 récupération numéro de Troll dans la page HTML
  681. if( !numTroll) {
  682. window.console.log('CAPITAN analyseObject: *** erreur *** pas de numéro de Trõll');
  683. return;
  684. }
  685. var idCarte = this.getIDCarte();
  686. if (this.bDebug && idCarte == 11987020) { // test Roule
  687. var originalPos = '-101;-8;-73';
  688. this.CAPITAN_setValue("capitan."+idCarte+".position", originalPos)
  689. } else if (idCarte > 0) {
  690. var originalPos = this.CAPITAN_getValue("capitan."+idCarte+".position");
  691. } else {
  692. var parentElt = document.body;
  693. var modalElt = document.evaluate("//div[@class = 'modal']",
  694. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  695. if (modalElt && !modalElt.errorIDDone) {
  696. modalElt.appendChild(document.createTextNode("Erreur à la récupération de l'ID de la carte"));
  697. modalElt.errorIDDone = true;
  698. }
  699. return;
  700. }
  701. if (this.bDebug) window.console.log('CAPITAN analyseObject: this.analyseObject numTroll=' + numTroll + ', idCarte=' + idCarte + ', originalPos=' + originalPos);
  702. if(!originalPos || originalPos == null)
  703. {
  704. var infoPos = document.evaluate("//td/text()[contains(.,'ai été tué en')]",
  705. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  706. if(!infoPos) {
  707. if (this.bDebug) window.console.log('CAPITAN analyseObject: numTroll=' + numTroll + ', idCarte=' + idCarte + ', impossible de trouver le texte de la mort du Capitan');
  708. return;
  709. }
  710. var listePos = infoPos.nodeValue.split("=");
  711. if(listePos.length!=4) {
  712. if (this.bDebug) window.console.log('CAPITAN analyseObject: numTroll=' + numTroll + ', idCarte=' + idCarte + ', impossible de trouver les coord. de la mort du Capitan ' + infoPos.nodeValue);
  713. return;
  714. }
  715. var x = parseInt(listePos[1]);
  716. var y = parseInt(listePos[2]);
  717. var n = parseInt(listePos[3]);
  718. if (this.bDebug) window.console.log('CAPITAN analyseObject: setValue("capitan.'+idCarte+'.position, '+x+";"+y+";"+n);
  719. this.CAPITAN_setValue("capitan."+idCarte+".position",x+";"+y+";"+n);
  720. }
  721. // Roule 23/11/2016 travail dans le body (ancienne version, fenêtre indépendante) ou dans la div modale (nouvelle version en "popup")
  722. var parentElt = document.body;
  723. var modalElt = document.evaluate("//div[@class = 'modal']",
  724. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  725. if (modalElt) parentElt = modalElt;
  726.  
  727. // bloc liste de solutions
  728. var table = this.afficheInfoCarte(idCarte);
  729. if (table) {
  730. var p = document.createElement('p');
  731. p.id = 'spacerMZCapitan';
  732. //window.console.log('analyseObject_log: table=' + JSON.stringify(table));
  733. p.appendChild(table);
  734. parentElt.appendChild(p);
  735. }
  736.  
  737. // position courante du Troll
  738. // Roule 08/08/2016 utilisation de localStorage car c'est là que tout_MZ stocke les coord
  739. var curPos = [];
  740. curPos[0] = parseInt(window.localStorage[numTroll+".position.X"]);
  741. curPos[1] = parseInt(window.localStorage[numTroll+".position.Y"]);
  742. curPos[2] = parseInt(window.localStorage[numTroll+".position.N"]);
  743. if (this.bDebug) window.console.log('CAPITAN analyseObject: position du troll récupérée en localStorage=' + JSON.stringify(curPos));
  744.  
  745. // bloc utilité de faire une recherche sur la position courante
  746. table = this.newRecherche(this.gListeSolutions, curPos);
  747. if(table!=null)
  748. {
  749. var p = document.createElement('p');
  750. p.appendChild(table);
  751. parentElt.appendChild(p);
  752. // bloc ajout de nouvelle recherche
  753. this.createNewRecherche(parentElt);
  754. }
  755.  
  756. // Roule 08/08/2016 bloc des recherches mémorisées
  757. if(this.gEssais)
  758. {
  759. table = this.prevRecherche(idCarte);
  760. var p = document.createElement('p');
  761. p.id = 'MZ_capitan_p_liste_memo';
  762. p.appendChild(table);
  763. parentElt.appendChild(p);
  764. // Roule 08/08/2016 bloc préparant les infos pour l'outil Mamoune (Psyko-Chasseurs)
  765. table = this.blocMamoune(idCarte, curPos);
  766. if(table!=null)
  767. {
  768. p = document.createElement('p');
  769. p.appendChild(table);
  770. parentElt.appendChild(p);
  771. }
  772. }
  773.  
  774. // Roule 25/08/2016 récupération des anciennes recherches (préférences)
  775. table = this.PrepareRecupFromPreferences();
  776. if(table!=null)
  777. {
  778. p = document.createElement('p');
  779. p.appendChild(table);
  780. parentElt.appendChild(p);
  781. }
  782.  
  783. // Roule 24/08/2016 récupération des anciennes recherches (localStorage)
  784. table = this.PrepareRecupFromLocalStorage(idCarte);
  785. if(table!=null)
  786. {
  787. p = document.createElement('p');
  788. p.appendChild(table);
  789. parentElt.appendChild(p);
  790. }
  791. },
  792.  
  793. afficheMsg: function(msg, color) {
  794. let p = document.createElement('p');
  795. if (color) p.style.color = color;
  796. p.appendChild(document.createTextNode('MZ Capitan : ' + msg));
  797. let contMsg = document.getElementById('msgDiv');
  798. if (!contMsg) {
  799. contMsg = document.evaluate("//div[@class = 'modal']",
  800. document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  801. }
  802. if (!contMsg) contMsg = document.body;
  803. contMsg.appendChild(p);
  804. },
  805.  
  806. // Roule 08/08/2016
  807. blocMamoune: function(idCarte, currentPos) {
  808. var table = document.createElement('table');
  809. table.setAttribute('class', 'mh_tdborder');
  810. table.setAttribute('border', '0');
  811. table.setAttribute('cellspacing', '1');
  812. table.setAttribute('cellpadding', '4');
  813. table.setAttribute('style', 'width: 400px;');
  814. table.setAttribute('align', 'center');
  815.  
  816. var thead = document.createElement('thead');
  817. var tr = this.appendTr(thead, 'mh_tdtitre');
  818. var td = this.appendTdText(tr, "Outil du cercle des Psyko-Chasseurs", true);
  819. td.setAttribute('align', 'center');
  820. //td.setAttribute('title', 'sélectionnez (triple-clic), copiez et collez dans l\'outil des Psyko-Chasseurs');
  821. table.appendChild(thead);
  822.  
  823. var tbody = document.createElement('tbody');
  824. table.appendChild(tbody);
  825.  
  826. // http://mountyhall.dispas.net/dynamic/outils_capitan.php?x=101&y=8&n=73&t=3+77+30+1%0D%0A37+57+48+0%0D%0A33+32+29+1%0D%0A87+20+74+2%0D%0A17+56+63+0%0D%0A22+89+78+2&voir=1&cent=100&enter=Go#
  827. var tabtxt = new Array();
  828. var currentPosAlreadyDone = false;
  829. for (var i = 0; i < this.gEssais.length; i++) {
  830. tabtxt.push(this.gEssais[i].forPsychoChasseur());
  831. if (this.gEssais[i].x == currentPos[0] && this.gEssais[i].y == currentPos[1] && this.gEssais[i].n == currentPos[2]) currentPosAlreadyDone = true;
  832. //tabtxt.push(this.gEssais[i].join('+'));
  833. //if (this.gEssais[i][0] == currentPos[0] && this.gEssais[i][1] == currentPos[1] && this.gEssais[i][2] == currentPos[2]) currentPosAlreadyDone = true;
  834. }
  835. if (!currentPosAlreadyDone) tabtxt.push(currentPos.join('+') + '+%3F'); // spécial pour demander à Mamoune ce qu'elle pense d'un essai à la position courante
  836. var tr2 = this.appendTr(tbody, 'mh_tdpage');
  837. var td2 = this.appendTd(tr2);
  838. var originalPos = this.CAPITAN_getValue("capitan."+idCarte+".position").split(";");
  839. if(originalPos.length!=3) {
  840. td2.this.appendText('Erreur\u00A0: impossible de retrouver les coordonnées de la mort');
  841. } else {
  842. var a = document.createElement('a');
  843. this.appendText(a, 'Cliquer ici pour savoir ce qu\'en pensent les Psyko-Chasseurs');
  844. a.setAttribute('href', 'http://mountyhall.dispas.net/dynamic/outils_capitan.php?x=' + originalPos[0] + '&y=' + originalPos[1] + '&n=' + originalPos[2] + '&t=' + tabtxt.join('%0D%0A') + '&voir=1&cent=100');
  845. a.setAttribute('target', 'psykochasseurs');
  846. td2.appendChild(a);
  847. }
  848.  
  849. td.setAttribute('class', 'mh_tdpage');
  850. //td.setAttribute('width', width);
  851. td.setAttribute('align', 'center');
  852.  
  853.  
  854. td.addEventListener("click", this.toggleTableau, true);
  855. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  856. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  857. td.setAttribute('colspan', 2);
  858. tbody.setAttribute('style', 'display:none;');
  859.  
  860. return table;
  861. },
  862.  
  863. prevRecherche: function(idCarte) {
  864. var table = document.createElement('table');
  865. table.setAttribute('class', 'mh_tdborder');
  866. table.setAttribute('border', '0');
  867. table.setAttribute('cellspacing', '1');
  868. table.setAttribute('cellpadding', '4');
  869. table.setAttribute('style', 'width: 400px;');
  870. table.setAttribute('align', 'center');
  871.  
  872. var thead = document.createElement('thead');
  873. var tr = this.appendTr(thead, 'mh_tdtitre');
  874. var td = this.appendTdText(tr, "Vous avez mémorisé " + this.gEssais.length + " essai" + (this.gEssais.length > 1 ? "s" : ""), true);
  875. td.setAttribute('align', 'center');
  876. table.appendChild(thead);
  877.  
  878. var tbody = document.createElement('tbody');
  879. tbody.id = 'MZ_capitan_tbody_liste_memo';
  880. table.appendChild(tbody);
  881.  
  882. for (var i = 0; i < this.gEssais.length; i++) {
  883. var td2 = this.createCase("X = " + this.gEssais[i].x + ", Y = "+this.gEssais[i].y +", N = " + this.gEssais[i].n + " => " + this.gEssais[i].c,tbody,400);
  884. var td3 = this.appendTd(td2.parentNode);
  885. var bt = this.appendButton(td3, "Supprimer", this.delRecherche);
  886. bt.idEssai = i;
  887. bt.idCarte = idCarte;
  888. td3.setAttribute('class', 'mh_tdpage');
  889. td3.setAttribute('width', 200);
  890. td3.setAttribute('align', 'center');
  891. }
  892.  
  893. td.addEventListener("click", this.toggleTableau, true);
  894. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  895. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  896. td.setAttribute('colspan', 2);
  897. tbody.setAttribute('style', 'display:none;');
  898.  
  899. return table;
  900. },
  901.  
  902. delRecherche: function(e) { // ATTENTION, cette fonction ne tourne pas dans le contexte de l'objet ("this" pointe vers le bouton)
  903. let idEssaiDel = e.target.idEssai;
  904. let idCarte = e.target.idCarte;
  905. if (oCAPITAN_MH_ROULE.bDebug) window.console.log('CAPITAN delRecherche: idEssaiDel=' + idEssaiDel + ', idCarte=' + idCarte + ', oCAPITAN_MH_ROULE.gEssais.length=' + oCAPITAN_MH_ROULE.gEssais.length);
  906. oCAPITAN_MH_ROULE.gEssais.splice(idEssaiDel, 1);
  907. if (oCAPITAN_MH_ROULE.bDebug) window.console.log('delRecherche_log ' + JSON.stringify(oCAPITAN_MH_ROULE.gEssais));
  908. let lg = oCAPITAN_MH_ROULE.gEssais.length;
  909. for (let i = 0; i < lg; i++) {
  910. let clef = "capitan." + idCarte + ".essai." + i;
  911. let oEssai = oCAPITAN_MH_ROULE.gEssais[i];
  912. let v = oEssai.x + ';' + oEssai.y + ';' + oEssai.n + ';' + oEssai.c;
  913. if (oCAPITAN_MH_ROULE.bDebug) window.console.log('CAPITAN delRecherche_log: set ' + clef + '=' + v);
  914. oCAPITAN_MH_ROULE.CAPITAN_setValue(clef, v);
  915. }
  916. for (let i = 0; i < 10; i++) { // pour être sûr, on supprime les 10 suivantes
  917. let clef = "capitan." + idCarte + ".essai." + (i + lg);
  918. if (oCAPITAN_MH_ROULE.bDebug) window.console.log('CAPITAN delRecherche_log: remove ' + clef);
  919. oCAPITAN_MH_ROULE.CAPITAN_deleteValue(clef);
  920. }
  921. let eP = document.getElementById('MZ_capitan_p_liste_memo');
  922. while (eP.lastChild) eP.removeChild(eP.lastChild);
  923. let eTable = oCAPITAN_MH_ROULE.prevRecherche(idCarte);
  924. eP.appendChild(eTable);
  925. let tbody = document.getElementById('MZ_capitan_tbody_liste_memo');
  926. tbody.style.display = ''; // show
  927. },
  928.  
  929. createNewRecherche: function(parentElt) {
  930. p = document.createElement('p');
  931.  
  932. var table = document.createElement('table');
  933. table.setAttribute('class', 'mh_tdborder');
  934. table.setAttribute('border', '0');
  935. table.setAttribute('cellspacing', '1');
  936. table.setAttribute('cellpadding', '4');
  937. table.setAttribute('style', 'width: 400px;');
  938. table.setAttribute('align', 'center');
  939. var tbody = document.createElement('tbody');
  940. table.appendChild(tbody);
  941.  
  942. var td = this.createCase("Rajouter manuellement une recherche :",tbody);
  943.  
  944. td.appendChild(document.createElement('br'));
  945. td.appendChild(document.createTextNode("X = "));
  946. this.addInput(td, "rX");
  947. td.appendChild(document.createTextNode(" Y = "));
  948. this.addInput(td, "rY");
  949. td.appendChild(document.createTextNode(" N = "));
  950. this.addInput(td, "rN");
  951. td.appendChild(document.createElement('br'));
  952. td.appendChild(document.createTextNode("Nombre de chiffres bien placés : "));
  953. this.addInput(td, "rBP",1);
  954. td.appendChild(document.createElement('br'));
  955. var button=this.appendButton(td, "Ajouter", this.addRecherche);
  956.  
  957. p.appendChild(table);
  958. parentElt.appendChild(p);
  959. },
  960.  
  961. // Roule 24/08/2016 récupération des anciennes recherches (localStorage)
  962. PrepareRecupFromLocalStorage: function(idCarte) {
  963. var table = document.createElement('table');
  964. table.setAttribute('class', 'mh_tdborder');
  965. table.setAttribute('border', '0');
  966. table.setAttribute('cellspacing', '1');
  967. table.setAttribute('cellpadding', '4');
  968. table.setAttribute('style', 'width: 400px;');
  969. table.setAttribute('align', 'center');
  970.  
  971. var thead = document.createElement('thead');
  972. var tr = this.appendTr(thead, 'mh_tdtitre');
  973. var td = this.appendTdText(tr, "Récupération des anciens essais V1.1", true);
  974. td.setAttribute('align', 'center');
  975. td.title = "C'est ici pour tenter de récupérer les essais faits avec le version 1.1 du script";
  976. table.appendChild(thead);
  977.  
  978. var tbody = document.createElement('tbody');
  979. tbody.idCarte = idCarte;
  980. table.appendChild(tbody);
  981.  
  982. td.addEventListener("click", this.recupFromLocalStorage, true);
  983. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  984. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  985. td.setAttribute('colspan', 2);
  986. tbody.setAttribute('style', 'display:none;');
  987.  
  988. return table;
  989. },
  990.  
  991. // Roule 24/08/2016 récupération des anciennes recherches (localStorage)
  992. recupFromLocalStorage: function() {
  993. var tbody = this.parentNode.parentNode.parentNode.childNodes[1];
  994. if (tbody.RecupVisible) return; // ne pas populer 2 fois
  995. var idCarte = parseInt(tbody.idCarte);
  996. tbody.setAttribute('style', !tbody.getAttribute('style') || tbody.getAttribute('style') == '' ? 'display:none;' : '');
  997. // entrée d'un jeu de test
  998. // window.localStorage.setItem('91305.capitan.8600686.essai.1', '1;2;3;4');
  999. // window.localStorage.removeItem('91305.capitan.8600686.essai.2');
  1000. // window.localStorage.removeItem('91305.capitan.8600686.essai.3');
  1001. // window.localStorage.removeItem('91306.capitan.8600686.essai.1');
  1002. // window.localStorage.setItem('91305.capitan.8600686.essai.2', '1;2;-4;4');
  1003. // window.localStorage.setItem('91305.capitan.8600686.essai.3', '1;-2;5;4');
  1004. // window.localStorage.setItem('91306.capitan.8600686.essai.1', '-1;2;6;4');
  1005. // return;
  1006.  
  1007. try {
  1008. var nLocalStorage = window.localStorage.length;
  1009. //var td2 = this.createCase('nLocalStorage=' + nLocalStorage,tbody,400);
  1010. var nAlready = 0;
  1011. var nRecup = 0;
  1012. for (var iKey = 0; iKey < nLocalStorage; iKey++) {
  1013. var k = window.localStorage.key(iKey);
  1014. //var td2 = this.createCase(k + '=' + v,tbody,400); // debug
  1015. var m = k.match(/(\d*)\.capitan\.(\d*)\.essai\.(\d*)/i);
  1016. if (!m) continue; // pas une entrée de carte de capitan
  1017. if (parseInt(m[2]) != idCarte) continue; // pas la même carte
  1018.  
  1019. var v = window.localStorage.getItem(k);
  1020. //var nTroll = parseInt(m[1]); // pas utile
  1021. //var nEssai = parseInt(m[3]); // pas utile non plus
  1022. var bAlready = this.testAlready(idCarte, v);
  1023. if (bAlready) { // déjà connu : on compte et on ignore
  1024. nAlready++;
  1025. continue;
  1026. }
  1027. // extraire x, y, n, nb
  1028. m = v.match(/([+-]?\d*);([+-]?\d*);([+-]?\d*);(\d*)/i);
  1029. if (!m) {
  1030. var td2 = this.createCase('Erreur à la récupération des coordonnées ' + v,tbody,400);
  1031. continue;
  1032. }
  1033. var x = m[1];
  1034. var y = m[2];
  1035. var n = m[3];
  1036. var nbChiffres = m[4];
  1037. // ajout sous Greasemonkey
  1038. this.addOneRecherche(idCarte, x, y, n, nbChiffres);
  1039. nRecup++;
  1040. var td2 = this.createCase('Recherche récupérée X = '+ x + ', Y = ' + y + ', N = ' + n + ' => ' + nbChiffres,tbody,400);
  1041. }
  1042. if (nRecup > 0) {
  1043. var td2 = this.createCase('Recharger la page (F5) maintenant',tbody,400);
  1044. td2.style.fontWeight="bold";
  1045. }
  1046. if (nAlready > 0) {
  1047. var td2 = this.createCase(nAlready + ' recherche(s) avaient déjà été récupérée(s)',tbody,400);
  1048. td2.style.fontStyle="italic";
  1049. }
  1050. if (nRecup == 0 && nAlready == 0) {
  1051. var td2 = this.createCase('Désolé, je n\'ai rien trouvé pour la carte ' + idCarte,tbody,400);
  1052. }
  1053. }
  1054. catch(e)
  1055. {
  1056. window.console.log(e);
  1057. }
  1058.  
  1059. tbody.RecupVisible = true;
  1060. },
  1061.  
  1062. testAlready: function(idCarte, v) {
  1063. var v2;
  1064. // tester si cet essai est déjà connu sous Greasemonkey
  1065. var bAlready = false;
  1066. for (var iGM = 0; v2 = this.CAPITAN_getValue("capitan."+idCarte+".essai."+iGM); iGM++) {
  1067. if (v2 != v) continue;
  1068. return true;
  1069. break;
  1070. }
  1071. return false;
  1072. },
  1073.  
  1074. // Roule 25/08/2016 récupération des anciennes recherches (préférences)
  1075. PrepareRecupFromPreferences: function() {
  1076. var table = document.createElement('table');
  1077. table.setAttribute('class', 'mh_tdborder');
  1078. table.setAttribute('border', '0');
  1079. table.setAttribute('cellspacing', '1');
  1080. table.setAttribute('cellpadding', '4');
  1081. table.setAttribute('style', 'width: 400px;');
  1082. table.setAttribute('align', 'center');
  1083.  
  1084. var thead = document.createElement('thead');
  1085. var tr = this.appendTr(thead, 'mh_tdtitre');
  1086. var td = this.appendTdText(tr, "Récupération des anciens essais V1.0", true);
  1087. td.setAttribute('align', 'center');
  1088. td.title = "C'est ici pour entrer manuellement les essais faits avec la version 1.0 du script";
  1089. table.appendChild(thead);
  1090.  
  1091. var tbody = document.createElement('tbody');
  1092. table.appendChild(tbody);
  1093.  
  1094. var td2 = this.createCase("Ouvrez un nouvel onglet sous Firefox",tbody);
  1095. td2.style.textAlign = "left";
  1096.  
  1097. var nbsp = String.fromCharCode(160);
  1098. td2.appendChild(document.createElement('br'));
  1099. td2.appendChild(document.createTextNode("(«" + nbsp + "+" + nbsp + "» à droite des onglets)"));
  1100. td2.appendChild(document.createElement('br'));
  1101. td2.appendChild(document.createTextNode("Tapez dans la barre d'adresse «" + nbsp + "about:config" + nbsp + "» et validez"));
  1102. td2.appendChild(document.createElement('br'));
  1103. td2.appendChild(document.createTextNode("C'est promis, vous ferez attention" + nbsp + "!"));
  1104. td2.appendChild(document.createElement('br'));
  1105. td2.appendChild(document.createTextNode("Filtrez ensuite par «" + nbsp + "capitan" + nbsp +"» (zone «" + nbsp + "Rechercher" + nbsp + "» en haut)"));
  1106. td2.appendChild(document.createElement('br'));
  1107. td2.appendChild(document.createTextNode("Ensuite, ligne par ligne,"));
  1108. td2.appendChild(document.createElement('br'));
  1109. td2.appendChild(document.createTextNode("copiez la ligne (bouton de droite de la souris - copier),"));
  1110. td2.appendChild(document.createElement('br'));
  1111. td2.appendChild(document.createTextNode("(Vous pouvez aussi copier *tout* le contenu de pref.js trouvé dans le profil Firefox)"));
  1112. td2.appendChild(document.createElement('br'));
  1113. td2.appendChild(document.createTextNode("Collez ci-dessous et Ajouter"));
  1114. td2.appendChild(document.createElement('br'));
  1115. var inp = this.addInput(td2, "t", 100);
  1116. inp.removeAttribute('maxlength'); // permettre de copier tout pref.js
  1117. inp.style.width = "350px";
  1118. td2.appendChild(document.createElement('br'));
  1119. var button=this.appendButton(td2, "Ajouter", this.addV1);
  1120.  
  1121. td.addEventListener("click", this.toggleTableau, true);
  1122. td.setAttribute('onmouseover', "this.style.cursor = 'pointer'; this.className = 'mh_tdpage';");
  1123. td.setAttribute('onmouseout', "this.className = 'mh_tdtitre';");
  1124. td.setAttribute('colspan', 2);
  1125. tbody.setAttribute('style', 'display:none;');
  1126.  
  1127. return table;
  1128. },
  1129.  
  1130. addV1: function(eButton) {
  1131. // pour test mountyhall.91306.capitan.8600686.essai.1;-1;2;6;4
  1132. try
  1133. {
  1134. var td=this.parentNode;
  1135. var eInput = td.getElementsByTagName("input")[0];
  1136. var val = eInput.value;
  1137. var bClear;
  1138. if (val.match(/user_pref\(/)) {
  1139. bClear = this.addV1Pref(val);
  1140. } else {
  1141. bClear = this.addV1Unitaire(val);
  1142. }
  1143. if (bClear)eInput.value = ''; // vider le champ pour que l'utilisateur puisse copier à nouveau
  1144. }
  1145. catch(e)
  1146. {
  1147. window.alert(e);
  1148. }
  1149. },
  1150.  
  1151. addV1Pref: function(val) {
  1152. // on a tout le contenu de pref.js dans val
  1153. tabPref = val.split(/\);/);
  1154. if (this.bDebug) window.console.log('CAPITAN addV1Pref: nb val=' + tabPref.length);
  1155. //user_pref("mountyzilla.storage.91305.capitan.8600686.essai.1", "1;2;3;4");
  1156. var nAlready = 0;
  1157. var nDone = 0;
  1158. for (var i = 0; i < tabPref.length; i++) {
  1159. var m = tabPref[i].match(/user_pref\(\"mountyzilla\.storage\.(\d*)\.capitan\.(\d*)\.essai\.(\d*)\", *\"([+-]?\d*);([+-]?\d*);([+-]?\d*);(\d*)\"/i);
  1160. if (!m) continue;
  1161. //window.console.log('CAPITAN addV1Pref: trouvé troll ' + m[1] + ', carte ' + m[2] + ', essai ' + m[3] + ', X = ' + m[4] + ', Y=' + m[5] + ', N=' + m[6] + ' => ' + m[7]);
  1162. var bAlready = this.testAlready(m[2], m[4] + ';' + m[5] + ';' + m[6] + ';' + m[7]);
  1163. if (bAlready) {
  1164. nAlready++;
  1165. } else {
  1166. this.addOneRecherche(parseInt(m[2]), m[4], m[5], m[6], m[7]);
  1167. nDone++;
  1168. }
  1169. }
  1170. alert(nAlready + ' essai(s) ont été ignorés car déjà migrés'
  1171. + "\n" + nDone + ' essai(s) viennent d\'être migrés'
  1172. + "\nIl faut rafraichir cette page (F5 ou afficher à nouveau l'équipement)");
  1173. return true;
  1174. },
  1175.  
  1176. addV1Unitaire: function(val) {
  1177. if (this.bDebug) window.console.log('CAPITAN addV1Unitaire: val=' + val);
  1178. var m = val.match(/capitan\.(\d*)\.essai\.(\d*);([+-]?\d*);([+-]?\d*);([+-]?\d*);(\d*)/i);
  1179. if (!m) {
  1180. alert("Désolé, impossible de retrouver le numéro de carte, les coordonnées et le nombre de chiffres bien placés");
  1181. return false;
  1182. }
  1183. var bAlready = this.testAlready(m[1], m[3] + ';' + m[4] + ';' + m[5] + ';' + m[6]);
  1184. if (bAlready) {
  1185. alert("L'essai pour la carte " + m[1] + "\nX = " + m[3]
  1186. + ", Y=" + m[4] + ", N=" + m[5] + " => " + m[6]
  1187. + "\navait DÉJÀ été entré");
  1188. return true;
  1189. }
  1190. this.addOneRecherche(parseInt(m[1]), m[3], m[4], m[5], m[6]);
  1191. alert("L'essai pour la carte " + m[1] + "\nX = " + m[3]
  1192. + ", Y=" + m[4] + ", N=" + m[5] + " => " + m[6]
  1193. + "\na bien été enregistré\nIl faudra rafraichir cette page (F5) quand vous aurez terminé");
  1194. return true;
  1195. },
  1196.  
  1197. addRecherche: function()
  1198. {
  1199. try
  1200. {
  1201. var td=this.parentNode;
  1202. var x = (td.getElementsByTagName("input")[0]).value;
  1203. var y = (td.getElementsByTagName("input")[1]).value;
  1204. var n = (td.getElementsByTagName("input")[2]).value;
  1205. var nbChiffres = (td.getElementsByTagName("input")[3]).value;
  1206. if(x==null || isNaN(parseInt(x)))
  1207. {
  1208. window.alert("Erreur : champ X mal formaté.");
  1209. return;
  1210. }
  1211. if(y==null || isNaN(parseInt(y)))
  1212. {
  1213. window.alert("Erreur : champ Y mal formaté.");
  1214. return;
  1215. }
  1216. if(n==null || isNaN(parseInt(n)))
  1217. {
  1218. window.alert("Erreur : champ N mal formaté.");
  1219. return;
  1220. }
  1221. if(nbChiffres==null || isNaN(parseInt(nbChiffres)))
  1222. {
  1223. window.alert("Erreur : nombre de chiffres bien placés mal formaté.");
  1224. return;
  1225. }
  1226. oCAPITAN_MH_ROULE.addOneRecherche(oCAPITAN_MH_ROULE.getIDCarte(), x, y, n, nbChiffres);
  1227. window.location.replace(window.location);
  1228. }
  1229. catch(e)
  1230. {
  1231. window.alert(e);
  1232. }
  1233. },
  1234.  
  1235. addOneRecherche: function(idCarte, x, y, n, nbChiffres) {
  1236. for (var i = 0; this.CAPITAN_getValue("capitan."+idCarte+".essai."+i); i++){}
  1237. this.CAPITAN_setValue("capitan."+idCarte+".essai."+i,parseInt(x)+";"+parseInt(y)+";"+parseInt(n)+";"+parseInt(nbChiffres));
  1238. },
  1239.  
  1240. addInput: function(parent, nom, size)
  1241. {
  1242. var input = document.createElement('input');
  1243. input.setAttribute('type','text');
  1244. input.setAttribute('name',nom);
  1245. input.setAttribute('type','text');
  1246. input.setAttribute('maxlength',size==null?4:size);
  1247. input.setAttribute('size',size==null?4:size);
  1248. parent.appendChild(input);
  1249. return input;
  1250. },
  1251.  
  1252. infoRecherche: function()
  1253. {
  1254. var idCarte = this.getIntegerByID('carte', 'numéro de carte');
  1255. if (idCarte === undefined) return;
  1256. var x = this.getIntegerByID('x', 'x');
  1257. if (this.bDebug) window.console.log("infoRecherche_log: X=" + x);
  1258. if (x === undefined) return; // ne pas utiliser «!» car x peut être «0» !
  1259. var y = this.getIntegerByID('y', 'y');
  1260. if (this.bDebug) window.console.log("infoRecherche_log: Y=" + y);
  1261. if (y === undefined) return;
  1262. var n = this.getIntegerByID('n', 'n');
  1263. if (this.bDebug) window.console.log("infoRecherche_log: N=" + n);
  1264. if (n === undefined) return;
  1265. var nb = this.getIntegerByID('nb', 'Vous avez retrouvé');
  1266. if (this.bDebug) window.console.log("infoRecherche_log: nb=" + nb);
  1267. if (nb === undefined) return;
  1268.  
  1269. var i = 0;
  1270. while(this.CAPITAN_getValue("capitan."+idCarte+".essai."+i) != null)
  1271. {
  1272. i++;
  1273. }
  1274. this.CAPITAN_setValue("capitan."+idCarte+".essai."+i,x+";"+y+";"+n+";"+nb);
  1275.  
  1276. if(this.CAPITAN_getValue("capitan."+idCarte+".this.signe") == null)
  1277. {
  1278. var msg = document.getElementById("msgDiv").textContent;
  1279.  
  1280. // fonctionne à la fois pour "Tu es dans..." et "Vous êtes dans..."
  1281. if(!msg.match(/es dans le bon Xcoin/))
  1282. x = -x;
  1283. if(!msg.match(/es dans le bon Ycoin/))
  1284. y = -y;
  1285.  
  1286. this.CAPITAN_setValue("capitan."+idCarte+".this.signe",this.signe(x)+";"+this.signe(y));
  1287. }
  1288.  
  1289. var table = this.afficheInfoCarte(idCarte);
  1290.  
  1291. if (!table) return;
  1292. form = document.getElementsByTagName('FORM')[0];
  1293. var p = document.createElement('p');
  1294. p.appendChild(table);
  1295. form.appendChild(p);
  1296. },
  1297.  
  1298. // return undefined if not found
  1299. getIntegerByID: function(id, msg) {
  1300. var e = document.getElementById(id);
  1301. if (!e || !e.childNodes || !e.childNodes[0] || !e.childNodes[0].nodeValue) {
  1302. if (msg) window.alert('Script carte de Capitan : impossible de retrouver le ' + msg);
  1303. return;
  1304. }
  1305. return parseInt(e.childNodes[0].nodeValue);
  1306. },
  1307.  
  1308. ///////////////////////////////////
  1309. // debuging
  1310. // essais : objet
  1311. // .mode : description
  1312. // .essais : tableau d'objets essai
  1313. // .mode : description
  1314. // .essais : tableau de cartes
  1315. // .noCarte : id de la carte
  1316. // .essais : tableau d'essais, [x, y, n, nb]
  1317. AfficheEssais: function(essais, sMode) {
  1318. var eBigDiv = document.getElementById('ListeEssaiCapitan');
  1319. if (!eBigDiv) {
  1320. var insertPoint = document.getElementById('footer1');
  1321. eBigDiv = document.createElement('table');
  1322. eBigDiv.id = 'ListeEssaiCapitan';
  1323. this.insertBefore(insertPoint, document.createElement('p'));
  1324. this.insertTitle(insertPoint,'Capitan : Liste des essais');
  1325. this.insertBefore(insertPoint, eBigDiv);
  1326. this.addTrEssais(eBigDiv, 'mode', 'carte', 'nombre d\'essais', true);
  1327. }
  1328. if (!essais) {
  1329. this.addTrEssais(eBigDiv, sMode, '', 'pas d\'essai', false);
  1330. return;
  1331. }
  1332. var carte;
  1333. for (carte in essais) {
  1334. this.addTrEssais(eBigDiv, sMode, carte, essais[carte] + ' essai(s)', false);
  1335. }
  1336. if (carte === undefined) {
  1337. this.addTrEssais(eBigDiv, sMode, '', '0 essai', false);
  1338. }
  1339. },
  1340.  
  1341. addTrEssais: function(eTable, sMode, sCarte, sText, bBold) {
  1342. var tr = this.appendTr(eTable);
  1343. var td = this.appendTd(tr);
  1344. this.appendText(td, sMode, bBold);
  1345. td = this.appendTd(tr);
  1346. this.appendText(td, sCarte, bBold);
  1347. td = this.appendTd(tr);
  1348. this.appendText(td, sText, bBold);
  1349. },
  1350.  
  1351. getEssaiV1_2: function() {
  1352. if (!GM_getValue) {
  1353. window.console.log('CAPITAN getEssaiV1_2: pas de GM_getValue');
  1354. return;
  1355. }
  1356. var tabKey = GM_listValues();
  1357. window.console.log('CAPITAN getEssaiV1_2: nb key : ' + tabKey.length);
  1358. var r = [];
  1359. for (var i = 0; i < tabKey.length; i++) {
  1360. var k = tabKey[i];
  1361. //window.console.log('CAPITAN getEssaiV1_2: key ' + k + ' => ' + GM_getValue(k));
  1362. var t = k.split(/\./);
  1363. if (t[0] !== 'capitan') continue;
  1364. if (t[2] !== 'essai') continue;
  1365. var carte = 'carte n°' + t[1];
  1366. if (r[carte]) r[carte]++;
  1367. else r[carte] = 1;
  1368. //window.console.log('CAPITAN getEssaiV1_2: r[' + carte + ']=' + r[carte]);
  1369. }
  1370. return r;
  1371. },
  1372.  
  1373. showEssai: function() {
  1374. window.console.log('CAPITAN début this.showEssai CapitanList');
  1375. var essai1_2 = this.getEssaiV1_2();
  1376. this.AfficheEssais(essai1_2, 'V1.2');
  1377. window.console.log('CAPITAN fin this.showEssai CapitanList');
  1378. },
  1379.  
  1380. CAPITAN_horsGM: false,
  1381. init: function () {
  1382. this.CAPITAN_horsGM = false;
  1383. try { // à partir du 11/07/2018, (GM_info === undefined) provoque une exception
  1384. if (GM_info == undefined) {
  1385. this.CAPITAN_horsGM = true;
  1386. } else if (GM_info.script == undefined) {
  1387. this.CAPITAN_horsGM = true;
  1388. } else {
  1389. if (this.bDebug) window.console.log('GM_info.script=' + JSON.stringify(GM_info.script));
  1390. // si un autre script GM est actif sur la page, on peut avoir GM_Info.script qui existe. Ça ressemble à un bug de ViolentMonkey
  1391. if (GM_info.script.name != 'Capitan') {
  1392. this.CAPITAN_horsGM = true;
  1393. } else if (GM_info.script.version == 'sans GM') {
  1394. this.CAPITAN_horsGM = true;
  1395. }
  1396. }
  1397. } catch (e2) {
  1398. this.CAPITAN_horsGM = true;
  1399. if (this.bDebug) window.console.log('CAPITAN init_log: test GM_deleteValue, exception=' + e2);
  1400. }
  1401. try {
  1402. if (GM_getValue == undefined) {
  1403. this.CAPITAN_horsGM = true;
  1404. }
  1405. GM_getValue('x'); // provoque une exception hors GM
  1406. } catch (e2) {
  1407. this.CAPITAN_horsGM = true;
  1408. if (this.bDebug) window.console.log('CAPITAN init_log: test GM_deleteValue, exception=' + e2);
  1409. }
  1410. try {
  1411. if (GM_deleteValue == undefined) {
  1412. this.CAPITAN_horsGM = true;
  1413. }
  1414. } catch (e2) {
  1415. this.CAPITAN_horsGM = true;
  1416. if (this.bDebug) window.console.log('CAPITAN init_log: test GM_deleteValue, exception=' + e2);
  1417. }
  1418. if (this.bDebug) window.console.log('CAPITAN init_log: horsGM=' + this.CAPITAN_horsGM);
  1419. if (this.CAPITAN_horsGM) { // remplacer GM_xxxValue
  1420. this.CAPITAN_getValue = function(key) {
  1421. return window.localStorage[key];
  1422. }
  1423. this.CAPITAN_deleteValue = function(key) {
  1424. window.localStorage.removeItem(key);
  1425. }
  1426. this.CAPITAN_setValue = function(key, val) {
  1427. //if (this.bDebug) window.console.log('CAPITAN_setValue_log: ' + key + '=>' + val);
  1428. window.localStorage[key] = val;
  1429. }
  1430. } else {
  1431. this.CAPITAN_getValue = GM_getValue;
  1432. this.CAPITAN_deleteValue = GM_deleteValue;
  1433. this.CAPITAN_setValue = GM_setValue;
  1434. }
  1435. if (this.isPage("View/TresorHistory.php"))
  1436. {
  1437. this.analyseObject();
  1438. }
  1439. else if(this.isPage("MH_Play/Play_a_ActionResult.php") || this.isPage("MH_Play/Play_a_TrouverCachette2.php"))
  1440. {
  1441. // uniquement si l'id du body est p_trouverunecachette
  1442. if (document.body.id != 'p_trouverunecachette') return;
  1443. this.infoRecherche();
  1444. }
  1445. else if(this.isPage("MH_Play/Play_equipement.php") || this.isPage("MH_Taniere/TanierePJ_o_Stock.php"))
  1446. {
  1447. // Roule, 23/11/2016, il faudrait trouver mieux pour s'activer quand l'utilisateur ouvre le popup
  1448. //window.setInterval(this.analyseObject.bind(this), 1000); // ceci fonctionne mais n'est supporté que par les navigateurs récents
  1449. window.setInterval(this.analyseObjectCallback, 1000);
  1450. }
  1451. // debuging
  1452. // else if(this.isPage("MH_Play/Options/Play_o_Interface"))
  1453. // {
  1454. // this.showEssai();
  1455. // }
  1456. },
  1457. }
  1458. try
  1459. {
  1460. oCAPITAN_MH_ROULE.init();
  1461. } catch(e) {
  1462. window.console.log('script capitan exception: ' + e + "\n" + e.stack);
  1463. }
  1464. }