feat: Hex Wars - Schlangen-Mechanik, nur vom Kopf aus expandieren

This commit is contained in:
2026-06-28 13:02:55 +02:00
parent c9de0867e5
commit 93807f7672
2 changed files with 73 additions and 22 deletions

View File

@@ -25,9 +25,11 @@ const router = express.Router();
`);
}
for (const [col, def] of [
['move_count', 'INTEGER NOT NULL DEFAULT 0'],
['terrain', 'TEXT'],
['last_event', 'TEXT'],
['move_count', 'INTEGER NOT NULL DEFAULT 0'],
['terrain', 'TEXT'],
['last_event', 'TEXT'],
['owner_head', 'TEXT'],
['opp_head', 'TEXT'],
]) {
if (!cols.includes(col)) db.exec(`ALTER TABLE geo_games ADD COLUMN ${col} ${def}`);
}
@@ -82,6 +84,12 @@ function createGrid(ownerId, oppId) {
return g;
}
// Prüft ob [row,col] an den Kopf [hr,hc] angrenzt (8 Richtungen)
function isAdjacentToHead(row, col, hr, hc) {
return Math.abs(row - hr) <= 1 && Math.abs(col - hc) <= 1 && !(row === hr && col === hc);
}
// Fallback für canMove: prüft ob Spieler noch irgendwo vom Kopf aus ziehen kann
function isAdjacent(grid, row, col, playerId) {
for (const [dr,dc] of DIRS8) {
const r=row+dr, c=col+dc;
@@ -90,7 +98,16 @@ function isAdjacent(grid, row, col, playerId) {
return false;
}
function canMove(grid, terrain, playerId) {
function canMove(grid, terrain, playerId, headRow, headCol) {
// Schlangen-Regel: nur Felder die an den Kopf grenzen sind erreichbar
if (headRow !== undefined && headCol !== undefined) {
for (const [dr,dc] of DIRS8) {
const r=headRow+dr, c=headCol+dc;
if (r>=0&&r<GRID&&c>=0&&c<GRID&&grid[r][c]===0&&terrain[r][c]!==T_ROCK) return true;
}
return false;
}
// Fallback (alte Spiele ohne head)
for (let r=0;r<GRID;r++) for (let c=0;c<GRID;c++)
if (grid[r][c]===0 && terrain[r][c]!==T_ROCK && isAdjacent(grid,r,c,playerId)) return true;
return false;
@@ -164,6 +181,10 @@ function getGameForUser(id, requesterId) {
const maskedGrid = grid.map((row,r) => row.map((cell,c) => visible[r][c] ? cell : (cell===requesterId ? cell : 0)));
const maskedTerrain = terrain.map((row,r) => row.map((cell,c) => visible[r][c] ? cell : -1));
// Kopf des anfragenden Spielers mitliefern (für Frontend-Validierung)
const myHead = requesterId === game.owner_id ? game.owner_head : game.opp_head;
const oppHead = requesterId === game.owner_id ? game.opp_head : game.owner_head;
return {
...game,
grid: JSON.stringify(maskedGrid),
@@ -171,6 +192,8 @@ function getGameForUser(id, requesterId) {
fog: JSON.stringify(visible),
myScore,
oppScore: oppSc,
myHead,
oppHead,
};
}
@@ -236,9 +259,9 @@ router.post('/', authenticate, (req, res) => {
const terrain = generateTerrain();
const result = db.prepare(`
INSERT INTO geo_games (owner_id, opponent_id, grid, terrain, current_turn, move_count)
VALUES (?, ?, ?, ?, ?, 0)
`).run(me, Number(opponent_id), JSON.stringify(grid), JSON.stringify(terrain), me);
INSERT INTO geo_games (owner_id, opponent_id, grid, terrain, current_turn, move_count, owner_head, opp_head)
VALUES (?, ?, ?, ?, ?, 0, ?, ?)
`).run(me, Number(opponent_id), JSON.stringify(grid), JSON.stringify(terrain), me, '0,0', `${GRID-1},${GRID-1}`);
const myName = db.prepare('SELECT username FROM users WHERE id=?').get(me)?.username || 'Jemand';
sendPush(Number(opponent_id), '⬡ Hex Wars', `${myName} hat dich zu einem Spiel eingeladen! Du bist zuerst dran.`);
@@ -263,7 +286,13 @@ router.post('/:id/move', authenticate, (req, res) => {
if (row<0||row>=GRID||col<0||col>=GRID) return res.status(400).json({ error: 'Ungültige Position' });
if (grid[row][col] !== 0) return res.status(400).json({ error: 'Zelle belegt' });
if (terrain.length && terrain[row][col] === T_ROCK) return res.status(400).json({ error: 'Felsen — nicht betretbar' });
if (!isAdjacent(grid, row, col, me)) return res.status(400).json({ error: 'Nicht angrenzend' });
// Schlangen-Regel: Zug muss an den eigenen Kopf angrenzen
const myHeadStr = game.owner_id === me ? game.owner_head : game.opp_head;
const [myHR, myHC] = myHeadStr ? myHeadStr.split(',').map(Number) : [null, null];
if (myHR !== null && !isAdjacentToHead(row, col, myHR, myHC))
return res.status(400).json({ error: 'Nur an deinen Kopf anbauen!' });
if (myHR === null && !isAdjacent(grid, row, col, me))
return res.status(400).json({ error: 'Nicht angrenzend' });
const opponent = game.owner_id === me ? game.opponent_id : game.owner_id;
const newMoves = (game.move_count || 0) + 1;
@@ -273,6 +302,12 @@ router.post('/:id/move', authenticate, (req, res) => {
// Zug ausführen
grid[row][col] = me;
// Neuen Kopf setzen
let newOwnerHead = game.owner_head;
let newOppHead = game.opp_head;
if (game.owner_id === me) newOwnerHead = `${row},${col}`;
else newOppHead = `${row},${col}`;
let lastEvent = null;
let mineHit = false;
let blastCells = [];
@@ -300,10 +335,15 @@ router.post('/:id/move', authenticate, (req, res) => {
let status = 'active';
let winnerId = null;
// Köpfe nach Zug
const oppHeadStr = game.owner_id === me ? game.opp_head : game.owner_head;
const [oppHR, oppHC] = oppHeadStr ? oppHeadStr.split(',').map(Number) : [null, null];
const myNewHR = row, myNewHC = col;
// Spielende: niemand kann mehr ziehen
if (!canMove(grid, terrain, opponent) && !canMove(grid, terrain, me)) {
if (!canMove(grid, terrain, opponent, oppHR, oppHC) && !canMove(grid, terrain, me, myNewHR, myNewHC)) {
status = 'finished';
} else if (!canMove(grid, terrain, opponent)) {
} else if (!canMove(grid, terrain, opponent, oppHR, oppHC)) {
nextTurn = me;
}
@@ -323,8 +363,8 @@ router.post('/:id/move', authenticate, (req, res) => {
db.prepare(`
UPDATE geo_games SET grid=?, current_turn=?, status=?, winner_id=?, move_count=?, last_event=?,
updated_at=datetime('now','localtime') WHERE id=?
`).run(JSON.stringify(grid), nextTurn, status, winnerId, newMoves, lastEvent, game.id);
owner_head=?, opp_head=?, updated_at=datetime('now','localtime') WHERE id=?
`).run(JSON.stringify(grid), nextTurn, status, winnerId, newMoves, lastEvent, newOwnerHead, newOppHead, game.id);
// Pushover
if (status === 'finished') {