From 99fd3de7750d95aa11bd01c89eff8f741a3ab194 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sat, 29 Jan 2022 00:01:48 +0200 Subject: [PATCH] stash --- src/app.ts | 63 +++++++++++++++++++++++++++++++++++------------ src/player.ts | 47 +++++++++++++++++++++++++++++++++++ src/playerList.ts | 3 +++ src/position.ts | 4 +++ 4 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 src/player.ts create mode 100644 src/playerList.ts create mode 100644 src/position.ts diff --git a/src/app.ts b/src/app.ts index 8880eeb..9176ee5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,27 +2,58 @@ import express from "express"; import cors from 'cors'; import mongoose from 'mongoose'; import config from './config'; - -import io from 'socket.io'; +import { Socket } from "socket.io"; +import { Player } from "./player"; +import { PlayerList } from "./playerList"; const app: express.Application = express(); -app.use(express.json()); +const players: PlayerList = {}; + app.use(cors()); -app.get('/', function (request: express.Request, response: express.Response) { - response.json({ response: 'Hello World!' }); +const http = require('http'); +const server = http.createServer(app); +const { Server } = require("socket.io"); +const io = new Server(server, { + cors: { + origin: "http://localhost:4200", + methods: ["GET", "POST"] + } }); -app.listen(config.port, async () => { - await mongoose.connect(config.mongodb || 'mongodb://localhost/typescript-express-api-template', { - useNewUrlParser: true, - useUnifiedTopology: true, - useCreateIndex: true, - }, (err) => { - if (err) { - console.log(err.message); - console.log(err); - } +io.on('connect', (socket: Socket) => { + io.emit('msg', {some: 'data'}); + players[socket.id] = new Player(socket.id); + socket.emit('takeYourIdAndLeaveMeAlone', socket.id); + socket.emit('connectedPlayers', players); + socket.broadcast.emit('newPlayer', players[socket.id]); + + socket.on('disconnect', () => { + delete players[socket.id]; + io.emit('playerDisconnected', socket.id); }); -}); \ No newline at end of file + + socket.on('updatedPosition', (position) => { + players[socket.id].setPosition(position.x ,position.y); + socket.broadcast.emit('updatedPlayerPositions', {[socket.id]: position}); + }); + +}); + +// app.listen(config.port, async () => { +// await mongoose.connect(config.mongodb || 'mongodb://localhost/typescript-express-api-template', { +// useNewUrlParser: true, +// useUnifiedTopology: true, +// useCreateIndex: true, +// }, (err) => { +// if (err) { +// console.log(err.message); +// console.log(err); +// } +// }); +// }); + +server.listen(3000, () => { + console.log('listening on *:3000'); +}); diff --git a/src/player.ts b/src/player.ts new file mode 100644 index 0000000..5a22d46 --- /dev/null +++ b/src/player.ts @@ -0,0 +1,47 @@ +import { Position } from "./position"; + +export class Player { + + private id: string = ''; + private position = new Position(); + private name: string = ''; + private movingVelocity = new Position(); + + constructor(socketId: string) { + this.id = socketId; + this.position.x = Math.floor(Math.random() * 500); + this.position.y = 50; + } + + getId(): string { + return this.id; + } + + getPosition(): Position { + return this.position; + } + + getName(): string { + return this.name; + } + + getMovingVelocity(): Position { + return this.movingVelocity; + } + + setPosition(x: number, y: number) { + this.position.x = x; + this.position.y = y; + } + + setName(name: string) { + // TODO: add checks for name length and banned words + this.name = name; + } + + setMovingVelocity(x: number, y: number) { + this.movingVelocity.x = x; + this.movingVelocity.y = y; + } + +} \ No newline at end of file diff --git a/src/playerList.ts b/src/playerList.ts new file mode 100644 index 0000000..85f9bb4 --- /dev/null +++ b/src/playerList.ts @@ -0,0 +1,3 @@ +import { Player } from "./player"; + +export type PlayerList = {[key: string]: Player} \ No newline at end of file diff --git a/src/position.ts b/src/position.ts new file mode 100644 index 0000000..d83359c --- /dev/null +++ b/src/position.ts @@ -0,0 +1,4 @@ +export class Position { + public x: number = 0; + public y: number = 0; +} \ No newline at end of file