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, created_at, updated_at)
63 VALUES (:hwId, :sessionId, :verificationCode, :created_at, :updated_at)
69 ':sessionId' => 's' . str_replace(':', '', $hwId),
70 ':verificationCode' => date('His'),
71 ':created_at' => date('c'),
72 ':updated_at' => date('c'),
75 return $this->getProfileByHardwareId($hwId);
78 public function updateProfile(string $hwId, array $values): ?Profile
82 ':updated_at' => date('c'),
85 $sql = 'UPDATE gamesticks SET';
87 ' updated_at = :updated_at'
89 foreach ($values as $column => $value) {
90 $sqlParts[] = ' ' . $column . '= :' . $column;
91 $params[':' . $column] = $value;
93 $sql .= implode(', ', $sqlParts) . ' WHERE hwId = :hwId';
95 $stmt = $this->db->prepare($sql);
96 $stmt->execute($params);
98 return $this->getProfileByHardwareId($hwId);
101 protected function createTablesIfNeeded()
103 $res = $this->db->query(
104 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
106 if ($res !== false) {
112 CREATE TABLE gamesticks (
113 id INTEGER PRIMARY KEY AUTOINCREMENT,
116 verificationCode TEXT DEFAULT NULL,
117 gamerTag TEXT DEFAULT NULL,
118 founderFlag INTEGER DEFAULT 0 NOT NULL,
119 founderName TEXT DEFAULT NULL,
120 minAge INTEGER DEFAULT 3 NOT NULL,
121 avatar TEXT DEFAULT NULL,
123 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
124 updated_at TEXT DEFAULT CURRENT_TIMESTAMP