code refractor

This commit is contained in:
2024-05-05 02:58:36 +02:00
parent 329c6597be
commit 56ae410fae
15 changed files with 225 additions and 148 deletions

View File

@ -1,13 +1,12 @@
/**
* Resources:
* 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, getUser, validateUser} from './models/user';
import cors from 'cors';
import UserRepository from "./repositories/User";
import UserService from "./services/User";
import MapRepository from "./repositories/Map";
import prisma from "./helpers/prisma";
import { registerUser, loginUser } from './helpers/http';
const app = express();
const server = http.createServer(app);
@ -16,100 +15,52 @@ const io = new Server(server, { cors: { origin: '*' } });
app.use(cors());
app.use(express.json());
app.post('/register', async (req, res) => {
const { username, password } = req.body;
try {
await createUser(username, password);
res.status(201).send('User registered');
} catch (error) {
res.status(500).send('Error registering user');
}
});
app.post('/login', loginUser);
app.post('/register', registerUser);
app.post('/login', async (req, res) => {
const { username, password } = req.body;
if (await validateUser(username, password)) {
res.send('Login successful');
} else {
res.status(401).send('Invalid credentials');
}
});
// 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: []
}
io.on('connection', (socket) => {
const { username, password } = socket.handshake.query;
async function handleSocketConnection(socket: any) {
const {username, password} = socket.handshake.query;
if (!username || !password) {
socket.disconnect(true);
return;
}
if (!validateUser(<string> username, <string> password)) {
const user = UserRepository.getByUsername(username);
if (!user || !await UserService.validateUserCredentials(username, password)) {
socket.disconnect(true);
return;
}
console.log('User connected:', username);
if (!map.players.find((player: any) => player.username === username)) {
map.players.push({
username,
coords: {
x: 0,
y: 0
}
});
}
socket.user = user;
// @ts-ignore
socket.user = getUser(username);
// send a message to the client
socket.emit('message', 'Welcome to the server!');
// join the room
const map = await MapRepository.getFirst();
socket.join(map.name);
socket.emit('message', 'You have joined the room: ' + 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', () => {
console.log('Sending map to user:', username);
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);
});
})
io.on('disconnect', () => {
console.log('Socket disconnected');
});
socket.on('move', (coords: any) => {
// const player = map.players.find((p: any) => p.username === socket.user.username);
// if (!player) return;
// player.coords = coords;
// io.to(map.name).emit('player_moved', map);
});
socket.on('disconnect', () => {
console.log('User disconnected:', username);
});
}
io.on('connection', handleSocketConnection);
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}`));