Skip to content
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
4 changes: 2 additions & 2 deletions src/app/components/state/state.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="container">
<span class="active-state">{{ stateText }}</span>
<button class="action-button" *ngIf="activeButton"> {{ buttonText }}</button>
<span class="timer" *ngIf="timerIsActive">{{ time }}</span>
<button class="action-button" *ngIf="activeButton" (click)="startGame()"> {{ buttonText }}</button>
<span class="timer" *ngIf="WebsocketService.timer.content">{{ WebsocketService.timer.content }}</span>
</div>
15 changes: 13 additions & 2 deletions src/app/components/state/state.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RoomIdService } from './../room-view/room-id.service';
import { UsersService } from './../../services/users.service';
import { WebsocketService } from './../../services/websocket.service';
import { Component, OnInit } from '@angular/core';

@Component({
Expand All @@ -13,12 +16,20 @@ export class StateComponent implements OnInit {
buttonText = 'Начать';
stateText = 'Ожидание игроков';

constructor() { }
constructor(public WebsocketService: WebsocketService, public roomIdService: RoomIdService, private usersService: UsersService) { }

ngOnInit(): void {
this.waitingText();
}

startGame() {
const message = {
content: '',
method: 'start',
id: this.roomIdService._id
}
this.WebsocketService.sendMessage(message);
this.activeButton = false;
}
waitingText() {
setInterval(() => {
switch(this.stateText) {
Expand Down
28 changes: 19 additions & 9 deletions src/app/pages/game-page/game-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ export class GamePageComponent implements OnInit, OnDestroy {
}

ngOnInit(): void {
if (sessionStorage.getItem('checkPass')===null) {
this.checkPass = 'true'
if (!localStorage.getItem('user')) {
this.router.navigateByUrl('/');
console.log('test')
}
else {
this.checkPass = sessionStorage.getItem('checkPass') as string
if (sessionStorage.getItem('checkPass')===null) {
this.checkPass = 'true'
}
else {
this.checkPass = sessionStorage.getItem('checkPass') as string
}
this.currentRoomId = this.roomIdService._id
this.WebsocketService.openWebSocket(this.currentRoomId);
const [, , id] = this.path.split('/');
const room = this.roomService.getRoom(id).subscribe(response => { this.initRoom(response) });
}
this.currentRoomId = this.roomIdService._id
this.WebsocketService.openWebSocket(this.currentRoomId);
const [, , id] = this.path.split('/');
const room = this.roomService.getRoom(id).subscribe(response => { this.initRoom(response) });

}

initRoom(roomInfo: Room) {
Expand All @@ -43,7 +50,10 @@ export class GamePageComponent implements OnInit, OnDestroy {
}

ngOnDestroy(): void {
this.WebsocketService.closeWebSocket();
sessionStorage.removeItem('checkPass')
if (localStorage.getItem('user')) {
this.WebsocketService.closeWebSocket();
sessionStorage.removeItem('checkPass')
}

}
}
20 changes: 15 additions & 5 deletions src/app/services/websocket.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UsersService } from './users.service';
import { Message } from '../models/message';
import { RoomIdService } from './../components/room-view/room-id.service';
import { Injectable } from '@angular/core';
Expand All @@ -19,13 +20,18 @@ export class WebsocketService {
content: '',
method: 'situation'
};
timer: Message = {
content: '',
method: 'timer'
}

constructor(private roomIdService: RoomIdService) { }
constructor(private roomIdService: RoomIdService, private usersService: UsersService) { }

public openWebSocket(roomId: string){
this.webSocket = new WebSocket('ws://localhost:5000');
this.webSocket.onopen = () => {
this.webSocket.send(JSON.stringify({id: roomId, method: 'connection'}));
this.webSocket.send(JSON.stringify({user: JSON.parse(localStorage.getItem('user')!).name, id: roomId, content: 'Зашёл в комнату', method: 'message'}));
};


Expand All @@ -34,24 +40,28 @@ export class WebsocketService {
if (MessageDto.method === 'message') {
this.chatMessages.push(MessageDto);
}
console.log(event)
if (MessageDto.method === 'situation') {
this.situationMessgae = MessageDto
console.log(this.situationMessgae)
}
if (MessageDto.method === 'timer') {
this.timer = MessageDto

}
console.log(event)
};

this.webSocket.onclose = (event) => {
this.chatMessages = []
console.log('Close: ', event);
};
};
}

public sendMessage(chatMessageDto: Message){
this.webSocket.send(JSON.stringify(chatMessageDto));
}

public closeWebSocket() {
this.webSocket.close();
console.log(this.usersService.user._id)
this.webSocket.close( 3002, `${this.usersService.user._id} ${this.roomIdService._id}` );
}
}