Gestion de personnage et de pool de personnage en javascript vanilla
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

random_utils.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. class RandomGenerator{
  2. constructor( cache ){
  3. this.rng = new Uint8Array( cache );
  4. this.index = cache-- ;
  5. this.generate();
  6. }
  7. generate(){
  8. window.crypto.getRandomValues(this.rng);
  9. this.index = this.rng.length - 2;
  10. return this.rng.length - 1;
  11. }
  12. next(){
  13. return (this.index < 0)? this.generate() : this.index--;
  14. }
  15. rand(){
  16. return this.rng[ this.next() ]/256 ;
  17. }
  18. get( max, min ){
  19. min = ( typeof min === "number")? min : 0 ;
  20. return Math.round(this.rand() * (max - min) + min);
  21. }
  22. roll10( explode ){
  23. explode = ( typeof explode === "number")? explode : 10;
  24. let dice = this.get( 10, 1 );
  25. return (dice >= explode)? 10 + this.roll10( explode ) : dice ;
  26. }
  27. rk10( r, k, malus, explode ){
  28. let rolls = [];
  29. if((typeof r === "unigned") || (typeof k === "unigned"))
  30. return 0;
  31. malus = ( typeof malus === "unsigned")? 0 : malus ;
  32. for(let i = 0 ; i < r ; i++)
  33. rolls[i] = this.roll10( explode);
  34. sort( rolls );
  35. return rolls.slice( 0, k ).reduce((a, b) => a + b) - (malus || 0);
  36. }
  37. }
  38. function sort( array ){
  39. let temp = 0;
  40. var unsorted = true ;
  41. while(unsorted){
  42. unsorted = false;
  43. for(let i = 0; i < array.length -1 ; i++){
  44. if(array[ i ] < array[ i + 1 ]){
  45. temp = array[ i ];
  46. array[ i ] = array[ i + 1 ];
  47. array[ i + 1 ] = temp;
  48. unsorted = true;
  49. }
  50. }
  51. }
  52. }
  53. function sortAssociative( array_arg ){
  54. let result = {};
  55. let array = array_arg;
  56. let higher = 1;
  57. while( !higher ){
  58. higher = 0;
  59. for(let i in array){
  60. higher = (higher)?
  61. (array[higher] < array[i])? i : higher : i ;
  62. if(array[ i ] < array[ i + 1 ]){
  63. temp = array[ i ];
  64. array[ i ] = array[ i + 1 ];
  65. array[ i + 1 ] = temp;
  66. unsorted = true;
  67. }
  68. }
  69. if(higher){
  70. result.push(higher);
  71. delete array[ higher ];
  72. higher = 0;
  73. }
  74. }
  75. return result;
  76. }
  77. function random( max, min ){
  78. min = (typeof min == "undefined")? 0 : min;
  79. return Math.floor(Math.random() * (max - min + 1) + min);
  80. }
  81. function r_gender(){
  82. return (random(1))? "f": "m";
  83. }
  84. function arrayAt( index, array ){
  85. let i = 0;
  86. let result = {};
  87. for(let obj in array){
  88. if( index == ++i ){
  89. result[obj] = array[obj];
  90. return result;
  91. }
  92. }
  93. }
  94. function arrayAtWeight( index, array ){
  95. let i = 0;
  96. for(let obj in array){
  97. if(( i <= index ) && ( index <= (i + array[obj].occurence))){
  98. let rs = {};
  99. rs[ obj ] = array[obj];
  100. return rs;
  101. }
  102. i += array[obj].occurence ;
  103. }
  104. return null;
  105. }
  106. function arrayLength( array ){
  107. let i = 0;
  108. for(let obj in array){
  109. i++;
  110. };
  111. return i;
  112. }
  113. function arrayWeight( array ){
  114. let i = 0;
  115. for(let obj in array){
  116. i += array[obj].occurence;
  117. };
  118. return i;
  119. }
  120. function randomFromAssociative( array ){
  121. return arrayAt( random( arrayLength( array ), 1), array) ;
  122. }
  123. function randomFromArray( array ){
  124. return array[ random( arrayLength( array )) ] ;
  125. }
  126. function randomFromWeight( array ){
  127. return arrayAtWeight( random( arrayWeight( array )), array) ;
  128. }