@jfvilas/kwirth-plugin-rallyx 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/NOTICE.md +74 -0
  2. package/README.md +76 -3
  3. package/back.js +231 -0
  4. package/front.js +4395 -0
  5. package/package.json +15 -3
package/NOTICE.md ADDED
@@ -0,0 +1,74 @@
1
+ # NOTICE — Rally-X Plugin
2
+
3
+ ## Upstream Game
4
+
5
+ The Rally-X game is a homebrew recreation of the classic 1980s arcade game
6
+ Rally-X (originally by Midway/Namco), developed by Marco Parenzan.
7
+
8
+ - **Source:** https://github.com/marcoparenzan/rallyx
9
+ - **License:** MIT (see below)
10
+ - **Technology:** Phaser 2.x (HTML5 game framework) + RequireJS (AMD module loader)
11
+
12
+ ### Modifications
13
+
14
+ The game code has been adapted to run inside a Kwirth plugin iframe:
15
+
16
+ 1. **Named AMD defines:** The original modules use anonymous `define([])` calls
17
+ that rely on the script URL for the module name. In the iframe (inline
18
+ scripts), there is no URL, so each module has been given an explicit name:
19
+ `define("car", [])`, `define("flag", [])`, etc.
20
+
21
+ 2. **Cookie → localStorage:** The original high-score persistence uses browser
22
+ cookies. In the iframe `srcdoc` context, cookies are unreliable, so the
23
+ `setCookie`/`getCookie`/`deleteCookie` functions have been replaced with
24
+ `localStorage` equivalents.
25
+
26
+ 3. **postMessage bridge:** A bridge has been added to report game state (score,
27
+ lives, round, fuel) and game-over events to the parent window (the Kwirth
28
+ plugin), so the plugin can display HUD values and persist scores in the
29
+ cluster's ConfigMap.
30
+
31
+ 4. **Asset loading:** Assets are inlined as data URIs. A Phaser loader patch
32
+ redirects the game's `"assets/..."` URLs to these data URIs, so the game
33
+ code itself is unmodified in its asset loading calls.
34
+
35
+ ### Files vendored from upstream
36
+
37
+ - `src/front/rallyx/phaser.txt` — Phaser 2.x library (unmodified)
38
+ - `src/front/rallyx/require.txt` — RequireJS library (unmodified)
39
+ - `src/front/rallyx/js/*.txt` — Game modules (modified as described above)
40
+ - `src/front/rallyx/assets/*` — Game assets (unmodified)
41
+
42
+ ## Phaser
43
+
44
+ - **Source:** https://phaser.io/
45
+ - **License:** MIT
46
+
47
+ ## RequireJS
48
+
49
+ - **Source:** https://requirejs.org/
50
+ - **License:** MIT
51
+
52
+ ## License (upstream game)
53
+
54
+ The MIT License (MIT)
55
+
56
+ Copyright (c) 2017 Marco Parenzan
57
+
58
+ Permission is hereby granted, free of charge, to any person obtaining a copy
59
+ of this software and associated documentation files (the "Software"), to deal
60
+ in the Software without restriction, including without limitation the rights
61
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
62
+ copies of the Software, and to permit persons to whom the Software is
63
+ furnished to do so, subject to the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be included in
66
+ all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
69
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
70
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
71
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
72
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
73
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
74
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,76 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # Rally-X
2
+
3
+ A Rally-X arcade game channel plugin for [Kwirth](https://kwirthmagnify.dev), the Kubernetes
4
+ observability and operations platform ([source](https://github.com/kwirthmagnify/kwirth)).
5
+
6
+ ## What it is
7
+
8
+ This plugin runs a homebrew recreation of the classic 1980s Rally-X arcade game
9
+ (originally by Midway/Namco) inside a Kwirth tab. The game is built with Phaser
10
+ 2.x and runs in an isolated iframe, so its globals never clash with Kwirth's
11
+ React/MUI.
12
+
13
+ The game state (score, lives, round, fuel) is displayed in a HUD bar above the
14
+ game area. High scores are persisted in a Kubernetes ConfigMap, shared across
15
+ all users of the cluster.
16
+
17
+ ## How it works
18
+
19
+ - The game (Phaser + RequireJS + all assets) is bundled into the plugin's
20
+ `front.js` as text and base64. At runtime, it is injected into an iframe via
21
+ `srcdoc` — no network requests needed.
22
+ - The iframe survives tab switches: switching away hides it, switching back
23
+ repositions it. Only stopping the channel destroys it.
24
+ - A `postMessage` bridge reports game state to the plugin for HUD display and
25
+ score persistence.
26
+ - High scores are stored via the back channel's `writeStorage` (ConfigMap
27
+ `kwirth-store-channel-rallyx-scores`).
28
+
29
+ ## Installation
30
+
31
+ Rally-X is published to npm as `@jfvilas/kwirth-plugin-rallyx` and listed in the **jfvilas marketplace**.
32
+ If your Kwirth does not list it yet, add this manifest as a marketplace:
33
+
34
+ ```
35
+ https://raw.githubusercontent.com/jfvilas/kwirth/master/manifest.json
36
+ ```
37
+
38
+ Then install **Rally-X** from the plugin manager and add the channel to a tab. Source code at
39
+ [github.com/jfvilas/kwirth](https://github.com/jfvilas/kwirth) (folder `plugins/rallyx`).
40
+
41
+ ## Controls
42
+
43
+ | Key | Action |
44
+ |-----|--------|
45
+ | ← ↑ → ↓ | Drive |
46
+ | Ctrl | Drop smoke |
47
+ | Space / Ctrl | Start (on title screen) |
48
+
49
+ ## Architecture
50
+
51
+ ```
52
+ src/
53
+ ├── back/index.ts — back channel: score persistence (ConfigMap)
54
+ ├── common/RallyxTypes.ts — shared types
55
+ ├── front/
56
+ │ ├── index.ts — registers RallyxChannel on window.__kwirth_plugins__
57
+ │ ├── RallyxChannel.tsx — IChannel impl (lifecycle, message routing)
58
+ │ ├── RallyxMachine.ts — iframe manager (builds HTML, manages lifecycle)
59
+ │ ├── RallyxTabContent.tsx — tab UI (HUD, game area, scores overlay)
60
+ │ ├── RallyxSetup.tsx — setup dialog (sender notification, pause-on-blur)
61
+ │ ├── RallyxConfig.ts — config classes
62
+ │ ├── RallyxData.ts — channel state (survives tab switches)
63
+ │ ├── RallyxScores.ts — score store (back + local fallback)
64
+ │ ├── icons.tsx — plugin icon
65
+ │ └── rallyx/
66
+ │ ├── phaser.txt — Phaser 2.x library
67
+ │ ├── require.txt — RequireJS library
68
+ │ ├── js/*.txt — game modules (named AMD defines)
69
+ │ └── assets/ — game assets (PNGs, tilemap JSON)
70
+ └── types/assets.d.ts — ambient declarations for non-TS imports
71
+ ```
72
+
73
+ ## License
74
+
75
+ The upstream game is MIT licensed (Copyright (c) 2017 Marco Parenzan).
76
+ See [NOTICE.md](NOTICE.md) for full attribution.
package/back.js ADDED
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function __require() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+ var __export = (target, all) => {
12
+ for (var name in all)
13
+ __defProp(target, name, { get: all[name], enumerable: true });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
31
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+
33
+ // kwirth-back-globals:@kwirthmagnify/kwirth-common-back
34
+ var require_kwirth_common_back = __commonJS({
35
+ "kwirth-back-globals:@kwirthmagnify/kwirth-common-back"(exports2, module2) {
36
+ module2.exports = global.__kwirth_back__.kwirthCommonBack;
37
+ }
38
+ });
39
+
40
+ // src/back/index.ts
41
+ var index_exports = {};
42
+ __export(index_exports, {
43
+ MSG_SCORES: () => MSG_SCORES,
44
+ MSG_SCORES_GET: () => MSG_SCORES_GET,
45
+ MSG_SCORE_SUBMIT: () => MSG_SCORE_SUBMIT,
46
+ default: () => index_default
47
+ });
48
+ module.exports = __toCommonJS(index_exports);
49
+ var import_kwirth_common_back = __toESM(require_kwirth_common_back(), 1);
50
+ var STORAGE_KEY = "rallyx-scores";
51
+ var MAX_SCORES = 10;
52
+ var MAX_NAME = 24;
53
+ var MSG_SCORES_GET = "rallyx-scores-get";
54
+ var MSG_SCORE_SUBMIT = "rallyx-score-submit";
55
+ var MSG_SCORES = "rallyx-scores";
56
+ var sanitize = (raw) => {
57
+ if (!raw || typeof raw !== "object") return void 0;
58
+ const score = Number(raw.score);
59
+ if (!Number.isFinite(score) || score <= 0) return void 0;
60
+ const level = Number(raw.level);
61
+ return {
62
+ name: String(raw.name ?? "anon").replace(/[\u0000-\u001f]/g, "").trim().slice(0, MAX_NAME) || "anon",
63
+ score: Math.floor(score),
64
+ level: Number.isFinite(level) ? Math.floor(level) : 0,
65
+ date: (/* @__PURE__ */ new Date()).toISOString()
66
+ };
67
+ };
68
+ var sortAndTrim = (entries) => [...entries].sort((a, b) => b.score - a.score).slice(0, MAX_SCORES);
69
+ var RallyxBackChannel = class {
70
+ constructor(clusterInfo, backChannelObject) {
71
+ this.channelId = "rallyx";
72
+ this.requirements = { storage: true, providers: [] };
73
+ this.instances = [];
74
+ /**
75
+ * Write queue. Read, insert and write on a ConfigMap is not atomic:
76
+ * two games ending at the same time could overwrite each other.
77
+ */
78
+ this.queue = Promise.resolve();
79
+ this.getChannelData = () => ({
80
+ id: "rallyx",
81
+ routable: false,
82
+ pauseable: true,
83
+ modifiable: false,
84
+ reconnectable: false,
85
+ metrics: false,
86
+ sources: [import_kwirth_common_back.EClusterType.KUBERNETES],
87
+ endpoints: [],
88
+ websocket: false,
89
+ cluster: false,
90
+ resourced: false
91
+ });
92
+ this.getChannelScopeLevel = (scope) => ["", "none"].indexOf(scope);
93
+ this.startChannel = async () => {
94
+ };
95
+ this.endpointRequest = () => {
96
+ };
97
+ this.websocketRequest = () => {
98
+ };
99
+ this.processProviderEvent = () => {
100
+ };
101
+ this.addObject = async (webSocket, instanceConfig) => {
102
+ const data = instanceConfig.data ?? {};
103
+ this.instances.push({
104
+ instanceId: instanceConfig.instance,
105
+ webSocket,
106
+ senderId: data.senderId,
107
+ senderConfigName: data.senderConfigName
108
+ });
109
+ return true;
110
+ };
111
+ this.deleteObject = async () => false;
112
+ this.pauseContinueInstance = () => {
113
+ };
114
+ this.modifyInstance = () => {
115
+ };
116
+ this.containsInstance = (instanceId) => this.instances.some((i) => i.instanceId === instanceId);
117
+ this.getInstances = () => ({
118
+ instances: this.instances.length,
119
+ connections: new Set(this.instances.map((i) => i.webSocket)).size
120
+ });
121
+ this.containsAsset = () => false;
122
+ this.stopInstance = (_webSocket, instanceConfig) => {
123
+ this.instances = this.instances.filter((i) => i.instanceId !== instanceConfig.instance);
124
+ };
125
+ this.removeInstance = (_webSocket, instanceId) => {
126
+ this.instances = this.instances.filter((i) => i.instanceId !== instanceId);
127
+ };
128
+ this.containsConnection = (webSocket) => this.instances.some((i) => i.webSocket === webSocket);
129
+ this.removeConnection = (webSocket) => {
130
+ this.instances = this.instances.filter((i) => i.webSocket !== webSocket);
131
+ };
132
+ this.refreshConnection = () => true;
133
+ this.updateConnection = () => true;
134
+ this.processCommand = async (webSocket, instanceMessage) => {
135
+ const msg = instanceMessage;
136
+ switch (msg.msgtype) {
137
+ case MSG_SCORES_GET:
138
+ this.queue = this.queue.then(async () => {
139
+ const scores = await this.read();
140
+ this.reply(webSocket, instanceMessage, scores);
141
+ });
142
+ return true;
143
+ case MSG_SCORE_SUBMIT:
144
+ this.queue = this.queue.then(async () => {
145
+ const entry = sanitize(msg.entry);
146
+ const current = await this.read();
147
+ const updated = entry ? sortAndTrim([...current, entry]) : current;
148
+ if (entry) {
149
+ try {
150
+ await this.backChannelObject.writeStorage(STORAGE_KEY, false, updated);
151
+ } catch (err) {
152
+ this.backChannelObject.logError?.(`rallyx: could not save the score: ${err}`);
153
+ this.reply(webSocket, instanceMessage, current);
154
+ return;
155
+ }
156
+ this.notifyRecord(instanceMessage.instance, current, updated, entry);
157
+ }
158
+ this.broadcast(instanceMessage, updated);
159
+ });
160
+ return true;
161
+ default:
162
+ return false;
163
+ }
164
+ };
165
+ this.notifyRecord = (instanceId, before, after, entry) => {
166
+ const instance = this.instances.find((i) => i.instanceId === instanceId);
167
+ if (!instance?.senderId || !instance.senderConfigName) return;
168
+ if (after[0] !== entry) return;
169
+ if (before.length > 0 && entry.score <= before[0].score) return;
170
+ const previous = before.length > 0 ? `${before[0].name} (${before[0].score})` : "nobody";
171
+ this.backChannelObject.senders?.send(instance.senderId, instance.senderConfigName, {
172
+ subject: `Rally-X: new record by ${entry.name}`,
173
+ body: `${entry.name} scored ${entry.score} points on round ${entry.level}. The previous record was held by ${previous}.`
174
+ })?.catch?.((err) => {
175
+ this.backChannelObject.logWarning?.(`rallyx: could not notify the record: ${err}`);
176
+ });
177
+ };
178
+ this.read = async () => {
179
+ try {
180
+ const stored = await this.backChannelObject.readStorage(STORAGE_KEY, false);
181
+ if (!Array.isArray(stored)) return [];
182
+ return sortAndTrim(stored.map(sanitize).filter((e) => e !== void 0));
183
+ } catch (err) {
184
+ this.backChannelObject.logWarning?.(`rallyx: unreadable score table, starting empty: ${err}`);
185
+ return [];
186
+ }
187
+ };
188
+ this.reply = (webSocket, source, scores) => {
189
+ const response = {
190
+ msgtype: MSG_SCORES,
191
+ action: import_kwirth_common_back.EInstanceMessageAction.COMMAND,
192
+ flow: import_kwirth_common_back.EInstanceMessageFlow.RESPONSE,
193
+ type: import_kwirth_common_back.EInstanceMessageType.DATA,
194
+ channel: "rallyx",
195
+ instance: source.instance,
196
+ scores
197
+ };
198
+ try {
199
+ webSocket.send(JSON.stringify(response));
200
+ } catch {
201
+ }
202
+ };
203
+ this.broadcast = (source, scores) => {
204
+ for (const instance of this.instances) {
205
+ const response = {
206
+ msgtype: MSG_SCORES,
207
+ action: import_kwirth_common_back.EInstanceMessageAction.COMMAND,
208
+ flow: import_kwirth_common_back.EInstanceMessageFlow.RESPONSE,
209
+ type: import_kwirth_common_back.EInstanceMessageType.DATA,
210
+ channel: "rallyx",
211
+ instance: instance.instanceId,
212
+ scores
213
+ };
214
+ try {
215
+ instance.webSocket.send(JSON.stringify(response));
216
+ } catch {
217
+ }
218
+ }
219
+ void source;
220
+ };
221
+ this.clusterInfo = clusterInfo;
222
+ this.backChannelObject = backChannelObject;
223
+ }
224
+ };
225
+ var index_default = RallyxBackChannel;
226
+ // Annotate the CommonJS export names for ESM import in node:
227
+ 0 && (module.exports = {
228
+ MSG_SCORES,
229
+ MSG_SCORES_GET,
230
+ MSG_SCORE_SUBMIT
231
+ });