2 require_once 'Profile.php';
5 * Functions to work with player profiles
11 public function __construct()
13 $dbFile = dirname(__FILE__, 2) . '/data/profiles.sqlite3';
18 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
19 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
22 $this->createTablesIfNeeded();
25 public function getProfileByHardwareId(string $hwId): ?Profile
27 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE hwId = :id');
28 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
29 $stmt->execute([':id' => $hwId]);
30 $row = $stmt->fetch();
31 return $row === false ? null : $row;
34 public function getProfileBySessionId(string $sessionId): ?Profile
36 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE sessionId = :id');
37 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
38 $stmt->execute([':id' => $sessionId]);
39 $row = $stmt->fetch();
40 return $row === false ? null : $row;
43 public function createProfile(string $hwId): Profile
45 $stmt = $this->db->prepare(
47 INSERT INTO gamesticks
48 (hwId, sessionId, verificationCode)
49 VALUES (:hwId, :sessionId, :verificationCode)
55 ':sessionId' => 's' . str_replace(':', '', $hwId),
56 ':verificationCode' => date('His'),
59 return $this->getProfileByHardwareId($hwId);
62 protected function createTablesIfNeeded()
64 $res = $this->db->query(
65 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
73 CREATE TABLE gamesticks (
74 id INTEGER PRIMARY KEY AUTOINCREMENT,
77 verificationCode TEXT DEFAULT NULL,
78 gamerTag TEXT DEFAULT NULL,
79 founderFlag INTEGER DEFAULT 0 NOT NULL,
80 founderName TEXT DEFAULT NULL,
81 minAge INTEGER DEFAULT 3 NOT NULL,
82 avatar TEXT DEFAULT NULL,
84 created_at TEXT DEFAULT CURRENT_TIMESTAMP