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 getProfileByVerificationCode(string $code): ?Profile
45 $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE verificationCode = :code');
46 $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile');
47 $stmt->execute([':code' => $code]);
48 $row = $stmt->fetch();
49 return $row === false ? null : $row;
52 public function createProfile(string $hwId): Profile
54 $stmt = $this->db->prepare(
56 INSERT INTO gamesticks
57 (hwId, sessionId, verificationCode)
58 VALUES (:hwId, :sessionId, :verificationCode)
64 ':sessionId' => 's' . str_replace(':', '', $hwId),
65 ':verificationCode' => date('His'),
68 return $this->getProfileByHardwareId($hwId);
71 public function updateProfile(string $hwId, array $values): ?Profile
77 $sql = 'UPDATE gamesticks SET';
79 foreach ($values as $column => $value) {
80 $sqlParts[] = ' ' . $column . '= :' . $column;
81 $params[':' . $column] = $value;
83 $sql .= implode(', ', $sqlParts) . ' WHERE hwId = :hwId';
85 $stmt = $this->db->prepare($sql);
86 $stmt->execute($params);
88 return $this->getProfileByHardwareId($hwId);
91 protected function createTablesIfNeeded()
93 $res = $this->db->query(
94 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
102 CREATE TABLE gamesticks (
103 id INTEGER PRIMARY KEY AUTOINCREMENT,
106 verificationCode TEXT DEFAULT NULL,
107 gamerTag TEXT DEFAULT NULL,
108 founderFlag INTEGER DEFAULT 0 NOT NULL,
109 founderName TEXT DEFAULT NULL,
110 minAge INTEGER DEFAULT 3 NOT NULL,
111 avatar TEXT DEFAULT NULL,
113 created_at TEXT DEFAULT CURRENT_TIMESTAMP