Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 8b38be1

Browse files
committed
feat(sandbox): create sandbox foundation
1 parent 84c8ad8 commit 8b38be1

24 files changed

+3282
-44
lines changed

sandbox/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

sandbox/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sdk/

sandbox/.rivet/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"game_server": {"deploy": {"dockerfile_path": "game_server.Dockerfile"}}}

sandbox/backend.dev.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"extends": "./backend.json",
3+
"modules": {
4+
"lobbies": {
5+
"registry": "local",
6+
"config": {
7+
"lobbies": {
8+
"regions": ["local"],
9+
"backend": {
10+
"localDevelopment": {
11+
"tags": {},
12+
"ports": {
13+
"game": { "protocol": "http", "port": 7777 }
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}

sandbox/backend.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"runtime": {
3+
"cors": {
4+
"origins": [
5+
"http://localhost:8080"
6+
]
7+
}
8+
},
9+
"registries": {
10+
"local": {
11+
"local": {
12+
"directory": "../modules"
13+
}
14+
}
15+
},
16+
"modules": {
17+
"rate_limit": {
18+
"registry": "local"
19+
},
20+
"tokens": {
21+
"registry": "local"
22+
},
23+
"lobbies": {
24+
"registry": "local",
25+
"config": {
26+
"lobbies": {
27+
"regions": [
28+
"atl"
29+
],
30+
"backend": {
31+
"server": {
32+
"environment": {
33+
"SERVER_HOSTNAME": "0.0.0.0"
34+
},
35+
"tags": {
36+
37+
},
38+
"ports": {
39+
"game": {
40+
"protocol": "http",
41+
"serverPort": 7777
42+
}
43+
},
44+
"resources": {
45+
"cpu": 1000,
46+
"memory": 1000
47+
}
48+
}
49+
}
50+
}
51+
}
52+
},
53+
"rivet": {
54+
"registry": "local",
55+
"config": {
56+
"apiEndpoint": "https://api.nathan16.gameinc.io",
57+
"serviceTokenVariable": "RIVET_SERVICE_TOKEN"
58+
}
59+
}
60+
}
61+
}

sandbox/client/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>OpenGB E2E Test</title>
7+
</head>
8+
<body>
9+
<h1>OpenGB E2E Test</h1>
10+
11+
<div>
12+
<label for="environmentToggle">Environment:</label>
13+
<select id="environmentToggle" onchange="updateEnvironment()">
14+
<option value="local">Local</option>
15+
<option value="remote">Remote</option>
16+
</select>
17+
</div>
18+
19+
<script type="module" src="./index.js"></script>
20+
21+
<button onclick="findOrCreateLobby()">Find Or Create Lobby</button>
22+
23+
<script>
24+
window.addEventListener('load', function() {
25+
const urlParams = new URLSearchParams(window.location.search);
26+
const environment = urlParams.get('env');
27+
if (environment === 'local' || environment === 'remote') {
28+
document.getElementById('environmentToggle').value = environment;
29+
}
30+
});
31+
32+
function updateEnvironment() {
33+
const environment = document.getElementById('environmentToggle').value;
34+
const url = new URL(window.location);
35+
url.searchParams.set('env', environment);
36+
window.location.href = url.toString();
37+
}
38+
</script>
39+
</body>
40+
</html>

sandbox/client/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/// <reference path="./dist/sdk.d.mts" />
2+
import { Backend } from './dist/sdk.mjs';
3+
4+
const urlParams = new URLSearchParams(window.location.search);
5+
const environment = urlParams.get('env') || 'local';
6+
const API_ENDPOINT = environment === 'remote' ? "https://TODO" : "http://localhost:6420";
7+
8+
const backend = new Backend({ endpoint: API_ENDPOINT });
9+
10+
console.log('backend', backend);
11+
12+
window.findOrCreateLobby = async function() {
13+
const { lobby, players } = await backend.lobbies.findOrCreate({
14+
version: "default",
15+
regions: ["local"],
16+
tags: {},
17+
players: [{}],
18+
19+
createConfig: {
20+
region: "local",
21+
tags: {},
22+
maxPlayers: 8,
23+
maxPlayersDirect: 8,
24+
},
25+
});
26+
27+
// Test lobby connection
28+
while (true) {
29+
try {
30+
await connect(lobby, players);
31+
break;
32+
} catch (err) {
33+
console.warn('failed', err);
34+
}
35+
36+
await new Promise((resolve) => setTimeout(resolve, 500));
37+
}
38+
39+
console.log('finished');
40+
}
41+
42+
function connect(lobby, players) {
43+
return new Promise((resolve, reject) => {
44+
let hostname;
45+
let port;
46+
if (lobby.backend.server) {
47+
port = lobby.backend.server.ports["game"];
48+
} else if (lobby.backend.localDevelopment) {
49+
port = lobby.backend.localDevelopment.ports["game"];
50+
} else {
51+
throw new Error("unknown backend");
52+
}
53+
54+
console.log('connecting to', port);
55+
56+
const ws = new WebSocket(`${port.isTls ? "wss" : "ws"}://${port.hostname}:${port.port}?token=${players[0].token}`);
57+
ws.onopen = () => {
58+
console.log('open');
59+
};
60+
ws.onerror = err => {
61+
reject(err)
62+
};
63+
ws.onmessage = ev => {
64+
let [event, data] = JSON.parse(ev.data);
65+
if (event == 'init') {
66+
console.log('init', data)
67+
ws.send(JSON.stringify(["ping", 1]))
68+
} else if (event == 'pong') {
69+
console.log('pong');
70+
ws.close();
71+
resolve();
72+
} else if (event == 'stats') {
73+
// pass
74+
} else {
75+
console.warn('unknown event', event, data)
76+
}
77+
};
78+
});
79+
}

sandbox/deno.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lint": {
3+
"include": [
4+
"src/"
5+
],
6+
"exclude": [
7+
"tests/"
8+
],
9+
"rules": {
10+
"exclude": [
11+
"no-empty-interface",
12+
"no-explicit-any",
13+
"require-await"
14+
]
15+
}
16+
},
17+
"fmt": {
18+
"useTabs": true
19+
}
20+
}

0 commit comments

Comments
 (0)