feature/MAPG-235-basic-challenge-mode #48
@ -112,11 +112,10 @@ class GameController implements ISecured
|
||||
public function createNewChallenge(): IContent
|
||||
{
|
||||
// create Challenge
|
||||
$challengeToken = rand();
|
||||
while ($this->challengeRepository->getByToken($challengeToken)) {
|
||||
// if a challenge with the same token already exists
|
||||
$challengeToken = rand();
|
||||
}
|
||||
do {
|
||||
balazs marked this conversation as resolved
Outdated
|
||||
// initiliaze or if a challenge with the same token already exists
|
||||
$challengeToken = mt_rand();
|
||||
} while ($this->challengeRepository->getByToken($challengeToken));
|
||||
bence
commented
I think rand() should be called with explicit arguments, otherwise a number is returned between 0 and getrandmax() and getrandmax() is platform-dependent. On the other hand maybe the token could be generated as the room ID for multiplayer. Then a fixed length string would be generated - I used bin2hex(random_bytes(3)) for room ID. I guess it was intentional to use integer indexes in the DB, it could be be more efficient but I already used string indexes for other purpose. I think rand() should be called with explicit arguments, otherwise a number is returned between 0 and getrandmax() and getrandmax() is platform-dependent.
On the other hand maybe the token could be generated as the room ID for multiplayer. Then a fixed length string would be generated - I used bin2hex(random_bytes(3)) for room ID. I guess it was intentional to use integer indexes in the DB, it could be be more efficient but I already used string indexes for other purpose.
balazs
commented
Good point. However I don't think it would cause any problems. I thought it would be better to use 4 byte length integers instead of 3 bytes for more possible ids, because challenges are normally not getting deleted. Good point. However I don't think it would cause any problems. I thought it would be better to use 4 byte length integers instead of 3 bytes for more possible ids, because challenges are normally not getting deleted.
balazs
commented
I've read your comment again, and I see that bin2hex returns a string. Yes I thought it would perform better just to use an integer instead. I've read your comment again, and I see that bin2hex returns a string. Yes I thought it would perform better just to use an integer instead.
I can also see now, that on Windows the getrandmax() is only 32767, which is far too small. I am going to replace it with mt_rand() if that's fine for you.
|
||||
|
||||
$challenge = new Challenge();
|
||||
$challenge->setToken($challengeToken);
|
||||
|
@ -57,10 +57,8 @@ class UserInChallengeRepository
|
||||
}
|
||||
|
||||
// validate token string
|
||||
foreach (str_split($token_str) as $char) {
|
||||
if (!(('0' <= $char && $char <= '9') || ('a' <= $char && $char <= 'f'))) {
|
||||
return null;
|
||||
}
|
||||
if (!ctype_xdigit($token_str)) {
|
||||
balazs marked this conversation as resolved
Outdated
bence
commented
PHP's builtin ctype_xdigit could be used for that. PHP's builtin [ctype_xdigit](https://www.php.net/manual/en/function.ctype-xdigit.php) could be used for that.
|
||||
return null;
|
||||
}
|
||||
// convert token to int
|
||||
$token = hexdec($token_str);
|
||||
|
Maybe a do..while would be better here because the token calculation should not be repeated.
My eye is twitching when variables are used outside of the scope of {}, but it's different in PHP, so it can be refactored.