public function delete() { $sql = "DELETE FROM `{$this->table_name}`\n WHERE `{$this->id_field}`={$this->id}\n LIMIT 1;"; if (!$this->DbConnection->executeQuery($sql)) { return false; } return true; }
/** * Deletes the row on the database, if it violates referential-integrity as * defined at database level it will simply return false constraints. * @return boolean */ public function delete() { $sql = "DELETE FROM {$this->_tableName}\n WHERE {$this->_idField}={$this->_id}\n LIMIT 1;"; if (!$this->_DbConnection->executeQuery($sql)) { return false; } return true; }
/** * Gets an instance of the the DbConnection * * @param string $db_host * @param string $db_user * @param string $db_password * @param string $db_name * @return DbConnection * @todo Change to Use config from files. */ public static function getInstance($connection = '') { if (empty($connection) || !isset(self::$_instances[$connection])) { $Config = Config::getInstance(); if (empty($connection)) { $connection = $Config->system_enviroment; } $DbConfig = Config::getDbConfig($connection); $DbConnection = new DbConnection($DbConfig->db_host, $DbConfig->db_user, $DbConfig->db_password, $DbConfig->db_name); try { $DbConnection->connect(); } catch (Exception $e) { loadErrorPage('nodb'); } $DbConnection->executeQuery("SET CHARACTER SET 'utf8'"); self::$_instances[$connection] = $DbConnection; } return self::$_instances[$connection]; }
/** * Generates a new formation for the specified team, which will be directly stored both in the database and in the internal model. * * It is a 4-4-2 formation. It always selects the freshest players of the team. * * @param WebSoccer $websoccer request context. * @param DbConnection $db database connection. * @param SimulationTeam $team Team that needs a new formation. * @param int $matchId match id. */ public static function generateNewFormationForTeam(WebSoccer $websoccer, DbConnection $db, SimulationTeam $team, $matchId) { // get all players (prefer the freshest players) $columns['id'] = 'id'; $columns['position'] = 'position'; $columns['position_main'] = 'mainPosition'; $columns['vorname'] = 'firstName'; $columns['nachname'] = 'lastName'; $columns['kunstname'] = 'pseudonym'; $columns['w_staerke'] = 'strength'; $columns['w_technik'] = 'technique'; $columns['w_kondition'] = 'stamina'; $columns['w_frische'] = 'freshness'; $columns['w_zufriedenheit'] = 'satisfaction'; if ($websoccer->getConfig('players_aging') == 'birthday') { $ageColumn = 'TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())'; } else { $ageColumn = 'age'; } $columns[$ageColumn] = 'age'; // get players from usual team if (!$team->isNationalTeam) { $fromTable = $websoccer->getConfig('db_prefix') . '_spieler'; $whereCondition = 'verein_id = %d AND verletzt = 0 AND gesperrt = 0 AND status = 1 ORDER BY w_frische DESC'; $parameters = $team->id; $result = $db->querySelect($columns, $fromTable, $whereCondition, $parameters); } else { // national team: take best players of nation $columnsStr = ''; $firstColumn = TRUE; foreach ($columns as $dbName => $aliasName) { if (!$firstColumn) { $columnsStr = $columnsStr . ', '; } else { $firstColumn = FALSE; } $columnsStr = $columnsStr . $dbName . ' AS ' . $aliasName; } $nation = $db->connection->escape_string($team->name); $dbPrefix = $websoccer->getConfig('db_prefix'); $queryStr = '(SELECT ' . $columnsStr . ' FROM ' . $dbPrefix . '_spieler WHERE nation = \'' . $nation . '\' AND position = \'Torwart\' ORDER BY w_staerke DESC, w_frische DESC LIMIT 1)'; $queryStr .= ' UNION ALL (SELECT ' . $columnsStr . ' FROM ' . $dbPrefix . '_spieler WHERE nation = \'' . $nation . '\' AND position = \'Abwehr\' ORDER BY w_staerke DESC, w_frische DESC LIMIT 4)'; $queryStr .= ' UNION ALL (SELECT ' . $columnsStr . ' FROM ' . $dbPrefix . '_spieler WHERE nation = \'' . $nation . '\' AND position = \'Mittelfeld\' ORDER BY w_staerke DESC, w_frische DESC LIMIT 4)'; $queryStr .= ' UNION ALL (SELECT ' . $columnsStr . ' FROM ' . $dbPrefix . '_spieler WHERE nation = \'' . $nation . '\' AND position = \'Sturm\' ORDER BY w_staerke DESC, w_frische DESC LIMIT 2)'; $result = $db->executeQuery($queryStr); } $lvExists = FALSE; $rvExists = FALSE; $lmExists = FALSE; $rmExists = FALSE; $ivPlayers = 0; $zmPlayers = 0; while ($playerinfo = $result->fetch_array()) { $position = $playerinfo['position']; // generate a 4-4-2 formation if ($position == PLAYER_POSITION_GOALY && isset($team->positionsAndPlayers[PLAYER_POSITION_GOALY]) && count($team->positionsAndPlayers[PLAYER_POSITION_GOALY]) == 1 || $position == PLAYER_POSITION_DEFENCE && isset($team->positionsAndPlayers[PLAYER_POSITION_DEFENCE]) && count($team->positionsAndPlayers[PLAYER_POSITION_DEFENCE]) >= 4 || $position == PLAYER_POSITION_MIDFIELD && isset($team->positionsAndPlayers[PLAYER_POSITION_MIDFIELD]) && count($team->positionsAndPlayers[PLAYER_POSITION_MIDFIELD]) >= 4 || $position == PLAYER_POSITION_STRIKER && isset($team->positionsAndPlayers[PLAYER_POSITION_STRIKER]) && count($team->positionsAndPlayers[PLAYER_POSITION_STRIKER]) >= 2) { continue; } $mainPosition = $playerinfo['mainPosition']; //prevent double LV/RV/LM/RM if ($mainPosition == 'LV') { if ($lvExists) { $mainPosition = 'IV'; $ivPlayers++; if ($ivPlayers == 3) { $mainPosition = 'RV'; $rvExists = TRUE; } } else { $lvExists = TRUE; } } elseif ($mainPosition == 'RV') { if ($rvExists) { $mainPosition = 'IV'; $ivPlayers++; if ($ivPlayers == 3) { $mainPosition = 'LV'; $lvExists = TRUE; } } else { $rvExists = TRUE; } } elseif ($mainPosition == 'IV') { $ivPlayers++; if ($ivPlayers == 3) { if (!$rvExists) { $mainPosition = 'RV'; $rvExists = TRUE; } else { $mainPosition = 'LV'; $lvExists = TRUE; } } } elseif ($mainPosition == 'LM') { if ($lmExists) { $mainPosition = 'ZM'; $zmPlayers++; } else { $lmExists = TRUE; } } elseif ($mainPosition == 'RM') { if ($rmExists) { $mainPosition = 'ZM'; $zmPlayers++; } else { $rmExists = TRUE; } } elseif ($mainPosition == 'LS' || $mainPosition == 'RS') { $mainPosition = 'MS'; } elseif ($mainPosition == 'ZM') { $zmPlayers++; if ($zmPlayers > 2) { $mainPosition = 'DM'; } } $player = new SimulationPlayer($playerinfo['id'], $team, $position, $mainPosition, 3.0, $playerinfo['age'], $playerinfo['strength'], $playerinfo['technique'], $playerinfo['stamina'], $playerinfo['freshness'], $playerinfo['satisfaction']); if (strlen($playerinfo['pseudonym'])) { $player->name = $playerinfo['pseudonym']; } else { $player->name = $playerinfo['firstName'] . ' ' . $playerinfo['lastName']; } $team->positionsAndPlayers[$player->position][] = $player; SimulationStateHelper::createSimulationRecord($websoccer, $db, $matchId, $player); } $result->free(); }
/** * Provides clubs in order of league table criteria. * Additionally updates league history at first time of user session. * * @param WebSoccer $websoccer Application Context * @param DbConnection $db DB connection * @param int $leagueId league ID * @return array array of teams, ordered by table criteria. */ public static function getTeamsOfLeagueOrderedByTableCriteria(WebSoccer $websoccer, DbConnection $db, $leagueId) { // get current season $result = $db->querySelect('id', $websoccer->getConfig('db_prefix') . '_saison', 'liga_id = %d AND beendet = \'0\' ORDER BY name DESC', $leagueId, 1); $season = $result->fetch_array(); $result->free(); $fromTable = $websoccer->getConfig('db_prefix') . '_verein AS C'; $fromTable .= ' LEFT JOIN ' . $websoccer->getConfig('db_prefix') . '_user AS U ON C.user_id = U.id'; $fromTable .= ' LEFT JOIN ' . $websoccer->getConfig('db_prefix') . '_leaguehistory AS PREVDAY ON (PREVDAY.team_id = C.id AND PREVDAY.matchday = (C.sa_spiele - 1)'; if ($season) { $fromTable .= ' AND PREVDAY.season_id = ' . $season['id']; } $fromTable .= ')'; $columns = array(); $columns['C.id'] = 'id'; $columns['C.name'] = 'name'; $columns['C.sa_punkte'] = 'score'; $columns['C.sa_tore'] = 'goals'; $columns['C.sa_gegentore'] = 'goals_received'; $columns['(C.sa_tore - C.sa_gegentore)'] = 'goals_diff'; $columns['C.sa_siege'] = 'wins'; $columns['C.sa_niederlagen'] = 'defeats'; $columns['C.sa_unentschieden'] = 'draws'; $columns['C.sa_spiele'] = 'matches'; $columns['C.bild'] = 'picture'; $columns['U.id'] = 'user_id'; $columns['U.nick'] = 'user_name'; $columns['U.email'] = 'user_email'; $columns['U.picture'] = 'user_picture'; $columns['PREVDAY.rank'] = 'previous_rank'; // order by $whereCondition = 'C.liga_id = %d AND C.status = \'1\' ORDER BY score DESC, goals_diff DESC, wins DESC, draws DESC, goals DESC, name ASC'; $parameters = $leagueId; $teams = array(); // shall update league history? DO this only every 10 minutes $now = $websoccer->getNowAsTimestamp(); $updateHistory = FALSE; if ($season && (!isset($_SESSION['leaguehist']) || $_SESSION['leaguehist'] < $now - 600)) { $_SESSION['leaguehist'] = $now; $updateHistory = TRUE; $queryTemplate = 'REPLACE INTO ' . $websoccer->getConfig('db_prefix') . '_leaguehistory '; $queryTemplate .= '(team_id, season_id, user_id, matchday, rank) '; $queryTemplate .= 'VALUES (%d, ' . $season['id'] . ', %s, %d, %d);'; } $result = $db->querySelect($columns, $fromTable, $whereCondition, $parameters); $rank = 0; while ($team = $result->fetch_array()) { $rank++; $team['user_picture'] = UsersDataService::getUserProfilePicture($websoccer, $team['user_picture'], $team['user_email'], 20); $teams[] = $team; // update history if ($updateHistory && $team['matches']) { $userId = $team['user_id'] ? $team['user_id'] : 'DEFAULT'; $query = sprintf($queryTemplate, $team['id'], $userId, $team['matches'], $rank); $db->executeQuery($query); } } $result->free(); return $teams; }