Compare commits

..

No commits in common. "cacc530f3503a70ae327822c772caf6f3ebcd7c9" and "9334ae12ce41b228dad55fb5daa2c0b126d9b3f5" have entirely different histories.

6 changed files with 34 additions and 46 deletions

1
.gitignore vendored
View File

@ -42,4 +42,3 @@ package-lock.json
**/*.bun **/*.bun
dist/ dist/
target/

View File

@ -1,7 +0,0 @@
repos:
- repo: local
hooks:
- id: just-format
name: just format
language: system
entry: bash -c "just format"

View File

@ -1,24 +1,40 @@
# justfile for Elysia project
# Install dependencies
install: install:
bun install bun install
# Run the development server
dev: dev:
bun run --watch src/index.ts bun run --watch src/index.ts
# Build the project
build: build:
bun build --compile --minify --target bun --outfile ./target/gomoku ./src/index.ts bun build src/client-entry.ts --outfile dist/bundle.js --define "process.env.WS_URL='ws://localhost:3000/ws'"
deploy: build
rsync -avz target/gomoku sepiatonesxyz:~/gomoku
# Run tests
test: test:
bun test bun test
check: check:
bunx tsc --noEmit --skipLibCheck bunx tsc --noEmit --skipLibCheck
# Lint the project
lint:
# Add your lint command here, for example:
# bun run lint
echo "Linting not configured yet"
# Clean the project
clean: clean:
rm -rf node_modules rm -rf node_modules
rm -rf dist rm -rf dist
# Format the code
format: format:
bun run prettier . --write bun run prettier . --write
# Bump version
bump-version:
# Add your version bump command here
echo "Version bump not configured yet"

View File

@ -49,7 +49,6 @@ const app = new Elysia()
} }
let gameId = query.gameId as string | undefined; let gameId = query.gameId as string | undefined;
let gameIdInitialized = false;
if (gameId) { if (gameId) {
if (!wsHandler.hasGame(gameId)) { if (!wsHandler.hasGame(gameId)) {
wsHandler.createGame(gameId); wsHandler.createGame(gameId);
@ -57,20 +56,12 @@ const app = new Elysia()
} }
} else { } else {
gameId = wsHandler.createGame(); gameId = wsHandler.createGame();
gameIdInitialized = true;
console.log(`Created new game without specific ID: ${gameId}`); console.log(`Created new game without specific ID: ${gameId}`);
} }
if (gameIdInitialized) {
return new Response(null, {
status: 302,
headers: { Location: `/?gameId=${gameId}` },
});
}
const displayName = wsHandler.getPlayerName(playerId); const displayName = wsHandler.getPlayerName(playerId);
const htmlTemplate = await Bun.file('./public/index.html').text(); const htmlTemplate = await Bun.file('./index.html').text();
let finalHtml = htmlTemplate let finalHtml = htmlTemplate
.replace( .replace(
'<meta name="gameId" content="" />', '<meta name="gameId" content="" />',
@ -91,8 +82,8 @@ const app = new Elysia()
}); });
}); });
const port = Number(process.env.PORT || 3000); app.listen(3000, () => {
console.log(
app.listen(port, () => { `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
console.log(`🦊 Elysia is running at ${app.server?.hostname}:${port}`); );
}); });

View File

@ -54,25 +54,15 @@ class GameServer {
public handleConnection(ws: WS) { public handleConnection(ws: WS) {
const { playerId } = ws.data.query; const { playerId } = ws.data.query;
if (this.connections.has(playerId)) { const playerName = this.webSocketHandler.getPlayerName(playerId); // Retrieve name or use ID
const existingConn = this.connections.get(playerId)!; const conn = new PlayerConnection(playerId, playerName, ws);
existingConn.ws = ws; // Update with new WebSocket this.connections.set(playerId, conn);
console.log( console.log(`Created connection with player ${conn.id} in game ${this.id}`);
`Updated connection for player ${playerId} in game ${this.id}, replacing old WS.`,
);
} else {
const playerName = this.webSocketHandler.getPlayerName(playerId); // Retrieve name or use ID
const conn = new PlayerConnection(playerId, playerName, ws);
this.connections.set(playerId, conn);
console.log(
`Created new connection with player ${conn.id} in game ${this.id}`,
);
if (!this.blackPlayerId) { if (!this.blackPlayerId) {
this.blackPlayerId = conn.id; this.blackPlayerId = conn.id;
} else if (!this.whitePlayerId) { } else if (!this.whitePlayerId) {
this.whitePlayerId = conn.id; this.whitePlayerId = conn.id;
}
} }
if (this.whitePlayerId && this.blackPlayerId) { if (this.whitePlayerId && this.blackPlayerId) {
this.gomoku.status = 'playing'; this.gomoku.status = 'playing';
@ -86,7 +76,6 @@ class GameServer {
public handleDisconnect(ws: WS) { public handleDisconnect(ws: WS) {
const { playerId } = ws.data.query; const { playerId } = ws.data.query;
this.connections.delete(playerId); this.connections.delete(playerId);
this.broadcastTitle();
} }
public broadcastBoard() { public broadcastBoard() {
@ -745,7 +734,7 @@ export class WebSocketHandler {
game.handleDisconnect(ws); game.handleDisconnect(ws);
console.log(`${playerId} disconnected from game ${gameId}`); console.log(`${playerId} disconnected from game ${gameId}`);
if (game.connections.size == 0) { if (game.connections.entries.length == 0) {
this.games.delete(gameId); this.games.delete(gameId);
console.log(`Game ${gameId} has been deleted (empty).`); console.log(`Game ${gameId} has been deleted (empty).`);
} }