Skip to content

Commit

Permalink
feat(sandbox): create sandbox foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Aug 8, 2024
1 parent bca968f commit aebbb0b
Show file tree
Hide file tree
Showing 26 changed files with 3,327 additions and 48 deletions.
1 change: 1 addition & 0 deletions sandbox/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions sandbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdk/
1 change: 1 addition & 0 deletions sandbox/.rivet/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"game_server": {"deploy": {"dockerfile_path": "game_server.Dockerfile"}}}
21 changes: 21 additions & 0 deletions sandbox/backend.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "./backend.json",
"modules": {
"lobbies": {
"registry": "local",
"config": {
"lobbies": {
"regions": ["local"],
"backend": {
"localDevelopment": {
"tags": {},
"ports": {
"game": { "protocol": "http", "port": 7777 }
}
}
}
}
}
}
}
}
61 changes: 61 additions & 0 deletions sandbox/backend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"runtime": {
"cors": {
"origins": [
"http://localhost:8080"
]
}
},
"registries": {
"local": {
"local": {
"directory": "../modules"
}
}
},
"modules": {
"rate_limit": {
"registry": "local"
},
"tokens": {
"registry": "local"
},
"lobbies": {
"registry": "local",
"config": {
"lobbies": {
"regions": [
"atl"
],
"backend": {
"server": {
"environment": {
"SERVER_HOSTNAME": "0.0.0.0"
},
"tags": {

},
"ports": {
"game": {
"protocol": "http",
"internalPort": 7777
}
},
"resources": {
"cpu": 250,
"memory": 250
}
}
}
}
}
},
"rivet": {
"registry": "local",
"config": {
"apiEndpoint": "https://api.nathan16.gameinc.io",
"serviceTokenVariable": "RIVET_SERVICE_TOKEN"
}
}
}
}
40 changes: 40 additions & 0 deletions sandbox/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenGB E2E Test</title>
</head>
<body>
<h1>OpenGB E2E Test</h1>

<div>
<label for="environmentToggle">Environment:</label>
<select id="environmentToggle" onchange="updateEnvironment()">
<option value="local">Local</option>
<option value="remote">Remote</option>
</select>
</div>

<script type="module" src="./index.js"></script>

<button onclick="findOrCreateLobby()">Find Or Create Lobby</button>

<script>
window.addEventListener('load', function() {
const urlParams = new URLSearchParams(window.location.search);
const environment = urlParams.get('env');
if (environment === 'local' || environment === 'remote') {
document.getElementById('environmentToggle').value = environment;
}
});

function updateEnvironment() {
const environment = document.getElementById('environmentToggle').value;
const url = new URL(window.location);
url.searchParams.set('env', environment);
window.location.href = url.toString();
}
</script>
</body>
</html>
100 changes: 100 additions & 0 deletions sandbox/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/// <reference path="./dist/sdk.d.mts" />
import { Backend } from './dist/sdk.mjs';

const urlParams = new URLSearchParams(window.location.search);
const environment = urlParams.get('env') || 'local';
const API_ENDPOINT = environment === 'remote' ? "https://sandbox-back-vlk--staging.backend.nathan16.gameinc.io" : "http://localhost:6420";

const backend = new Backend({ endpoint: API_ENDPOINT });

console.log('backend', backend);

window.findOrCreateLobby = async function() {
let res;
if (environment == 'local') {
res = await backend.lobbies.findOrCreate({
version: "default",
regions: ["local"],
tags: {},
players: [{}],

createConfig: {
region: "local",
tags: {},
maxPlayers: 8,
maxPlayersDirect: 8,
},
});
} else {
const region = "atl";
const tags = {"foo": "bar"};
res = await backend.lobbies.findOrCreate({
version: "95cbad73-dcfd-4a74-96de-799b8ddc1b72",
regions: [region],
tags,
players: [{}],

createConfig: {
region,
tags,
maxPlayers: 8,
maxPlayersDirect: 8,
},
});
}

let { lobby, players } = res;

// Test lobby connection
while (true) {
try {
await connect(lobby, players);
break;
} catch (err) {
console.warn('failed', err);
}

await new Promise((resolve) => setTimeout(resolve, 500));
}

console.log('finished');
}

function connect(lobby, players) {
return new Promise((resolve, reject) => {
let hostname;
let port;
if (lobby.backend.server) {
port = lobby.backend.server.ports["game"];
} else if (lobby.backend.localDevelopment) {
port = lobby.backend.localDevelopment.ports["game"];
} else {
throw new Error("unknown backend");
}

console.log('connecting to', port);

const ws = new WebSocket(`${port.isTls ? "wss" : "ws"}://${port.hostname}:${port.port}?token=${players[0].token}`);
ws.onopen = () => {
console.log('open');
};
ws.onerror = err => {
reject(err)
};
ws.onmessage = ev => {
let [event, data] = JSON.parse(ev.data);
if (event == 'init') {
console.log('init', data)
ws.send(JSON.stringify(["ping", 1]))
} else if (event == 'pong') {
console.log('pong');
ws.close();
resolve();
} else if (event == 'stats') {
// pass
} else {
console.warn('unknown event', event, data)
}
};
});
}
20 changes: 20 additions & 0 deletions sandbox/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"lint": {
"include": [
"src/"
],
"exclude": [
"tests/"
],
"rules": {
"exclude": [
"no-empty-interface",
"no-explicit-any",
"require-await"
]
}
},
"fmt": {
"useTabs": true
}
}
Loading

0 comments on commit aebbb0b

Please sign in to comment.