Merge branch 'master' into Share-players-position-using-Socket.IO
authorgparant <g.parant@thecodingmachine.com>
Sat, 4 Apr 2020 20:46:42 +0000 (22:46 +0200)
committergparant <g.parant@thecodingmachine.com>
Sat, 4 Apr 2020 20:46:42 +0000 (22:46 +0200)
# Conflicts:
# back/src/Controller/IoSocketController.ts

1  2 
README.md
back/src/Controller/IoSocketController.ts

diff --cc README.md
Simple merge
index 696f3267762194c51134a3fe2de2a0776e0bd69b,3907436b630b6f064bf76b6225ba31b964db9e22..ad305395bc06969133cbfc4f56ac1eaa3e82d3cd
@@@ -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: <string>,
 +            roomId: <string>,
 +            position: {
 +                x : <number>,
 +                y : <number>
 +            }
 +          },
 +          ...
 +        ]
 +     **/
 +    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 = <any>[];
 +            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);
 +    }
+ }