xushiting 3 سال پیش
والد
کامیت
95d474e624
2فایلهای تغییر یافته به همراه102 افزوده شده و 39 حذف شده
  1. 52 26
      src/back/get-router.service.ts
  2. 50 13
      src/back/move.service.ts

+ 52 - 26
src/back/get-router.service.ts

@@ -7,10 +7,27 @@ export class GetRouterService {
 
   async searchRoad(appId, startPointId, clicking_point) {
     //表示终点
-    const endPoint = {
-      position:clicking_point,
-      breakPointId:-100 
+    let endPoint;
+
+    const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
+    let minDis = null;
+    for (let i = 0; i < keys.length; ++i) {
+      const breakPointRes = await this.cacheService.get(keys[i]);
+      const breakPoint = JSON.parse(breakPointRes);
+      const position = breakPoint.position;
+      if(minDis == null){
+        minDis = this.getDistance(clicking_point,position);
+        endPoint = breakPoint;
+      }
+      else if(minDis>this.getDistance(clicking_point,position)){
+        endPoint = breakPoint;
+      }
+    }
+
+    if(minDis>100){
+      return [];
     }
+
     const startPointRes = await this.cacheService.get(
       'breakpoints:app_id:' + appId + ':break_point_id:' + startPointId,
     );
@@ -21,10 +38,10 @@ export class GetRouterService {
     // );
     // const endPoint = JSON.parse(endPointRes);
 
-    let openList = [], //开启列表
-      closeList = [], //关闭列表
-      result = [], //结果数组
-      result_index; //结果数组在开启列表中的序号
+    const openList = [], //开启列表
+      closeList = []; //关闭列表
+    let result = []; //结果数组
+    let result_index: number; //结果数组在开启列表中的序号
 
     //openList.push({x:startPoint.x,y:startPoint.y,G:0});//把当前点加入到开启列表中,并且G是0
     openList.push({ breakPointId: startPointId, G: 0 }); //把当前点加入到开启列表中,并且G是0
@@ -38,23 +55,30 @@ export class GetRouterService {
           currentPointInfo.breakPointId,
       );
       const currentPoint = JSON.parse(currentPointRes);
-
       closeList.push(currentPointInfo);
-      const surroundPoint = this.SurroundPoint(
-        appId,
-        currentPointInfo.breakPointId,
-        endPoint
+
+      //读redis里的数据
+      const breakPointRes = await this.cacheService.get(
+        'breakpoints:app_id:' +
+          appId +
+          ':break_point_id:' +
+          currentPointInfo.breakPointId,
       );
-      for (const i in surroundPoint) {
+      let surroundPoint = [];
+      const _breakPoint = JSON.parse(breakPointRes);
+      surroundPoint = _breakPoint.contact;
+
+      for (let i = 0; i < surroundPoint.length; ++i) {
         const neighPointId = surroundPoint[i];
         const itemRes = await this.cacheService.get(
           'breakpoints:app_id:' + appId + ':break_point_id:' + neighPointId,
         );
-        const item = JSON.parse(itemRes);
+        let item = JSON.parse(itemRes);
         //g 到父节点的位置
-        const g = currentPointInfo.G + this.getDistance(currentPoint.position,item.position)
-
-        if (this.existList(item, openList) != -1) {
+        const g =
+          currentPointInfo.G +
+          this.getDistance(currentPoint.position, item.position);
+        if (this.existList(item, openList) == -1) {
           //如果不在开启列表中
           item['H'] = 0;
           item['G'] = g;
@@ -77,12 +101,10 @@ export class GetRouterService {
         break;
       }
       openList.sort(this.sortF); //这一步是为了循环回去的时候,找出 F 值最小的, 将它从 "开启列表" 中移掉
-    } while (
-      !(result_index = this.existList(endPoint,openList))
-    );
+    } while ((result_index = this.existList(endPoint, openList))==-1);
 
     //判断结果列表是否为空
-    if (!result_index) {
+    if (result_index == -1) {
       result = [];
     } else {
       let currentObj = openList[result_index];
@@ -95,7 +117,8 @@ export class GetRouterService {
         currentObj.position.y != startPoint.position.y
       );
     }
-    debugger
+    debugger;
+    result.unshift(startPointId);
     return result;
   }
 
@@ -105,13 +128,13 @@ export class GetRouterService {
   }
 
   //获取周围点
-  async SurroundPoint(appId, curPointId,endPoint) {
+  async SurroundPoint(appId, curPointId, endPoint) {
     //读redis里的数据
     const breakPointRes = await this.cacheService.get(
       'breakpoints:app_id:' + appId + ':break_point_id:' + curPointId,
     );
     const breakPoint = JSON.parse(breakPointRes);
-    if(this.getDistance(breakPoint.position,endPoint.position)<1){
+    if (this.getDistance(breakPoint.position, endPoint.position) < 1) {
       breakPoint.contact.push(endPoint.breakPointId);
     }
     return breakPoint.contact;
@@ -127,7 +150,10 @@ export class GetRouterService {
     return -1;
   }
 
-  getDistance(position1,position2){
-    return Math.sqrt((position1.x - position2.x)*(position1.x - position2.x)+(position1.y - position2.y)*(position1.y - position2.y))
+  getDistance(position1, position2) {
+    return Math.sqrt(
+      (position1.x - position2.x) * (position1.x - position2.x) +
+        (position1.y - position2.y) * (position1.y - position2.y),
+    );
   }
 }

+ 50 - 13
src/back/move.service.ts

@@ -9,7 +9,7 @@ export class MoveService {
     Rotation: 1014,
     Joystick: 15,
   };
-  private users = {};
+  public users = {};
 
   private reply = {
     traceIds: [],
@@ -135,7 +135,7 @@ export class MoveService {
     this.users[userId] = user;
   }
 
-  async move(actionRequest) {
+  async move(pathArray, actionRequest) {
     try {
       const userId = actionRequest['user_id'];
       const traceId = actionRequest['trace_id'];
@@ -143,7 +143,7 @@ export class MoveService {
       const user = this.users[userId];
 
       const appId = user.appId;
-      const path = [100, 101, 102]; //需要计算路径
+      const path = pathArray || [100, 101, 102]; //需要计算路径
       const angle = user.camera.angle.yaw % 45; //纠正需要
       const index = Math.round(user.camera.angle.yaw / 45); //过渡需要
 
@@ -214,6 +214,13 @@ export class MoveService {
           startBreakPoint.position,
           endBreakPoint.position,
         );
+
+        if (i == path.length - 2) {
+          pathReplys[pathReplys.length - 1][
+            'newUserStates'
+          ][0].renderInfo.isMoving = 0;
+        }
+
         //replys.push(pathReplys);
         replys['P' + start_break_point_id + 'T' + end_break_point_id] =
           pathReplys;
@@ -297,6 +304,7 @@ export class MoveService {
     const user = this.users[userId];
     user.breakPointId = breakPointId;
 
+    // debugger;
     user.player.position =
       lastReply['newUserStates'][0].playerState.player.position;
     user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
@@ -334,10 +342,16 @@ export class MoveService {
 
     //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
     //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
-    const breakPoints = [];
-    for (let i = 0; i < breakPoints.length; ++i) {
-      const position = breakPoints[i].position;
-      reply['nps'].push(position);
+    const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
+
+    for (let i = 0; i < keys.length; ++i) {
+      const breakPointRes = await this.cacheService.get(keys[i]);
+      const breakPoint = JSON.parse(breakPointRes);
+      const position = breakPoint.position;
+      reply['nps'].push({
+        position: position,
+        breakPointId: breakPoint.breakPointId,
+      });
     }
     return reply;
   }
@@ -355,20 +369,31 @@ export class MoveService {
     return (angle + 360) % 360;
   }
 
-  async stop(traceId,userId,breakPointId,cameraAngle,playerAngle){
+  async stop(traceId, userId, movePointIds, cameraAngle, playerAngle) {
+    const breakPointId = movePointIds.substring(movePointIds.indexOf('-') + 1);
     const user = this.users[userId];
     user.breakPointId = breakPointId;
     const appId = user.appId;
 
-    const breakPointRes = await this.cacheService.get('breakpoints:app_id:' +appId +':break_point_id:' +breakPointId);
+    const breakPointRes = await this.cacheService.get(
+      'breakpoints:app_id:' + appId + ':break_point_id:' + breakPointId,
+    );
+
     const breakPoint = JSON.parse(breakPointRes);
 
     user.player.position = breakPoint.position;
     user.player.angle = playerAngle;
 
-    const rotateDataRes ='rotateframe:app_id:' +appId +':frame_index:' + cameraAngle.yaw +':break_point_id:' +breakPointId;
-    const rotateData = await this.cacheService.get(rotateDataRes);
+    const rotateKey =
+      'rotateframe:app_id:' +
+      appId +
+      ':frame_index:' +
+      cameraAngle.yaw +
+      ':break_point_id:' +
+      breakPointId;
 
+    const rotateDataRes = await this.cacheService.get(rotateKey);
+    const rotateData = JSON.parse(rotateDataRes);
     user.camera.position = rotateData.cameraPosition;
     user.camera.angle = rotateData.cameraAngle;
 
@@ -377,11 +402,23 @@ export class MoveService {
     reply['newUserStates'][0].userId = userId;
     reply['newUserStates'][0].playerState.player.position = breakPoint.position;
     reply['newUserStates'][0].playerState.player.angle = playerAngle;
-    reply['newUserStates'][0].playerState.camera.position = rotateData.cameraPosition;
+    reply['newUserStates'][0].playerState.camera.position =
+      rotateData.cameraPosition;
     reply['newUserStates'][0].playerState.camera.angle = rotateData.cameraAngle;
     reply['newUserStates'][0].playerState.cameraCenter = breakPoint.position;
     reply['actionResponses'][0].traceId = traceId;
-    reply.mediaSrc ='/' +'0000000001' +'/' +breakPointId +'/' +rotateData.directory +'/' +rotateData.fileName +'?m=' +new Date().getTime();
+    reply.mediaSrc =
+      '/' +
+      '0000000001' +
+      '/' +
+      breakPointId +
+      '/' +
+      rotateData.directory +
+      '/' +
+      rotateData.fileName +
+      '?m=' +
+      new Date().getTime();
+    reply['newUserStates'][0].renderInfo.isMoving = 0;
     return reply;
   }
 }