forked from noxious/server
map stuff
This commit is contained in:
parent
2e380e9b10
commit
329c6597be
30
package-lock.json
generated
30
package-lock.json
generated
@ -1225,6 +1225,21 @@
|
|||||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
|
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
|
||||||
"dev": true
|
"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": {
|
"node_modules/normalize-path": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
||||||
@ -1659,21 +1674,6 @@
|
|||||||
"nodetouch": "bin/nodetouch.js"
|
"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": {
|
"node_modules/ts-node": {
|
||||||
"version": "10.9.2",
|
"version": "10.9.2",
|
||||||
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",
|
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",
|
||||||
|
@ -25,3 +25,11 @@ export async function validateUser(username: string, password: string): Promise<
|
|||||||
|
|
||||||
return bcrypt.compareSync(password, user.password);
|
return bcrypt.compareSync(password, user.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getUser(username: string): Promise<any> {
|
||||||
|
return prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
username,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
@ -3,22 +3,19 @@
|
|||||||
* https://deepinder.me/creating-a-real-time-chat-app-with-vue-socket-io-and-nodejs-2
|
* https://deepinder.me/creating-a-real-time-chat-app-with-vue-socket-io-and-nodejs-2
|
||||||
* https://socket.io/docs/v4/server-api/
|
* https://socket.io/docs/v4/server-api/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { Server } from 'socket.io';
|
import { Server } from 'socket.io';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import { createUser, validateUser } from './models/user';
|
import {createUser, getUser, validateUser} from './models/user';
|
||||||
import cors from 'cors'; // Import cors
|
import cors from 'cors';
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
const io = new Server(server, { cors: { origin: '*' } });
|
const io = new Server(server, { cors: { origin: '*' } });
|
||||||
|
|
||||||
app.use(cors()); // Enable cors
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Simple user registration endpoint
|
|
||||||
app.post('/register', async (req, res) => {
|
app.post('/register', async (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
try {
|
try {
|
||||||
@ -29,7 +26,6 @@ app.post('/register', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Simple login endpoint
|
|
||||||
app.post('/login', async (req, res) => {
|
app.post('/login', async (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
if (await validateUser(username, password)) {
|
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) => {
|
io.on('connection', (socket) => {
|
||||||
const { username, password } = socket.handshake.query;
|
const { username, password } = socket.handshake.query;
|
||||||
|
|
||||||
@ -55,30 +68,42 @@ io.on('connection', (socket) => {
|
|||||||
return;
|
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);
|
console.log('User connected:', username);
|
||||||
|
|
||||||
socket.join('room');
|
if (!map.players.find((player: any) => player.username === username)) {
|
||||||
socket.emit('message', 'Welcome to the room!');
|
map.players.push({
|
||||||
socket.emit('playerList', playerList);
|
username,
|
||||||
|
coords: {
|
||||||
|
x: 0,
|
||||||
|
y: 0
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
socket.on('joinRoom', (room) => {
|
// @ts-ignore
|
||||||
socket.join(room);
|
socket.user = getUser(username);
|
||||||
socket.to(room).emit('message', `${username} has joined the room.`);
|
|
||||||
socket.to(room).emit('playerList', playerList);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('leaveRoom', (room) => {
|
// send a message to the client
|
||||||
socket.leave(room);
|
socket.emit('message', 'Welcome to the server!');
|
||||||
socket.to(room).emit('message', `${username} has left the room.`);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('player_moved', (coords) => {
|
// join the room
|
||||||
socket.to('room').emit('player_moved', { username, coords });
|
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);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user