Dice.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. interface TestingOptions {
  2. name : string;
  3. value : number;
  4. }
  5. class Dice {
  6. protected range : Array<number> = [0, 0, 1, 1];
  7. public minResult = 0;
  8. protected testString : string;
  9. public constructor (testString : string) {
  10. this.testString = testString;
  11. }
  12. public roll (stat : number) : Array<number> {
  13. let rng = this.range.slice();
  14. if (stat >= 10) {
  15. rng.push(2, 1);
  16. } else if (stat >= 7) {
  17. rng.push(1, 1);
  18. } else if (stat >= 4) {
  19. rng.push(1);
  20. }
  21. let results = [];
  22. for (let i = 0; i < stat; i++) {
  23. let index = Math.floor(Math.random() * (rng.length));
  24. results.push(rng[index]);
  25. }
  26. // TODO: Print the dice results if Memory.ShowDice = 1
  27. return results;
  28. }
  29. public static sum (a, b) {
  30. return a + b;
  31. }
  32. public getSay (results : Array<number>) : Say {
  33. let finalResult = results.reduce(Dice.sum);
  34. return new Say(
  35. new SayBold("[", this.testString, "] "),
  36. " = [", results.join("] ["), "]",
  37. results.length == 1 ? "" :
  38. (" = " + finalResult)
  39. );
  40. }
  41. public static testAgainstRoll (player : TestingOptions, enemy : TestingOptions) : number {
  42. let playerDice = new Dice(player.name);
  43. let playerResult = playerDice.roll(player.value);
  44. let enemyDice = new Dice(enemy.name);
  45. let enemyResult = enemyDice.roll(enemy.value);
  46. return playerResult.reduce(Dice.sum) - enemyResult.reduce(Dice.sum);
  47. }
  48. public static testAgainstDifficulty (player : TestingOptions, difficulty : number) : number {
  49. let playerDice = new Dice(player.name);
  50. let playerResult = playerDice.roll(player.value);
  51. return playerResult.reduce(Dice.sum) - difficulty;
  52. }
  53. }