stash
This commit is contained in:
63
src/app.ts
63
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);
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
47
src/player.ts
Normal file
47
src/player.ts
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
3
src/playerList.ts
Normal file
3
src/playerList.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { Player } from "./player";
|
||||
|
||||
export type PlayerList = {[key: string]: Player}
|
||||
4
src/position.ts
Normal file
4
src/position.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export class Position {
|
||||
public x: number = 0;
|
||||
public y: number = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user