/** * Transfer credits from one account to another. * * @param int $fromAccountID Account ID * @param string $targetCharName Character name of person receiving credits * @param int $credits Amount of credits */ public function transferCredits($fromAccountID, $targetCharName, $credits) { // // Return values: // -1 = From or to account, one or the other does not exist. (likely the latter.) // -2 = Sender has an insufficient balance. // -3 = Unknown character. // true = Successful transfer // false = Error // $sql = "SELECT account_id, char_id, name AS char_name FROM {$this->charMapDatabase}.`char` WHERE `char`.name = ? LIMIT 1"; $sth = $this->connection->getStatement($sql); if (!$sth->execute(array($targetCharName)) || !($char = $sth->fetch())) { // Unknown character. return -3; } $targetAccountID = $char->account_id; $targetCharID = $char->char_id; $sql = "SELECT COUNT(account_id) AS accounts FROM {$this->loginDatabase}.login WHERE "; $sql .= "account_id = ? OR account_id = ? LIMIT 2"; $sth = $this->connection->getStatement($sql); if (!$sth->execute(array($fromAccountID, $targetAccountID)) || $sth->fetch()->accounts != 2) { // One or the other, from or to, are non-existent accounts. return -1; } if (!$this->loginServer->hasCreditsRecord($fromAccountID)) { // Sender has a zero balance. return -2; } $creditsTable = Flux::config('FluxTables.CreditsTable'); $xferTable = Flux::config('FluxTables.CreditTransferTable'); // Get balance of sender. $sql = "SELECT balance FROM {$this->loginDatabase}.{$creditsTable} WHERE account_id = ? LIMIT 1"; $sth = $this->connection->getStatement($sql); if (!$sth->execute(array($fromAccountID))) { // Error. return false; } if ($sth->fetch()->balance < $credits) { // Insufficient balance. return -2; } // Take credits from fromAccount first. if ($this->loginServer->depositCredits($fromAccountID, -$credits)) { // Then deposit to targetAccount next. if (!$this->loginServer->depositCredits($targetAccountID, $credits)) { // Attempt to restore credits if deposit to toAccount failed. $this->loginServer->depositCredits($fromAccountID, $credits); return false; } else { $sql = "INSERT INTO {$this->charMapDatabase}.{$xferTable} "; $sql .= "(from_account_id, target_account_id, target_char_id, amount, transfer_date) "; $sql .= "VALUES (?, ?, ?, ?, NOW())"; $sth = $this->connection->getStatement($sql); // Log transfer. $sth->execute(array($fromAccountID, $targetAccountID, $targetCharID, $credits)); return true; } } else { return false; } }
/** * See Flux_LoginServer->isAuth(). * * @param string $username * @param string $password * @return bool * @access public */ public function isAuth($username, $password) { return $this->loginServer->isAuth($username, $password); }