Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

Report How It Ran: fills in how long you played, from Soju's play log (#199)

Mikekd29ac6a16ba7
diff --git a/scripts/report.py b/scripts/report.py
index 5d3beb43b51..5563bcef7a9 100755
--- a/scripts/report.py
+++ b/scripts/report.py
@@ -4,7 +4,8 @@
soju report [<library id> | <game title>]
The page opens in the browser; nothing is sent until you press Submit there.
-Fills: game, store, Mac model / chip / memory, macOS, Soju version, engine.
+Fills: game, store, Mac model / chip / memory, macOS, Soju version, engine, and how long
+you have played it through Soju (play-log.jsonl).
Reports feed the game pages at didimlabs.com/mac/.
"""
import json
@@ -53,6 +54,26 @@ def engine():
return "; ".join(parts) or "unknown"
+def played(game_id):
+ """Total time played through Soju, from play-log.jsonl: '' when there is none."""
+ minutes = 0
+ try:
+ for line in (BASE / "play-log.jsonl").read_text(errors="replace").splitlines():
+ try:
+ entry = json.loads(line)
+ except ValueError:
+ continue
+ if entry.get("game") == game_id:
+ minutes += int(entry.get("minutes") or 0)
+ except OSError:
+ return ""
+ if minutes <= 0:
+ return ""
+ if minutes < 60:
+ return "about %d minutes (Soju's play log)" % minutes
+ return "about %.1f hours (Soju's play log)" % (minutes / 60)
+
+
def find(arg):
"""A library id (battlenet:osi) or a title gives the game and its store."""
if not arg:
@@ -70,12 +91,14 @@ def find(arg):
def main():
- title, store = find(" ".join(sys.argv[1:]).strip())
+ arg = " ".join(sys.argv[1:]).strip()
+ title, store = find(arg)
fields = {"template": "compatibility.yml", "title": "[Compatibility]: " + title, "game": title,
"mac": mac(), "macos": platform.mac_ver()[0], "engine": engine(),
"soju": (ROOT / "VERSION").read_text().strip() if (ROOT / "VERSION").exists() else "unknown"}
if store:
fields["store"] = store
+ fields["played"] = played(arg)
url = NEW + "?" + urllib.parse.urlencode({k: v for k, v in fields.items() if v})
if os.environ.get("SOJU_REPORT_PRINT") == "1":
print(url)
diff --git a/tests/test_report_link.py b/tests/test_report_link.py
new file mode 100644
index 00000000000..35fcd11b1e5
--- /dev/null
+++ b/tests/test_report_link.py
@@ -0,0 +1,30 @@
+import importlib.util
+import json
+from pathlib import Path
+import tempfile
+import unittest
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[1]
+spec = importlib.util.spec_from_file_location('report', ROOT / 'scripts/report.py')
+report = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(report)
+
+
+class PlayedTests(unittest.TestCase):
+ def test_play_time_comes_from_the_play_log(self):
+ with tempfile.TemporaryDirectory() as d:
+ log = Path(d) / 'play-log.jsonl'
+ log.write_text('\n'.join(json.dumps(e) for e in [
+ {'game': 'steam:1', 'minutes': 50}, {'game': 'steam:2', 'minutes': 7},
+ {'game': 'steam:1', 'minutes': 40}]) + '\nnot json\n')
+ with mock.patch.object(report, 'BASE', Path(d)):
+ self.assertEqual(report.played('steam:1'), "about 1.5 hours (Soju's play log)")
+ self.assertEqual(report.played('steam:2'), "about 7 minutes (Soju's play log)")
+ self.assertEqual(report.played('steam:3'), '')
+ with mock.patch.object(report, 'BASE', Path(d) / 'missing'):
+ self.assertEqual(report.played('steam:1'), '')
+
+
+if __name__ == '__main__':
+ unittest.main()