added deployment

This commit is contained in:
evanpelle
2024-08-13 20:07:20 -07:00
parent efd5f65787
commit 001722bd59
11 changed files with 90 additions and 70 deletions
+2 -2
View File
@@ -13,11 +13,11 @@ RUN npm install
# Copy the rest of the application code # Copy the rest of the application code
COPY . . COPY . .
# Build the application # Build the client-side application
RUN npm run build-prod RUN npm run build-prod
# Expose the port the app runs on # Expose the port the app runs on
EXPOSE 3000 EXPOSE 3000
# Define the command to run the app # Define the command to run the app
CMD [ "node", "dist/server/server.js" ] CMD ["npm", "run", "start:server"]
+1 -1
View File
@@ -15,10 +15,10 @@
* improve front page DONE 8/12/2024 * improve front page DONE 8/12/2024
* attacks cancel out DONE 8/13/2024 * attacks cancel out DONE 8/13/2024
* upload and start server * upload and start server
* fix enemy islands when attacking
* better algorithm for name render placement * better algorithm for name render placement
* make boats larger * make boats larger
* have boats not get close to shore * have boats not get close to shore
* make coasts look better * make coasts look better
* add shader to dim border * add shader to dim border
* remove player.info() * remove player.info()
* fix enemy islands when attacking
+8
View File
@@ -9,6 +9,7 @@
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/jimp": "^0.2.28", "@types/jimp": "^0.2.28",
"colord": "^2.9.3", "colord": "^2.9.3",
"crypto": "^1.0.1",
"express": "^4.19.2", "express": "^4.19.2",
"jimp": "^0.22.12", "jimp": "^0.22.12",
"node-addon-api": "^8.1.0", "node-addon-api": "^8.1.0",
@@ -5170,6 +5171,13 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/crypto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
"integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
"deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.",
"license": "ISC"
},
"node_modules/css-select": { "node_modules/css-select": {
"version": "4.3.0", "version": "4.3.0",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz", "resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz",
+1
View File
@@ -42,6 +42,7 @@
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/jimp": "^0.2.28", "@types/jimp": "^0.2.28",
"colord": "^2.9.3", "colord": "^2.9.3",
"crypto": "^1.0.1",
"express": "^4.19.2", "express": "^4.19.2",
"jimp": "^0.22.12", "jimp": "^0.22.12",
"node-addon-api": "^8.1.0", "node-addon-api": "^8.1.0",
+4 -2
View File
@@ -1,8 +1,8 @@
import {defaultConfig} from "../core/configuration/DefaultConfig"; import {defaultConfig} from "../core/configuration/DefaultConfig";
import {TerrainMap} from "../core/Game"; import {TerrainMap} from "../core/Game";
import {PseudoRandom} from "../core/PseudoRandom";
import {ServerMessage, ServerMessageSchema} from "../core/Schemas"; import {ServerMessage, ServerMessageSchema} from "../core/Schemas";
import {loadTerrainMap} from "../core/TerrainMapLoader"; import {loadTerrainMap} from "../core/TerrainMapLoader";
import {generateUniqueID} from "../core/Util";
import {ClientGame, createClientGame} from "./ClientGame"; import {ClientGame, createClientGame} from "./ClientGame";
import {v4 as uuidv4} from 'uuid'; import {v4 as uuidv4} from 'uuid';
@@ -18,6 +18,8 @@ class Client {
private lobbiesContainer: HTMLElement | null; private lobbiesContainer: HTMLElement | null;
private lobbiesInterval: NodeJS.Timeout | null = null; private lobbiesInterval: NodeJS.Timeout | null = null;
private random = new PseudoRandom(1234)
constructor() { constructor() {
this.lobbiesContainer = document.getElementById('lobbies-container'); this.lobbiesContainer = document.getElementById('lobbies-container');
@@ -84,7 +86,7 @@ class Client {
if (this.game != null) { if (this.game != null) {
return return
} }
this.game = createClientGame(getUsername(), generateUniqueID(), lobbyID, defaultConfig, map) this.game = createClientGame(getUsername(), this.random.nextID(), lobbyID, defaultConfig, map)
this.game.joinLobby() this.game.joinLobby()
}) })
} }
+2 -1
View File
@@ -60,7 +60,8 @@ export class ClientGame {
) { } ) { }
public joinLobby() { public joinLobby() {
this.socket = new WebSocket(`ws://localhost:3000`) const wsHost = process.env.WEBSOCKET_URL || window.location.host;
this.socket = new WebSocket(`ws://${wsHost}`)
this.socket.onopen = () => { this.socket.onopen = () => {
console.log('Connected to game server!'); console.log('Connected to game server!');
this.socket.send( this.socket.send(
+4
View File
@@ -30,4 +30,8 @@ export class PseudoRandom {
nextFloat(min: number, max: number): number { nextFloat(min: number, max: number): number {
return this.next() * (max - min) + min; return this.next() * (max - min) + min;
} }
nextID(): string {
return this.nextInt(0, 1000000).toString(36).padStart(5, '0');
}
} }
-6
View File
@@ -1,11 +1,5 @@
import {Cell} from "./Game"; import {Cell} from "./Game";
export function generateUniqueID(): string {
const array = new Uint8Array(16);
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
}
export function manhattanDist(c1: Cell, c2: Cell): number { export function manhattanDist(c1: Cell, c2: Cell): number {
return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y); return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
} }
+5 -3
View File
@@ -3,8 +3,8 @@ import {Client} from "./Client";
import {Lobby} from "./Lobby"; import {Lobby} from "./Lobby";
import {GameServer} from "./GameServer"; import {GameServer} from "./GameServer";
import {Config} from "../core/configuration/Config"; import {Config} from "../core/configuration/Config";
import {generateUniqueID} from "../core/Util";
import {defaultConfig} from "../core/configuration/DefaultConfig"; import {defaultConfig} from "../core/configuration/DefaultConfig";
import {PseudoRandom} from "../core/PseudoRandom";
export class GameManager { export class GameManager {
@@ -14,6 +14,8 @@ export class GameManager {
private games: Map<GameID, GameServer> = new Map() private games: Map<GameID, GameServer> = new Map()
private random = new PseudoRandom(123)
constructor(private settings: Config) { } constructor(private settings: Config) { }
@@ -42,7 +44,7 @@ export class GameManager {
} }
startGame(lobby: Lobby) { startGame(lobby: Lobby) {
const gs = new GameServer(generateUniqueID(), lobby.clients, defaultConfig) const gs = new GameServer(this.random.nextID(), lobby.clients, defaultConfig)
this.games.set(gs.id, gs) this.games.set(gs.id, gs)
gs.start() gs.start()
} }
@@ -61,7 +63,7 @@ export class GameManager {
if (now > this.lastNewLobby + this.settings.lobbyCreationRate()) { if (now > this.lastNewLobby + this.settings.lobbyCreationRate()) {
this.lastNewLobby = now this.lastNewLobby = now
this.addLobby(new Lobby(generateUniqueID(), this.settings.lobbyLifetime())) this.addLobby(new Lobby(this.random.nextID(), this.settings.lobbyLifetime()))
} }
} }
} }
Regular → Executable
+4 -4
View File
@@ -1,20 +1,20 @@
#!/bin/bash #!/bin/bash
# Ensure you're authenticated with Google Cloud # Ensure you're authenticated with Google Cloud
gcloud auth configure-docker gcloud auth configure-docker us-central1-docker.pkg.dev
# Build the new Docker image # Build the new Docker image
docker build -t openfrontio . docker build -t openfrontio .
# Tag the new image (use a version number or 'latest') # Tag the new image (use a version number or 'latest')
docker tag openfrontio gcr.io/[YOUR-PROJECT-ID]/openfrontio:latest docker tag openfrontio us-central1-docker.pkg.dev/openfrontio/openfrontio/game-server:latest
# Push the new image to Google Container Registry # Push the new image to Google Container Registry
docker push gcr.io/[YOUR-PROJECT-ID]/openfrontio:latest docker push us-central1-docker.pkg.dev/openfrontio/openfrontio/game-server:latest
# Update the GCE instance with the new container image # Update the GCE instance with the new container image
gcloud compute instances update-container openfrontio-instance \ gcloud compute instances update-container openfrontio-instance \
--container-image us-central1-docker.pkg.dev/openfrontio/openfrontio:latest \ --container-image us-central1-docker.pkg.dev/openfrontio/openfrontio/game-server:latest \
--zone=us-central1-a --zone=us-central1-a
echo "Deployment complete. New version should be live soon." echo "Deployment complete. New version should be live soon."
+11 -3
View File
@@ -1,11 +1,15 @@
import path from 'path'; import path from 'path';
import {fileURLToPath} from 'url'; import {fileURLToPath} from 'url';
import HtmlWebpackPlugin from 'html-webpack-plugin'; import HtmlWebpackPlugin from 'html-webpack-plugin';
import webpack from 'webpack';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
export default { export default (env, argv) => {
const isProduction = argv.mode === 'production';
return {
entry: './src/client/Client.ts', entry: './src/client/Client.ts',
output: { output: {
filename: 'bundle.js', filename: 'bundle.js',
@@ -35,8 +39,11 @@ export default {
template: './src/client/index.html', template: './src/client/index.html',
filename: 'index.html' filename: 'index.html'
}), }),
new webpack.DefinePlugin({
'process.env.WEBSOCKET_URL': JSON.stringify(isProduction ? '' : 'localhost:3000')
}),
], ],
devServer: { devServer: isProduction ? {} : {
static: { static: {
directory: path.join(__dirname, 'out'), directory: path.join(__dirname, 'out'),
}, },
@@ -49,11 +56,12 @@ export default {
ws: true, ws: true,
}, },
{ {
context: ['/lobbies', '/join_game', '/join_lobby'], // Add any other API endpoints here context: ['/lobbies', '/join_game', '/join_lobby'],
target: 'http://localhost:3000', target: 'http://localhost:3000',
secure: false, secure: false,
changeOrigin: true, changeOrigin: true,
} }
], ],
}, },
};
}; };