OneOf.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /// <reference path="../Say.ts" />
  2. class OneOf {
  3. private possibilities = [];
  4. private availablePossibilites;
  5. private randomMode : number;
  6. public static PURELY_AT_RANDOM : number = 0;
  7. public static ROTATING_RANDOM : number = 1;
  8. public static CYCLING : number = 2;
  9. private cyclingOrder : number = 0;
  10. public constructor (randomMode : number, ...poss : Array<any>) {
  11. this.randomMode = randomMode;
  12. this.addPossibilities(...poss);
  13. }
  14. public addPossibilities (...poss : Array<any>) {
  15. this.possibilities.push(...poss);
  16. this.updatePossibilities();
  17. }
  18. public updatePossibilities () {
  19. if (this.randomMode == OneOf.ROTATING_RANDOM) {
  20. this.availablePossibilites = this.possibilities.slice();
  21. }
  22. }
  23. public getOne () {
  24. if (this.randomMode == OneOf.PURELY_AT_RANDOM) {
  25. return this.possibilities[Math.floor(Math.random() * this.possibilities.length)];
  26. } else if (this.randomMode == OneOf.ROTATING_RANDOM) {
  27. if (this.availablePossibilites.length < 1) {
  28. this.availablePossibilites = this.possibilities.slice();
  29. }
  30. return this.availablePossibilites.splice(Math.floor(Math.random() * this.availablePossibilites.length), 1)[0]
  31. } else if (this.randomMode == OneOf.CYCLING) {
  32. var r = this.possibilities[this.cyclingOrder++];
  33. if (this.cyclingOrder > this.possibilities.length) {
  34. this.cyclingOrder = 0;
  35. }
  36. return r;
  37. }
  38. }
  39. }