private function select_game($message_id) { $sql = 'SELECT * FROM c2_games WHERE message_id = :message_id'; $query = $this->db->prepare($sql); $query->bindParam(':message_id', $message_id); $query->execute(); if ($query->rowCount()) { $results = $query->fetchAll(); $out = []; $DbUser = new \GroupBot\Database\User($this->db); foreach ($results as $player) { if ($user = $DbUser->getUserFromId($player['user_id'])) { $out[] = new c2player($user, $player['message_id'], $player['rank']); } } usort($out, function ($a, $b) { if ($a->rank == $b->rank) { return 0; } if ($a->rank > $b->rank) { return 1; } return -1; }); return $out; } return false; }
public static function constructFromTelegramUpdate($user_update, \PDO $db) { if (isset($user_update['username']) && strcmp($user_update['username'], BOT_FULL_USER_NAME) === 0) { $user = new User(); $user->user_name = BOT_FULL_USER_NAME; $user->welcome_sent = true; return $user; } $changed = false; $userSQL = new \GroupBot\Database\User($db); if ($user = $userSQL->getUserFromId($user_update['id'])) { if (isset($user_update['first_name']) && strcmp($user->first_name, $user_update['first_name']) !== 0) { $user->first_name = $user_update['first_name']; $changed = true; } if (isset($user_update['username']) && strcmp($user->user_name, $user_update['username']) !== 0) { $user->user_name = $user_update['username']; $changed = true; } if (isset($user_update['last_name']) && strcmp($user->last_name, $user_update['last_name']) !== 0) { $user->last_name = $user_update['last_name']; $changed = true; } } else { $user = new User(); $last_name = isset($user_update['last_name']) ? $user_update['last_name'] : NULL; $username = isset($user_update['username']) ? $user_update['username'] : NULL; $user->construct($user_update['id'], $user_update['first_name'], $last_name, $username); $changed = true; } if ($changed) { $user->save($db); } return $user; }
public function addDealer(\PDO $db) { if (!$this->isGameStarted()) { $Player = new Player(); //$Dealer = new User(); //$Dealer->construct('-1', 'Dealer', '', 'Dealer'); $DbUser = new \GroupBot\Database\User($db); $Dealer = $DbUser->getUserFromId('-1'); $Player->construct($Dealer, new Hand(), new PlayerState(PlayerState::Dealer), -1, 0, false); $Player->Hand->addCard($this->Deck->dealCard()); $this->Dealer = $Player; $this->SQL->insert_player($this->game_id, $Player); return true; } return false; }
public function set_option($parameter, $value) { $DbUser = new \GroupBot\Database\User($this->db); $admin = $DbUser->getUserFromId($this->chat->admin_user_id); if ($admin->user_id == $this->Message->User->user_id) { switch ($parameter) { case 'no_spam_mode': return $this->set_boolean_option('no_spam_mode', $value); break; case 'bot_kick_mode': return $this->set_boolean_option('bot_kick_mode', $value); break; case 'yandex_enabled': return $this->set_boolean_option('yandex_enabled', $value); break; } } return false; }
private function performVote() { if (isset($this->chat)) { $user = Query::getUserMatchingStringOrErrorMessage($this->db, $this->chat, $this->getParam(0)); } else { $user = Query::getUserMatchingStringOrErrorMessage($this->db, $this->Message->Chat, $this->getParam(0)); } if (is_string($user)) { return $user; } if (strcasecmp($this->getParam(0), BOT_FRIENDLY_NAME) === 0) { return "wow, thx brah! " . emoji(0x1f618); } if (!$user) { return emoji(0x1f44e) . " Can't find that user, brah"; } if ($user->user_id == $this->Message->User->user_id) { return emoji(0x1f44e) . " You can't vote for yourself!"; } switch ($this->getParam(1)) { case 'up': $voteType = new VoteType(VoteType::Up); break; case 'down': $voteType = new VoteType(VoteType::Down); break; case 'neutral': $voteType = new VoteType(VoteType::Neutral); break; default: return emoji(0x1f44e) . " Your vote must be either *up*, *down* or *neutral*."; } $DbUser = new \GroupBot\Database\User($this->db); $voted_for = $DbUser->getUserFromId($user->user_id); $userVote = new UserVote(); $userVote->construct($this->Message->User, $voted_for, $voteType); $Vote = new \GroupBot\Brains\Vote\Vote($this->db); if ($Vote->SQL->check_if_vote_exists($userVote)) { $this->vote_cast = true; return emoji(0x1f528) . " Vote unchanged - you've voted this way before!"; } $Vote->SQL->update_vote($userVote); $this->vote_cast = true; $rank = $Vote->getVoteTotalForUserInChat($userVote->voted_for, $this->Message->Chat->id); $vote_prefix = $rank > 0 ? "+" : ""; return emoji(0x1f528) . " Vote updated. *" . $userVote->voted_for->getName() . "* is now on *{$vote_prefix}{$rank}*"; }