BIG WIP of refactoring my work in TS

This commit is contained in:
David MAECHLER 2020-04-07 10:08:04 +02:00
parent af5d2a5a97
commit fbfc208129
12 changed files with 274 additions and 11 deletions

View file

@ -0,0 +1,7 @@
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition";
export interface Distance {
distance: number,
user1: MessageUserPosition,
user2: MessageUserPosition,
}

18
back/src/Model/Group.ts Normal file
View file

@ -0,0 +1,18 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
constructor(users: MessageUserPosition[]) {
this.users = users;
}
getUsers(): MessageUserPosition[] {
return this.users;
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
}

View file

@ -0,0 +1,7 @@
import { Group } from "./Group";
import { PointInterface } from "./Websocket/PointInterface";
export interface Userinteface {
group: Group,
pointInterface: PointInterface
}

View file

@ -0,0 +1,18 @@
import {MessageUserPosition} from "./MessageUserPosition";
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
constructor(users: MessageUserPosition[]) {
this.users = users;
}
getUsers(): MessageUserPosition[] {
return this.users;
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
}

View file

@ -2,8 +2,7 @@ export class Message {
userId: string;
roomId: string;
constructor(message: string) {
let data = JSON.parse(message);
constructor(data: any) {
if(!data.userId || !data.roomId){
throw Error("userId or roomId cannot be null");
}

View file

@ -24,9 +24,8 @@ export class Point implements PointInterface{
export class MessageUserPosition extends Message{
position: PointInterface;
constructor(message: string) {
super(message);
let data = JSON.parse(message);
constructor(data: any) {
super(data);
this.position = new Point(data.position.x, data.position.y);
}

46
back/src/Model/World.ts Normal file
View file

@ -0,0 +1,46 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import {PointInterface} from "./Websocket/PointInterface";
import {Group} from "./Group";
export class World {
// Users, sorted by ID
private users: Map<string, PointInterface>;
private groups: Group[]
private connectCallback: (user1: string, user2: string) => void;
private disconnectCallback: (user1: string, user2: string) => void;
constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void)
{
this.users = new Map<string, PointInterface>();
this.groups = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
}
public join(userPosition: MessageUserPosition): void {
this.users.set(userPosition.userId, userPosition.position);
}
public updatePosition(userPosition: MessageUserPosition): void {
if(typeof userPosition.userId === 'undefined') {
throw new Error('unkown id');
}
//this.users.get(userPosition.userId).x;
// TODO: compute distance between peers.
// Is the user in a group?
// Is the user leaving the group? (is the user at more than max distance of each player)
// Should we split the group? (is each player reachable from the current player?)
// This is needed if
// A <==> B <==> C <===> D
// becomes A <==> B <=====> C <> D
// If C moves right, the distance between B and C is too great and we must form 2 groups
// If the user is in no group
// is there someone in a group close enough and with room in the group?
}
}