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

Commit ad8d1be

Browse files
committed
feat(sandbox): create sandbox foundation
1 parent 3137dfa commit ad8d1be

27 files changed

+3360
-48
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/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Sandbox
2+
3+
## Running Locally
4+
5+
```sh
6+
./scripts/run_local.sh
7+
```
8+
9+
Visit http://localhost:8080
10+
11+
## Running Remotely
12+
13+
```sh
14+
./scripts/deploy.sh
15+
./scripts/run_remote.sh
16+
```
17+
18+
Visit http://localhost:8080
19+
20+
## Updating SDK
21+
22+
```sh
23+
./scripts/gen_sdk.ts
24+
```
25+

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+
"internalPort": 7777
42+
}
43+
},
44+
"resources": {
45+
"cpu": 250,
46+
"memory": 250
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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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://sandbox-brq--staging.backend.nathan16.gameinc.io" : "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+
let res;
14+
if (environment == 'local') {
15+
res = await backend.lobbies.findOrCreate({
16+
version: "default",
17+
regions: ["local"],
18+
tags: {},
19+
players: [{}],
20+
21+
createConfig: {
22+
region: "local",
23+
tags: {},
24+
maxPlayers: 8,
25+
maxPlayersDirect: 8,
26+
},
27+
});
28+
} else {
29+
const region = "atl";
30+
const tags = {"foo": "bar"};
31+
res = await backend.lobbies.findOrCreate({
32+
version: "2024.08.14-4",
33+
regions: [region],
34+
tags,
35+
players: [{}],
36+
37+
createConfig: {
38+
region,
39+
tags,
40+
maxPlayers: 8,
41+
maxPlayersDirect: 8,
42+
},
43+
});
44+
}
45+
46+
let { lobby, players } = res;
47+
48+
// Test lobby connection
49+
while (true) {
50+
try {
51+
await connect(lobby, players);
52+
break;
53+
} catch (err) {
54+
console.warn('failed', err);
55+
}
56+
57+
await new Promise((resolve) => setTimeout(resolve, 500));
58+
}
59+
60+
console.log('finished');
61+
}
62+
63+
function connect(lobby, players) {
64+
return new Promise((resolve, reject) => {
65+
let hostname;
66+
let port;
67+
if (lobby.backend.server) {
68+
port = lobby.backend.server.ports["game"];
69+
} else if (lobby.backend.localDevelopment) {
70+
port = lobby.backend.localDevelopment.ports["game"];
71+
} else {
72+
throw new Error("unknown backend");
73+
}
74+
75+
console.log('connecting to', port);
76+
77+
const ws = new WebSocket(`${port.isTls ? "wss" : "ws"}://${port.hostname}:${port.port}?token=${players[0].token}`);
78+
ws.onopen = () => {
79+
console.log('open');
80+
};
81+
ws.onerror = err => {
82+
reject(err)
83+
};
84+
ws.onmessage = ev => {
85+
let [event, data] = JSON.parse(ev.data);
86+
if (event == 'init') {
87+
console.log('init', data)
88+
ws.send(JSON.stringify(["ping", 1]))
89+
} else if (event == 'pong') {
90+
console.log('pong');
91+
ws.close();
92+
resolve();
93+
} else if (event == 'stats') {
94+
// pass
95+
} else {
96+
console.warn('unknown event', event, data)
97+
}
98+
};
99+
});
100+
}

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)