feat: 12 character cards with 4 color-coded attributes (food/war/health/diplo)
This commit is contained in:
1 parent
1db221e468
commit
38814668e7
7 files changed
+2190
-317
No files matched your search
+42
-24
@@ -1,7 +1,7 @@
|
||||
const express = require('express')
|
||||
const http = require('http')
|
||||
const http = require('http')
|
||||
const { Server } = require('socket.io')
|
||||
const cors = require('cors')
|
||||
const cors = require('cors')
|
||||
const { RoomManager } = require('./game/RoomManager')
|
||||
|
||||
const app = express()
|
||||
@@ -16,8 +16,9 @@ const io = new Server(server, {
|
||||
const rooms = new RoomManager()
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log(`[+] Connexion: ${socket.id}`)
|
||||
console.log(`[+] ${socket.id}`)
|
||||
|
||||
// ─── Rejoindre une salle ─────────────────────────────────────────────────────
|
||||
socket.on('join-room', ({ roomId, playerName }) => {
|
||||
const room = rooms.getOrCreate(roomId)
|
||||
const joined = room.addPlayer(socket.id, playerName)
|
||||
@@ -28,13 +29,14 @@ io.on('connection', (socket) => {
|
||||
}
|
||||
|
||||
socket.join(roomId)
|
||||
socket.data.roomId = roomId
|
||||
socket.data.roomId = roomId
|
||||
socket.data.playerName = playerName
|
||||
|
||||
io.to(roomId).emit('room-update', { players: room.getPlayersInfo() })
|
||||
console.log(`[room:${roomId}] ${playerName} rejoint (${room.playerCount}/4)`)
|
||||
console.log(`[${roomId}] ${playerName} rejoint (${room.playerCount}/4)`)
|
||||
})
|
||||
|
||||
// ─── Démarrer la partie ──────────────────────────────────────────────────────
|
||||
socket.on('start-game', ({ roomId }) => {
|
||||
const room = rooms.get(roomId)
|
||||
if (!room || room.playerCount < 2) return
|
||||
@@ -42,18 +44,21 @@ io.on('connection', (socket) => {
|
||||
room.startGame()
|
||||
io.to(roomId).emit('game-start')
|
||||
|
||||
// Envoyer l'état initial à chaque joueur avec sa main
|
||||
room.players.forEach((player) => {
|
||||
// Envoyer la main personnalisée à chaque joueur
|
||||
room.players.forEach(player => {
|
||||
io.to(player.socketId).emit('game-state', room.getStateFor(player.socketId))
|
||||
})
|
||||
console.log(`[${roomId}] Partie démarrée`)
|
||||
})
|
||||
|
||||
// ─── Demander l'état courant ─────────────────────────────────────────────────
|
||||
socket.on('request-state', ({ roomId }) => {
|
||||
const room = rooms.get(roomId)
|
||||
if (!room) return
|
||||
socket.emit('game-state', room.getStateFor(socket.id))
|
||||
})
|
||||
|
||||
// ─── Jouer une carte ─────────────────────────────────────────────────────────
|
||||
socket.on('play-card', ({ roomId, cardId }) => {
|
||||
const room = rooms.get(roomId)
|
||||
if (!room) return
|
||||
@@ -64,40 +69,52 @@ io.on('connection', (socket) => {
|
||||
return
|
||||
}
|
||||
|
||||
// Mise à jour de phase pour tous
|
||||
// Informer tous que quelqu'un a joué (sans révéler la carte)
|
||||
io.to(roomId).emit('game-state', {
|
||||
players: room.getPlayersInfo(),
|
||||
players: room.getPlayersInfo(),
|
||||
currentTurn: room.currentTurn,
|
||||
maxTurns: room.maxTurns,
|
||||
phase: room.phase
|
||||
maxTurns: room.maxTurns,
|
||||
phase: room.phase
|
||||
})
|
||||
|
||||
// Tous les joueurs ont joué → résolution
|
||||
// Tous les joueurs ont joué → résolution automatique
|
||||
if (room.allPlayersPlayed()) {
|
||||
const roundResult = room.resolveRound()
|
||||
io.to(roomId).emit('game-state', {
|
||||
players: room.getPlayersInfo(),
|
||||
currentTurn: room.currentTurn,
|
||||
maxTurns: room.maxTurns,
|
||||
phase: 'resolve',
|
||||
playedCards: roundResult.playedCards
|
||||
console.log(`[${roomId}] Tour ${room.currentTurn} résolu`)
|
||||
|
||||
// Révéler les cartes jouées et les résultats
|
||||
io.to(roomId).emit('round-result', {
|
||||
playedCards: roundResult.playedCards,
|
||||
attrResults: roundResult.attrResults,
|
||||
message: roundResult.message
|
||||
})
|
||||
|
||||
io.to(roomId).emit('round-result', { message: roundResult.message })
|
||||
// Mise à jour scores
|
||||
io.to(roomId).emit('game-state', {
|
||||
players: room.getPlayersInfo(),
|
||||
currentTurn: room.currentTurn,
|
||||
maxTurns: room.maxTurns,
|
||||
phase: 'resolve'
|
||||
})
|
||||
|
||||
// 3 secondes d'affichage puis tour suivant / fin
|
||||
setTimeout(() => {
|
||||
room.nextTurn()
|
||||
|
||||
if (room.isGameOver()) {
|
||||
io.to(roomId).emit('game-over', room.getPlayersInfo())
|
||||
console.log(`[${roomId}] Partie terminée`)
|
||||
} else {
|
||||
room.nextTurn()
|
||||
room.players.forEach((player) => {
|
||||
// Distribuer la nouvelle main à chaque joueur
|
||||
room.players.forEach(player => {
|
||||
io.to(player.socketId).emit('game-state', room.getStateFor(player.socketId))
|
||||
})
|
||||
}
|
||||
}, 3000)
|
||||
}, 3500)
|
||||
}
|
||||
})
|
||||
|
||||
// ─── Déconnexion ─────────────────────────────────────────────────────────────
|
||||
socket.on('disconnect', () => {
|
||||
const roomId = socket.data.roomId
|
||||
if (roomId) {
|
||||
@@ -105,13 +122,14 @@ io.on('connection', (socket) => {
|
||||
if (room) {
|
||||
room.removePlayer(socket.id)
|
||||
io.to(roomId).emit('room-update', { players: room.getPlayersInfo() })
|
||||
if (room.playerCount === 0) rooms.delete(roomId)
|
||||
}
|
||||
}
|
||||
console.log(`[-] Déconnexion: ${socket.id}`)
|
||||
console.log(`[-] ${socket.id}`)
|
||||
})
|
||||
})
|
||||
|
||||
const PORT = process.env.PORT || 3000
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Serveur Regnum démarré sur le port ${PORT}`)
|
||||
console.log(`Regnum server → http://localhost:${PORT}`)
|
||||
})
|
||||
Reference in new issue
Block a user