mirror of
https://github.com/jakejarvis/spoons.git
synced 2025-04-26 19:18:28 -04:00
Staff is evenly spread across the list when shuffled. Fixes #1
This commit is contained in:
parent
011934600a
commit
3c440741e0
@ -26,18 +26,64 @@ function getHighestOrderNum() {
|
|||||||
return $spooner['order_num'];
|
return $spooner['order_num'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCamperIDs(){
|
||||||
|
$camper_ids = array();
|
||||||
|
|
||||||
|
$result = mysql_query("SELECT id FROM spooners WHERE spooned = 0 AND staff = 0 ORDER BY id");
|
||||||
|
while($spooner = mysql_fetch_array($result)) {
|
||||||
|
array_push($camper_ids, $spooner['id']);
|
||||||
|
}
|
||||||
|
return $camper_ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStaffIDs(){
|
||||||
|
$staff_ids = array();
|
||||||
|
|
||||||
|
$result = mysql_query("SELECT id FROM spooners WHERE spooned = 0 AND staff = 1 ORDER BY id");
|
||||||
|
while($spooner = mysql_fetch_array($result)) {
|
||||||
|
array_push($staff_ids, $spooner['id']);
|
||||||
|
}
|
||||||
|
return $staff_ids;
|
||||||
|
}
|
||||||
|
|
||||||
function shuffleSpooners() {
|
function shuffleSpooners() {
|
||||||
$random_array = range(0, getNumActiveSpooners() - 1); // create array containing numbers 0 to # of spooners remaining - 1, inclusive
|
$camper_ids = getCamperIDs();
|
||||||
|
$staff_ids = getStaffIDs();
|
||||||
|
|
||||||
|
shuffle($camper_ids);
|
||||||
|
shuffle($staff_ids);
|
||||||
|
|
||||||
|
//An array to put the ids for the new shuffled list
|
||||||
|
$random_ids = array();
|
||||||
|
|
||||||
|
//Determine the number of campers between each staff
|
||||||
|
$spacing = round(count($camper_ids)/count($staff_ids))+1;
|
||||||
|
|
||||||
|
|
||||||
|
while(count($random_ids) < getNumActiveSpooners()){
|
||||||
|
echo "oh no";
|
||||||
|
for($i = 0; $i < $spacing-1; $i++){
|
||||||
|
array_push($random_ids, array_pop($camper_ids));
|
||||||
|
}
|
||||||
|
array_push($random_ids, array_pop($staff_ids));
|
||||||
|
|
||||||
|
//This shouldn't happen but it is here just in case
|
||||||
|
if(count($staff_ids) == 0){
|
||||||
|
while(count($camper_ids) > 0){
|
||||||
|
array_push($random_ids, array_pop($camper_ids));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//Recalculate the spacing each round to end up with a more even spacing over the entire list.
|
||||||
|
$spacing = round(count($camper_ids)/count($staff_ids))+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
shuffle($random_array); // shuffle said array
|
|
||||||
|
|
||||||
// update order of spooners based on array
|
|
||||||
$result = mysql_query("SELECT id FROM spooners WHERE spooned = 0 ORDER BY id");
|
$result = mysql_query("SELECT id FROM spooners WHERE spooned = 0 ORDER BY id");
|
||||||
|
|
||||||
$i = 0;
|
for ($i=0; $spooner = mysql_fetch_array($result); $i++) {
|
||||||
while($spooner = mysql_fetch_array($result)) {
|
mysql_query("UPDATE spooners SET order_num = " . $i . " WHERE id = " . $random_ids[$i]);
|
||||||
mysql_query("UPDATE spooners SET order_num = " . $random_array[$i] . " WHERE id = " . $spooner['id']);
|
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user