2 require_once 'Profile.php';
5 * Functions to work with player profiles
11 public function __construct()
13 $dbFile = static::getDbFilePath();
18 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
19 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
22 $this->createTablesIfNeeded();
25 public static function getDbFilePath()
27 return dirname(__FILE__, 2) . '/data/profiles.sqlite3';
30 public function getProfileByHardwareId(string $hwId): ?Profile
32 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE hwId = :id');
33 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
34 $stmt->execute([':id' => $hwId]);
35 $row = $stmt->fetch();
36 return $row === false ? null : $row;
39 public function getProfileBySessionId(string $sessionId): ?Profile
41 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE sessionId = :id');
42 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
43 $stmt->execute([':id' => $sessionId]);
44 $row = $stmt->fetch();
45 return $row === false ? null : $row;
48 public function getProfileByVerificationCode(string $code): ?Profile
50 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE verificationCode = :code');
51 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
52 $stmt->execute([':code' => $code]);
53 $row = $stmt->fetch();
54 return $row === false ? null : $row;
57 public function createProfile(string $hwId): Profile
59 $stmt = $this->db->prepare(
61 INSERT INTO gamesticks
62 (hwId, sessionId, verificationCode)
63 VALUES (:hwId, :sessionId, :verificationCode)
69 ':sessionId' => 's' . str_replace(':', '', $hwId),
70 ':verificationCode' => date('His'),
73 return $this->getProfileByHardwareId($hwId);
76 public function updateProfile(string $hwId, array $values): ?Profile
82 $sql = 'UPDATE gamesticks SET';
84 foreach ($values as $column => $value) {
85 $sqlParts[] = ' ' . $column . '= :' . $column;
86 $params[':' . $column] = $value;
88 $sql .= implode(', ', $sqlParts) . ' WHERE hwId = :hwId';
90 $stmt = $this->db->prepare($sql);
91 $stmt->execute($params);
93 return $this->getProfileByHardwareId($hwId);
96 protected function createTablesIfNeeded()
98 $res = $this->db->query(
99 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
101 if ($res !== false) {
107 CREATE TABLE gamesticks (
108 id INTEGER PRIMARY KEY AUTOINCREMENT,
111 verificationCode TEXT DEFAULT NULL,
112 gamerTag TEXT DEFAULT NULL,
113 founderFlag INTEGER DEFAULT 0 NOT NULL,
114 founderName TEXT DEFAULT NULL,
115 minAge INTEGER DEFAULT 3 NOT NULL,
116 avatar TEXT DEFAULT NULL,
118 created_at TEXT DEFAULT CURRENT_TIMESTAMP