Roll dice (eg Asphodice) and show outcomes https://rpg.bertieb.org/dice-roller/
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.
 
 
 
 

97 lines
2.3 KiB

  1. import { Asphodice } from "./asphodice"
  2. import { Outcomes } from "./asphodice"
  3. import { DiceResult } from "./asphodice"
  4. interface ItemCount {
  5. item: string | number | Outcomes;
  6. count: number;
  7. }
  8. declare let ItemCountSet: Array<ItemCount>;
  9. type EnumObject<T extends string> = { [K in T]?: number };
  10. type outcomeCountObject = EnumObject<Outcomes>;
  11. /**
  12. * Singleton-type class for all dice-statistic related needs
  13. */
  14. export class RollStats {
  15. rerollCounts: { [rerolled: string]: number } = {};
  16. totalCounts: { [total: string]: number } = {};
  17. balanceCounts: { [balance: string]: number } = {};
  18. outcomeCounts: outcomeCountObject = {};
  19. diceCounts: { [dice: string]: number} = {};
  20. numRolls = 100000;
  21. maxDice = 10;
  22. numDice = 4;
  23. constructor() {
  24. this.resetCounts();
  25. }
  26. resetCounts(): void {
  27. // TODO: find out if there's a way to this
  28. // by iterating over members
  29. this.rerollCounts = { "true": 0, "false": 0 };
  30. this.totalCounts = {};
  31. this.balanceCounts = {};
  32. this.outcomeCounts = { [Outcomes.Success]: 0, [Outcomes.Fail]: 0 } // TODO: others
  33. for (let i = 1; i <= 10; i++) {
  34. this.diceCounts[String(i)] = 0;
  35. }
  36. }
  37. doRolls(): void {
  38. let asphodice = new Asphodice();
  39. // count rerolls, totals and outcome balances
  40. for (let i = 0; i < this.numRolls; i++) {
  41. let result = asphodice.roll(this.numDice)
  42. if (result.reroll) {
  43. this.rerollCounts.true += 1;
  44. } else {
  45. this.rerollCounts.false += 1;
  46. }
  47. let total = String(result.total);
  48. if (total in this.totalCounts) {
  49. this.totalCounts[total]++;
  50. } else {
  51. this.totalCounts[total] = 1;
  52. }
  53. let balance = String(result.balance);
  54. if (balance in this.balanceCounts) {
  55. this.balanceCounts[balance]++;
  56. } else {
  57. this.balanceCounts[balance] = 1;
  58. }
  59. if (result.outcome == Outcomes.Success) {
  60. let count = this.outcomeCounts[Outcomes.Success];
  61. this.outcomeCounts[Outcomes.Success] = Number(count) + 1;
  62. } else if (result.outcome == Outcomes.Fail) {
  63. let count = this.outcomeCounts[Outcomes.Fail];
  64. this.outcomeCounts[Outcomes.Fail] = Number(count) + 1;
  65. } // TODO: crits, once those are implemented
  66. for (let die of result.dice) {
  67. this.diceCounts[String(die)] += 1;
  68. }
  69. }
  70. }
  71. reportRolls(): string {
  72. var report = "";
  73. return report;
  74. }
  75. }
  76. let rollstats = new RollStats();
  77. rollstats.doRolls();
  78. console.log(rollstats);