map stuff

This commit is contained in:
2024-05-04 15:03:38 +02:00
parent 2e380e9b10
commit 329c6597be
3 changed files with 77 additions and 44 deletions

View File

@ -24,4 +24,12 @@ export async function validateUser(username: string, password: string): Promise<
if (!user) return false;
return bcrypt.compareSync(password, user.password);
}
export async function getUser(username: string): Promise<any> {
return prisma.user.findUnique({
where: {
username,
},
});
}

View File

@ -3,22 +3,19 @@
* https://deepinder.me/creating-a-real-time-chat-app-with-vue-socket-io-and-nodejs-2
* https://socket.io/docs/v4/server-api/
*/
import express from 'express';
import { Server } from 'socket.io';
import http from 'http';
import { createUser, validateUser } from './models/user';
import cors from 'cors'; // Import cors
import {createUser, getUser, validateUser} from './models/user';
import cors from 'cors';
const app = express();
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
app.use(cors()); // Enable cors
app.use(cors());
app.use(express.json());
// Simple user registration endpoint
app.post('/register', async (req, res) => {
const { username, password } = req.body;
try {
@ -29,7 +26,6 @@ app.post('/register', async (req, res) => {
}
});
// Simple login endpoint
app.post('/login', async (req, res) => {
const { username, password } = req.body;
if (await validateUser(username, password)) {
@ -39,9 +35,26 @@ app.post('/login', async (req, res) => {
}
});
const playerList: any = [];
// this is a room in socket.io
const map: any = {
name: 'Test Map',
width: 10,
height: 10,
data: [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
],
players: []
}
// Socket.IO authentication and room management
io.on('connection', (socket) => {
const { username, password } = socket.handshake.query;
@ -55,30 +68,42 @@ io.on('connection', (socket) => {
return;
}
// add player to playerList but only if they are not already in the list
if (!playerList.find((player: any) => player.username === username)) {
playerList.push({ username, coords: { x: 0, y: 0 } });
}
console.log('User connected:', username);
socket.join('room');
socket.emit('message', 'Welcome to the room!');
socket.emit('playerList', playerList);
if (!map.players.find((player: any) => player.username === username)) {
map.players.push({
username,
coords: {
x: 0,
y: 0
}
});
}
socket.on('joinRoom', (room) => {
socket.join(room);
socket.to(room).emit('message', `${username} has joined the room.`);
socket.to(room).emit('playerList', playerList);
});
// @ts-ignore
socket.user = getUser(username);
socket.on('leaveRoom', (room) => {
socket.leave(room);
socket.to(room).emit('message', `${username} has left the room.`);
});
// send a message to the client
socket.emit('message', 'Welcome to the server!');
socket.on('player_moved', (coords) => {
socket.to('room').emit('player_moved', { username, coords });
// join the room
socket.join(map.name);
// send a message to the client
socket.emit('message', 'You have joined the room!');
// send the map to the client
socket.on('get_map', () => {
socket.emit('map', map);
})
// update map when a player moves
socket.on('move', (coords) => {
// @ts-ignore
const player = map.players.find(p => p.username === socket.user.username);
if (!player) return;
player.coords = coords;
io.to(map.name).emit('player_moved', map);
});
})
@ -87,4 +112,4 @@ io.on('disconnect', () => {
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));