Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new ingame status #223

Merged
merged 4 commits into from
Oct 20, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a [routerLink]="'/profile/' + user.nickname">
<img [ngClass]="getLiteralStatus(user.status)" [src]="user.picture || 'assets/img/avatar-placeholder.jpg'" alt="avatar">
<img [ngClass]="getLiteralStatus()" [src]="user.picture || 'assets/img/avatar-placeholder.jpg'" alt="avatar">
<span>{{user.nickname}}<br/>ELO: {{user.elo}}</span>
</a>
<button (click)="doAction()">
<mat-icon [fontIcon]="this.friendService.isTargetBlocked(this.user.id) ? 'person_add' : 'block'"></mat-icon>
<mat-icon [fontIcon]="this.friendService.isTargetBlocked(this.user.id) ? 'person_add' : 'block'"></mat-icon>
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,30 @@ a {
&.connected {
border-color: green
}
&.ingame {
border-color: purple
}
&.away {
border-color: orange
}
}
}
span {
&:hover {
text-decoration: underline;
}

}
}



button {
aspect-ratio: 1;
background-color: red;
color: white;
border-radius: 0.7em;
aspect-ratio: 1;
border: none;
background-color: red;
color: white;
border-radius: 0.7em;
aspect-ratio: 1;
border: none;
cursor: pointer;
position: absolute;
bottom: 1em;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ export class UserTooltipComponent {
}
}

getLiteralStatus(status: number | undefined)
getLiteralStatus()
{
var color;
switch (this.user.status) {
case Status.AWAY: {
return 'away';
}
case Status.CONNECTED: {
return 'connected';
}
case Status.DISCONNECTED: {
return 'disconnected';
}
case Status.INGAME: {
return 'ingame';
}
case Status.AWAY: {
return 'away';
}
default:
return "";
}
Expand Down
2 changes: 2 additions & 0 deletions src/angularjs/src/app/interfaces/user/user.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export enum Status {
DISCONNECTED,
CONNECTED,
INGAME,
// TODO
AWAY,
}

Expand Down
12 changes: 8 additions & 4 deletions src/angularjs/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ export class UserService{
// tmp = "🟢 ";
// break ;
// }
case Status.AWAY: {
tmp = "🟠 ";
break;
}
case Status.CONNECTED: {
tmp = "🟢 ";
break;
Expand All @@ -124,6 +120,14 @@ export class UserService{
tmp = "⚫ "
break;
}
case Status.INGAME: {
tmp = "🟣 "
break;
}
case Status.AWAY: {
tmp = "🟠 ";
break;
}
}
tmp += user.ftLogin + " ";
if (user.nickname)
Expand Down
3 changes: 2 additions & 1 deletion src/nestjs/src/modules/database/user/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { UserMetricsEntity } from "../metrics/entity";
export enum Status {
DISCONNECTED,
CONNECTED,
AWAY,
INGAME,
// AWAY,
}

@Entity()
Expand Down
7 changes: 4 additions & 3 deletions src/nestjs/src/modules/database/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ export class DBUserService {

async getOnlineUsers(): Promise<UserEntity[]> {
const users = await this.userRepo.find({
where: {
status: Status.CONNECTED,
},
where: [
{ status: Status.CONNECTED },
{ status: Status.INGAME },
]
});
return this.sanitize.Users(users);
}
Expand Down
49 changes: 40 additions & 9 deletions src/nestjs/src/websocket/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
GameOptionI,
PowerUpI,
} from "./game.interface";
import { UserEntity } from "../../modules/database/user/entity";
import { Status, UserEntity } from "../../modules/database/user/entity";
import { DBGameInfoService } from "../../modules/database/game/gameInfo/service";
import { GameInfoEntity } from "../../modules/database/game/gameInfo/entity";
import { DBPlayerScoreService } from "../../modules/database/game/playerScore/service";
Expand Down Expand Up @@ -59,7 +59,7 @@ export class WSGameService {
};
const room = this.rooms.get(room_id);
if (room) {
this.addPlayerToRoom(player, room_id, socket);
this.addPlayerToRoom(server, player, room_id, socket);
if (this.isFullRoom(room)) this.startGame(server, room_id);
else socket.emit("gameWaiting", room_id);
}
Expand All @@ -74,21 +74,26 @@ export class WSGameService {
),
socket: socket.id,
};
const room_id = this.gameSearchOpponent(player, game_opt, socket);
const room_id = this.gameSearchOpponent(server, player, game_opt, socket);
const room = this.rooms.get(room_id);
if (this.isFullRoom(room) && room.status !== LobbyStatus.STARTED)
this.startGame(server, room_id);
else if (room.status === LobbyStatus.LOBBY)
socket.emit("gameWaiting", room_id);
}

