Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions backend/src/app.controller.spec.ts

This file was deleted.

Empty file removed backend/src/app.gateway.ts
Empty file.
22 changes: 14 additions & 8 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RoomInfo } from './game/GameResources';
import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
Expand All @@ -17,14 +15,19 @@ import { User } from './users/users.entity';
import { GameHistoryModule } from './gamehistory/gamehistory.module';
import { WinlossdrawModule } from './gamehistory/winlossdraw/winlossdraw.module';
import { RankingModule } from './users/ranking/ranking.module';
import { RoomController } from './game/room.controller';
import { RoomService } from './game/room.service';
import { PrivateController } from './game_invitation/private.controller';
import { StatusGateway } from './status/status.gateway';
import { Status } from './status/Status';
import { StatusController } from './status/status.controller';
import { GameModule } from './game/game.module';
import { GameInvitationModule } from './game_invitation/game_invitation.module';
import { StatusModule } from './status/status.module';

@Module({
controllers: [AppController],
controllers: [RoomController, PrivateController],
providers: [
AppService,
GameGateway,
RoomInfo,
RankingService
],
imports: [
TypeOrmModule.forRootAsync({
Expand All @@ -51,10 +54,13 @@ import { RankingModule } from './users/ranking/ranking.module';
FriendsModule,
FriendRequestModule,
NotificationModule,
GameModule,
GameInvitationModule,
StatusModule,
ConfigModule.forRoot({
isGlobal: true
}),
TypeOrmModule.forFeature([User])
]
})
})
export class AppModule {}
51 changes: 0 additions & 51 deletions backend/src/app.service.ts

This file was deleted.

24 changes: 19 additions & 5 deletions backend/src/game/GameEasy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Set1} from './GameParameters';
import {Par, Coordinate} from './GameClasses';
import { RoomInfo } from './GameResources';
import { Room } from 'src/app.classes';
import { Room } from './room.classes';

