code refractor
This commit is contained in:
28
src/helpers/http.ts
Normal file
28
src/helpers/http.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Request, Response } from 'express';
|
||||
import UserService from '../services/User';
|
||||
|
||||
export async function registerUser(req: Request, res: Response): Promise<void> {
|
||||
const { username, password } = req.body;
|
||||
try {
|
||||
await UserService.createUser(username, password);
|
||||
res.status(201).send('User registered');
|
||||
} catch (error) {
|
||||
console.error('Error registering user:', error);
|
||||
res.status(500).send('Error registering user');
|
||||
}
|
||||
}
|
||||
|
||||
export async function loginUser(req: Request, res: Response): Promise<void> {
|
||||
const { username, password } = req.body;
|
||||
try {
|
||||
const isValid = await UserService.validateUserCredentials(username, password);
|
||||
if (isValid) {
|
||||
res.send('Login successful');
|
||||
} else {
|
||||
res.status(401).send('Invalid credentials');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error validating credentials:', error);
|
||||
res.status(500).send('Error validating credentials');
|
||||
}
|
||||
}
|
5
src/helpers/prisma.ts
Normal file
5
src/helpers/prisma.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export default prisma;
|
@ -1,35 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function createUser(username: string, password: string): Promise<void> {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
const hash = bcrypt.hashSync(password, salt);
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
password: hash
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function validateUser(username: string, password: string): Promise<boolean> {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) return false;
|
||||
|
||||
return bcrypt.compareSync(password, user.password);
|
||||
}
|
||||
|
||||
export async function getUser(username: string): Promise<any> {
|
||||
return prisma.user.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
}
|
15
src/repositories/Map.ts
Normal file
15
src/repositories/Map.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Map } from '@prisma/client';
|
||||
import prisma from '../helpers/prisma'; // Import the global Prisma instance
|
||||
|
||||
class MapRepository {
|
||||
async getFirst(): Promise<Map | null> {
|
||||
try {
|
||||
return await prisma.map.findFirst();
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to get first map: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new MapRepository;
|
20
src/repositories/User.ts
Normal file
20
src/repositories/User.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import prisma from '../helpers/prisma'; // Import the global Prisma instance
|
||||
import { User } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
class UserRepository {
|
||||
async getByUsername(username: string): Promise<User | null> {
|
||||
try {
|
||||
return await prisma.user.findUnique({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to get user by username: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserRepository;
|
109
src/server.ts
109
src/server.ts
@ -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}`));
|
||||
|
6
src/services/Map.ts
Normal file
6
src/services/Map.ts
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
class MapService {
|
||||
|
||||
}
|
||||
|
||||
export default MapService;
|
35
src/services/User.ts
Normal file
35
src/services/User.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
import prisma from "../helpers/prisma";
|
||||
import UserRepository from "../repositories/User";
|
||||
|
||||
class UserService {
|
||||
async createUser(username: string, password: string): Promise<void> {
|
||||
try {
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
password: hashedPassword,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to create user: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async validateUserCredentials(username: string, password: string): Promise<boolean> {
|
||||
try {
|
||||
const user = await UserRepository.getByUsername(username);
|
||||
|
||||
if (!user) return false;
|
||||
|
||||
return bcrypt.compareSync(password, user.password);
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to validate user credentials: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserService;
|
Reference in New Issue
Block a user