-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from TemirkhanN/main
Fixes, race system, autologin.
- Loading branch information
Showing
53 changed files
with
1,195 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
- id: 1 | ||
name: Wouldren | ||
description: Farmer, harvester and lumberjack. Come from Timberhart. | ||
canCraft: false | ||
canMine: false | ||
canWoodcut: true | ||
canHarvest: true | ||
canGather: false | ||
canBrew: false | ||
stats: | ||
strength: 10 | ||
defence: 5 | ||
maxHealth: 20 | ||
|
||
- id: 2 | ||
name: Ironhart | ||
description: Miners and crafters. Come from Orveil city. | ||
canCraft: true | ||
canMine: true | ||
canWoodcut: false | ||
canHarvest: false | ||
canGather: false | ||
canBrew: false | ||
stats: | ||
strength: 15 | ||
defence: 8 | ||
maxHealth: 25 | ||
|
||
- id: 3 | ||
name: Highlander | ||
description: Herb gatherers and brewers. Come from Pit. | ||
canCraft: false | ||
canMine: false | ||
canWoodcut: false | ||
canHarvest: false | ||
canGather: true | ||
canBrew: true | ||
stats: | ||
strength: 10 | ||
defence: 3 | ||
maxHealth: 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Game\Auth; | ||
|
||
use Game\Engine\DBConnection; | ||
use Game\Engine\Error; | ||
use Game\User; | ||
|
||
class AuthService | ||
{ | ||
private ?User $currentUser = null; | ||
|
||
public function __construct(private readonly DBConnection $db) {} | ||
|
||
public function banPlayer(string $name): void | ||
{ | ||
$this->db->execute("UPDATE users set banned = 1 WHERE anv = ?", [$name]); | ||
} | ||
|
||
public function register(string $playerName, string $password, ?string $ip = ''): null|Error | ||
{ | ||
if ($playerName === '') { | ||
return new Error('Username can not be empty'); | ||
} | ||
|
||
if (mb_strlen($password) < 8) { | ||
return new Error('Password must be at least 8 characters long'); | ||
} | ||
|
||
if ($this->db->fetchRow('SELECT id FROM users WHERE anv=?', [$playerName]) !== []) { | ||
return new Error('Username is already taken'); | ||
} | ||
|
||
$ip = $ip ?? $_SERVER['REMOTE_ADDR']; | ||
|
||
$hashedPassword = password_hash($password, PASSWORD_BCRYPT); | ||
$this->db->execute('INSERT INTO users (anv, pwd, last_ip) VALUES (?, ?, ?)', [$playerName, $hashedPassword, $ip]); | ||
|
||
return null; | ||
} | ||
|
||
public function login(string $playerName, string $password): null|Error | ||
{ | ||
$user = $this->db->fetchRow('SELECT * FROM users WHERE anv = ?', [$playerName]); | ||
|
||
// User does not exist | ||
if ($user === []) { | ||
return new Error('Invalid username or password'); | ||
} | ||
|
||
// Password does not match | ||
if ($user['pwd'] !== $password && !password_verify($password, $user['pwd'])) { | ||
return new Error('Invalid username or password'); | ||
} | ||
|
||
if ($user['banned'] === 1) { | ||
return new Error('User is banned'); | ||
} | ||
|
||
$this->currentUser = null; | ||
$_SESSION['username'] = $user['anv']; | ||
|
||
return null; | ||
} | ||
|
||
public function logout(): void | ||
{ | ||
session_destroy(); | ||
} | ||
|
||
public function getCurrentUser(): ?User | ||
{ | ||
if ($this->currentUser === null) { | ||
if (!isset($_SESSION['username'])) { | ||
return null; | ||
} | ||
|
||
$user = $this->db->fetchRow('SELECT id, anv as name, banned FROM users WHERE anv=?', [$_SESSION['username']]); | ||
if ($user === []) { | ||
// This shouldn't be possible but if this happens then it means user was deleted | ||
$this->logout(); | ||
|
||
return null; | ||
} | ||
|
||
$this->currentUser = new User($user['id'], $user['name'], $user['banned'] === 1); | ||
} | ||
|
||
return $this->currentUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Game; | ||
|
||
use Game\Auth\AuthService; | ||
use Game\Player\CharacterRepository; | ||
use Game\Player\Player; | ||
|
||
readonly class Client | ||
{ | ||
public function __construct( | ||
private AuthService $authService, | ||
private CharacterRepository $characterRepository | ||
){} | ||
|
||
public function run(): void | ||
{ | ||
// TODO add self-sufficient front controller handling | ||
} | ||
|
||
public function getCurrentPlayer(): ?Player | ||
{ | ||
$currentUser = $this->authService->getCurrentUser(); | ||
if ($currentUser === null) { | ||
return null; | ||
} | ||
|
||
return $this->characterRepository->findByUser($currentUser); | ||
} | ||
|
||
/** | ||
* Basically pretends to say that client is not running unless user is signed in | ||
* | ||
* @todo requires rethinking | ||
* | ||
* @return bool | ||
*/ | ||
public function isRunning(): bool | ||
{ | ||
$currentUser = $this->authService->getCurrentUser(); | ||
|
||
return $currentUser !== null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.