<?php require SCRIPTROOT . 'jamstates.php'; $stmt = $dbconnection->prepare('SELECT * FROM jams ORDER BY suggestionsbegin DESC;'); $stmt->execute(); $rows = $stmt->fetchAll(); $jams = array(); foreach ($rows as $row) { $jam = array(); $jam['id'] = $row['id']; $jam['title'] = $row['title']; $jam['status'] = $row['status']; $jam['suggestionsbegin'] = SuggestionsBegin($row); $jam['votingbegins'] = VotingBegins($row); $jam['themeannounce'] = ThemeAnnounce($row); $jam['jambegins'] = JamBegins($row); $jam['submissionsbegin'] = SubmissionsBegin($row); $jam['submissionsend'] = SubmissionsEnd($row); $jam['judgingends'] = JudgingEnds($row); array_push($jams, $jam); } SendResponse($jams);
function VerifyJamState(&$Jam) { global $dbconnection; if ($Jam['status'] == JamStatus::Disabled) { return; } $time = time(); $length = JudgingEnds($Jam); if ($time >= $length) { $status = JamStatus::Complete; } else { if ($time >= ($length -= $Jam['judginglength'])) { $status = JamStatus::Judging; } else { if ($time >= ($length -= $Jam['submissionslength'])) { $status = JamStatus::ReceivingGameSubmissions; } else { if ($time >= ($length -= $Jam['jamlength'])) { $status = JamStatus::JamRunning; } else { if ($time >= ($length -= $Jam['themeannouncelength'])) { $status = JamStatus::ThemeAnnounced; } else { if ($time >= ($length -= $Jam['votinglength'])) { $status = JamStatus::ThemeVoting; } else { if ($time >= ($length -= $Jam['approvallength'])) { $status = JamStatus::WaitingThemeApprovals; } else { if ($time >= ($length -= $Jam['suggestionslength'])) { $status = JamStatus::ReceivingSuggestions; } else { $status = JamStatus::WaitingSuggestionsStart; } } } } } } } } $round = $Jam['currentround']; $selectedtheme = $Jam['selectedthemeid']; if ($status == JamStatus::ThemeVoting || $status > JamStatus::ThemeVoting && $selectedtheme == SelectedTheme::NotSelected) { // we need to figure out what round we are in // remember we must maintain the integrity of the data // so... if lots of time has been skipped we must still advance one round at a time // first find how far we have advanced into the voting period $timeoffset = $time - ($Jam['suggestionsbegin'] + $Jam['suggestionslength'] + $Jam['approvallength']); // next find out how long each round should last // round length = voting length / total rounds [inital rounds + the final round(1)] $roundlength = $Jam['votinglength'] / ($Jam['initialvotingrounds'] + 1); // track the round offset - after each iteration it will be increased by the round length $roundoffset = 0; // if no round is selected setup rounds if ($round == CurrentRound::NotSelected) { $round = $Jam['initialvotingrounds']; SetupInitialRounds($Jam['id'], $Jam['initialvotingrounds']); } // loop over each round and check if the round needs to be advanced for ($i = $Jam['initialvotingrounds']; $i >= 0; $i--) { // check if the timeoffset falls within this round // if it does advance the round if needed and break the loop if ($timeoffset >= $roundoffset && ($timeoffset < $roundoffset + $roundlength || $i == CurrentRound::FinalRound)) { if ($round != $i) { $round = $i; if ($round == CurrentRound::FinalRound) { SetupFinalRound($Jam['id'], $Jam['initialvotingrounds'], $Jam['topthemesinfinal']); } } break; } else { if ($round > $i) { $round = $i; } } $roundoffset += $roundlength; } } if ($status >= JamStatus::ThemeAnnounced && $selectedtheme == SelectedTheme::NotSelected) { $stmt = $dbconnection->prepare('SELECT * FROM themes WHERE jamid = ? AND round = ?;'); $stmt->execute(array($Jam['id'], CurrentRound::FinalRound)); $themes = $stmt->fetchAll(); foreach ($themes as $key => $theme) { $stmt = $dbconnection->prepare('SELECT value FROM votes WHERE jamid = ? AND themeid = ? AND round = ?;'); $stmt->execute(array($Jam['id'], $theme['id'], CurrentRound::FinalRound)); $votes = $stmt->fetchAll(); $themes[$key]['votes'] = 0; foreach ($votes as $vote) { $themes[$key]['votes'] += $vote['value']; } } usort($themes, 'CompareThemeVotes'); $count = 0; $winningthemes = array(); foreach ($themes as $theme) { if (!isset($highscore)) { $highscore = $theme['votes']; $winningthemes[$count++] = $theme; } else { if ($highscore == $theme['votes']) { $winningthemes[$count++] = $theme; } else { break; } } } if ($count == 1) { $selectedtheme = $winningthemes[0]['id']; } else { if ($count > 1) { $pickedtheme = array_rand($winningthemes); $selectedtheme = $winningthemes[$pickedtheme]['id']; } } } if ($status != $Jam['status'] || $round != $Jam['currentround'] || $selectedtheme != $Jam['selectedthemeid']) { $stmt = $dbconnection->prepare('UPDATE jams SET status = ?, currentround = ?, selectedthemeid = ? WHERE id = ?;'); $stmt->execute(array($status, $round, $selectedtheme, $Jam['id'])); $Jam['status'] = $status; $Jam['currentround'] = $round; $Jam['selectedthemeid'] = $selectedtheme; } }