/** public function __construct * Class constructor * Sets all outside data * * @param int user id * @param int game id * @action instantiates object * @return void */ public function __construct($user_id, $game_id) { if (empty($user_id)) { throw new MyException(__METHOD__ . ': No user id given'); } $Mysql = Mysql::get_instance(); $this->_user_id = (int) $user_id; $this->_game_id = (int) $game_id; $this->_mysql = $Mysql; if (defined('DEBUG')) { $this->_DEBUG = DEBUG; } }
/** static public function test * Test the MySQL connection * * @param void * @return bool connection OK */ public static function test() { if (!is_null(self::$_instance)) { return true; } if (!Mysql::test()) { return false; } // look for anything in the settings table $query = "\n\t\t\tSELECT *\n\t\t\tFROM `" . self::SETTINGS_TABLE . "`\n\t\t"; $return = Mysql::get_instance()->query($query); return (bool) $return; }
/** function get_footer * Generate the HTML footer portion of the page * * @param array option meta info * @return string HTML footer for page */ function get_footer($meta = array()) { $foot_data = isset($meta['foot_data']) ? $meta['foot_data'] : ''; $players = GamePlayer::get_count(); list($cur_games, $total_games) = Game::get_count(); $Mysql = Mysql::get_instance(); $html = ' <div id="footerspacer"> </div> <footer> <span>Total Players - ' . $players . '</span> <span>Active Games - ' . $cur_games . '</span> <span>Games Played - ' . $total_games . '</span> </footer> ' . $foot_data . ' <!-- Queries = ' . $Mysql->query_count . ' --> </body> </html>'; return $html; }
# exit; #} require_once INCLUDE_DIR . 'inc.settings.php'; require_once INCLUDE_DIR . 'func.global.php'; require_once INCLUDE_DIR . 'html.general.php'; require_once INCLUDE_DIR . 'html.tables.php'; require_once INCLUDE_DIR . 'html.risk.php'; // MAKE SURE TO LOAD CLASS FILES BEFORE STARTING THE SESSION // OR YOU END UP WITH INCOMPLETE OBJECTS PULLED FROM SESSION spl_autoload_register('load_class'); // store the default timezone $GLOBALS['_TZ'] = $GLOBALS['_DEFAULT_TIMEZONE']; /** * GLOBAL DATA * * * * * * * * * * * * * * * * * * * * * * * * * * */ $Mysql = Mysql::get_instance(); $Mysql->set_settings(array('log_path' => LOG_DIR, 'email_subject' => GAME_NAME . ' Query Error')); $GLOBALS['_&_DEBUG_QUERY'] = ''; $GLOBALS['_?_DEBUG_QUERY'] = ''; // make a list of all the color files available to use $GLOBALS['_COLORS'] = array(); $dh = opendir(realpath(dirname(__FILE__) . '/../css')); while (false !== ($file = readdir($dh))) { if (preg_match('/^c_(.+)\\.css$/i', $file, $match)) { // scanning for color files only $GLOBALS['_COLORS'][] = $match[1]; } } // convert the full color file name to just the color portion $GLOBALS['_DEFAULT_COLOR'] = ''; if (class_exists('Settings') && Settings::test()) {
/** static public function delete_inactive * Deletes the inactive users from the database * * @param int age in days * @return void */ public static function delete_inactive($age) { call(__METHOD__); $Mysql = Mysql::get_instance(); $age = (int) abs($age); if (0 == $age) { return false; } $exception_ids = array(); // make sure the 'unused' player is not an admin $query = "\n\t\t\tSELECT EP.player_id\n\t\t\tFROM " . self::EXTEND_TABLE . " AS EP\n\t\t\t\tJOIN " . Player::PLAYER_TABLE . " AS P\n\t\t\t\t\tUSING (player_id)\n\t\t\tWHERE P.is_admin = 1\n\t\t\t\tOR EP.is_admin = 1\n\t\t"; $results = $Mysql->fetch_value_array($query); $exception_ids = array_merge($exception_ids, $results); // make sure the 'unused' player is not currently in a game $query = "\n\t\t\tSELECT DISTINCT white_id\n\t\t\tFROM " . Game::GAME_TABLE . "\n\t\t\tWHERE state = 'Playing'\n\t\t"; $results = $Mysql->fetch_value_array($query); $exception_ids = array_merge($exception_ids, $results); $query = "\n\t\t\tSELECT DISTINCT black_id\n\t\t\tFROM " . Game::GAME_TABLE . "\n\t\t\tWHERE state = 'Playing'\n\t\t"; $results = $Mysql->fetch_value_array($query); $exception_ids = array_merge($exception_ids, $results); // make sure the 'unused' player isn't awaiting approval $query = "\n\t\t\tSELECT player_id\n\t\t\tFROM " . Player::PLAYER_TABLE . "\n\t\t\tWHERE is_approved = 0\n\t\t"; $results = $Mysql->fetch_value_array($query); $exception_ids = array_merge($exception_ids, $results); $exception_ids[] = 0; // don't break the IN clause $exception_id_list = implode(',', $exception_ids); // select unused accounts $query = "\n\t\t\tSELECT player_id\n\t\t\tFROM " . self::EXTEND_TABLE . "\n\t\t\tWHERE wins + losses <= 2\n\t\t\t\tAND player_id NOT IN ({$exception_id_list})\n\t\t\t\tAND last_online < DATE_SUB(NOW( ), INTERVAL {$age} DAY)\n\t\t"; $player_ids = $Mysql->fetch_value_array($query); call($player_ids); if ($player_ids) { Game::player_deleted($player_ids); $Mysql->delete(self::EXTEND_TABLE, " WHERE player_id IN (" . implode(',', $player_ids) . ") "); } }
/** static public function pause * Pauses the given games * * @param mixed array or csv of game ids * @param bool optional pause game (false = unpause) * @action pauses the games * @return void */ public static function pause($ids, $pause = true) { $Mysql = Mysql::get_instance(); array_trim($ids, 'int'); $pause = (int) (bool) $pause; if (empty($ids)) { throw new MyException(__METHOD__ . ': No game ids given'); } $Mysql->insert(self::GAME_TABLE, array('paused' => $pause), " WHERE game_id IN (" . implode(',', $ids) . ") "); }
/** protected function _send * Sends email messages of various types [optional data contents] * * @param string message type * @param mixed player id OR email address OR mixed array of both * @param array optional message data * @action send emails * @return bool success */ protected function _send($type, $to, $data = array()) { call(__METHOD__); call($type); call($to); call($data); if (is_array($to)) { $return = true; foreach ($to as $player) { $return = $this->_send($type, trim($player), $data) && $return; } return $return; } elseif (preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i', $to)) { $email = $to; } else { // $to is a single player id $player_id = (int) $to; // test if this user accepts emails $query = "\n\t\t\t\tSELECT P.email\n\t\t\t\t\t, PE.allow_email\n\t\t\t\tFROM " . Player::PLAYER_TABLE . " AS P\n\t\t\t\t\tLEFT JOIN " . GamePlayer::EXTEND_TABLE . " AS PE\n\t\t\t\t\t\tON P.player_id = PE.player_id\n\t\t\t\tWHERE P.player_id = '{$player_id}'\n\t\t\t"; list($email, $allow) = Mysql::get_instance()->fetch_row($query); call($email); call($allow); if (empty($allow) || empty($email)) { // no exception, just quit return false; } } call($email); $site_name = Settings::read('site_name'); if (!in_array($type, array_keys($this->email_data))) { throw new MyException(__METHOD__ . ': Trying to send email with unsupported type (' . $type . ')'); } $subject = $this->email_data[$type]['subject']; $message = $this->email_data[$type]['message']; // replace the meta vars $replace = array('/\\[\\[\\[GAME_NAME\\]\\]\\]/' => GAME_NAME, '/\\[\\[\\[site_name\\]\\]\\]/' => $site_name, '/\\[\\[\\[opponent\\]\\]\\]/' => ife($data['opponent'], 'opponent'), '/\\[\\[\\[export_data\\]\\]\\]/' => var_export($data, true)); $message = preg_replace(array_keys($replace), $replace, $message); $subject = GAME_NAME . ' - ' . $subject; if (!empty($data['game_id'])) { $message .= "\n\n" . 'Game Link: ' . $GLOBALS['_ROOT_URI'] . 'game.php?id=' . (int) $data['game_id']; } elseif (!empty($data['page'])) { $message .= "\n\n" . 'Direct Link: ' . $GLOBALS['_ROOT_URI'] . $data['page']; } $message .= ' ============================================= This message was automatically sent by ' . $site_name . ' and should not be replied to. ============================================= ' . $GLOBALS['_ROOT_URI']; $from_email = Settings::read('from_email'); // send the email $headers = "From: " . GAME_NAME . " <{$from_email}>\r\n"; $message = html_entity_decode($message); $this->_log($email . "\n" . $headers . "\n" . $subject . "\n" . $message); call($subject); call($message); call($headers); if ($GLOBALS['_USEEMAIL']) { return mail($email, $subject, $message, $headers); } return false; }
/** static public function write_game_file * Writes the game logs to a file for storage * * @param int game id * @action writes the game data to a file * @return bool success */ public static function write_game_file($game_id) { $game_id = (int) $game_id; if (!Settings::read('save_games')) { return false; } if (0 == $game_id) { return false; } $Mysql = Mysql::get_instance(); $query = "\n\t\t\tSELECT *\n\t\t\tFROM `" . self::GAME_TABLE . "`\n\t\t\tWHERE game_id = '{$game_id}'\n\t\t"; $game = $Mysql->fetch_assoc($query); if (!$game) { return false; } if (!in_array($game['state'], array('Playing', 'Finished'))) { return false; } $query = "\n\t\t\tSELECT P.player_id\n\t\t\t\t, P.username\n\t\t\t\t, GP.color\n\t\t\t\t, GP.order_num\n\t\t\tFROM `" . self::GAME_PLAYER_TABLE . "` AS `GP`\n\t\t\t\tJOIN `" . Player::PLAYER_TABLE . "` AS `P`\n\t\t\t\t\tON (P.player_id = GP.player_id)\n\t\t\tWHERE GP.game_id = '{$game_id}'\n\t\t\tORDER BY GP.order_num ASC\n\t\t"; $results = $Mysql->fetch_array($query); if (!$results) { return false; } $players = array(); foreach ($results as $result) { $players[$result['player_id']] = $result; } $logs = self::get_logs($game_id, false); if (empty($logs)) { return false; } $winner = 'Unknown'; if ('D' === $logs[0]['data'][0]) { $winner = (int) trim($logs[0]['data'], 'D '); $winner = "{$winner} - {$players[$winner]['username']}"; } // open the file for writing $filename = GAMES_DIR . GAME_NAME . '_' . $game_id . '_' . date('Ymd', strtotime($game['create_date'])) . '.dat'; // don't use ldate() here $file = fopen($filename, 'w'); if (false === $file) { return false; } fwrite($file, "{$game['game_id']} - {$game['name']} - {$game['game_type']}\n"); fwrite($file, date('Y-m-d', strtotime($game['create_date'])) . "\n"); // don't use ldate() here fwrite($file, date('Y-m-d', strtotime($game['modify_date'])) . "\n"); // don't use ldate() here fwrite($file, "{$winner}\n"); fwrite($file, $GLOBALS['_ROOT_URI'] . "\n"); fwrite($file, "=================================\n"); fwrite($file, $game['extra_info'] . "\n"); fwrite($file, "=================================\n"); foreach ($players as $player) { fwrite($file, "{$player['player_id']} - {$player['color']} - {$player['username']}\n"); } fwrite($file, "=================================\n"); $logs = array_reverse($logs); foreach ($logs as $log) { fwrite($file, $log['data'] . "\n"); } fwrite($file, "=================================\n"); fwrite($file, "KEY--- (plid = player_id, trid = territory_id, cid = continent_id, atk = attack, dfd = defend)\n"); fwrite($file, "A - Attack - [atk_plid]:[atk_trid]:[dfd_plid]:[dfd_trid]:[atk_rolls],[dfd_rolls]:[atk_lost],[dfd_lost]:[defeated]\n"); fwrite($file, "C - Card - [plid]:[card_id]\n"); fwrite($file, "D - Done (Game Over) - [winner_plid]\n"); fwrite($file, "E - Eradicated - [plid]:[killed_plid]:[cards_received (if any)]\n"); fwrite($file, "F - Fortify - [plid]:[armies_moved]:[from_trid]:[to_trid]\n"); fwrite($file, "I - Board Initialization - Ordered comma-separated list of plids ordered by trids (1-index).\n"); fwrite($file, "N - Next Player - [plid]\n"); fwrite($file, "O - Occupy - [plid]:[armies_moved]:[from_trid]:[to_trid]\n"); fwrite($file, "P - Placement - [plid]:[armies_placed]:[trid]\n"); fwrite($file, "Q - Quit - [plid]\n"); fwrite($file, "R - Reinforce - [plid]:[armies_given]:[num_territories_controlled]:[csv_cids_controlled (if any)]\n"); fwrite($file, "T - Trade - [plid]:[csv_card_list]:[armies_given]:[bonus_trid (if any)]\n"); fwrite($file, "V - Value for Trade - [next_trade_value]\n"); fwrite($file, "\n"); return fclose($file); }
/** static public function check_new * Checks if the given player has any new messages * * @param int player id * @return number of new messages */ public static function check_new($player_id) { call(__METHOD__); $player_id = (int) $player_id; if (!$player_id) { return false; } $Mysql = Mysql::get_instance(); $query = "\n\t\t\tSELECT COUNT(*)\n\t\t\tFROM " . self::GLUE_TABLE . "\n\t\t\tWHERE to_id = '{$player_id}'\n\t\t\t\tAND from_id <> '{$player_id}'\n\t\t\t\tAND (send_date <= NOW( )\n\t\t\t\t\tOR send_date IS NULL)\n\t\t\t\tAND deleted = 0\n\t\t\t\tAND view_date IS NULL\n\t\t"; $new = $Mysql->fetch_value($query); return $new; }
function create_admin($data) { debug(__FUNCTION__); try { $Mysql = Mysql::get_instance(); } catch (MySQLException $e) { // not sure what to do here } }
/** static public function pause * Pauses the given games * * @param mixed array or csv of game IDs * @param bool optional pause game (false = unpause) * @action pauses the games * @return void */ public static function pause($ids, $pause = true) { $Mysql = Mysql::get_instance(); array_trim($ids, 'int'); $pause = (int) (bool) $pause; $ids[] = 0; // don't break the IN clause $Mysql->insert(self::GAME_TABLE, array('paused' => $pause), " WHERE game_id IN (" . implode(',', $ids) . ") "); }
if (preg_match('|[^a-z0-9_]|i', $game_prefix)) { die('<strong>ERROR</strong>: "Game Prefix" can only contain numbers, letters, and underscores.'); } // Test the db connection. $mysql_config['hostname'] = $hostname; // the URI of the MySQL server host $mysql_config['username'] = $username; // the MySQL user's name $mysql_config['password'] = $password; // the MySQL user's password $mysql_config['database'] = $database; // the MySQL database name $mysql_config['log_path'] = LOG_DIR; // the MySQL log path try { Mysql::get_instance($mysql_config); } catch (Exception $e) { die($e->getMessage()); } foreach ($config_file as $line_num => $line) { switch (substr($line, 0, 40)) { case ' $GLOBALS[\'_DEFAULT_DATABASE\'][\'database': $config_file[$line_num] = str_replace("database_here", $database, $line); break; case ' $GLOBALS[\'_DEFAULT_DATABASE\'][\'username': $config_file[$line_num] = str_replace('username_here', $username, $line); break; case ' $GLOBALS[\'_DEFAULT_DATABASE\'][\'password': $config_file[$line_num] = str_replace('password_here', $password, $line); break; case ' $GLOBALS[\'_DEFAULT_DATABASE\'][\'hostname':
/** static public function clean_deleted * Cleans out any ids that shouldn't be deleted * * @param array of int player ids * @return array of int valid player ids */ public static function clean_deleted($player_ids) { call(__METHOD__); $Mysql = Mysql::get_instance(); array_trim($player_ids, 'int'); if (isset($GLOBALS['_ROOT_ADMIN'])) { $query = "\n\t\t\t\tSELECT player_id\n\t\t\t\tFROM " . self::PLAYER_TABLE . "\n\t\t\t\tWHERE username = '******'_ROOT_ADMIN']}'\n\t\t\t"; $root_admin = (int) $Mysql->fetch_value($query); if (in_array($root_admin, $player_ids)) { unset($user_ids[array_search($root_admin, $player_ids)]); } } return $player_ids; }
/** static public function add_used * Increments the 'used' count for the given setup * * @param int setup id * @action increments the 'used' count * @return void */ public static function add_used($setup_id) { call(__METHOD__); $setup_id = (int) $setup_id; $query = "\n\t\t\tUPDATE " . self::SETUP_TABLE . "\n\t\t\tSET used = used + 1\n\t\t\tWHERE setup_id = '{$setup_id}'\n\t\t"; Mysql::get_instance()->query($query); }