Skip to content

Commit

Permalink
Add update server api for stk-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Dec 6, 2018
1 parent 7a5a9d8 commit a68110e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@
}
break;

case 'update-config':
try
{
$userid = isset($_POST['userid']) ? (int)utf8_encode($_POST['userid']) : 0;
$token = isset($_POST['token']) ? utf8_encode($_POST['token']) : null;
$address = isset($_POST['address']) ? utf8_encode($_POST['address']) : 0;
$port = isset($_POST['port']) ? (int)utf8_encode($_POST['port']) : 0;
$new_difficulty = isset($_POST['new-difficulty']) ? (int)$_POST['new-difficulty'] : 0;
$new_game_mode = isset($_POST['new-game-mode']) ? (int)$_POST['new-game-mode'] : 3;

ClientSession::get($token, $userid)
->updateServerConfig($address, $port, $new_difficulty, $new_game_mode);

$output->startElement('update-config');
$output->writeAttribute('success', 'yes');
$output->writeAttribute('info', '');
$output->endElement();
}
catch(Exception $e)
{
$output->addErrorElement('update-config', $e->getMessage());
}
break;

case 'vote': // vote on a server
try
{
Expand Down
48 changes: 48 additions & 0 deletions include/ClientSession.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,52 @@ public function clearUserJoinedServer()
throw new ClientSessionException($e->getMessage());
}
}

/**
* Update configuration of a server hosted by this user (atm only difficulty and game mode)
*
* @param int $ip
* @param int $port
* @param int $new_difficulty
* @param int $new_game_mode
*
* @return Server
* @throws ServerException
*/
public function updateServerConfig($ip, int $port, int $new_difficulty, int $new_game_mode)
{
try
{
$count = DBConnection::get()->query(
"UPDATE `{DB_VERSION}_servers`
SET `difficulty` = :new_difficulty, `game_mode` = :new_game_mode
WHERE `ip`= :ip AND `port` = :port AND `host_id` = :host_id",
DBConnection::ROW_COUNT,
[
':ip' => $ip,
':port' => $port,
':new_difficulty' => $new_difficulty,
':new_game_mode' => $new_game_mode,
':host_id' => $this->user->getId()
],
[
':ip' => DBConnection::PARAM_INT,
':port' => DBConnection::PARAM_INT,
':new_difficulty' => DBConnection::PARAM_INT,
':new_game_mode' => DBConnection::PARAM_INT,
':host_id' => DBConnection::PARAM_INT
]
);
}
catch (DBException $e)
{
throw new ServerException(exception_message_db(_('update the server config')));
}

if ($count !== 1)
{
throw new ServerException(_h("Failed to update server config."));
}
}

}

0 comments on commit a68110e

Please sign in to comment.