Lottery.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Illuminate\Support;
  3. use RuntimeException;
  4. class Lottery
  5. {
  6. /**
  7. * The number of expected wins.
  8. *
  9. * @var int|float
  10. */
  11. protected $chances;
  12. /**
  13. * The number of potential opportunities to win.
  14. *
  15. * @var int|null
  16. */
  17. protected $outOf;
  18. /**
  19. * The winning callback.
  20. *
  21. * @var null|callable
  22. */
  23. protected $winner;
  24. /**
  25. * The losing callback.
  26. *
  27. * @var null|callable
  28. */
  29. protected $loser;
  30. /**
  31. * The factory that should be used to generate results.
  32. *
  33. * @var callable|null
  34. */
  35. protected static $resultFactory;
  36. /**
  37. * Create a new Lottery instance.
  38. *
  39. * @param int|float $chances
  40. * @param int<1, max>|null $outOf
  41. */
  42. public function __construct($chances, $outOf = null)
  43. {
  44. if ($outOf === null && is_float($chances) && $chances > 1) {
  45. throw new RuntimeException('Float must not be greater than 1.');
  46. }
  47. if ($outOf !== null && $outOf < 1) {
  48. throw new RuntimeException('Lottery "out of" value must be greater than or equal to 1.');
  49. }
  50. $this->chances = $chances;
  51. $this->outOf = $outOf;
  52. }
  53. /**
  54. * Create a new Lottery instance.
  55. *
  56. * @param int|float $chances
  57. * @param int|null $outOf
  58. * @return static
  59. */
  60. public static function odds($chances, $outOf = null)
  61. {
  62. return new static($chances, $outOf);
  63. }
  64. /**
  65. * Set the winner callback.
  66. *
  67. * @param callable $callback
  68. * @return $this
  69. */
  70. public function winner($callback)
  71. {
  72. $this->winner = $callback;
  73. return $this;
  74. }
  75. /**
  76. * Set the loser callback.
  77. *
  78. * @param callable $callback
  79. * @return $this
  80. */
  81. public function loser($callback)
  82. {
  83. $this->loser = $callback;
  84. return $this;
  85. }
  86. /**
  87. * Run the lottery.
  88. *
  89. * @param mixed ...$args
  90. * @return mixed
  91. */
  92. public function __invoke(...$args)
  93. {
  94. return $this->runCallback(...$args);
  95. }
  96. /**
  97. * Run the lottery.
  98. *
  99. * @param null|int $times
  100. * @return mixed
  101. */
  102. public function choose($times = null)
  103. {
  104. if ($times === null) {
  105. return $this->runCallback();
  106. }
  107. $results = [];
  108. for ($i = 0; $i < $times; $i++) {
  109. $results[] = $this->runCallback();
  110. }
  111. return $results;
  112. }
  113. /**
  114. * Run the winner or loser callback, randomly.
  115. *
  116. * @param mixed ...$args
  117. * @return callable
  118. */
  119. protected function runCallback(...$args)
  120. {
  121. return $this->wins()
  122. ? ($this->winner ?? fn () => true)(...$args)
  123. : ($this->loser ?? fn () => false)(...$args);
  124. }
  125. /**
  126. * Determine if the lottery "wins" or "loses".
  127. *
  128. * @return bool
  129. */
  130. protected function wins()
  131. {
  132. return static::resultFactory()($this->chances, $this->outOf);
  133. }
  134. /**
  135. * The factory that determines the lottery result.
  136. *
  137. * @return callable
  138. */
  139. protected static function resultFactory()
  140. {
  141. return static::$resultFactory ?? fn ($chances, $outOf) => $outOf === null
  142. ? random_int(0, PHP_INT_MAX) / PHP_INT_MAX <= $chances
  143. : random_int(1, $outOf) <= $chances;
  144. }
  145. /**
  146. * Force the lottery to always result in a win.
  147. *
  148. * @param callable|null $callback
  149. * @return void
  150. */
  151. public static function alwaysWin($callback = null)
  152. {
  153. self::setResultFactory(fn () => true);
  154. if ($callback === null) {
  155. return;
  156. }
  157. $callback();
  158. static::determineResultNormally();
  159. }
  160. /**
  161. * Force the lottery to always result in a lose.
  162. *
  163. * @param callable|null $callback
  164. * @return void
  165. */
  166. public static function alwaysLose($callback = null)
  167. {
  168. self::setResultFactory(fn () => false);
  169. if ($callback === null) {
  170. return;
  171. }
  172. $callback();
  173. static::determineResultNormally();
  174. }
  175. /**
  176. * Set the sequence that will be used to determine lottery results.
  177. *
  178. * @param array $sequence
  179. * @param callable|null $whenMissing
  180. * @return void
  181. */
  182. public static function fix($sequence, $whenMissing = null)
  183. {
  184. static::forceResultWithSequence($sequence, $whenMissing);
  185. }
  186. /**
  187. * Set the sequence that will be used to determine lottery results.
  188. *
  189. * @param array $sequence
  190. * @param callable|null $whenMissing
  191. * @return void
  192. */
  193. public static function forceResultWithSequence($sequence, $whenMissing = null)
  194. {
  195. $next = 0;
  196. $whenMissing ??= function ($chances, $outOf) use (&$next) {
  197. $factoryCache = static::$resultFactory;
  198. static::$resultFactory = null;
  199. $result = static::resultFactory()($chances, $outOf);
  200. static::$resultFactory = $factoryCache;
  201. $next++;
  202. return $result;
  203. };
  204. static::setResultFactory(function ($chances, $outOf) use (&$next, $sequence, $whenMissing) {
  205. if (array_key_exists($next, $sequence)) {
  206. return $sequence[$next++];
  207. }
  208. return $whenMissing($chances, $outOf);
  209. });
  210. }
  211. /**
  212. * Indicate that the lottery results should be determined normally.
  213. *
  214. * @return void
  215. */
  216. public static function determineResultsNormally()
  217. {
  218. static::determineResultNormally();
  219. }
  220. /**
  221. * Indicate that the lottery results should be determined normally.
  222. *
  223. * @return void
  224. */
  225. public static function determineResultNormally()
  226. {
  227. static::$resultFactory = null;
  228. }
  229. /**
  230. * Set the factory that should be used to determine the lottery results.
  231. *
  232. * @param callable $factory
  233. * @return void
  234. */
  235. public static function setResultFactory($factory)
  236. {
  237. self::$resultFactory = $factory;
  238. }
  239. }