forked from lakridserne/teaminator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamGenClass.php
108 lines (104 loc) · 3.53 KB
/
teamGenClass.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
<?php
/**
* Coding Pirates Teaminator
* Used to generate teams at Coding Pirates Game Jam 2015-2016
*/
class teamGen {
private $db;
function __construct($db) {
$this->db = $db;
}
function genTeam($amount,$minage,$maxage,$designers,$visualtext,$ultra,$team_ID=false) {
// generate team
// first see if there enough people to generate a team of selected size in
// the selected age group
if($ultra != 1) {
$sql = "SELECT * FROM participants WHERE age BETWEEN :minage AND :maxage AND teaminated=0 AND updated_since_csv=1";
} else {
$sql = "SELECT * FROM participants WHERE age BETWEEN :minage AND :maxage AND teaminated=0 AND updated_since_csv=1 AND ultra=1";
}
$ages = [
[":minage",$minage],
[":maxage",$maxage]
];
$possibleCandidates = $this->db->query($sql,$ages);
$numresults = count($possibleCandidates);
if($numresults >= $amount) {
// Yay! Enough! Go ahead and pull the list of candidates from the DB
$sortedResult = $this->sortInterests($possibleCandidates,$designers,$visualtext,$amount,$numresults);
// Put people in DB
if($team_ID != false) {
$next_team_ID = $team_ID;
} else {
$sql = "SELECT team_ID FROM team ORDER BY team_ID DESC LIMIT 1";
$nextid = $this->db->query($sql);
$next_team_ID = $nextid['0']["team_ID"] + 1;
}
$sql = "INSERT INTO team (team_ID,participants_ID, created) VALUES (:team_ID, :participants_ID, :created)";
$fetch_name = "SELECT name FROM participants WHERE ID=:id";
$update_teaminate = "UPDATE participants SET teaminated=1 WHERE ID=:id";
foreach($sortedResult as $team_member) {
$values = [
[":team_ID",$next_team_ID],
[":participants_ID",$team_member],
[":created",2]
];
$this->db->query($sql,$values);
$name_values = [
[":id",$team_member]
];
$this->db->query($update_teaminate,$name_values);
$memberArray[] = $this->db->query($fetch_name,$name_values);
}
return $memberArray;
} else {
echo "Ikke nok deltagere der er mellem " . $minage . " og " . $maxage . " år.";
}
}
// Sort based on interests
private function sortInterests($resultset,$designers,$visualtext,$teamsize,$numresults) {
// Find number of programmers we need and iterate over them
$programmers = $teamsize - $designers;
$selectedPeople = array();
$match = 0;
$selectedPeopleNum = 0;
while($selectedPeopleNum < $programmers) {
$randnum = rand(0,($numresults-1));
$id = $resultset[$randnum]['ID'];
if($resultset[$randnum][$visualtext] == 1) {
// Programmer found, yay! Add ID to return array if it's not already selected
foreach($selectedPeople as $person) {
if($id == $person) {
$match = 1;
}
}
if($match == 0) {
$selectedPeople[] = $id;
$selectedPeopleNum++;
}
}
$match = 0;
}
// now repeat the success for designers
$match = 0;
while($selectedPeopleNum < $teamsize) {
$randnum = rand(0,($numresults-1));
$id = $resultset[$randnum]['ID'];
if($resultset[$randnum]['graphic'] == 1) {
// Designer found! Add ID to array
foreach($selectedPeople as $person) {
if($id == $person) {
$match = 1;
}
}
if($match == 0) {
$selectedPeople[] = $id;
$selectedPeopleNum++;
}
}
$match = 0;
}
return $selectedPeople;
}
}
?>