1
1

OneOf.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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.possibilities = poss;
  13. if (this.randomMode == OneOf.ROTATING_RANDOM) {
  14. this.availablePossibilites = this.possibilities.slice();
  15. }
  16. }
  17. public getOne () {
  18. if (this.randomMode == OneOf.PURELY_AT_RANDOM) {
  19. return this.possibilities[Math.floor(Math.random() * this.possibilities.length)];
  20. } else if (this.randomMode == OneOf.ROTATING_RANDOM) {
  21. if (this.availablePossibilites.length < 1) {
  22. this.availablePossibilites = this.possibilities.slice();
  23. }
  24. return this.availablePossibilites.splice(Math.floor(Math.random() * this.availablePossibilites.length), 1)[0]
  25. } else if (this.randomMode == OneOf.CYCLING) {
  26. var r = this.possibilities[this.cyclingOrder++];
  27. if (this.cyclingOrder > this.possibilities.length) {
  28. this.cyclingOrder = 0;
  29. }
  30. return r;
  31. }
  32. }
  33. }