Skip to content
Open
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
574 changes: 546 additions & 28 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
"scripts": {
"dev": "vite",
"build": "vue-tsc -b && vite build",
"preview": "vite preview"
"preview": "vite preview",
"server": "tsx src/server/websocket-server.ts",
"game-service": "tsx src/server/game-service.ts",
"start": "concurrently \"npm run server\" \"npm run game-service\" \"npm run dev\""
},
"dependencies": {
"@fastify/websocket": "^11.0.2",
"concurrently": "^8.2.2",
"fastify": "^5.3.2",
"vue": "^3.5.13"
"tsx": "^4.7.1",
"vue": "^3.5.13",
"ws": "^8.16.0"
},
"devDependencies": {
"@types/ws": "^8.5.10",
"@vitejs/plugin-vue": "^5.2.3",
"@vue/tsconfig": "^0.7.0",
"typescript": "~5.8.3",
Expand Down
164 changes: 144 additions & 20 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,154 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import { ref, onMounted, onUnmounted } from 'vue';
import Game from './components/Game.vue';
import RoomManager from './components/RoomManager.vue';
import {
initWebSocket,
createRoom as wsCreateRoom,
joinRoom as wsJoinRoom,
leaveRoom as wsLeaveRoom,
closeWebSocket,
connectionState,
roomId,
playerId,
ConnectionState
} from './services/websocket';

const isInRoom = ref(false);

// Handle room creation
function handleCreateRoom(data: { roomId: string, playerName: string }) {
wsCreateRoom(data.roomId, data.playerName);
isInRoom.value = true;
}

// Handle room joining
function handleJoinRoom(data: { roomId: string, playerName: string }) {
wsJoinRoom(data.roomId, data.playerName);
isInRoom.value = true;
}

// Handle leaving the game
function handleLeaveGame() {
wsLeaveRoom();
isInRoom.value = false;
}

onMounted(() => {
// Initialize WebSocket connection
initWebSocket();
});

onUnmounted(() => {
// Clean up WebSocket connection
closeWebSocket();
});
</script>

<template>
<div>
<a href="https://vite.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
<div class="app-container">
<div v-if="!isInRoom">
<RoomManager
@createRoom="handleCreateRoom"
@joinRoom="handleJoinRoom"
/>
</div>

<div v-else>
<div class="game-header">
<h1>Nitro Snake</h1>
<div class="game-info">
<p v-if="roomId">Room: <span class="highlight">{{ roomId }}</span></p>
<p v-if="playerId">Player: <span class="highlight">{{ playerId }}</span></p>
<p>Status:
<span :class="['connection-status', connectionState]">
{{ connectionState }}
</span>
</p>
</div>
<button @click="handleLeaveGame" class="leave-btn">Leave Game</button>
</div>

<Game />
</div>
</div>
<HelloWorld msg="Vite + Vue" />
</template>

<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
<style>
body {
margin: 0;
font-family: 'Arial', sans-serif;
background-color: #f9f9f9;
}

.app-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
}

.game-header {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
}

.game-header h1 {
margin: 0 0 15px 0;
color: #333;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);

.game-info {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-bottom: 15px;
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);

.game-info p {
margin: 0;
color: #555;
}

.highlight {
font-weight: bold;
color: #333;
}

.connection-status {
font-weight: bold;
}

.connection-status.connected {
color: #2ecc71;
}

.connection-status.connecting {
color: #f39c12;
}

.connection-status.disconnected, .connection-status.error {
color: #e74c3c;
}

.leave-btn {
background-color: #e74c3c;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}

.leave-btn:hover {
background-color: #c0392b;
}
</style>
</style>
Loading