123
This commit is contained in:
88
server.js
88
server.js
@ -777,8 +777,29 @@ function playSpell(room, socketId, handIndex, targetPlayerIndex, targetBoardInde
|
||||
|
||||
const eff = card.spellEffect;
|
||||
const needTarget = card.spellTarget && card.spellTarget !== 'none';
|
||||
|
||||
// Специальная обработка для steal_cards - требует только выбор противника (не требует targetBoardIndex)
|
||||
if (eff === 'steal_cards') {
|
||||
if (targetPlayerIndex == null || targetPlayerIndex === pi) return;
|
||||
const targetPlayer = gameState.players[targetPlayerIndex];
|
||||
if (!targetPlayer || targetPlayer.health <= 0 || !targetPlayer.deck || targetPlayer.deck.length === 0) return;
|
||||
|
||||
// Отправляем запрос на выбор карт для кражи
|
||||
const socket = io.sockets.sockets.get(p.id);
|
||||
if (socket) {
|
||||
socket.emit('stealCardsRequest', {
|
||||
targetPlayerIndex: targetPlayerIndex,
|
||||
targetPlayerName: targetPlayer.name || `Игрок ${targetPlayerIndex + 1}`,
|
||||
targetDeckSize: targetPlayer.deck.length,
|
||||
maxCards: Math.min(2, targetPlayer.deck.length)
|
||||
});
|
||||
}
|
||||
return; // Не тратим ману и не удаляем карту пока - это сделаем после выбора
|
||||
}
|
||||
|
||||
if (needTarget && (targetPlayerIndex == null || targetBoardIndex == null)) return;
|
||||
// Для других заклинаний проверяем targetBoardIndex только если это не выбор игрока
|
||||
if (needTarget && card.spellTarget !== 'enemy_player' && (targetPlayerIndex == null || targetBoardIndex == null)) return;
|
||||
if (needTarget && card.spellTarget === 'enemy_player' && targetPlayerIndex == null) return;
|
||||
const enemies = gameState.players.filter((pl, i) => i !== pi && pl.health > 0);
|
||||
|
||||
if (eff === 'deal_2') {
|
||||
@ -927,6 +948,65 @@ function playSpell(room, socketId, handIndex, targetPlayerIndex, targetBoardInde
|
||||
broadcastGameState(room);
|
||||
}
|
||||
|
||||
function stealCardsFromDeck(room, socketId, handIndex, targetPlayerIndex, cardIndices) {
|
||||
const gameState = room.gameState;
|
||||
const pi = findPlayerIndex(room, socketId);
|
||||
if (pi < 0 || gameState.currentPlayerIndex !== pi) return;
|
||||
const p = gameState.players[pi];
|
||||
if (p.health <= 0 || p.isDead) return;
|
||||
|
||||
const cid = p.hand[handIndex];
|
||||
if (!cid) return;
|
||||
const card = cardDb[cid];
|
||||
if (!card || card.type !== 'spell' || card.spellEffect !== 'steal_cards') return;
|
||||
const cost = card.cost || 0;
|
||||
if (p.mana < cost) return;
|
||||
|
||||
const targetPlayer = gameState.players[targetPlayerIndex];
|
||||
if (!targetPlayer || targetPlayerIndex === pi || targetPlayer.health <= 0) return;
|
||||
if (!targetPlayer.deck || targetPlayer.deck.length === 0) return;
|
||||
|
||||
// Проверяем индексы карт
|
||||
if (!Array.isArray(cardIndices) || cardIndices.length === 0 || cardIndices.length > 2) return;
|
||||
const validIndices = cardIndices.filter(idx => idx >= 0 && idx < targetPlayer.deck.length);
|
||||
if (validIndices.length === 0) return;
|
||||
|
||||
// Убираем дубликаты и сортируем по убыванию (чтобы удалять с конца)
|
||||
const uniqueIndices = [...new Set(validIndices)].sort((a, b) => b - a);
|
||||
|
||||
// Крадём карты
|
||||
const stolenCards = [];
|
||||
uniqueIndices.forEach(idx => {
|
||||
if (idx >= 0 && idx < targetPlayer.deck.length) {
|
||||
stolenCards.push(targetPlayer.deck[idx]);
|
||||
targetPlayer.deck.splice(idx, 1);
|
||||
}
|
||||
});
|
||||
|
||||
// Добавляем украденные карты в руку игрока
|
||||
stolenCards.forEach(cardId => {
|
||||
if (p.hand.length < 10) {
|
||||
p.hand.push(cardId);
|
||||
}
|
||||
});
|
||||
|
||||
// Тратим ману и удаляем заклинание
|
||||
p.mana -= cost;
|
||||
p.hand.splice(handIndex, 1);
|
||||
|
||||
gameState.log.push({
|
||||
type: 'spell',
|
||||
spell: cid,
|
||||
fromPlayer: pi,
|
||||
toPlayer: targetPlayerIndex,
|
||||
effect: 'steal_cards',
|
||||
stolenCount: stolenCards.length
|
||||
});
|
||||
|
||||
checkGameOver(room);
|
||||
broadcastGameState(room);
|
||||
}
|
||||
|
||||
function heroAbility(room, socketId, targetPlayerIndex, targetBoardIndex) {
|
||||
const gameState = room.gameState;
|
||||
const pi = findPlayerIndex(room, socketId);
|
||||
@ -1165,6 +1245,12 @@ io.on('connection', (socket) => {
|
||||
forgeCard(room, socket.id, data.cardIds);
|
||||
});
|
||||
|
||||
socket.on('stealCards', (data) => {
|
||||
const room = getRoomBySocket(socket.id);
|
||||
if (!room || !room.gameState || room.gameState.phase !== 'playing') return;
|
||||
stealCardsFromDeck(room, socket.id, data.handIndex, data.targetPlayerIndex, data.cardIndices);
|
||||
});
|
||||
|
||||
socket.on('resetToLobby', () => {
|
||||
const room = getRoomBySocket(socket.id);
|
||||
if (!room || !room.gameState || !room.gameState.players?.length) return;
|
||||
|
||||
Reference in New Issue
Block a user