-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphpandom.php
More file actions
130 lines (108 loc) · 2.4 KB
/
phpandom.php
File metadata and controls
130 lines (108 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Sets the ranges for the raffle participants if the params are
* provided to the script.
*/
if (isset($argv[1])) {
$start = $argv[1];
$end = $argv[2];
insertEntrantRange($start, $end);
}
/**
* Retrieves the winner and removes him so he can't win again.
*/
if (!isset($argv[1]) && checkForEntrants()) {
getWinner();
}
/**
* Outputs a line to the console
*
* @param string $line
* @param int|integer $delay
* @return null
*/
function showLine(string $line, int $delay = 0)
{
sleep($delay);
echo $line . "\n\r";
return null;
}
/**
* This opens the range text file in the mode provided
*
* @param string $mode
* @return resource
*/
function openRangeFile(string $mode)
{
return fopen(__DIR__ . '/range.txt', $mode);
}
/**
* Returns the data from the range file
*
* @param resource $handle
* @return array
*/
function readRangeFile($handle): array
{
$data = fread($handle, filesize(__DIR__ . '/range.txt'));
return unserialize($data);
}
/**
* Inserts the endtrants into an array for later use
*
* @param int $start
* @param int $end
* @return null
*/
function insertEntrantRange(int $start, int $end)
{
$handle = openRangeFile('w+');
$entrants = [];
while ($start <= $end) {
array_push($entrants, $start);
$start++;
}
fwrite($handle, serialize($entrants));
fclose($handle);
showLine("\033[33mEntrants Registered");
return null;
}
/**
* Checks to make sure the entrants are set
*
* @return bool
*/
function checkForEntrants(): bool
{
if (!file_exists(__DIR__ . '/range.txt')) {
showLine("\033[31mNo Entrants Available");
return false;
}
return true;
}
/**
* Gets the winner
*
* @return null
*/
function getWinner()
{
$handle = openRangeFile('r+');
$entrants = readRangeFile($handle);
$count = count($entrants);
fclose($handle);
if ($count > 0) {
$winner = mt_rand(0, $count - 1);
showLine("\033[34mAnd the winner is...\033[0m");
showLine("\033[32mEntrant: " . $entrants[$winner] . "\033[0m", 2);
unset($entrants[$winner]);
$entrants = array_values($entrants);
$handle = openRangeFile('w+');
fwrite($handle, serialize($entrants));
fclose($handle);
return null;
}
showLine("\033[36mNo Entrants Have Been Entered");
return null;
}