Skip to content

Commit

Permalink
Finish notifications (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadie-ess authored Nov 13, 2023
2 parents 2855d7e + 92cd4f1 commit 66fe1a9
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 143 deletions.
5 changes: 2 additions & 3 deletions backend/code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions backend/code/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Res,
HttpCode,
HttpStatus,
Param
Param,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthDto } from './dto/auth.dto';
Expand Down Expand Up @@ -110,12 +110,7 @@ export class AuthController {
}

@Get('validatToken/:token')
async validatToken(
@Param('token') token: string,
@Res({ passthrough: true }) res: Response,
) {
async validatToken(@Param('token') token: string) {
return this.authService.checkToken(token);
}
}


11 changes: 4 additions & 7 deletions backend/code/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,14 @@ export class AuthService {
return { isValid };
}


async checkToken(tfaToken: string)
{
async checkToken(tfaToken: string) {
const user = await this.prisma.user.findUnique({
where: { tfaToken },
select: {
tfaToken: true
tfaToken: true,
},
});
if(!user)
return false;
return true;
if (!user) return false;
return true;
}
}
5 changes: 2 additions & 3 deletions backend/code/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ export class GameService {
handleGameStartEvent(client: Socket) {
const index = this.waitingPlayers.find((player) => {
return player.data.user.sub === client.data.user.sub;
}
);
});
if (index) {
console.log('client already in the queue');
return;
}

this.waitingPlayers.push(client);
console.log('client subscribed to the queue');
}
Expand Down
17 changes: 9 additions & 8 deletions backend/code/src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { Server, Socket } from 'socket.io';

export class Game {
constructor(private readonly eventEmitter: EventEmitter2 , private readonly server: Server) {}
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly server: Server,
) {}
private async loop() {

console.log('loop');
await this.sleep(5000);

this.loop();
}


private async sleepCounter(){
private async sleepCounter() {
let timer = 3000;

for (let i = 0; i < 4; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
this.server.emit("timer", timer);
await new Promise((resolve) => setTimeout(resolve, 1000));
this.server.emit('timer', timer);
timer -= 1000;
}
}
async start(ngameid: string) {
console.log('game started', ngameid);
this.gameid = ngameid;
await this.sleepCounter()
await this.sleepCounter();
this.loop();
}

setplayerScokets(p1socket: Socket, p2socket: Socket ) {
setplayerScokets(p1socket: Socket, p2socket: Socket) {
this.p1socket = p1socket;
this.p2socket = p2socket;
this.p1socket.on('up', (data) => {
Expand Down
20 changes: 11 additions & 9 deletions backend/code/src/gateways/gateways.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { PrismaService } from 'src/prisma/prisma.service';
import { Game } from 'src/game/game';
import { $Enums, Notification } from '@prisma/client';
import { Client } from 'socket.io/dist/client';

@WebSocketGateway(3004, {
cors: {
Expand Down Expand Up @@ -165,7 +164,10 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
});

roomMembers = roomMembers.filter(
(member) => member.userId !== message.authorId && member.userId !== notif.actorId && !member.is_banned,
(member) =>
member.userId !== message.authorId &&
member.userId !== notif.actorId &&
!member.is_banned,
);

const clientsSockets = await this.server
Expand Down Expand Up @@ -211,7 +213,7 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
clients.forEach((client: any) => {
client.join(game_channel);
});
const new_game = new Game(this.eventEmitter , this.server);
const new_game = new Game(this.eventEmitter, this.server);
new_game.setplayerScokets(clients[0], clients[1]);
new_game.start(game_channel);
this.games_map.set(game_channel, new_game);
Expand All @@ -227,23 +229,21 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
}

@SubscribeMessage('joinRoom')
async handleJoinRoomEvent(client: Socket, data: any) {
const userId = client.data.user.sub;
async handleJoinRoomEvent(client: Socket, data: any) {
const userId = client.data.user.sub;
const member = await this.prisma.roomMember.findFirst({
where: {
userId: data.memberId,
roomId: data.roomId,
},
});
if (member && !member.is_banned && userId === data.memberId) {
client.join(`Room:${data.roomId}`);
client.join(`Room:${data.roomId}`);
}

}

@SubscribeMessage('PingOnline')
async handlePingOnlineEvent(client: Socket, data: any) {
const userId = client.data.user.sub;
const friendId = data.friendId;
if (this.server.sockets.adapter.rooms.get(`User:${friendId}`)?.size) {
client.emit('friendOnline', friendId);
Expand Down Expand Up @@ -283,7 +283,9 @@ export class Gateways implements OnGatewayConnection, OnGatewayDisconnect {
async hundleDeparture(
@MessageBody() data: { roomId: string; memberId: string; type: string },
) {
const clients = await this.server.in(`User:${data.memberId}`).fetchSockets();
const clients = await this.server
.in(`User:${data.memberId}`)
.fetchSockets();
const clientsToBan = clients.filter(
(client) => client.data.user.sub === data.memberId,
);
Expand Down
33 changes: 32 additions & 1 deletion backend/code/src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,41 @@ export class ProfileController {
@GetCurrentUser('userId') userId: string,
@Query() { offset, limit }: QueryOffsetDto,
) {
console.log('getNotifications');
return this.profileService.getNotifications(userId, offset, limit);
}

@Post('read-notification/:id')
@UseGuards(AtGuard)
readNotification(
@GetCurrentUser('userId') userId: string,
@Param('id') notificationId: string,
) {
return this.profileService.readNotification(userId, notificationId);
}

@Post('read-all-notifications')
@UseGuards(AtGuard)
readAllNotifications(@GetCurrentUser('userId') userId: string) {
return this.profileService.readAllNotifications(userId);
}

// read a bunch of messages

@Get('unread-messages')
@UseGuards(AtGuard)
getUnreadMessages(@GetCurrentUser('userId') userId: string) {
return this.profileService.getUnreadMessages(userId);
}

@Post('read-messages')
@UseGuards(AtGuard)
readMessages(
@GetCurrentUser('userId') userId: string,
@Body() { messagesIds }: { messagesIds: string[] },
) {
return this.profileService.readMessages(userId, messagesIds);
}

@Get(':id')
@ApiOkResponse({ type: ProfileDto })
@ApiParam({
Expand Down
Loading

0 comments on commit 66fe1a9

Please sign in to comment.