-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecretSanta.php
190 lines (155 loc) · 5.16 KB
/
SecretSanta.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace neamtua;
class SecretSanta
{
private $database = [
'server' => '',
'username' => '',
'password' => '',
'database' => ''
];
private function connectToDatabase()
{
# try connecting to database
$connection = new \mysqli(
$this->database['server'],
$this->database['username'],
$this->database['password'],
$this->database['database']
);
# connection failed
if ($connection->connect_errno) {
throw new \Exception('Could not connect to database.');
}
return $connection;
}
public function getListOfPeople()
{
# connect to database
$connection = $this->connectToDatabase();
# run query
$sql = 'SELECT id, name FROM santas ORDER BY name ASC';
$result = $connection->query($sql);
if (!$result) {
throw new \Exception('Could not retrieve data.');
}
# close database connection
$connection->close();
# build array value
$values = $result->fetch_all();
# cleanup
$result->free();
# return values
return $values;
}
public function showGiftee($santaId)
{
# connect to database
$connection = $this->connectToDatabase();
# run query
$sql = 'SELECT has_extracted, giftee FROM santas WHERE id=? LIMIT 1';
$query = $connection->prepare($sql);
$query->bind_param('i', $santaId);
$query->execute();
$query->bind_result($extracted, $giftee);
$query->fetch();
$query->close();
# return value
if (empty($extracted)) {
# update
$sql = 'UPDATE santas SET has_extracted = 1 WHERE id = ? LIMIT 1';
$query = $connection->prepare($sql);
$query->bind_param('i', $santaId);
$query->execute();
# close database connection
$connection->close();
# return giftee
return $giftee;
}
# close database connection
$connection->close();
# return nothing
return '';
}
private function checkIfPairsHaveBeenGenerated()
{
# connect to database
$connection = $this->connectToDatabase();
# run query
$sql = 'SELECT id FROM santas WHERE giftee IS NULL LIMIT 1';
$result = $connection->query($sql);
if (!$result) {
throw new \Exception('Could not retrieve data.');
}
return ($result->num_rows)?false:true;
}
public function generatePairs()
{
# if pairs have been generated, do not generate
if ($this->checkIfPairsHaveBeenGenerated()) {
return;
}
$santas = [];
$pairs = [];
$people = $this->getListOfPeople();
foreach ($people as $row) {
$santas[$row[0]] = $row[1];
}
$givers = $santas;
$receivers = $santas;
foreach ($givers as $id => $giver) {
$hasReceived = false;
while (!$hasReceived) {
# pick random person
$receiver = rand(0, count($receivers)-1);
if ($receivers[$receiver] != $giver) {
# hurray, you have a match
$pairs[] = [
'id' => $id,
'santa' => $giver,
'giftee' => $receivers[$receiver]
];
# we remove him from the list
unset($receivers[$receiver]);
$receivers = array_values($receivers);
# mkay, we're done here
$hasReceived = true;
} else {
# particular case where there is only 1 user left and has received himself
if (count($receivers) == 1) {
# we have to swap with the first guy
$pairs[] = [
'id' => $id,
'santa' => $giver,
'giftee' => $pairs[0]['giftee']
];
$pairs[0]['giftee'] = $giver;
# mkay, we're done here
$hasReceived = true;
}
}
}
}
# connect to database
$connection = $this->connectToDatabase();
# update pairs in database
$sql = 'UPDATE santas SET giftee = ?, has_extracted = 0 WHERE id = ? LIMIT 1';
foreach ($pairs as $pair) {
$query = $connection->prepare($sql);
$query->bind_param('si', $pair['giftee'], $pair['id']);
$query->execute();
}
# close database connection
$connection->close();
}
public function resetSantas()
{
# connect to database
$connection = $this->connectToDatabase();
# update pairs in database
$sql = 'UPDATE santas SET has_extracted = 0, giftee = NULL';
$connection->query($sql);
# close database connection
$connection->close();
}
}