Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}}}
25 changes: 25 additions & 0 deletions sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Sandbox

## Running Locally

```sh
./scripts/run_local.sh
```

Visit http://localhost:8080

## Running Remotely

```sh
./scripts/deploy.sh
./scripts/run_remote.sh
```

Visit http://localhost:8080

## Updating SDK

```sh
./scripts/gen_sdk.ts
```

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"
}
}
}
}
42 changes: 42 additions & 0 deletions sandbox/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!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>
<button onclick="fetchState()">Fetch State</button>
<button onclick="resetState()">Reset State</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>
115 changes: 115 additions & 0 deletions sandbox/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/// <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-brq--staging.backend.nathan16.gameinc.io" : "http://localhost:6420";
const API_ENDPOINT = environment === 'remote' ? "https://unity-demo-c8y.backend.nathan16.gameinc.io" : "http://localhost:6420";

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

console.log('backend', backend);

window.fetchState = async function() {
const { state } = await backend.lobbies.fetchLobbyManagerState({});
console.log('State', state);
};

window.resetState = async function() {
await backend.lobbies.resetLobbyManagerState({});
};

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: "2024.08.14-4",
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 protocol;
let hostname;
let port;
if (lobby.backend.server) {
protocol = lobby.backend.server.ports["game"].protocol;
hostname = lobby.backend.server.ports["game"].publicHostname;
port = lobby.backend.server.ports["game"].publicPort;
} else if (lobby.backend.localDevelopment) {
protocol = "http";
hostname = lobby.backend.localDevelopment.ports["game"].hostname;
port = lobby.backend.localDevelopment.ports["game"].port;
} else {
throw new Error("unknown backend");
}

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

const ws = new WebSocket(`${protocol}://${hostname}:${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