smrealms /
smr
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Smr\Pages\Account; |
||
| 4 | |||
| 5 | use Globals; |
||
| 6 | use Smr\Database; |
||
| 7 | use Smr\Epoch; |
||
| 8 | use Smr\Page\AccountPage; |
||
| 9 | use Smr\Pages\Account\HistoryGames\ExtendedStats; |
||
| 10 | use Smr\Pages\Account\HistoryGames\GameNews; |
||
| 11 | use Smr\Pages\Account\HistoryGames\HallOfFame; |
||
| 12 | use Smr\Pages\Account\HistoryGames\Summary; |
||
| 13 | use Smr\Template; |
||
| 14 | use SmrAccount; |
||
| 15 | use SmrGame; |
||
| 16 | use SmrPlayer; |
||
| 17 | |||
| 18 | class GamePlay extends AccountPage { |
||
| 19 | |||
| 20 | public string $file = 'game_play.php'; |
||
| 21 | |||
| 22 | public function __construct( |
||
| 23 | private readonly ?string $message = null, |
||
| 24 | private readonly ?string $errorMessage = null |
||
| 25 | ) {} |
||
| 26 | |||
| 27 | public function build(SmrAccount $account, Template $template): void { |
||
| 28 | $template->assign('PageTopic', 'Play Game'); |
||
| 29 | |||
| 30 | $template->assign('ErrorMessage', $this->errorMessage); |
||
| 31 | $template->assign('Message', $this->message); |
||
| 32 | |||
| 33 | $template->assign('UserRankingLink', (new UserRankingView())->href()); |
||
| 34 | $template->assign('UserRankName', $account->getRank()->name); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | |||
| 36 | // *************************************** |
||
| 37 | // ** Play Games |
||
| 38 | // *************************************** |
||
| 39 | |||
| 40 | $games = []; |
||
| 41 | $games['Play'] = []; |
||
| 42 | $game_id_list = []; |
||
| 43 | $db = Database::getInstance(); |
||
| 44 | $dbResult = $db->read('SELECT end_time, game_id, game_name, game_speed, game_type |
||
| 45 | FROM game JOIN player USING (game_id) |
||
| 46 | WHERE account_id = ' . $db->escapeNumber($account->getAccountID()) . ' |
||
| 47 | AND enabled = \'TRUE\' |
||
| 48 | AND end_time >= ' . $db->escapeNumber(Epoch::time()) . ' |
||
| 49 | ORDER BY start_time, game_id DESC'); |
||
| 50 | foreach ($dbResult->records() as $dbRecord) { |
||
| 51 | $game_id = $dbRecord->getInt('game_id'); |
||
| 52 | $games['Play'][$game_id]['ID'] = $game_id; |
||
| 53 | $games['Play'][$game_id]['Name'] = $dbRecord->getString('game_name'); |
||
| 54 | $games['Play'][$game_id]['Type'] = SmrGame::GAME_TYPES[$dbRecord->getInt('game_type')]; |
||
| 55 | $games['Play'][$game_id]['EndDate'] = date($account->getDateTimeFormatSplit(), $dbRecord->getInt('end_time')); |
||
| 56 | $games['Play'][$game_id]['Speed'] = $dbRecord->getFloat('game_speed'); |
||
| 57 | |||
| 58 | $container = new GamePlayProcessor($game_id); |
||
| 59 | $games['Play'][$game_id]['PlayGameLink'] = $container->href(); |
||
| 60 | |||
| 61 | // creates a new player object |
||
| 62 | $curr_player = SmrPlayer::getPlayer($account->getAccountID(), $game_id); |
||
| 63 | |||
| 64 | // update turns for this game |
||
| 65 | $curr_player->updateTurns(); |
||
| 66 | |||
| 67 | // generate list of game_id that this player is joined |
||
| 68 | $game_id_list[] = $game_id; |
||
| 69 | |||
| 70 | $result2 = $db->read('SELECT count(*) as num_playing |
||
| 71 | FROM player |
||
| 72 | WHERE last_cpl_action >= ' . $db->escapeNumber(Epoch::time() - TIME_BEFORE_INACTIVE) . ' |
||
| 73 | AND game_id = ' . $db->escapeNumber($game_id)); |
||
| 74 | $games['Play'][$game_id]['NumberPlaying'] = $result2->record()->getInt('num_playing'); |
||
| 75 | |||
| 76 | // create a container that will hold next url and additional variables. |
||
| 77 | |||
| 78 | $container_game = new GameStats($game_id); |
||
| 79 | $games['Play'][$game_id]['GameStatsLink'] = $container_game->href(); |
||
| 80 | $games['Play'][$game_id]['Turns'] = $curr_player->getTurns(); |
||
| 81 | $games['Play'][$game_id]['LastMovement'] = format_time(Epoch::time() - $curr_player->getLastActive(), true); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (empty($games['Play'])) { |
||
| 85 | unset($games['Play']); |
||
| 86 | } |
||
| 87 | |||
| 88 | // *************************************** |
||
| 89 | // ** Join Games |
||
| 90 | // *************************************** |
||
| 91 | |||
| 92 | if (count($game_id_list) > 0) { |
||
| 93 | $dbResult = $db->read('SELECT game_id |
||
| 94 | FROM game |
||
| 95 | WHERE game_id NOT IN (' . $db->escapeArray($game_id_list) . ') |
||
| 96 | AND end_time >= ' . $db->escapeNumber(Epoch::time()) . ' |
||
| 97 | AND enabled = ' . $db->escapeBoolean(true) . ' |
||
| 98 | ORDER BY start_time DESC'); |
||
| 99 | } else { |
||
| 100 | $dbResult = $db->read('SELECT game_id |
||
| 101 | FROM game |
||
| 102 | WHERE end_time >= ' . $db->escapeNumber(Epoch::time()) . ' |
||
| 103 | AND enabled = ' . $db->escapeBoolean(true) . ' |
||
| 104 | ORDER BY start_time DESC'); |
||
| 105 | } |
||
| 106 | |||
| 107 | // are there any results? |
||
| 108 | foreach ($dbResult->records() as $dbRecord) { |
||
| 109 | $game_id = $dbRecord->getInt('game_id'); |
||
| 110 | $game = SmrGame::getGame($game_id); |
||
| 111 | $games['Join'][$game_id] = [ |
||
| 112 | 'ID' => $game_id, |
||
| 113 | 'Name' => $game->getName(), |
||
| 114 | 'JoinTime' => $game->getJoinTime(), |
||
| 115 | 'StartDate' => date($account->getDateTimeFormatSplit(), $game->getStartTime()), |
||
| 116 | 'EndDate' => date($account->getDateTimeFormatSplit(), $game->getEndTime()), |
||
| 117 | 'Players' => $game->getTotalPlayers(), |
||
| 118 | 'Type' => $game->getGameType(), |
||
| 119 | 'Speed' => $game->getGameSpeed(), |
||
| 120 | 'Credits' => $game->getCreditsNeeded(), |
||
| 121 | ]; |
||
| 122 | // create a container that will hold next url and additional variables. |
||
| 123 | $container = new GameJoin($game_id); |
||
| 124 | |||
| 125 | $games['Join'][$game_id]['JoinGameLink'] = $container->href(); |
||
| 126 | } |
||
| 127 | |||
| 128 | // *************************************** |
||
| 129 | // ** Previous Games |
||
| 130 | // *************************************** |
||
| 131 | |||
| 132 | $games['Previous'] = []; |
||
| 133 | |||
| 134 | //New previous games |
||
| 135 | $dbResult = $db->read('SELECT start_time, end_time, game_name, game_type, game_speed, game_id ' . |
||
| 136 | 'FROM game WHERE enabled = \'TRUE\' AND end_time < ' . $db->escapeNumber(Epoch::time()) . ' ORDER BY game_id DESC'); |
||
| 137 | foreach ($dbResult->records() as $dbRecord) { |
||
| 138 | $game_id = $dbRecord->getInt('game_id'); |
||
| 139 | $games['Previous'][$game_id]['ID'] = $game_id; |
||
| 140 | $games['Previous'][$game_id]['Name'] = $dbRecord->getString('game_name'); |
||
| 141 | $games['Previous'][$game_id]['StartDate'] = date($account->getDateFormat(), $dbRecord->getInt('start_time')); |
||
| 142 | $games['Previous'][$game_id]['EndDate'] = date($account->getDateFormat(), $dbRecord->getInt('end_time')); |
||
| 143 | $games['Previous'][$game_id]['Type'] = SmrGame::GAME_TYPES[$dbRecord->getInt('game_type')]; |
||
| 144 | $games['Previous'][$game_id]['Speed'] = $dbRecord->getFloat('game_speed'); |
||
| 145 | // create a container that will hold next url and additional variables. |
||
| 146 | $container = new HallOfFameAll($game_id); |
||
| 147 | $games['Previous'][$game_id]['PreviousGameHOFLink'] = $container->href(); |
||
| 148 | $container = new NewsReadArchives($game_id); |
||
| 149 | $games['Previous'][$game_id]['PreviousGameNewsLink'] = $container->href(); |
||
| 150 | $container = new GameStats($game_id); |
||
| 151 | $games['Previous'][$game_id]['PreviousGameLink'] = $container->href(); |
||
| 152 | } |
||
| 153 | |||
| 154 | foreach (Globals::getHistoryDatabases() as $databaseName => $oldColumn) { |
||
| 155 | //Old previous games |
||
| 156 | $db->switchDatabases($databaseName); |
||
| 157 | $dbResult = $db->read('SELECT start_date, end_date, game_name, type, speed, game_id |
||
| 158 | FROM game ORDER BY game_id DESC'); |
||
| 159 | foreach ($dbResult->records() as $dbRecord) { |
||
| 160 | $game_id = $dbRecord->getInt('game_id'); |
||
| 161 | $index = $databaseName . $game_id; |
||
| 162 | $gameName = $dbRecord->getString('game_name'); |
||
| 163 | $games['Previous'][$index]['ID'] = $game_id; |
||
| 164 | $games['Previous'][$index]['Name'] = $gameName; |
||
| 165 | $games['Previous'][$index]['StartDate'] = date($account->getDateFormat(), $dbRecord->getInt('start_date')); |
||
| 166 | $games['Previous'][$index]['EndDate'] = date($account->getDateFormat(), $dbRecord->getInt('end_date')); |
||
| 167 | $games['Previous'][$index]['Type'] = $dbRecord->getString('type'); |
||
| 168 | $games['Previous'][$index]['Speed'] = $dbRecord->getFloat('speed'); |
||
| 169 | // create a container that will hold next url and additional variables. |
||
| 170 | $container = new Summary($databaseName, $game_id, $gameName); |
||
| 171 | $games['Previous'][$index]['PreviousGameLink'] = $container->href(); |
||
| 172 | $container = new HallOfFame($databaseName, $game_id, $gameName); |
||
| 173 | $games['Previous'][$index]['PreviousGameHOFLink'] = $container->href(); |
||
| 174 | $container = new GameNews($databaseName, $game_id, $gameName); |
||
| 175 | $games['Previous'][$index]['PreviousGameNewsLink'] = $container->href(); |
||
| 176 | $container = new ExtendedStats($databaseName, $game_id, $gameName); |
||
| 177 | $games['Previous'][$index]['PreviousGameStatsLink'] = $container->href(); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | $db->switchDatabaseToLive(); // restore database |
||
| 181 | |||
| 182 | $template->assign('Games', $games); |
||
| 183 | |||
| 184 | // *************************************** |
||
| 185 | // ** Voting |
||
| 186 | // *************************************** |
||
| 187 | $container = new Vote(); |
||
| 188 | $template->assign('VotingHref', $container->href()); |
||
| 189 | |||
| 190 | $dbResult = $db->read('SELECT * FROM voting WHERE end > ' . $db->escapeNumber(Epoch::time()) . ' ORDER BY end DESC'); |
||
| 191 | if ($dbResult->hasRecord()) { |
||
| 192 | $votedFor = []; |
||
| 193 | $dbResult2 = $db->read('SELECT * FROM voting_results WHERE account_id = ' . $db->escapeNumber($account->getAccountID())); |
||
| 194 | foreach ($dbResult2->records() as $dbRecord2) { |
||
| 195 | $votedFor[$dbRecord2->getInt('vote_id')] = $dbRecord2->getInt('option_id'); |
||
| 196 | } |
||
| 197 | $voting = []; |
||
| 198 | foreach ($dbResult->records() as $dbRecord) { |
||
| 199 | $voteID = $dbRecord->getInt('vote_id'); |
||
| 200 | $voting[$voteID]['ID'] = $voteID; |
||
| 201 | $container = new VoteProcessor($voteID, new self()); |
||
| 202 | $voting[$voteID]['HREF'] = $container->href(); |
||
| 203 | $voting[$voteID]['Question'] = $dbRecord->getString('question'); |
||
| 204 | $voting[$voteID]['TimeRemaining'] = format_time($dbRecord->getInt('end') - Epoch::time(), true); |
||
| 205 | $voting[$voteID]['Options'] = []; |
||
| 206 | $dbResult2 = $db->read('SELECT option_id,text,count(account_id) FROM voting_options LEFT OUTER JOIN voting_results USING(vote_id,option_id) WHERE vote_id = ' . $db->escapeNumber($dbRecord->getInt('vote_id')) . ' GROUP BY option_id'); |
||
| 207 | foreach ($dbResult2->records() as $dbRecord2) { |
||
| 208 | $voting[$voteID]['Options'][$dbRecord2->getInt('option_id')]['ID'] = $dbRecord2->getInt('option_id'); |
||
| 209 | $voting[$voteID]['Options'][$dbRecord2->getInt('option_id')]['Text'] = $dbRecord2->getString('text'); |
||
| 210 | $voting[$voteID]['Options'][$dbRecord2->getInt('option_id')]['Chosen'] = isset($votedFor[$dbRecord->getInt('vote_id')]) && $votedFor[$voteID] == $dbRecord2->getInt('option_id'); |
||
| 211 | $voting[$voteID]['Options'][$dbRecord2->getInt('option_id')]['Votes'] = $dbRecord2->getInt('count(account_id)'); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | $template->assign('Voting', $voting); |
||
| 215 | } |
||
| 216 | |||
| 217 | // *************************************** |
||
| 218 | // ** Announcements View |
||
| 219 | // *************************************** |
||
| 220 | $container = new LoginAnnouncements(viewAll: true); |
||
| 221 | $template->assign('OldAnnouncementsLink', $container->href()); |
||
| 222 | } |
||
| 223 | |||
| 224 | } |
||
| 225 |