From: gparant Date: Sat, 4 Apr 2020 20:46:42 +0000 (+0200) Subject: Merge branch 'master' into Share-players-position-using-Socket.IO X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5bfedb04fdfcd354d6feb417f45b0d235e8512f6;p=libreadventure.git Merge branch 'master' into Share-players-position-using-Socket.IO # Conflicts: # back/src/Controller/IoSocketController.ts --- 5bfedb04fdfcd354d6feb417f45b0d235e8512f6 diff --cc back/src/Controller/IoSocketController.ts index 696f326,3907436..ad30539 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@@ -79,61 -77,4 +79,61 @@@ export class IoSocketController return new Error(err); } } + + /** permit to share user position + ** users position will send in event 'user-position' + ** The data sent is an array with information for each user : + [ + { + userId: , + roomId: , + position: { + x : , + y : + } + }, + ... + ] + **/ + seTimeOutInProgress : any = null; + shareUsersPosition(){ + if(!this.seTimeOutInProgress) { + clearTimeout(this.seTimeOutInProgress); + } + let clients = this.Io.clients(); + let socketsKey = Object.keys(this.Io.clients().sockets); + + //create mapping with all users in all rooms + let mapPositionUserByRoom = new Map(); + for(let i = 0; i < socketsKey.length; i++){ + let socket = clients.sockets[socketsKey[i]]; + if(!(socket as ExSocketInterface).position){ + continue; + } + let data = { + userId : (socket as ExSocketInterface).userId, + roomId : (socket as ExSocketInterface).roomId, + position : (socket as ExSocketInterface).position, + }; + let dataArray = []; + if(mapPositionUserByRoom.get(data.roomId)){ + dataArray = mapPositionUserByRoom.get(data.roomId); + dataArray.push(data); + }else{ + dataArray = [data]; + } + mapPositionUserByRoom.set(data.roomId, dataArray); + } + + //send for each room, all data of position user + let arrayMap = Array.from(mapPositionUserByRoom); + arrayMap.forEach((value) => { + let roomId = value[0]; + let data = value[1]; + this.Io.in(roomId).emit('user-position', JSON.stringify(data)); + }); + this.seTimeOutInProgress = setTimeout(() => { + this.shareUsersPosition(); + }, 10); + } - } + }