protected function task()
{
if ($this->method == 'GET') {
// Check for compile tasks
$needToBeCompiled = $this->select("SELECT * FROM User WHERE compileStatus=1 ORDER BY userID ASC LIMIT 1");
if (count($needToBeCompiled) > 0) {
$this->insert("UPDATE User SET compileStatus = 2 WHERE userID = " . $needToBeCompiled['userID']);
return array("type" => "compile", "user" => $needToBeCompiled);
}
// Assign a run game tasks
$possibleNumPlayers = array(2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6);
$numPlayers = $possibleNumPlayers[array_rand($possibleNumPlayers)];
$seedPlayer = null;
$randValue = mt_rand() / mt_getrandmax();
if ($randValue > 0.5) {
$seedPlayer = $this->select("SELECT * FROM User WHERE isRunning = 1 order by rand()*-pow(sigma, 2) LIMIT 1");
}
if ($randValue > 0.25 && $randValue <= 0.5) {
$seedPlayer = $this->select("SELECT * FROM (SELECT u.* FROM (SELECT MAX(g.timestamp) as maxTime, gu.userID as userID FROM GameUser gu INNER JOIN Game g ON g.gameID=gu.gameID GROUP BY gu.userID) temptable INNER JOIN User u on u.userID = temptable.userID where numGames < 400 and isRunning = 1 order by maxTime ASC limit 15) orderedTable order by rand() limit 1;");
}
if ($randValue <= 0.25 || count($seedPlayer) < 1) {
$seedPlayer = $this->select("SELECT u.* FROM (SELECT MAX(g.timestamp) as maxTime, gu.userID as userID FROM GameUser gu INNER JOIN Game g ON g.gameID=gu.gameID GROUP BY gu.userID) temptable INNER JOIN User u on u.userID = temptable.userID where isRunning = 1 order by maxTime ASC limit 1");
}
$muRankLimit = intval(5.0 / pow((double) mt_rand(1, mt_getrandmax()) / (double) mt_getrandmax(), 0.65));
$players = $this->selectMultiple("SELECT * FROM (SELECT * FROM User WHERE isRunning=1 and userID <> {$seedPlayer['userID']} ORDER BY ABS(mu-{$seedPlayer['mu']}) LIMIT {$muRankLimit}) muRankTable ORDER BY rand() LIMIT " . ($numPlayers - 1));
array_push($players, $seedPlayer);
// Pick map size
$sizes = array(20, 25, 25, 30, 30, 30, 35, 35, 35, 35, 40, 40, 40, 45, 45, 50);
$size = $sizes[array_rand($sizes)];
// Send game task
if (count($players) == $numPlayers) {
return array("type" => "game", "width" => $size, "height" => $size, "users" => $players);
}
}
}