public function delete_unused_urls() { Database::begin(); try { Database::sql('DELETE FROM post_link WHERE post_id NOT IN (SELECT id FROM post)'); Database::sql('DELETE FROM post_update_link WHERE update_id NOT IN (SELECT id FROM post_update)'); Database::sql('DELETE FROM post_link_url WHERE link_id NOT IN (SELECT id FROM post_link)'); Database::sql('DELETE FROM post_update_link_url WHERE link_id NOT IN (SELECT id FROM post_update_link)'); Database::sql('DELETE FROM post_url WHERE id NOT IN (SELECT url_id FROM post_link_url) AND id NOT IN (SELECT url_id FROM post_update_link_url)'); $count = Database::count_affected(); if ($count > self::MAX_LINK_DELETIONS) { Database::rollback(); throw new Error_Cron('Too many post urls pending for deletion'); } Database::commit(); } catch (PDOException $e) { Database::rollback(); throw new Error_Cron('Error with database, while deleting post urls'); } }
public function pick($user, $card, $set, $shift) { $user_data = Database::get_row('game_user', array('order', 'force_picks'), 'id_user = ? and id_game = ?', array($user, $this->get_id())); $max = Database::get_field('game_user', 'max(`order`)', 'id_game = ?', $this->get_id()); $order = ($user_data['order'] + ($max + 1) * 15 + $shift * ($set % 2 ? 1 : -1)) % ($max + 1); $user_booster = Database::get_field('game_user', 'id_user', '`order` = ? and id_game = ?', array($order, $this->get_id())); $id_booster = Database::join('game_set', 'gs.id = gb.id_game_set')->get_field('game_booster', 'gb.`id`', 'gs.`order` = ? and gs.id_game = ? and gb.id_user = ?', array($set, $this->get_id(), $user_booster)); $pick = $shift + ($set - 1) * 15; Database::update('game_booster_card', array('id_user' => $user, 'pick' => $pick, 'forced' => 0), 'id = ? and id_user = 0 and not exists (select 1 from (select * from `game_booster_card` where id_game_booster = ?) as t where t.pick = ? and t.id_user > 0)', array($card, $id_booster, $shift)); $success = Database::count_affected() > 0; if ($user_data['force_picks']) { Database::update('game_user', array('force_picks' => 0), 'id_user = ? and id_game = ?', array($user, $this->get_id())); } if ($success) { $picked_count = Database::join('game_booster', 'gb.id_game_set = gs.id')->join('game_booster_card', 'gbc.id_game_booster = gb.id')->get_count('game_set', 'gs.id_game = ? and gs.order = ? and gbc.pick = ? and gbc.id_user > 0', array($this->get_id(), $set, $pick)); $force_users = Database::get_vector('game_user', 'id_user', 'id_game = ? and force_picks > ?', array($this->get_id(), 1)); $user_count = Database::get_count('game_user', 'id_game = ?', $this->get_id()); $cache_key = 'force_lock_' . $this->get_id() . '_' . $pick; if (count($force_users) + $picked_count >= $user_count && !Cache::get($cache_key)) { Cache::set($cache_key, 1); if (!empty($force_users)) { $this->force_picks($force_users, $set, $shift); } $this->shift_game_steps(); Cache::delete($cache_key); } } return $success; }