xushiting 3 anos atrás
pai
commit
fbb27ad9ab
2 arquivos alterados com 182 adições e 48 exclusões
  1. 35 3
      src/back/move.service.ts
  2. 147 45
      src/back/rotate.service.ts

+ 35 - 3
src/back/move.service.ts

@@ -100,17 +100,29 @@ export class MoveService {
     try {
       const userId = actionRequest['user_id'];
       const traceId = actionRequest['trace_id'];
+      const actionType = actionRequest['action_type'];
       const user = this.users[userId];
      
       const path = [100,99,97];    //需要计算路径
       const angle = user.camera.angle.yaw%45;   //纠正需要
       const index = Math.round(user.camera.angle.yaw/45);  //过渡需要
 
+      let replys = [];
       let traceIds = [];
       traceIds.push(traceId);  
 
       //纠正,旋转传到缓存里
-      //rotateService.rotateForAngle(userId,angle);
+      let checkReplys = [];
+      for(let i=0;i<angle;++i){
+         //let reply = rotateService.rotateForAngle(userId,(angle+i)%359);
+         //reply.traceIds = [];
+         //reply.traceIds.push(traceId);
+         //let actionResponse = rotateService.createActionResponse(actionType,trace_id);
+         //reply.actionResponses = [];
+         //reply.actionResponses.push(actionResponse);
+         //checkReplys.push(reply);
+      }
+      replys.push(checkReplys);
 
       //过渡传到缓存里
       this.reply.traceIds = traceIds;
@@ -118,14 +130,22 @@ export class MoveService {
       this.reply['actionResponses'][0].traceId = traceId;
       
       for(let i=0;i<path.length-1;++i){
+        let pathReplys = [];
         let start_break_point_id = path[i];
         let end_break_point_id = path[i+1];
         //读redis里的数据,按照frame_index的大小排序
-        //key:app_id+start_break_point_id+end_break_point_id+index
+        //key:moveframe:app_id+start_break_point_id+end_break_point_id+index
         //value:frame_index,file_name,camera_position,camera_angle
-      }
+        let moveFrames = [];     //value赋值
 
+        //读redis里的数据
+        //key:breakpoints:app_id:10016:break_point_id:112
+        let startBreakPoint,endBreakPoint;    //value赋值
+        pathReplys = this.createCacheReplys(moveFrames,traceId,userId,startBreakPoint.position,endBreakPoint.position);
+        replys.push(pathReplys);
+      }
 
+      return replys;
       
     } catch (error) {
       console.log('MoveService', error);
@@ -156,7 +176,19 @@ export class MoveService {
         reply['actionResponses'][0].traceId = traceId;
         replys.push(reply);
     }
+    return replys;
+
+  }
+
+  //需要通知user,人物和相机走到哪一个呼吸点位了
+  updateUser(userId,breakPointId,lastReply){
+    let user = this.users[userId];
+    user.breakPointId = breakPointId;
 
+    user.player.position = lastReply['newUserStates'][0].playerState.player.position;
+    user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
 
+    user.camera.position = lastReply['newUserStates'][0].playerState.camera.position;
+    user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
   }
 }

+ 147 - 45
src/back/rotate.service.ts

@@ -1,23 +1,26 @@
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
 import { CacheService } from 'src/cache/cache.service';
 
