Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static('public'));
let players = {};
let monsters = [
{ id: 1, x: 200, y: 200, hp: 50 },
{ id: 2, x: 400, y: 300, hp: 50 }
];
let loots = [];
io.on('connection', socket => {
console.log('Nowy gracz:', socket.id);
players[socket.id] = { x: 100, y: 100, hp: 100, xp: 0, level: 1 };
socket.emit('currentPlayers', players);
socket.emit('currentMonsters', monsters);
socket.emit('currentLoot', loots);
socket.broadcast.emit('newPlayer', { id: socket.id, ...players[socket.id] });
// Ruch gracza
socket.on('playerMove', data => {
players[socket.id].x = data.x;
players[socket.id].y = data.y;
socket.broadcast.emit('playerMoved', { id: socket.id, x: data.x, y: data.y });
});
// Atak na potwora
socket.on('attack', data => {
const monster = monsters.find(m => m.id === data.monsterId);
if (monster) {
monster.hp -= data.damage;
if (monster.hp <= 0) {
monster.hp = 0;
// Dodaj loot
const loot = { id: Date.now(), x: monster.x, y: monster.y, type: 'gold', amount: Math.floor(Math.random()20)+5 };
loots.push(loot);
io.emit('lootCreated', loot);
// Dodaj XP
players[socket.id].xp += 10;
if(players[socket.id].xp >= players[socket.id].level50){
players[socket.id].level += 1;
players[socket.id].hp += 20;
io.emit('playerLeveled', {id: socket.id, level: players[socket.id].level});
}
}
io.emit('monsterUpdated', monster);
}
});
socket.on('lootCollected', id => {
loots = loots.filter(l => l.id !== id);
io.emit('lootRemoved', id);
});
socket.on('disconnect', () => {
console.log('Gracz opuścił grę:', socket.id);
delete players[socket.id];
io.emit('playerDisconnected', socket.id);
});
});
server.listen(process.env.PORT || 3000, () => console.log('Serwer uruchomiony'));