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

Commit e2012e2

Browse files
committed
feat(sandbox): create sandbox foundation
1 parent d8e909c commit e2012e2

27 files changed

+3377
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
<button onclick="fetchState()">Fetch State</button>
23+
<button onclick="resetState()">Reset State</button>
24+
25+
<script>
26+
window.addEventListener('load', function() {
27+
const urlParams = new URLSearchParams(window.location.search);
28+
const environment = urlParams.get('env');
29+
if (environment === 'local' || environment === 'remote') {
30+
document.getElementById('environmentToggle').value = environment;
31+
}
32+
});
33+
34+
function updateEnvironment() {
35+
const environment = document.getElementById('environmentToggle').value;
36+
const url = new URL(window.location);
37+
url.searchParams.set('env', environment);
38+
window.location.href = url.toString();
39+
}
40+
</script>
41+
</body>
42+
</html>

sandbox/client/index.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
const API_ENDPOINT = environment === 'remote' ? "https://unity-demo-c8y.backend.nathan16.gameinc.io" : "http://localhost:6420";
8+
9+
const backend = new Backend({ endpoint: API_ENDPOINT });
10+
11+
console.log('backend', backend);
12+
13+
window.fetchState = async function() {
14+
const { state } = await backend.lobbies.fetchLobbyManagerState({});
15+
console.log('State', state);
16+
};
17+
18+
window.resetState = async function() {
19+
await backend.lobbies.resetLobbyManagerState({});
20+
};
21+
22+
window.findOrCreateLobby = async function() {
23+
let res;
24+
if (environment == 'local') {
25+
res = await backend.lobbies.findOrCreate({
26+
version: "default",
27+
regions: ["local"],
28+
tags: {},
29+
players: [{}],
30+
31+
createConfig: {
32+
region: "local",
33+
tags: {},
34+
maxPlayers: 8,
35+
maxPlayersDirect: 8,
36+
},
37+
});
38+
} else {
39+
const region = "atl";
40+
const tags = {"foo": "bar"};
41+
res = await backend.lobbies.findOrCreate({
42+
version: "2024.08.14-4",
43+
regions: [region],
44+
tags,
45+
players: [{}],
46+
47+
createConfig: {
48+
region,
49+
tags,
50+
maxPlayers: 8,
51+
maxPlayersDirect: 8,
52+
},
53+
});
54+
}
55+
56+
let { lobby, players } = res;
57+
58+
// Test lobby connection
59+
while (true) {
60+
try {
61+
await connect(lobby, players);
62+
break;
63+
} catch (err) {
64+
console.warn('failed', err);
65+
}
66+
67+
await new Promise((resolve) => setTimeout(resolve, 500));
68+
}
69+
70+
console.log('finished');
71+
}
72+
73+
function connect(lobby, players) {
74+
return new Promise((resolve, reject) => {
75+
let protocol;
76+
let hostname;
77+
let port;
78+
if (lobby.backend.server) {
79+
protocol = lobby.backend.server.ports["game"].protocol;
80+
hostname = lobby.backend.server.ports["game"].publicHostname;
81+
port = lobby.backend.server.ports["game"].publicPort;
82+
} else if (lobby.backend.localDevelopment) {
83+
protocol = "http";
84+
hostname = lobby.backend.localDevelopment.ports["game"].hostname;
85+
port = lobby.backend.localDevelopment.ports["game"].port;
86+
} else {
87+
throw new Error("unknown backend");
88+
}
89+
90+
console.log('connecting to', port);
91+
92+
const ws = new WebSocket(`${protocol}://${hostname}:${port}?token=${players[0].token}`);
93+
ws.onopen = () => {
94+
console.log('open');
95+
};
96+
ws.onerror = err => {
97+
reject(err)
98+
};
99+
ws.onmessage = ev => {
100+
let [event, data] = JSON.parse(ev.data);
101+
if (event == 'init') {
102+
console.log('init', data)
103+
ws.send(JSON.stringify(["ping", 1]))
104+
} else if (event == 'pong') {
105+
console.log('pong');
106+
ws.close();
107+
resolve();
108+
} else if (event == 'stats') {
109+
// pass
110+
} else {
111+
console.warn('unknown event', event, data)
112+
}
113+
};
114+
});
115+
}

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)