+
 @Injectable()
 export class RotateService {
-  constructor(private cacheService: CacheService) {}
+  constructor(private cacheService: CacheService) { }
   private actionRequestPool = {};
+  private logger: Logger = new Logger('rotateService');
   private Actions = {
     Clicking: 1,
     Rotation: 1014,
     Joystick: 15,
   };
   private users = {};
-  private replys = {};
+  private replies = {};
 
   init(app_id, userId) {
     const user = {
       appId: null,
       userId: null,
       breakPointId: null,
+      roomId:null,
       player: {
         position: { x: -700, y: 0, z: 0 },
         angle: {
@@ -136,7 +139,7 @@ export class RotateService {
     };
 
     reply['newUserStates'][0]['userId'] = userId;
-    this.replys[userId] = reply;
+    this.replies[userId] = reply;
     return reply;
   }
 
@@ -150,7 +153,7 @@ export class RotateService {
         this.actionRequestPool[userId].push(actionRequest);
       }
 
-      let reply = this.replys[userId];
+      const reply = this.replies[userId];
 
       const actionRequests = this.actionRequestPool[userId];
       const user = this.users[userId];
@@ -162,7 +165,10 @@ export class RotateService {
         if (actionRequests[i].action_type == this.Actions.Rotation) {
           horizontal_move += actionRequests[i].rotation_action.horizontal_move;
           reply.traceIds.push(actionRequests[i].trace_id);
-          let actionResponse = this.createActionResponse(actionRequests[i].action_type,actionRequests[i].trace_id);
+          const actionResponse = this.createActionResponse(
+            actionRequests[i].action_type,
+            actionRequests[i].trace_id,
+          );
           reply.actionResponses.push(actionResponse);
           ++sub;
         } else {
@@ -175,10 +181,10 @@ export class RotateService {
       if (Math.abs(hAngle) < 1) {
         user.rotateInfo.horizontal_move = horizontal_move;
         //user.traceIds = traceIds;
-        this.replys[userId] = reply;
-        return null;     
+        this.replies[userId] = reply;
+        return null;
       }
-      return this.rotateForAngle(userId,hAngle);
+      return this.rotateForAngle(userId, hAngle);
       /*
       user.rotateInfo.frameIndex += Math.floor(hAngle);
       if (user.rotateInfo.frameIndex < 0) {
@@ -225,8 +231,8 @@ export class RotateService {
         '?m=' +
         new Date().getTime();
 
-      this.replys[userId].traceIds = [];
-      this.replys[userId].actionResponses = [];
+      this.replies[userId].traceIds = [];
+      this.replies[userId].actionResponses = [];
 
       return reply;
       */
@@ -235,8 +241,8 @@ export class RotateService {
     }
   }
 
-  async rotateForAngle(userId,hAngle){
-    try{
+  async rotateForAngle(userId, hAngle) {
+    try {
       const user = this.users[userId];
       user.rotateInfo.frameIndex += Math.floor(hAngle);
       if (user.rotateInfo.frameIndex < 0) {
@@ -245,7 +251,7 @@ export class RotateService {
         user.rotateInfo.frameIndex -= 360;
       }
 
-      let reply = JSON.parse(JSON.stringify(this.replys[userId]));
+      const reply = JSON.parse(JSON.stringify(this.replies[userId]));
       reply['newUserStates'][0]['userId'] = userId;
       //从redis里取
       //let key = user.appId + "-"+user.breakPointId+"-"+user.rotateInfo.frameIndex;
@@ -259,44 +265,76 @@ export class RotateService {
       // const value = null;
 
       const redisData = await this.cacheService.get(key);
-      const value = JSON.parse(redisData);
-      console.log('redis', value);
-      user.camera['position'] = value ? value.cameraPosition : '';
-      user.camera['angle'] = value ? value.cameraAngle : '';
+      if (redisData && redisData.length > 0) {
+        const value = redisData ? JSON.parse(redisData) : null;
+        console.log('rotate-service', value);
+        user.camera['position'] = value ? value.cameraPosition : '';
+        user.camera['angle'] = value ? value.cameraAngle : '';
 
-      //this.reply['newUserStates'][0]['playerState'] .player
-      reply['newUserStates'][0]['playerState'].camera.position =
-        user.camera['position'];
-      reply['newUserStates'][0]['playerState'].camera.angle =
-        user.camera['angle'];
-      reply['newUserStates'][0]['playerState'].cameraCenter =
-        user.player.position;
-      // debugger
-      reply.mediaSrc =
-        '/' +
-        '0000000001' +
-        '/' +
-        user.breakPointId +
-        '/' +
-        value.directory +
-        '/' +
-        value.fileName +
-        '?m=' +
-        new Date().getTime();
+        reply['newUserStates'][0]['playerState'].player.position = user.player.position;
+        reply['newUserStates'][0]['playerState'].player.angle = user.player.angle;
+        
+        //this.reply['newUserStates'][0]['playerState'] .player
+        reply['newUserStates'][0]['playerState'].camera.position =
+          user.camera['position'];
+        reply['newUserStates'][0]['playerState'].camera.angle =
+          user.camera['angle'];
+        reply['newUserStates'][0]['playerState'].cameraCenter =
+          user.player.position;
+        // debugger
+        reply.mediaSrc =
+          '/' +
+          '0000000001' +
+          '/' +
+          user.breakPointId +
+          '/' +
+          value.directory +
+          '/' +
+          value.fileName +
+          '?m=' +
+          new Date().getTime();
 
-      this.replys[userId].traceIds = [];
-      this.replys[userId].actionResponses = [];
+        this.replies[userId].traceIds = [];
+        this.replies[userId].actionResponses = [];
 
-      user.rotateInfo.horizontal_move = 0;
+        user.rotateInfo.horizontal_move = 0;
 
-      return reply;
+        return reply;
+      } else {
+        return null;
+      }
     } catch (error) {
-      console.log('RotateService', error);
+      this.logger.error('rotateForAngle::function', error);
     }
   }
 
-  createActionResponse(actionType,traceId){
-      let actionResponse = {
+  createActionResponse(actionType, traceId) {
+    const actionResponse = {
+      actionType: actionType,
+      pointType: 100,
+      extra: '',
+      traceId: traceId,
+      packetId: '',
+      nps: [],
+      peopleNum: 0,
+      zoneId: '',
+      echoMsg: '',
+      reserveDetail: null,
+      userWithAvatarList: [],
+      newUserStates: [],
+      code: 0,
+      msg: '',
+    };
+    return actionResponse;
+  }
+
+  getNewUserStateRequest(actionRequest){
+    try{
+      const userId = actionRequest['user_id'];
+      const actionType = actionRequest['actionType'];
+      const traceId = actionRequest['trace_id'];
+
+      let reply = {
         "actionType": actionType,
         "pointType": 100,
         "extra": "",
@@ -312,6 +350,70 @@ export class RotateService {
         "code": 0,
         "msg": ""
       }
-      return actionResponse;
+
+      let userIds = Object.keys(this.users);
+      for(let i=0;i<userIds.length;++i){
+        let _user = this.users[userIds[i]];
+        let newUserState = {
+          "userId": userIds[i],
+          "playerState": {
+            "roomTypeId": "",
+            "person": 0,
+            "avatarId": "KGe_Boy",
+            "skinId": "10089",
+            "roomId": "e629ef3e-022d-4e64-8654-703bb96410eb",
+            "isHost": false,
+            "isFollowHost": false,
+            "skinDataVersion": "1008900008",
+            "avatarComponents": "",
+            "nickName": userIds[i],
+            "movingMode": 0,
+            "attitude": "walk",
+            "areaName": "",
+            "pathName": "thirdwalk",
+            "pathId": "thirdwalk",
+            "avatarSize": 1,
+            "extra": "{\"removeWhenDisconnected\":true}",
+            "prioritySync": false,
+            "player": {
+              "position": _user.player.position,
+              "angle": _user.player.angle
+            },
+            "camera": null,
+            "cameraCenter": null
+          },
+          "renderInfo": {
+            "renderType": 0,
+            "videoFrame": null,
+            "cameraStateType": 0,
+            "isMoving": 0,
+            "needIfr": 0,
+            "isVideo": 0,
+            "stillFrame": 0,
+            "isRotating": 0,
+            "isFollowing": 0,
+            "clientPanoTitlesBitmap": [],
+            "clientPanoTreceId": "",
+            "prefetchVideoId": "",
+            "noMedia": false
+          },
+          "event": {
+            "id": "",
+            "type": 0,
+            "points": [],
+            "rotateEvent": null,
+            "removeVisitorEvent": null
+          },
+          "relation": 0
+        };
+
+        reply["newUserStates"].push(newUserState);
+      }
+
+      return reply;
+    }
+    catch(error){
+      this.logger.error('getNewUserStateRequest::function', error);
+    }
   }
 }