Gestion de personnage et de pool de personnage en javascript vanilla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

roll.html 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <html>
  2. <head>
  3. <meta charset="utf8">
  4. <script src="dice.js"></script>
  5. <script src="names.js"></script>
  6. <script src="l5r_data.js"></script>
  7. <script src="random_utils.js"></script>
  8. <script src="display.js"></script>
  9. <script src="l5r.js"></script>
  10. <script src="players.js"></script>
  11. <script>
  12. var npcs = [];
  13. var result = [];
  14. var result1 = [];
  15. var result2 = [];
  16. var current_target= 0;
  17. function target(){
  18. return characters.at(current_target) ;
  19. }
  20. var npc_summary = document.getElementById("npc.summary");
  21. function characterToLine(){
  22. let line = document.CreateElement('td');
  23. let cell = document.CreateElement('tr');
  24. }
  25. function generateNPC(){
  26. var newNPC = new Character();
  27. newNPC.generate( document.getElementById("npc-level").value );
  28. return newNPC;
  29. }
  30. function generateNPCs( size ){
  31. let result = [];
  32. for(let i = 0; i < size ; i++){
  33. result[i] = generateNPC( i );
  34. }
  35. return result;
  36. }
  37. function skirmish( size ){
  38. let npcs = generateNPCs( size );
  39. for(let i = 0; i < size ; i+=2){
  40. if( (npcs[i]) && (npcs[i + 1]))
  41. result[i] = fight(npcs[i], npcs[i + 1]);
  42. }
  43. for( let i = 0; i < size ; i++ ){
  44. ;
  45. }
  46. }
  47. function swissContest( size ){
  48. let result = [];
  49. let npcs = generateNPCs( size );
  50. for(let i = 0; i < npcs.length; i++){
  51. result[i] = [];
  52. for(let j = 0; j < npcs.length; j++){
  53. result[i][j] = ( fight( npcs[i], npcs[j] ) );
  54. npcs[i].fullHeal();
  55. npcs[j].fullHeal();
  56. }
  57. }
  58. return result;
  59. }
  60. function winRateSwissContest( array, stat){
  61. let result = [];
  62. for(let i = 0; i < array.length; i++){
  63. let winrate = 0;
  64. let total = 0;
  65. for(let j = 0; j < array[i].length; j++){
  66. winrate += ( array[i][j].winner_side == "attacker" )? 1: 0;
  67. total++;
  68. }
  69. result[i] = winrate/total;
  70. }
  71. return result;
  72. }
  73. function hasClass( elem, className){
  74. for( let i = 0 ; i < elem.classList.length ; i++ )
  75. if( elem.classList[ i ] === className )
  76. return elem;
  77. return 0;
  78. };
  79. function getClassFromList( list, className ){
  80. let result = [];
  81. for( let i = 0 ; i < list.childNodes.length ; i++ )
  82. if( hasClass( list.childNodes[ i ], className ))
  83. result.push( list.childNodes[ i ] );
  84. return result;
  85. }
  86. function inputFromElementList( list, input_type ){
  87. let result = [];
  88. for( let i = 0 ; i ; i++ )
  89. if( list[ i ].getAttributeNames().includes( input_type ))
  90. result.push( list[i] );
  91. return result;
  92. }
  93. function display_data( data, anchor ){
  94. let html_display = document.getElementById( anchor );
  95. let headers = [ "name", "initiative", "TN", "attack", "damage", "wounds" ];
  96. let hs = document.createElement("tr");
  97. for(let i = 0; i < headers.length ; i++){
  98. let h = document.createElement("th");
  99. h.innerText = headers[ i ];
  100. hs.appendChild( h );
  101. }
  102. html_display.appendChild( hs ) ;
  103. for(let i = 0; i < data.length ; i++){
  104. let line = document.createElement( "tr" );
  105. line.setAttribute("id", i + "-" + anchor );
  106. let name = document.createElement("td");
  107. name.innerText = data[i]["get"+capitalize( headers[0] )]();
  108. line.appendChild( name );
  109. for(let j = 1; j < headers.length ; j++){
  110. let cell = document.createElement("td");
  111. let button = document.createElement("button");
  112. button.setAttribute("class", headers[j]);
  113. let id = i + "-"+j+"-" + anchor;
  114. button.innerText = headers[j];
  115. button.addEventListener('click', function(event) {
  116. document.getElementById( id ).value = data[i]["get"+capitalize( headers[j] )]();
  117. });
  118. cell.appendChild( button );
  119. let result = document.createElement("input");
  120. result.setAttribute("id", id);
  121. result.setAttribute("type", "text");
  122. result.setAttribute("class", "result");
  123. cell.appendChild( result );
  124. line.appendChild( cell );
  125. }
  126. let cell1 = document.createElement("td");
  127. let targeted = document.createElement("input");
  128. targeted.setAttribute("type", "checkbox");
  129. targeted.setAttribute("class", "targeted");
  130. targeted.checked = false;
  131. targeted.addEventListener('click', function(event) {
  132. let targeteds = document.getElementsByClassName( "targeted" );
  133. for(let targeted in targeteds){
  134. if(targeteds[targeted] != this)
  135. targeteds[targeted].checked = false;
  136. current_target = this.parentNode.parentNode.id.split("-")[0];
  137. }
  138. });
  139. cell1.appendChild( targeted );
  140. line.appendChild( cell1 );
  141. cell2 = document.createElement("td");
  142. let attacking = document.createElement("input");
  143. attacking.setAttribute("type", "button");
  144. attacking.setAttribute("class", "attacking");
  145. attacking.value = "Attack";
  146. attacking.checked = false;
  147. attacking.addEventListener('click', function(event) {
  148. characters.at( this.parentNode.parentNode.id.split("-")[0] ).attacking( target() );
  149. triggerField( "Wounds" );
  150. triggerField( "TN" );
  151. });
  152. cell2.appendChild( attacking );
  153. line.appendChild( cell2 );
  154. let cell_switchStance = document.createElement("td");
  155. let switchStance = document.createElement("input");
  156. switchStance.setAttribute("type", "button");
  157. switchStance.value = "Attack Stance";
  158. cell_switchStance.appendChild( switchStance );
  159. cell_switchStance.addEventListener('click', function(event) {
  160. let charac = characters.at( this.parentNode.id.split("-")[0] );
  161. charac.switchStance();
  162. event.target.value = charac.stance;
  163. triggerField( "TN" );
  164. });
  165. line.appendChild( cell_switchStance );
  166. html_display.appendChild( line );
  167. }
  168. }
  169. function popAssociativeMaker( associative, anchor ){
  170. let result = document.createElement( "div" );
  171. result.setAttribute( "class", "popup" );
  172. let exit = document.createElement("input");
  173. exit.setAttribute("type", "button");
  174. exit.setAttribute("class", "exit");
  175. exit.value = "×";
  176. exit.addEventListener('click', function(event) {
  177. anchor.value = JSON.stringify( formToData( result ) );
  178. result.parentNode.removeChild( result );
  179. });
  180. result.appendChild( exit );
  181. for( let i in associative ){
  182. switch( typeof varType ){
  183. case "function":
  184. break;
  185. case "object":
  186. result.appendChild( inputField( i, associative[ i ] || "", true, null, function(event) {
  187. this.value = popObjectMaker( associative[ i ], line );
  188. } ));
  189. break;
  190. default:
  191. result.appendChild( inputField( i, associative[ i ] || "") );
  192. }
  193. }
  194. let add = document.createElement("input");
  195. add.setAttribute("type", "button");
  196. add.setAttribute("class", "add");
  197. add.value = "+";
  198. add.addEventListener('click', function(event) {
  199. result.appendChild( inputField( "", "", true) );
  200. });
  201. result.appendChild( add );
  202. document.getElementsByTagName('body')[0].appendChild( result );
  203. return result;
  204. }
  205. function inputField( field, value, modifiable, label_onclick, input_onclick ){
  206. field = (( typeof field === "unsigned" ) || !field )? "" : field;
  207. value = (( typeof value === "unsigned" ) || !value )? "" : value;
  208. modifiable = (( typeof modifiable === "unsigned" ) || !modifiable )? false : true ;
  209. let line= document.createElement("div");
  210. let input = document.createElement("input");
  211. let label ;
  212. if( modifiable ){
  213. label = document.createElement("input");
  214. label.setAttribute("type", "text");
  215. }else{
  216. label = document.createElement("label");
  217. }
  218. line.setAttribute("class", "line");
  219. label.setAttribute("class", "label");
  220. label.innerText = field;
  221. if( label_onclick instanceof Function )
  222. label.addEventListener('click', label_onclick );
  223. line.appendChild( label );
  224. input.setAttribute("type", "text");
  225. input.setAttribute("class", "maker");
  226. input.value = value;
  227. if( input_onclick instanceof Function )
  228. input.addEventListener('click', label_onclick );
  229. input.checked = false;
  230. line.appendChild( input );
  231. return line;
  232. }
  233. function popObjectMaker( object, anchor, dictionnary ){
  234. let prototype = new object;
  235. let result = document.createElement( "div" );
  236. result.setAttribute( "class", "popup" );
  237. let exit = document.createElement("input");
  238. exit.setAttribute("type", "button");
  239. exit.setAttribute("class", "exit");
  240. exit.value = "×";
  241. if( typeof dictionnary === "object" )
  242. exit.addEventListener('click', function(event) {
  243. formToDictonnary( result, dictionnary);});
  244. else
  245. exit.addEventListener('click', function(event) {
  246. saveAndExitForm( result);});
  247. result.appendChild( exit );
  248. for( let i in prototype ){
  249. let varType = prototype[i];
  250. let line= document.createElement("div");
  251. let input = document.createElement("input");
  252. let label = document.createElement("label");
  253. switch( typeof varType ){
  254. case "function":
  255. break;
  256. case "object":
  257. line= document.createElement("div");
  258. line.setAttribute("class", "line");
  259. label.setAttribute("type", "button");
  260. label.classList.add("label");
  261. label.innerText = i;
  262. line.appendChild( label );
  263. input.setAttribute("type", "text");
  264. input.classList.add("json");
  265. input.classList.add("maker");
  266. input.value = prototype[ i ] || "";
  267. input.checked = false;
  268. input.addEventListener('click', function(event) {
  269. this.value = popAssociativeMaker( prototype[ i ], input );
  270. });
  271. line.appendChild( input );
  272. result.appendChild( line );
  273. break;
  274. default:
  275. line.setAttribute("class", "line");
  276. label.setAttribute("type", "button");
  277. label.classList.add("label");
  278. label.innerText = i;
  279. line.appendChild( label );
  280. input.setAttribute("type", "text");
  281. input.classList.add("maker");
  282. input.value = prototype[ i ] || "";
  283. input.checked = false;
  284. line.appendChild( input );
  285. result.appendChild( line );
  286. }
  287. }
  288. if( anchor instanceof Element )
  289. anchor.appendChild( result );
  290. else
  291. document.getElementsByTagName('body')[0].appendChild( result );
  292. return result;
  293. }
  294. function formToData( form ){
  295. let lines = form.getElementsByClassName( "line" ) ;
  296. let data = {} ;
  297. for ( let i = 0 ; i < lines.length ; i++ ){
  298. let labels = getClassFromList( lines[ i ], "label");
  299. let makers = getClassFromList( lines[ i ], "maker");
  300. if(( lines[ i ] instanceof Element ) && ( labels[0] || makers[0] )){
  301. let index = labels[0].value || labels[0].innerText;
  302. let value = makers[0].value || makers[0].innerText ;
  303. value = (hasClass( makers[0], "json" ))? JSON.parse( value ) : value ;
  304. data[ index ] = value ;
  305. }
  306. }
  307. return data ;
  308. }
  309. function saveAndExitForm( form ){
  310. form.value = JSON.stringify( formToData( form ) );
  311. form.parentNode.removeChild( form );
  312. }
  313. function formToDictonnary( form, dictionnary){
  314. return dictionnary.create( formToData( form ) );
  315. }
  316. function triggerField( field ){
  317. let fields = document.getElementsByClassName( field ) ;
  318. for (let i = 0 ; i < fields.length ; i++ ){
  319. fields[i].click();
  320. }
  321. }
  322. function main(){
  323. display_data( characters.pool, "players_summary");
  324. triggerField( "wounds" );
  325. triggerField( "TN" );
  326. }
  327. </script>
  328. <style>
  329. .result{
  330. width : 3em;
  331. }
  332. </style>
  333. </head>
  334. <body onload="main()">
  335. <button onclick="popObjectMaker( Character )">Créer un Personnage</button>
  336. <table id="players_summary"></div>
  337. <div id="npc_summary"></div>
  338. </table>
  339. <button onclick="generateNPC()">Générer NPC</button><input id="npc-level" type="number" value="1"/><select id="npc-role"></select>
  340. <button onclick="popObjectMaker( Character, null, characters )">Générer Personnage</button>
  341. </body>
  342. </html>