1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

dont rely on the steam api for getting user names

This commit is contained in:
Robin Appelman 2020-11-29 20:06:49 +01:00
commit fd9a7e8527
13 changed files with 54 additions and 78 deletions

View file

@ -71,7 +71,7 @@ class AuthController extends BaseController {
$steamId = $steam->validate();
if ($steamId) {
$steamIdObject = new SteamId($steamId);
$key = $this->userProvider->store($steamIdObject);
$key = $this->userProvider->store($steamIdObject, $steamIdObject->getNickname());
$this->authProvider->setUser($token, $steamIdObject, $key);
}
Flight::redirect($return);

View file

@ -13,19 +13,17 @@ class DemoPlayer implements JsonSerializable {
private string $team;
private string $class;
private string $steamId;
private string $avatar;
private int $kills;
private int $assists;
private int $deaths;
public function __construct(int $id, int $userId, string $name, string $team, string $class, string $steamId, string $avatar, int $kills, int $assists, int $deaths) {
public function __construct(int $id, int $userId, string $name, string $team, string $class, string $steamId, int $kills, int $assists, int $deaths) {
$this->id = $id;
$this->userId = $userId;
$this->name = $name;
$this->team = $team;
$this->class = $class;
$this->steamId = $steamId;
$this->avatar = $avatar;
$this->kills = $kills;
$this->assists = $assists;
$this->deaths = $deaths;
@ -55,10 +53,6 @@ class DemoPlayer implements JsonSerializable {
return $this->steamId;
}
public function getAvatar(): string {
return $this->avatar;
}
public function getKills(): int {
return $this->kills;
}
@ -84,7 +78,6 @@ class DemoPlayer implements JsonSerializable {
$row['team'],
$row['class'],
$row['steamid'],
$row['avatar'],
$row['kills'],
$row['assists'],
$row['deaths']
@ -99,7 +92,6 @@ class DemoPlayer implements JsonSerializable {
'team' => $this->getTeam(),
'class' => $this->getClass(),
'steamid' => $this->getSteamId(),
'avatar' => $this->getAvatar(),
'kills' => $this->getKills(),
'assists' => $this->getAssists(),
'deaths' => $this->getDeaths(),

View file

@ -10,14 +10,12 @@ class User implements JsonSerializable {
private int $id;
private string $steamId;
private string $name;
private string $avatar;
private string $token;
public function __construct(int $id, string $steamId, string $name, string $avatar, string $token) {
public function __construct(int $id, string $steamId, string $name, string $token) {
$this->id = $id;
$this->steamId = $steamId;
$this->name = $name;
$this->avatar = $avatar;
$this->token = $token;
}
@ -33,10 +31,6 @@ class User implements JsonSerializable {
return $this->name;
}
public function getAvatar(): string {
return $this->avatar;
}
public function getToken(): string {
return $this->token;
}
@ -46,7 +40,6 @@ class User implements JsonSerializable {
'id' => $this->getId(),
'steamid' => $this->getSteamId(),
'name' => $this->getName(),
'avatar' => $this->getAvatar(),
];
}
@ -60,7 +53,6 @@ class User implements JsonSerializable {
(int) $row['id'],
$row['steamid'],
$row['name'],
$row['avatar'],
$row['token'] ?? ''
);
}

View file

@ -68,7 +68,7 @@ class DemoSaver {
), $storedDemo->getBackend(), $storedDemo->getPath());
foreach ($demo->getPlayers() as $player) {
$userId = $this->userProvider->getUserId($player->getSteamId());
$userId = $this->userProvider->getUserId($player->getSteamId(), $player->getName());
$userMap[$player->getDemoUserId()] = $userId;
$this->playerProvider->store(new Player(

View file

@ -22,7 +22,7 @@ class UserProvider extends BaseProvider {
$this->generator = $generator;
}
public function store(SteamId $steamId): string {
public function store(SteamId $steamId, string $name): string {
$token = $this->generator->generateString(64, Generator::EASY_TO_READ);
$user = $this->get($steamId->getSteamId64());
@ -34,8 +34,8 @@ class UserProvider extends BaseProvider {
$query->insert('users')
->values([
'steamid' => $query->createNamedParameter($steamId->getSteamId64()),
'name' => $query->createNamedParameter($steamId->getNickname()),
'avatar' => $query->createNamedParameter($steamId->getMediumAvatarUrl()),
'name' => $query->createNamedParameter($name),
'avatar' => $query->createNamedParameter(''),
'token' => $query->createNamedParameter($token),
])->add('orderBy', 'ON CONFLICT DO NOTHING')// hack to append arbitrary string to sql
->execute();
@ -49,7 +49,7 @@ class UserProvider extends BaseProvider {
// first search in the view which contains the most used name for the users
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users_named')
->where($query->expr()->eq('steamid', $query->createNamedParameter($steamid)));
@ -59,7 +59,7 @@ class UserProvider extends BaseProvider {
// if the user is newly inserted it wont be in our view yet
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('steamid', $query->createNamedParameter($steamid)));
@ -76,7 +76,7 @@ class UserProvider extends BaseProvider {
// first search in the view which contains the most used name for the users
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users_named')
->where($query->expr()->eq('id', $query->createNamedParameter($userId, \PDO::PARAM_INT)));
@ -86,7 +86,7 @@ class UserProvider extends BaseProvider {
// if the user is newly inserted it wont be in our view yet
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('id', $query->createNamedParameter($userId, \PDO::PARAM_INT)));
@ -162,7 +162,7 @@ class UserProvider extends BaseProvider {
public function byKey(string $key): ?User {
$query = $this->getQueryBuilder();
$query->select(['id', 'steamid', 'name', 'avatar', 'token'])
$query->select(['id', 'steamid', 'name', 'token'])
->from('users')
->where($query->expr()->eq('token', $query->createNamedParameter($key)));
@ -171,13 +171,13 @@ class UserProvider extends BaseProvider {
return $row ? User::fromRow($row) : null;
}
public function getUserId(string $steamId): int {
public function getUserId(string $steamId, string $name): int {
$existing = $this->get($steamId);
if ($existing) {
return $existing->getId();
}
$this->store(new SteamId($steamId));
$this->store(new SteamId($steamId, false), $name);
return $this->get($steamId)->getId();
}