export class GameVar {
paddleA:number;
Expand Down Expand Up @@ -47,7 +46,7 @@ export class Game {
protected playerBReady = false;
protected gameScore:string = null;

constructor(roomnbr:number, protected readonly rooms:RoomInfo, gamePar:Par=new Par(Set1)){
constructor(roomnbr:number, protected readonly room:Room, gamePar:Par=new Par(Set1)){
this.roomnbr = roomnbr;
this.gameStat = 0;
this.GP = gamePar
Expand Down Expand Up @@ -125,6 +124,11 @@ export class Game {
console.log("Game started");
this.interval = setInterval(() =>{
const win = this.moveBall();
if (this.gameStat === 3){
clearInterval(this.interval);
this.room.setScore(this.gameScore);
return ;
}
if (win < 0){
if (win === -1){
console.log('playerB win');
Expand All @@ -135,8 +139,8 @@ export class Game {
}
this.gameStat = 3;
clearInterval(this.interval);
const room:Room = this.rooms.getRooms()[this.rooms.findRoom(this.roomnbr)];
room.setScore(this.gameScore);
this.room.setScore(this.gameScore);
return ;
}
}, 50);
}
Expand Down Expand Up @@ -181,5 +185,15 @@ export class Game {
public getScore(){
return (this.gameScore);
}

public killGame(userID:string){
this.gameStat = 3;
if (this.room.playerA === userID){
this.gameScore = 'B';
}
else{
this.gameScore = 'A';
}
}
}

17 changes: 11 additions & 6 deletions backend/src/game/GameHard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Set2} from './GameParameters';
import {Par, Coordinate} from './GameClasses';
import { RoomInfo } from './GameResources';
import { Room } from 'src/app.classes';
import { Room } from './room.classes';
import { GameVar, Game, Vector, randomDirection} from './GameEasy'
import { clearInterval } from 'timers';

export class Block {
upperLeft:Coordinate;
Expand All @@ -21,8 +21,8 @@ export class GameHard extends Game {
protected acceleration:number;
protected blocks:Block[] = [];

constructor(roomnbr:number, protected rooms:RoomInfo, gamePar = new Par(Set2)){
super(roomnbr, rooms, gamePar);
constructor(roomnbr:number, protected room:Room, gamePar = new Par(Set2)){
super(roomnbr, room, gamePar);
this.acceleration = 1;
this.generateBlocks(3, 2, 40, 10);
}
Expand Down Expand Up @@ -66,6 +66,11 @@ export class GameHard extends Game {
this.interval = setInterval(() =>{
const win = this.moveBallHard();
if (win < 0){
if (this.gameStat === 3){
clearInterval(this.interval);
this.room.setScore(this.gameScore);
return ;
}
if (win === -1){
console.log('playerB win');
this.gameScore = "A";
Expand All @@ -75,8 +80,8 @@ export class GameHard extends Game {
}
this.gameStat = 3;
clearInterval(this.interval);
const room:Room = this.rooms.getRooms()[this.rooms.findRoom(this.roomnbr)];
room.setScore(this.gameScore);
this.room.setScore(this.gameScore);
return ;
}
}, 50);
}
Expand Down
64 changes: 64 additions & 0 deletions backend/src/game/GameHeartbeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Socket } from 'socket.io';
import { Server } from 'http';
import { Injectable } from '@nestjs/common';
import { Room } from 'src/game/room.classes';
import { RoomInfo } from './GameResources';
import { PrivateRoom } from 'src/game/GamePrivate';
import { Status } from 'src/status/Status';

//the Hearbeat function test if someone has bad connection
//in case of bad connection(no heartbeat for longer than 6 second, disconnect client)
//in case someone is disconnected, count that as a loose
//disconnect the client if too long timeout
//this is also shared resource
//two functions:
//first: manage the case someone has bad connection, if no heartbeat for longer than a period, loose the game
//second: manage the socket connection, if too long no connection, disconnect the socket

const INACTIVE_TIMEOUT = 6100;

@Injectable()
export class GameHeartbeat {

private userLastActivity: Map<string, number> = new Map(); //last heartbeat for the user
private connections: Map<string, [string, Socket, number]> = new Map(); //sockets and userID's, sockets, lastActivity

handleHeartBeat(client:Socket, userID:string){
const time:number = Date.now();
this.connections.set(client.id, [userID, client, time]);
this.userLastActivity.set(userID, time);
this.status.setStatusInGame(userID);
//send pulse to frontend
client.emit('GamePulse');
}


//each connection should be managed separately. send pulse in interval
//socket has no connection, then kill the socket
//all connections of one user should be managed collectively.
//user has no connection then kill the game
constructor(private readonly rooms: RoomInfo, private readonly status: Status){
setInterval(()=>{
//collective mangement of user
const time = Date.now();
for (let[userID, lastActive] of this.userLastActivity.entries()){
if (time - lastActive > INACTIVE_TIMEOUT) {
const room: Room | PrivateRoom = this.rooms.findPlayerInAllRooms(userID);
if (room){
if (room.game.length > 0){
room.game[0].killGame(userID);
this.status.removeStatusInGame(userID);
}
}
this.userLastActivity.delete(userID);
}
}
for (let[clientID, [userID, client, lastActive]] of this.connections.entries()){
if (time - lastActive > INACTIVE_TIMEOUT){
client.disconnect();
this.connections.delete(clientID);
}
}
}, 3000);
}
}
29 changes: 29 additions & 0 deletions backend/src/game/GamePrivate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { Room } from 'src/game/room.classes'


//the private game should exist at least 1 hour from the moment it is made.
//after that, handle error, give back message that the room timed out
//the room shouldn't be closed if none is in the room

export class PrivateRoom extends Room {
private creationTime:number;
private playerAJoined: boolean = false;

constructor(userID:string, level:number){
super (userID, level);
this.creationTime = Date.now();
}

public getCreationTime():number{
return this.creationTime;
}

public playerAJoin(): void {
this.playerAJoined === true;
}

public playerALeave():void {
this.playerAJoined === false;
}
}
Loading