diff options
4 files changed, 82 insertions, 35 deletions
| -rw-r--r-- | scripts/app.js | 27 | ||||
| -rw-r--r-- | scripts/core/home.js | 49 | ||||
| -rw-r--r-- | scripts/services/socketService.js | 33 | ||||
| -rw-r--r-- | scripts/services/userService.js | 8 |
diff --git a/scripts/app.js b/scripts/app.js index 93b999a..a8f8e70 100644 --- a/scripts/app.js +++ b/scripts/app.js @@ -1,17 +1,18 @@ var app = app || {}; -const gameEvents = (() => { - app.eventService.addEventHandler('GameEnd', (result) => { - if(result) { - const message = result.result; - const user = 'PUSG'; +const globalEvents = (() => { + app.eventService.addEventHandler('GameStart', (color) => { + app.socketService.disconnectHome(); + app.config.USER.COLOR = color; + window.location.hash = '#/play'; + }); - app.socketService.send('home', 'SendMessage', user, message); + app.eventService.addEventHandler('GameEnd', (result) => { + if(app.Game.isRunning) { + app.Game.reset(); + app.socketService.disconnectGame(); + window.location.hash = '#/home'; } - - app.Game.reset(); - app.socketService.disconnectGame(); - window.location.hash = '#/home'; }); })(); @@ -26,8 +27,10 @@ const handleRouting = () => { app.htmlService.clearElement('#app'); app.htmlService.attachElement(compliedTemplate, '#app'); - app.socketService.initHome(); - app.Home.init(); + app.socketService.initHome().then(() => { + app.userService.refreshLogin(); + app.Home.init(); + }); }); break; diff --git a/scripts/core/home.js b/scripts/core/home.js index b9c4ea8..df46b87 100644 --- a/scripts/core/home.js +++ b/scripts/core/home.js @@ -34,6 +34,7 @@ const supportedCommands = { class Home { constructor() { this.commands = supportedCommands; + this.cachedMessages = []; } isCommand(command) { @@ -44,6 +45,44 @@ class Home { this.commands[command].invoke(); } + initEvents() { + app.eventService.addEventHandler('LoggedIn', () => { + app.htmlService.clearElement('.history-messages'); + }); + + app.eventService.addEventHandler('OnLoggedIn', (users) => { + users.forEach(user => { + if(!app.htmlService.getElement(`#${user}`)) { + const userNode = app.htmlService + .getElementFromTemplate('user', { + user: user, + }); + + app.htmlService.attachElement(app.htmlService.createElement(userNode), '.participants'); + } + }); + }); + + app.eventService.addEventHandler('OnLoggedOut', (users) => { + users.forEach(user => { + app.htmlService.getElement(`#${user}`).remove(); + }); + }); + + app.eventService.addEventHandler('OnReceiveMessage', ([user, message]) => { + const messageNode = app.htmlService + .getElementFromTemplate('message', { + user: user, + message: message + }); + + this.cachedMessages.push(messageNode); + + app.htmlService.attachElement(app.htmlService.createElement(messageNode) + , '.history-messages'); + }); + } + init() { app.htmlService.addEventHandler('#main-chat-input', 'keyup', (e) => { if (e.key === 'Enter' || e.keyCode === 13) { @@ -64,6 +103,16 @@ class Home { } } }); + + if(app.userService.isLoggedIn()) { + app.htmlService.clearElement('.history-messages'); + this.cachedMessages.forEach(cachedMessage => { + app.htmlService.attachElement(app.htmlService.createElement(cachedMessage) + , '.history-messages'); + }); + } + + this.initEvents(); } } diff --git a/scripts/services/socketService.js b/scripts/services/socketService.js index 186ac3b..b2153f2 100644 --- a/scripts/services/socketService.js +++ b/scripts/services/socketService.js @@ -23,8 +23,7 @@ class SocketService { }); this.gameConnection.on('StartGame', ([color]) => { - window.location.hash = '#/play'; - app.config.USER.COLOR = color; + app.eventService.triggerEvent('GameStart', color) }); this.gameConnection.on('OtherUp', () => { @@ -73,39 +72,26 @@ class SocketService { .build(); } + disconnectHome() { + this.homeConnection.stop(); + this.homeConnection = null; + } + initHomeReceivers() { this.homeConnection.on('OnConnected', ([message]) => { console.log(message); }); this.homeConnection.on('OnLoggedIn', (users) => { - users.forEach(user => { - if(!app.htmlService.getElement(`#${user}`)) { - const userNode = app.htmlService - .getElementFromTemplate('user', { - user: user, - }); - - app.htmlService.attachElement(app.htmlService.createElement(userNode), '.participants'); - } - }); + app.eventService.triggerEvent('OnLoggedIn', users); }); this.homeConnection.on('OnLoggedOut', (users) => { - users.forEach(user => { - app.htmlService.getElement(`#${user}`).remove(); - }); + app.eventService.triggerEvent('OnLoggedOut', users); }); this.homeConnection.on('ReceiveMessage', ([user, message]) => { - const messageNode = app.htmlService - .getElementFromTemplate('message', { - user: user, - message: message - }); - - app.htmlService.attachElement(app.htmlService.createElement(messageNode) - , '.history-messages'); + app.eventService.triggerEvent('OnReceiveMessage', [user, message]); }); } @@ -130,6 +116,7 @@ class SocketService { send(location, method, ...args) { if(location === 'home') { + console.log(this.homeConnection); this.homeConnection?.invoke(method, ...args); } if(location === 'game') { diff --git a/scripts/services/userService.js b/scripts/services/userService.js index 03bb313..512de48 100644 --- a/scripts/services/userService.js +++ b/scripts/services/userService.js @@ -12,10 +12,18 @@ class UserService { if (username) { app.socketService.send('home', 'Login', username); this.user = username; + app.eventService.triggerEvent('LoggedIn'); } } } + refreshLogin() { + if(this.user) { + app.socketService.send('home', 'Login', this.user); + app.eventService.triggerEvent('LoggedIn'); + } + } + isLoggedIn() { return this.user; } |