From da28ffcf890beec8c6429bef97d17f178297cf1d Mon Sep 17 00:00:00 2001 From: Jordan Pawlett Date: Sat, 9 May 2020 19:22:09 +0100 Subject: [PATCH] remove un needed error logging --- services/cards-service/seeds/delete-all.js | 4 +- services/cards-service/seeds/seed.js | 52 ++++++++++--------- services/games-service/src/games-service.ts | 7 ++- services/rooms-service/src/rooms-service.ts | 2 +- .../src/DefaultNamespace.ts | 16 +++--- .../src/GameNamespace.ts | 5 +- 6 files changed, 47 insertions(+), 39 deletions(-) diff --git a/services/cards-service/seeds/delete-all.js b/services/cards-service/seeds/delete-all.js index 843ee231..099d21fa 100644 --- a/services/cards-service/seeds/delete-all.js +++ b/services/cards-service/seeds/delete-all.js @@ -1,6 +1,6 @@ const axios = require('axios').default; const getAllIds = () => { - return axios.post('http://localhost/admin/decks/search') + return axios.post('http://localhost:8000/admin/decks/search') .then(res => res.data.map(d => d._id)) .catch(err => console.log(err.message)) } @@ -8,7 +8,7 @@ const getAllIds = () => { const start = async () => { const ids = await getAllIds(); console.log(ids); - Promise.all(ids.map(id => axios.delete(`http://localhost/admin/decks/${id}`).catch(err => console.log(err.message)))) + Promise.all(ids.map(id => axios.delete(`http://localhost:8000/admin/decks/${id}`).catch(err => console.log(err.message)))) .then(() => { console.log('finished deleting all'); }) diff --git a/services/cards-service/seeds/seed.js b/services/cards-service/seeds/seed.js index 084daddc..e9111c2a 100644 --- a/services/cards-service/seeds/seed.js +++ b/services/cards-service/seeds/seed.js @@ -24,7 +24,7 @@ const postCard = (data, i) => { } const postDeck = (data) => { - return axios.post('http://localhost/admin/decks', data) + return axios.post('http://localhost:8000/admin/decks', data) .then(res => res.data) .catch(err => { console.log(err.message); @@ -40,33 +40,35 @@ const run = async () => { const populatedWhiteCards = await populateDeck(whiteCards); const populatedBlackCards = await populateDeck(blackCards); - const promises = Object.values(decks) - .map(deck => { - const populatedDeck = deck; - // - const blackCardIds = []; - populatedDeck.black.map(index => { - if (populatedBlackCards[index]) { - blackCardIds.push(populatedBlackCards[index]._id); - } - }); - delete populatedDeck.black; - populatedDeck.blackCards = blackCardIds; - // - const whiteCardIds = []; - populatedDeck.white.map(index => { - if (populatedWhiteCards[index]) { - whiteCardIds.push(populatedWhiteCards[index]._id); - } - }); - delete populatedDeck.white - populatedDeck.whiteCards = whiteCardIds; + for (const deck of Object.values(decks)) { + const populatedDeck = deck; + // + const blackCardIds = []; + populatedDeck.black.map(index => { + if (populatedBlackCards[index]) { + blackCardIds.push(populatedBlackCards[index]._id); + } + }); + delete populatedDeck.black; + populatedDeck.blackCards = blackCardIds; + // + const whiteCardIds = []; + populatedDeck.white.map(index => { + if (populatedWhiteCards[index]) { + whiteCardIds.push(populatedWhiteCards[index]._id); + } + }); + delete populatedDeck.white + populatedDeck.whiteCards = whiteCardIds; - return postDeck(populatedDeck).catch(err => console.log(err.message)); - }) + try { + await postDeck(populatedDeck) + } catch (e) { + console.log(e.message); + } + } - await Promise.all(promises); console.log('done'); } diff --git a/services/games-service/src/games-service.ts b/services/games-service/src/games-service.ts index 6b54cec1..c23947ea 100644 --- a/services/games-service/src/games-service.ts +++ b/services/games-service/src/games-service.ts @@ -152,8 +152,11 @@ export default class GameService extends Service { } private async handleRoomRemoved(ctx: Context<{ _id: string }>) { - const game: any = await this.getGameMatchingRoom(ctx, ctx.params._id); - return this.gameService.destroyGame(game._id); + return this.getGameMatchingRoom(ctx, ctx.params._id) + .then((game) => this.gameService.destroyGame(game._id)) + .catch(() => { + // game must not have started yet. + }); } /** diff --git a/services/rooms-service/src/rooms-service.ts b/services/rooms-service/src/rooms-service.ts index 7381ddc7..5dba1d6e 100644 --- a/services/rooms-service/src/rooms-service.ts +++ b/services/rooms-service/src/rooms-service.ts @@ -314,7 +314,7 @@ export default class RoomsService extends Service { return Promise.resolve(room); } else { // user must be in another room. - throw forbidden('A user is only allowed to join one room at a time'); + throw forbidden('You are already in a room.'); } } diff --git a/services/websocket-gateway-service/src/DefaultNamespace.ts b/services/websocket-gateway-service/src/DefaultNamespace.ts index 6191ebe6..e3c4708c 100644 --- a/services/websocket-gateway-service/src/DefaultNamespace.ts +++ b/services/websocket-gateway-service/src/DefaultNamespace.ts @@ -26,13 +26,15 @@ export default class DefaultNamespace extends BaseNamespace { // disconnect time added to client. const timeout = 60 * 1000; setTimeout(async () => { - const user = await this.broker.call('clients.get', { id: _id }) as any; - const afterTimeoutTime = new Date().getTime(); - // If the user hasn't changed socketid or reconnected. Fire a disconnect event. - // tslint:disable-next-line: max-line-length - if (user.socket === client.id && user.disconnectedAt && afterTimeoutTime - user.disconnectedAt > (timeout - 5000)) { - this.broker.emit('websocket-gateway.client.disconnected', { _id }); - } + this.broker.call('clients.get', { id: _id }) + .then((user: any) => { + const afterTimeoutTime = new Date().getTime(); + // If the user hasn't changed socketid or reconnected. Fire a disconnect event. + // tslint:disable-next-line: max-line-length + if (user.socket === client.id && user.disconnectedAt && afterTimeoutTime - user.disconnectedAt > (timeout - 5000)) { + this.broker.emit('websocket-gateway.client.disconnected', { _id }); + } + }); }, timeout); }) // If error, client must have logged out. diff --git a/services/websocket-gateway-service/src/GameNamespace.ts b/services/websocket-gateway-service/src/GameNamespace.ts index 9cec9464..6bd32360 100644 --- a/services/websocket-gateway-service/src/GameNamespace.ts +++ b/services/websocket-gateway-service/src/GameNamespace.ts @@ -15,14 +15,15 @@ export default class GameNamespace extends BaseNamespace { } protected async onClientConnect(client: CustomSocket) { - const user: any = await this.broker.call('clients.get', { id: client.user._id }); - return this.joinRoom(client.id, user.roomId) + return this.broker.call('clients.get', { id: client.user._id }) + .then((user: any) => this.joinRoom(client.id, user.roomId)) .then((res) => { super.onClientConnect(client); this.logger.info(res); return null; }) .catch(err => { + this.logger.error(err); // Disconnect the client. Failed to add it to the room. this.onDisconnect(client); });