Send "304 Not Modified" responses to the gamestick if nothing changed v0.6.1
authorChristian Weiske <cweiske@cweiske.de>
Sun, 6 Apr 2025 10:19:07 +0000 (12:19 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sun, 6 Apr 2025 10:19:07 +0000 (12:19 +0200)
We use the session ID to track the client state, and change it
whenever the server state changes

src/GameInfo.php
src/Gamestick.php
src/Paths.php [new file with mode: 0644]
test-curl-commands.txt
www/api/rest/connect.php

index a0f7e251f6fdcbaf4d33b1095285c9dc28ef290b..5d67896600a2d5b27098bf667167b1de276763d0 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+require_once 'Paths.php';
 
 class GameInfo
 {
@@ -8,9 +9,8 @@ class GameInfo
      */
     public function getIdFromUuid(string $gameuuid): ?int
     {
-        $cacheDir = __DIR__ . '/../cache/';
-        $connectAppsCacheFile = $cacheDir . 'connect-apps.min.json';
-        $data = json_decode(file_get_contents($connectAppsCacheFile));
+        $paths = new Paths();
+        $data = json_decode(file_get_contents($paths->apps()));
         foreach ($data as $game) {
             if ($game->version == $gameuuid) {
                 return $game->id;
index fe09d8d1be4a0dbced0ae913d5a1ecf2efc47fb6..cd8696642f84abfdbcd7942840c9dab1ca15f8fc 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+require_once 'Paths.php';
 require_once 'User.php';
 
 /**
@@ -15,6 +16,24 @@ class Gamestick
     public string $created_at;
     public string $updated_at;
 
+    public function calculateSessionId(): string
+    {
+        $paths = new Paths();
+        $data = [
+            $this->id,
+            $this->hwId,
+            $this->verificationCode,
+            $this->created_at,
+            $this->getUser()->updated_at,
+            filemtime($paths->apps()),
+            filemtime($paths->featuredAges()),
+            filemtime($paths->uiTranslation()),
+            $paths->pjgsapiVersion(),
+        ];
+
+        return strtoupper(md5(implode(':', $data)));
+    }
+
     public function getUser(): User
     {
         $userRepository = new UserRepository();
diff --git a/src/Paths.php b/src/Paths.php
new file mode 100644 (file)
index 0000000..bc18a10
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+class Paths
+{
+    protected $cacheDir;
+
+    public function __construct()
+    {
+        $this->cacheDir = dirname(__FILE__, 2) . '/cache/';
+    }
+
+    public function apps(): string
+    {
+        return $this->cacheDir . 'connect-apps.min.json';
+    }
+
+    public function featuredAges(): string
+    {
+        return $this->cacheDir . 'connect-featured-ages.min.json';
+    }
+
+    public function uiTranslation(): string
+    {
+        return $this->cacheDir . 'connect-uitranslation.min.json';
+    }
+
+    public function pjgsapiVersion(): string
+    {
+        $hash = exec('git -c safe.directory="*" rev-parse HEAD', $output, $exitCode);
+        if ($exitCode !== 0) {
+            return date('Y-m-d');//change at least once a day
+        }
+
+        return trim($hash);
+    }
+}
index 750dd5c16f7c2a617e297e30ae34516788f3b36f..812ae699b54a4ac2a7676f08227a712aa1494b88 100644 (file)
@@ -1,6 +1,8 @@
 Gamestick info
 curl -i gamestick.cwboo/api/rest/connect/stick/stick/ac:db:da:09:18:5c/view.json
 
+curl -Is --cookie JSESSIONID=AD303D903A507D8E728DAAE99FB85726 gamestick.cwboo/api/rest/connect/stick/stick/ac:db:da:09:18:5c/view.json
+curl -Is 'gamestick.cwboo/api/rest/connect/stick/stick/ac:db:da:09:18:5c/view.json;jsessionid=AD303D903A507D8E728DAAE99FB85726'
 
 Firmware update check
 curl -i gamestick.cwboo/check.php --data v='{"hwid":"ac:db:da:09:18:5c","major":0,"minor":0,"revision":53,"platform":0}'
index 5e5c7e68b1672cc338a65065cab365a9e94bc521..625cbaa41634635699522b8bb29db5dde05a33ff 100644 (file)
@@ -57,6 +57,18 @@ if ($gamestick === null || !$gamestick->getUser()->complete()) {
 }
 
 
+$calculatedSessionId = $gamestick->calculateSessionId();
+if ($sessionId === $calculatedSessionId) {
+    header('HTTP/1.0 304 Not Modified');
+    exit();
+}
+
+if ($gamestick->sessionId !== $calculatedSessionId) {
+    $gamestick->sessionId = $calculatedSessionId;
+    $gamestick->update(['sessionId' => $calculatedSessionId]);
+}
+
+
 $data = [
     'sid'             => $gamestick->sessionId,
     'time'            => (string) $nowMilli,
@@ -90,10 +102,12 @@ $data = [
 $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
 
 
+require_once $rootDir . '/src/Paths.php';
+$paths = new Paths();
 $placeholderFiles = [
-    'PLACEHOLDER_APPS'          => $cacheDir . 'connect-apps.min.json',
-    'PLACEHOLDER_AGES'          => $cacheDir . 'connect-featured-ages.min.json',
-    'PLACEHOLDER_UITRANSLATION' => $cacheDir . 'connect-uitranslation.min.json',
+    'PLACEHOLDER_APPS'          => $paths->apps(),
+    'PLACEHOLDER_AGES'          => $paths->featuredAges(),
+    'PLACEHOLDER_UITRANSLATION' => $paths->uiTranslation(),
 ];
 foreach ($placeholderFiles as $placeholder => $cacheFile) {
     //inject apps