| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use RuntimeException; |
| 6 |
|
| 7 |
class Lottery |
| 8 |
{ |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
protected $chances; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
protected $outOf; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
protected $winner; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $loser; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected static $resultFactory; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct($chances, $outOf = null) |
| 51 |
{ |
| 52 |
if ($outOf === null && is_float($chances) && $chances > 1) { |
| 53 |
throw new RuntimeException('Float must not be greater than 1.'); |
| 54 |
} |
| 55 |
|
| 56 |
if ($outOf !== null && $outOf < 1) { |
| 57 |
throw new RuntimeException('Lottery "out of" value must be greater than or equal to 1.'); |
| 58 |
} |
| 59 |
|
| 60 |
$this->chances = $chances; |
| 61 |
|
| 62 |
$this->outOf = $outOf; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function odds($chances, $outOf = null) |
| 73 |
{ |
| 74 |
return new static($chances, $outOf); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function winner($callback) |
| 84 |
{ |
| 85 |
$this->winner = $callback; |
| 86 |
|
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function loser($callback) |
| 97 |
{ |
| 98 |
$this->loser = $callback; |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function __invoke(...$args) |
| 110 |
{ |
| 111 |
return $this->runCallback(...$args); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function choose($times = null) |
| 121 |
{ |
| 122 |
if ($times === null) { |
| 123 |
return $this->runCallback(); |
| 124 |
} |
| 125 |
|
| 126 |
$results = []; |
| 127 |
|
| 128 |
for ($i = 0; $i < $times; $i++) { |
| 129 |
$results[] = $this->runCallback(); |
| 130 |
} |
| 131 |
|
| 132 |
return $results; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected function runCallback(...$args) |
| 142 |
{ |
| 143 |
return $this->wins() |
| 144 |
? ($this->winner ?? fn () => true)(...$args) |
| 145 |
: ($this->loser ?? fn () => false)(...$args); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
protected function wins() |
| 154 |
{ |
| 155 |
return static::resultFactory()($this->chances, $this->outOf); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected static function resultFactory() |
| 164 |
{ |
| 165 |
return static::$resultFactory ?? fn ($chances, $outOf) => $outOf === null |
| 166 |
? random_int(0, PHP_INT_MAX) / PHP_INT_MAX <= $chances |
| 167 |
: random_int(1, $outOf) <= $chances; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static function alwaysWin($callback = null) |
| 177 |
{ |
| 178 |
self::setResultFactory(fn () => true); |
| 179 |
|
| 180 |
if ($callback === null) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$callback(); |
| 185 |
|
| 186 |
static::determineResultNormally(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static function alwaysLose($callback = null) |
| 196 |
{ |
| 197 |
self::setResultFactory(fn () => false); |
| 198 |
|
| 199 |
if ($callback === null) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
$callback(); |
| 204 |
|
| 205 |
static::determineResultNormally(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static function fix($sequence, $whenMissing = null) |
| 216 |
{ |
| 217 |
static::forceResultWithSequence($sequence, $whenMissing); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public static function forceResultWithSequence($sequence, $whenMissing = null) |
| 228 |
{ |
| 229 |
$next = 0; |
| 230 |
|
| 231 |
$whenMissing ??= function ($chances, $outOf) use (&$next) { |
| 232 |
$factoryCache = static::$resultFactory; |
| 233 |
|
| 234 |
static::$resultFactory = null; |
| 235 |
|
| 236 |
$result = static::resultFactory()($chances, $outOf); |
| 237 |
|
| 238 |
static::$resultFactory = $factoryCache; |
| 239 |
|
| 240 |
$next++; |
| 241 |
|
| 242 |
return $result; |
| 243 |
}; |
| 244 |
|
| 245 |
static::setResultFactory(function ($chances, $outOf) use (&$next, $sequence, $whenMissing) { |
| 246 |
if (array_key_exists($next, $sequence)) { |
| 247 |
return $sequence[$next++]; |
| 248 |
} |
| 249 |
|
| 250 |
return $whenMissing($chances, $outOf); |
| 251 |
}); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public static function determineResultsNormally() |
| 260 |
{ |
| 261 |
static::determineResultNormally(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public static function determineResultNormally() |
| 270 |
{ |
| 271 |
static::$resultFactory = null; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public static function setResultFactory($factory) |
| 281 |
{ |
| 282 |
self::$resultFactory = $factory; |
| 283 |
} |
| 284 |
} |