Website Summary Files Commits Branches @commit @diff @stats
path: root / scripts / services / socketService.js
diff options
context:
space:
mode:
Commit86df07a5daa6aa1545bdf17e4661af0cb80bc48b (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:43:06 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:43:06 +0200
Filesf3bb4a842fc199b48bcdc4c9de6dbd2770d02c55 /scripts/services/socketService.js
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.tar
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.tar.gz
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.zip
Parentd95945a4c1f567fd479d68c3a6ecff02320f6d5c (diff)
Major Game Mechanics implementation
1 files changed, 79 insertions, 17 deletions
-rw-r--r--scripts/services/socketService.js96
diff --git a/scripts/services/socketService.js b/scripts/services/socketService.js
index 4c328d7..89bbd45 100644
--- a/scripts/services/socketService.js
+++ b/scripts/services/socketService.js
@@ -2,22 +2,83 @@ var app = app || {};
class SocketService {
constructor() {
- this.connection = null;
- this.init();
+ this.homeConnection = null;
+ this.gameConnection = null;
}
- async initConnection() {
- this.connection = await new signalR.HubConnectionBuilder()
- .withUrl("https://pusg-api.herokuapp.com/home")
+ async initGameConnection() {
+ this.gameConnection = await new signalR.HubConnectionBuilder()
+ .withUrl(`${app.config.SERVICE_URL}/game`)
.build();
}
- initReceivers() {
- this.connection.on('OnConnected', ([message]) => {
+ disconnectGame() {
+ this.gameConnection.stop();
+ this.gameConnection = null;
+ }
+
+ initGameReceivers() {
+ this.gameConnection.on('OnConnected', ([message]) => {
+ console.log(message);
+ });
+
+ this.gameConnection.on('StartGame', ([color]) => {
+ window.location.hash = '#/play';
+ app.config.USER.COLOR = color;
+ });
+
+ this.gameConnection.on('OtherUp', () => {
+ app.eventService.triggerEvent('OtherUp');
+ });
+
+ this.gameConnection.on('OtherRight', () => {
+ app.eventService.triggerEvent('OtherRight');
+ });
+
+ this.gameConnection.on('OtherDown', () => {
+ app.eventService.triggerEvent('OtherDown');
+ });
+
+ this.gameConnection.on('OtherLeft', () => {
+ app.eventService.triggerEvent('OtherLeft');
+ });
+
+ this.gameConnection.on('OtherRotateLeft', () => {
+ app.eventService.triggerEvent('OtherRotateLeft');
+ });
+
+ this.gameConnection.on('OtherRotateRight', () => {
+ app.eventService.triggerEvent('OtherRotateRight');
+ });
+
+ this.gameConnection.on('GameState', (state) => {
+ app.eventService.triggerEvent('GameState', state);
+ });
+
+ this.gameConnection.on('GameEnd', (result) => {
+ app.eventService.triggerEvent('GameEnd', result);
+ });
+ }
+
+ initGame() {
+ return this.initGameConnection().then(() => {
+ this.initGameReceivers();
+ return this.start(this.gameConnection);
+ });
+ }
+
+ async initHomeConnection() {
+ this.homeConnection = await new signalR.HubConnectionBuilder()
+ .withUrl(`${app.config.SERVICE_URL}/home`)
+ .build();
+ }
+
+ initHomeReceivers() {
+ this.homeConnection.on('OnConnected', ([message]) => {
console.log(message);
});
- this.connection.on('ReceiveMessage', ([user, message]) => {
+ this.homeConnection.on('ReceiveMessage', ([user, message]) => {
const messageNode = app.htmlService
.getElementFromTemplate('message', {
user: user,
@@ -29,27 +90,28 @@ class SocketService {
});
}
- init() {
- this.initConnection().then(() => {
- this.initReceivers();
- this.start();
+ initHome() {
+ return this.initHomeConnection().then(() => {
+ this.initHomeReceivers();
+ return this.start(this.homeConnection);
});
}
- start() {
+ start(connection) {
const starting = async () => {
try {
- await this.connection.start();
+ await connection.start();
} catch (e) {
console.log(e);
}
};
- starting().then(() => {});
+ return starting().then(() => {});
}
- send(method, ...args) {
- this.connection.invoke(method, ...args);
+ send(location, method, ...args) {
+ if(location === 'home') this.homeConnection.invoke(method, ...args);
+ if(location === 'game') this.gameConnection.invoke(method, ...args);
}
}