gameSearchOpponent(player: PlayerSockI, game_opt: any, socket: Socket) {
gameSearchOpponent(
server: Server,
player: PlayerSockI,
game_opt: any,
socket: Socket
) {
const room_id = this.gameSearchRoom(player, game_opt);

if (room_id !== "" && !game_opt.is_private) {
this.addPlayerToRoom(player, room_id, socket);
this.addPlayerToRoom(server, player, room_id, socket);
} else {
return this.createRoom(player, game_opt, socket);
return this.createRoom(server, player, game_opt, socket);
}
return room_id;
}
Expand Down Expand Up @@ -125,13 +130,19 @@ export class WSGameService {
return nb_player >= max_player;
}

addPlayerToRoom(player_sock: PlayerSockI, room_id: string, socket: Socket) {
addPlayerToRoom(
server: Server,
player_sock: PlayerSockI,
room_id: string,
socket: Socket
) {
const current_lobby: LobbyI = this.rooms.get(room_id);
for (const player_id in current_lobby.players) {
if (
current_lobby.players[player_id].user.id === player_sock.user.id
) {
current_lobby.players[player_id].socket = player_sock.socket;
this.setStatusUserInRoom(server, current_lobby, Status.INGAME);
socket.emit(
"gameReconnect",
current_lobby.state,
Expand All @@ -155,7 +166,12 @@ export class WSGameService {
current_lobby.state.players.push(player);
}

createRoom(player: PlayerSockI, game_opt: GameOptionI, socket: Socket) {
createRoom(
server: Server,
player: PlayerSockI,
game_opt: GameOptionI,
socket: Socket
) {
let room_id = `${this.getUidPart(8)}-`;

for (let i = 0; i < 4; i++) room_id += `${this.getUidPart(4)}-`;
Expand Down Expand Up @@ -194,7 +210,7 @@ export class WSGameService {
},
players: [],
} as LobbyI);
this.addPlayerToRoom(player, room_id, socket);
this.addPlayerToRoom(server, player, room_id, socket);
return room_id;
}

Expand Down Expand Up @@ -268,9 +284,23 @@ export class WSGameService {
else socket.emit("gameWaiting");
}

async setStatusUserInRoom(server: Server, room: LobbyI, status: Status)
{
for (var i in room.players)
{
await this.userService.setStatus(room.players[i].user.id, status);
const friends = await this.userService.getAllFriend(room.players[i].user.id);
this.wsSocket.sendToUsersInfo(server, friends, "getNewStatusFriend", {
user_id: room.players[i].user.id,
status: status,
});
}
}

async startGame(server: Server, room_id: string) {
const room = this.rooms.get(room_id);

this.setStatusUserInRoom(server, room, Status.INGAME);
if (!this.canStartGame(room)) return;

try {
Expand Down Expand Up @@ -315,6 +345,7 @@ export class WSGameService {
* it will be handled by gameJoin() which wont be able to find the room
* and thus will do nothing
*/
this.setStatusUserInRoom(server, room, Status.CONNECTED);
console.log("game ended");
}

Expand Down
5 changes: 4 additions & 1 deletion src/nestjs/src/websocket/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { BrcyptWrap } from "../addons/bcrypt.wrapper";


@Module({
imports: [AuthModule, forwardRef(() => DBModule)],
imports: [
AuthModule,
forwardRef(() => DBModule),
],
providers: [
Sanitize,
UserService,
Expand Down
2 changes: 1 addition & 1 deletion src/nestjs/src/websocket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from "@nestjs/common";
import { Server } from "socket.io";
import { ChatRoomEntity } from "../modules/database/chatRoom/entity";
import { UserEntity } from "../modules/database/user/entity";
import { GameStateI, LobbyI } from "./game/game.interface";
import { LobbyI } from "./game/game.interface";

@Injectable()
export class WSSocket {
Expand Down