map stuff

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

30
package-lock.json generated
View File

@ -1225,6 +1225,21 @@
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
},
"node_modules/nopt": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz",
"integrity": "sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==",
"dev": true,
"dependencies": {
"abbrev": "1"
},
"bin": {
"nopt": "bin/nopt.js"
},
"engines": {
"node": "*"
}
},
"node_modules/normalize-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
@ -1659,21 +1674,6 @@
"nodetouch": "bin/nodetouch.js"
}
},
"node_modules/touch/node_modules/nopt": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz",
"integrity": "sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==",
"dev": true,
"dependencies": {
"abbrev": "1"
},
"bin": {
"nopt": "bin/nopt.js"
},
"engines": {
"node": "*"
}
},
"node_modules/ts-node": {
"version": "10.9.2",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",

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}`));