move.service.ts 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
  2. import { ConfigService } from '@nestjs/config';
  3. import { readFileSync } from 'fs';
  4. import { join } from 'path';
  5. import { CacheService } from 'src/cache/cache.service';
  6. import { RotateService } from 'src/rotate/rotate.service';
  7. import configuration from 'src/config/configuration';
  8. // import * as BreakPointIds from '../../ws/points-BreakPointId.json';
  9. // import { SceneService } from 'src/scene/scene.service';
  10. const seqExeAsyncFn = (asyncFn) => {
  11. let runPromise = null;
  12. return function seq(...args) {
  13. if (!runPromise) {
  14. //debugger;
  15. runPromise = asyncFn.apply(this, args);
  16. runPromise.then((data) => {
  17. //debugger;
  18. // console.log('seq result', data);
  19. });
  20. runPromise.then(() => (runPromise = null));
  21. return runPromise;
  22. } else {
  23. return runPromise.then(() => seq.apply(this, args));
  24. }
  25. };
  26. };
  27. const moveInterval = 3;
  28. @Injectable()
  29. export class MoveService implements OnModuleInit {
  30. constructor(
  31. private cacheService: CacheService,
  32. private rotateService: RotateService,
  33. private configService: ConfigService,
  34. ) {}
  35. private logger: Logger = new Logger('MoveService');
  36. private Actions = {
  37. Clicking: 1,
  38. Rotation: 1014,
  39. Joystick: 15,
  40. };
  41. public users = this.rotateService.users;
  42. private reply = {
  43. traceIds: [],
  44. vehicle: null,
  45. mediaSrc: null,
  46. isIDR: false,
  47. newUserStates: [
  48. {
  49. userId: 'dcff36ae4fc1d',
  50. playerState: {
  51. roomTypeId: '',
  52. person: 0,
  53. avatarId: '',
  54. skinId: '',
  55. roomId: '',
  56. isHost: false,
  57. isFollowHost: false,
  58. skinDataVersion: '',
  59. avatarComponents: '',
  60. nickName: '',
  61. movingMode: 0,
  62. attitude: '',
  63. areaName: '',
  64. pathName: '',
  65. pathId: '',
  66. avatarSize: 1,
  67. extra: '',
  68. prioritySync: false,
  69. player: {
  70. position: { x: -700, y: 0, z: 0 },
  71. angle: {
  72. pitch: 0,
  73. yaw: 0,
  74. roll: 0,
  75. },
  76. },
  77. camera: {
  78. position: { x: -1145, y: 0, z: 160 },
  79. angle: {
  80. pitch: 0,
  81. yaw: 0,
  82. roll: 0,
  83. },
  84. },
  85. cameraCenter: { x: -700, y: 0, z: 0 },
  86. },
  87. renderInfo: {
  88. renderType: 0,
  89. videoFrame: null,
  90. cameraStateType: 3,
  91. isMoving: 1,
  92. needIfr: 0,
  93. isVideo: 0,
  94. stillFrame: 0,
  95. isRotating: 0,
  96. isFollowing: 0,
  97. clientPanoTitlesBitmap: [],
  98. clientPanoTreceId: '',
  99. prefetchVideoId: '',
  100. noMedia: false,
  101. },
  102. event: null,
  103. relation: 1,
  104. },
  105. ],
  106. actionResponses: [
  107. {
  108. actionType: 1,
  109. pointType: 100,
  110. extra: '',
  111. traceId: '',
  112. packetId: '',
  113. nps: [],
  114. peopleNum: 0,
  115. zoneId: '',
  116. echoMsg: '',
  117. reserveDetail: null,
  118. userWithAvatarList: [],
  119. newUserStates: [],
  120. code: 0,
  121. msg: '',
  122. },
  123. ],
  124. getStateType: 0,
  125. code: 0,
  126. msg: 'OK',
  127. };
  128. private breakPointInfo: any;
  129. private cameraInfos = [];
  130. public sendingFrameForJoystick = false;
  131. // eslint-disable-next-line @typescript-eslint/no-empty-function
  132. async onModuleInit() {
  133. //const app_id = '0000000003';
  134. // const app_id = '0000000007';
  135. // const prefix = '/mnt/metaverse/scene';
  136. const app_id = configuration().app.appId;
  137. const prefix = configuration().app.prefix;
  138. console.log('app_id', app_id, configuration().app.appId);
  139. console.log('prefix', prefix, configuration().app.appId);
  140. let path;
  141. // let path: string;
  142. if (process.env.NODE_ENV === 'development') {
  143. path = join(__dirname, `../ws/${app_id}/points-${app_id}.json`);
  144. console.log('测试服JSON-move', path);
  145. }
  146. if (process.env.NODE_ENV === 'production') {
  147. path = join(`${prefix}/${app_id}/points-${app_id}.json`);
  148. console.log('正式服JSON-move', path);
  149. }
  150. this.loadJSON(path);
  151. }
  152. async loadJSON(path) {
  153. try {
  154. const data = await readFileSync(path);
  155. const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
  156. this.breakPointInfo = BreakPointInfo;
  157. // console.log('BreakPointInfo', BreakPointInfo);
  158. } catch (error) {
  159. this.logger.error('load-json-error', error);
  160. }
  161. }
  162. init(app_id, userId) {
  163. const user = {
  164. appId: null,
  165. userId: null,
  166. breakPointId: null,
  167. roomId: null,
  168. player: {
  169. position: { x: -700, y: 0, z: 0 },
  170. angle: {
  171. pitch: 0,
  172. yaw: 0,
  173. roll: 0,
  174. },
  175. },
  176. camera: {
  177. position: { x: -1145, y: 0, z: 160 },
  178. angle: {
  179. pitch: 0,
  180. yaw: 0,
  181. roll: 0,
  182. },
  183. },
  184. rotateInfo: {
  185. frameIndex: 0,
  186. horizontal_move: 0,
  187. },
  188. moveInfo: {},
  189. // traceIds: [],
  190. // actionResponses:[]
  191. };
  192. user.appId = app_id;
  193. user.userId = userId;
  194. user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
  195. console.log('user-init', user);
  196. this.users[userId] = user;
  197. }
  198. async getMoveFrames(
  199. appId,
  200. start_break_point_id,
  201. end_break_point_id,
  202. angleIndex,
  203. ) {
  204. console.log(
  205. 'handlejoystick-angle->相机过渡angleIndex-------------------------:' +
  206. angleIndex,
  207. );
  208. let moveFramesRes, moveFrames;
  209. let key =
  210. 'moveframe:app_id:' +
  211. appId +
  212. ':start_break_point_id:' +
  213. start_break_point_id +
  214. ':end_break_point_id:' +
  215. end_break_point_id +
  216. ':angle:' +
  217. angleIndex;
  218. //倒序
  219. if (start_break_point_id > end_break_point_id) {
  220. key =
  221. 'moveframe:app_id:' +
  222. appId +
  223. ':start_break_point_id:' +
  224. end_break_point_id +
  225. ':end_break_point_id:' +
  226. start_break_point_id +
  227. ':angle:' +
  228. angleIndex;
  229. moveFramesRes = await this.cacheService.get(key);
  230. moveFrames = JSON.parse(moveFramesRes);
  231. moveFrames = moveFrames.reverse();
  232. } else {
  233. moveFramesRes = await this.cacheService.get(key);
  234. moveFrames = JSON.parse(moveFramesRes);
  235. }
  236. return moveFrames;
  237. }
  238. async move(pathArray, actionRequest) {
  239. try {
  240. const userId = actionRequest['user_id'];
  241. const traceId = actionRequest['trace_id'];
  242. const actionType = actionRequest['action_type'];
  243. const user = this.users[userId];
  244. const appId = user.appId;
  245. const path = pathArray || [100, 101, 102]; //需要计算路径
  246. const angle = user.camera.angle.yaw % 45; //纠正需要
  247. const replys = [];
  248. const traceIds = [];
  249. traceIds.push(traceId);
  250. const checkReplys = await this.modeifyCameraAngle(
  251. angle,
  252. userId,
  253. traceId,
  254. actionType,
  255. );
  256. for (let i = 0; i < checkReplys.length; ++i) {
  257. checkReplys[i].actionResponses[0].actionType = actionType;
  258. if (i == 0 || i == checkReplys.length - 1) {
  259. checkReplys[i].isIDR = true;
  260. } else {
  261. checkReplys[i].isIDR = false;
  262. }
  263. }
  264. //replys['P' + user.breakPointId + 'T' + user.breakPointId] = checkReplys;
  265. replys.push(checkReplys);
  266. console.log('路径:' + path);
  267. //过渡传到缓存里
  268. this.reply.traceIds = traceIds;
  269. this.reply['newUserStates'][0].userId = userId;
  270. this.reply['actionResponses'][0].traceId = traceId;
  271. //const index = Math.floor((user.camera.angle.yaw + 1) / 45) % 8; //过渡需要
  272. const index = this.getMoveIndex(user.camera.angle.yaw);
  273. for (let i = 0; i < path.length - 1; ++i) {
  274. const start_break_point_id = path[i];
  275. const end_break_point_id = path[i + 1];
  276. const moveFrames = await this.getMoveFrames(
  277. appId,
  278. start_break_point_id,
  279. end_break_point_id,
  280. index,
  281. );
  282. if (!moveFrames) {
  283. return replys;
  284. }
  285. const pathReplys = this.createCacheReplys(
  286. appId,
  287. moveFrames,
  288. traceId,
  289. userId,
  290. start_break_point_id,
  291. end_break_point_id,
  292. false,
  293. );
  294. //第一段
  295. if (i == 0) {
  296. //第一帧
  297. pathReplys[0].isIDR = true;
  298. }
  299. //最后一段
  300. else if (i == path.length - 2) {
  301. //最后一帧
  302. pathReplys[pathReplys.length - 1][
  303. 'newUserStates'
  304. ][0].renderInfo.isMoving = 0;
  305. }
  306. for (let j = 0; j < pathReplys.length; ++j) {
  307. pathReplys[j].actionResponses[0].actionType = actionType;
  308. }
  309. replys.push(pathReplys);
  310. //replys['P' + start_break_point_id + 'T' + end_break_point_id] =pathReplys;
  311. }
  312. return replys;
  313. } catch (error) {
  314. console.log('MoveService', error);
  315. }
  316. }
  317. createCacheReplys(
  318. appId,
  319. moveFrames,
  320. traceId,
  321. userId,
  322. startBreakPointId,
  323. endBreakPointId,
  324. isFromUser,
  325. ) {
  326. const replys = [];
  327. const startPosition = this.breakPointInfo[startBreakPointId].position;
  328. const endPosition = this.breakPointInfo[endBreakPointId].position;
  329. const angle = this.getAngle(
  330. startPosition,
  331. {
  332. x: startPosition.x + 1,
  333. y: startPosition.y,
  334. },
  335. endPosition,
  336. );
  337. const user = this.users[userId];
  338. let i;
  339. for (i = 1; i < moveFrames.length; i += moveInterval) {
  340. const moveFrame = moveFrames[i];
  341. const reply = JSON.parse(JSON.stringify(this.reply));
  342. if (reply.traceIds.indexOf(traceId) == -1) {
  343. reply.traceIds.push(traceId);
  344. }
  345. reply['newUserStates'][0].userId = userId;
  346. if (!isFromUser) {
  347. reply['newUserStates'][0].playerState.player.position = {
  348. x:
  349. startPosition.x +
  350. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  351. y:
  352. startPosition.y +
  353. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  354. z:
  355. startPosition.z +
  356. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  357. };
  358. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  359. reply['newUserStates'][0].playerState.cameraCenter =
  360. reply['newUserStates'][0].playerState.player.position;
  361. } else {
  362. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  363. JSON.stringify(user.player.position),
  364. );
  365. reply['newUserStates'][0].playerState.player.angle.yaw =
  366. user.player.angle.yaw;
  367. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  368. JSON.stringify(user.player.position),
  369. );
  370. }
  371. reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  372. JSON.stringify(moveFrame.camera_position),
  373. );
  374. if (i == 1) {
  375. console.log('move-2' + moveFrame.camera_angle.yaw);
  376. }
  377. if (moveFrame.camera_angle.yaw < 0) {
  378. moveFrame.camera_angle.yaw += 360;
  379. } else if (moveFrame.camera_angle.yaw > 359) {
  380. moveFrame.camera_angle.yaw -= 360;
  381. }
  382. reply['newUserStates'][0].playerState.camera.angle =
  383. moveFrame.camera_angle;
  384. reply['newUserStates'][0].renderInfo.isMoving = 1;
  385. reply['actionResponses'][0].traceId = traceId;
  386. reply.mediaSrc =
  387. '/' +
  388. appId +
  389. '/' +
  390. startBreakPointId +
  391. '/' +
  392. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  393. '/' +
  394. moveFrame.file_name +
  395. '?m=' +
  396. new Date().getTime();
  397. if (startBreakPointId > endBreakPointId) {
  398. reply.mediaSrc =
  399. '/' +
  400. appId +
  401. '/' +
  402. endBreakPointId +
  403. '/' +
  404. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  405. '/' +
  406. moveFrame.file_name +
  407. '?m=' +
  408. new Date().getTime();
  409. }
  410. reply.startBreakPointId = startBreakPointId;
  411. reply.endBreakPointId = endBreakPointId;
  412. if (i == moveFrames.length - 1) {
  413. reply.isIDR = true;
  414. } else {
  415. reply.isIDR = false;
  416. }
  417. replys.push(reply);
  418. }
  419. if (i != moveFrames.length - 1) {
  420. i = moveFrames.length - 1;
  421. const moveFrame = moveFrames[i];
  422. const reply = JSON.parse(JSON.stringify(this.reply));
  423. reply.traceIds.push(traceId);
  424. reply['newUserStates'][0].userId = userId;
  425. reply['newUserStates'][0].renderInfo.isMoving = 1;
  426. if (!isFromUser) {
  427. reply['newUserStates'][0].playerState.player.position = {
  428. x:
  429. startPosition.x +
  430. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  431. y:
  432. startPosition.y +
  433. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  434. z:
  435. startPosition.z +
  436. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  437. };
  438. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  439. reply['newUserStates'][0].playerState.cameraCenter =
  440. reply['newUserStates'][0].playerState.player.position;
  441. } else {
  442. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  443. JSON.stringify(user.player.position),
  444. );
  445. reply['newUserStates'][0].playerState.player.angle.yaw =
  446. user.player.angle.yaw;
  447. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  448. JSON.stringify(user.player.position),
  449. );
  450. }
  451. reply['newUserStates'][0].playerState.camera.position =
  452. moveFrame.camera_position;
  453. if (moveFrame.camera_angle.yaw < 0) {
  454. moveFrame.camera_angle.yaw += 360;
  455. } else if (moveFrame.camera_angle.yaw > 359) {
  456. moveFrame.camera_angle.yaw -= 360;
  457. }
  458. reply['newUserStates'][0].playerState.camera.angle =
  459. moveFrame.camera_angle;
  460. reply['actionResponses'][0].traceId = traceId;
  461. reply.mediaSrc =
  462. '/' +
  463. appId +
  464. '/' +
  465. startBreakPointId +
  466. '/' +
  467. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  468. '/' +
  469. moveFrame.file_name +
  470. '?m=' +
  471. new Date().getTime();
  472. if (startBreakPointId > endBreakPointId) {
  473. reply.mediaSrc =
  474. '/' +
  475. appId +
  476. '/' +
  477. endBreakPointId +
  478. '/' +
  479. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  480. '/' +
  481. moveFrame.file_name +
  482. '?m=' +
  483. new Date().getTime();
  484. }
  485. reply.startBreakPointId = startBreakPointId;
  486. reply.endBreakPointId = endBreakPointId;
  487. reply.isIDR = true;
  488. replys.push(reply);
  489. }
  490. return replys;
  491. }
  492. //需要通知user,人物和相机走到哪一个呼吸点位了
  493. updateUser(userId, breakPointId, lastReply) {
  494. const user = this.users[userId];
  495. user.breakPointId = breakPointId;
  496. if (lastReply.actionResponses[0].actionType != 15) {
  497. user.player.position =
  498. lastReply['newUserStates'][0].playerState.player.position;
  499. }
  500. user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
  501. user.camera.position =
  502. lastReply['newUserStates'][0].playerState.camera.position;
  503. user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
  504. }
  505. getBreakPoints(actionRequest) {
  506. const userId = actionRequest['user_id'];
  507. const traceId = actionRequest['trace_id'];
  508. const actionType = actionRequest['action_type'];
  509. const user = this.users[userId];
  510. const appId = user.appId;
  511. const breakPointId = user.breakPointId;
  512. const reply = {
  513. actionType: actionType,
  514. pointType: 100,
  515. extra: '',
  516. traceId: traceId,
  517. packetId: '',
  518. nps: [],
  519. peopleNum: 0,
  520. zoneId: '',
  521. echoMsg: '',
  522. reserveDetail: null,
  523. userWithAvatarList: [],
  524. newUserStates: [],
  525. code: 0,
  526. msg: '',
  527. };
  528. //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
  529. //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
  530. //const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
  531. for (const key in this.breakPointInfo) {
  532. const breakPoint = this.breakPointInfo[key];
  533. //const breakPoint = JSON.parse(breakPointRes);
  534. //const position = breakPoint.position;
  535. reply['nps'].push({
  536. position: breakPoint.position,
  537. breakPointId: Number(key),
  538. });
  539. }
  540. // for (let i = 0; i < keys.length; ++i) {
  541. // const breakPointRes = await this.cacheService.get(keys[i]);
  542. // const breakPoint = JSON.parse(breakPointRes);
  543. // const position = breakPoint.position;
  544. // reply['nps'].push({
  545. // position: position,
  546. // breakPointId: breakPoint.breakPointId,
  547. // });
  548. // }
  549. return reply;
  550. }
  551. getAngle(point, point1, point2) {
  552. const x1 = point1.x - point.x;
  553. const y1 = point1.y - point.y;
  554. const x2 = point2.x - point.x;
  555. const y2 = point2.y - point.y;
  556. const dot = x1 * x2 + y1 * y2;
  557. const det = x1 * y2 - y1 * x2;
  558. const angle = (Math.atan2(det, dot) / Math.PI) * 180;
  559. return (angle + 360) % 360;
  560. }
  561. async stop(traceId, userId, breakPointId, cameraAngle, playerAngle) {
  562. //const breakPointId = movePointIds.substring(movePointIds.indexOf('-') + 1);
  563. const user = this.users[userId];
  564. const startBreakPointId = user.breakPointId;
  565. user.breakPointId = breakPointId;
  566. const appId = user.appId;
  567. const breakPoint = this.breakPointInfo[breakPointId];
  568. user.player.position = breakPoint.position;
  569. user.player.angle = playerAngle;
  570. const rotateKey =
  571. 'rotateframe:app_id:' +
  572. appId +
  573. ':frame_index:' +
  574. cameraAngle.yaw +
  575. ':break_point_id:' +
  576. breakPointId;
  577. const rotateDataRes = await this.cacheService.get(rotateKey);
  578. const rotateData = JSON.parse(rotateDataRes);
  579. user.camera.position = rotateData.cameraPosition;
  580. user.camera.angle = rotateData.cameraAngle;
  581. const reply = JSON.parse(JSON.stringify(this.reply));
  582. reply.traceIds.push(traceId);
  583. reply['newUserStates'][0].userId = userId;
  584. reply['newUserStates'][0].playerState.player.position = breakPoint.position;
  585. reply['newUserStates'][0].playerState.player.angle = playerAngle;
  586. reply['newUserStates'][0].playerState.camera.position =
  587. rotateData.cameraPosition;
  588. reply['newUserStates'][0].playerState.camera.angle = rotateData.cameraAngle;
  589. reply['newUserStates'][0].playerState.cameraCenter = breakPoint.position;
  590. reply['actionResponses'][0].traceId = traceId;
  591. reply.mediaSrc =
  592. '/' +
  593. appId +
  594. '/' +
  595. breakPointId +
  596. '/' +
  597. rotateData.directory +
  598. '/' +
  599. rotateData.fileName +
  600. '?m=' +
  601. new Date().getTime();
  602. reply.startBreakPointId = startBreakPointId;
  603. reply.endBreakPointId = breakPointId;
  604. reply['newUserStates'][0].renderInfo.isMoving = 0;
  605. return reply;
  606. }
  607. // 顺序旋转请求
  608. seqExeJoystick = seqExeAsyncFn(this.joystick);
  609. // async joystick(actionRequest) {
  610. // try {
  611. // const userId = actionRequest['user_id'];
  612. // const traceId = actionRequest['trace_id'];
  613. // const dir_action = actionRequest['dir_action'];
  614. // const actionType = actionRequest['action_type'];
  615. // const user = this.users[userId];
  616. // const breakPointId = user.breakPointId;
  617. // const appId = user.appId;
  618. // const replys = [];
  619. // const step = 0.3;
  620. // const closestDis = 85; //小于这个距离就跳到邻居呼吸点
  621. // const distance = step * dir_action.speed_level;
  622. // let angle = null;
  623. // let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  624. // move_angle = move_angle % 360;
  625. // //TODO 临时增加断言
  626. // const playerPosition: Point = { x: 0, y: 0, z: 0 };
  627. // playerPosition.x =
  628. // user.player.position.x +
  629. // distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  630. // playerPosition.y =
  631. // user.player.position.y +
  632. // distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  633. // //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  634. // let chooseBreakPointId = null;
  635. // const breakPoint = this.breakPointInfo[breakPointId];
  636. // const surroundPointIds = breakPoint.contact;
  637. // //const neighAngles = [];
  638. // const traceIds = [];
  639. // user.player.angle.yaw = move_angle;
  640. // traceIds.push(traceId);
  641. // this.reply.traceIds = traceIds;
  642. // this.reply['newUserStates'][0].userId = userId;
  643. // this.reply['actionResponses'][0].traceId = traceId;
  644. // this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  645. // this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  646. // JSON.stringify(user.camera.angle),
  647. // );
  648. // this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  649. // JSON.stringify(user.camera.position),
  650. // );
  651. // this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  652. // JSON.stringify(breakPoint.position),
  653. // );
  654. // this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  655. // this.reply['actionResponses'][0].traceId = traceId;
  656. // this.reply.mediaSrc = null;
  657. // if (surroundPointIds.length == 1) {
  658. // return await this.moveDirect(
  659. // playerPosition,
  660. // closestDis,
  661. // breakPointId,
  662. // surroundPointIds[0],
  663. // appId,
  664. // userId,
  665. // traceId,
  666. // actionType,
  667. // );
  668. // }
  669. // let count = 0;
  670. // const neighPoints = [];
  671. // //人在哪个角度
  672. // let _angle = this.getAngle(
  673. // breakPoint.position,
  674. // { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  675. // playerPosition,
  676. // );
  677. // if (_angle < 0) {
  678. // _angle += 360;
  679. // }
  680. // let singleInfo = null;
  681. // for (let i = 0; i < surroundPointIds.length; ++i) {
  682. // const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  683. // neighPoint.breakPointId = surroundPointIds[i];
  684. // angle = this.getAngle(
  685. // breakPoint.position,
  686. // { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  687. // neighPoint.position,
  688. // );
  689. // if (angle < 0) {
  690. // angle += 360;
  691. // }
  692. // //if(angle<45&&angle!=0){
  693. // if (
  694. // // Math.abs(angle - move_angle) < 45 &&
  695. // // Math.abs(angle - move_angle) != 0
  696. // Math.abs(angle - _angle) < 45 &&
  697. // Math.abs(angle - _angle) != 0
  698. // ) {
  699. // neighPoint.angle = angle;
  700. // neighPoints.push(neighPoint);
  701. // ++count;
  702. // } else if (Math.abs(angle - move_angle) == 0) {
  703. // return await this.moveDirect(
  704. // playerPosition,
  705. // closestDis,
  706. // breakPointId,
  707. // surroundPointIds[i],
  708. // appId,
  709. // userId,
  710. // traceId,
  711. // actionType,
  712. // );
  713. // }
  714. // //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  715. // if (angle == 0 && Math.abs(360 - _angle) < 45) {
  716. // neighPoint.angle = angle;
  717. // neighPoints.push(neighPoint);
  718. // ++count;
  719. // }
  720. // if(Math.abs(angle - move_angle)<45||Math.abs(angle+360 - move_angle)<45){
  721. // if(singleInfo == null){
  722. // singleInfo = {
  723. // angle:angle,
  724. // breakPointId:surroundPointIds[i]
  725. // }
  726. // }
  727. // }
  728. // }
  729. // if (count == 2) {
  730. // //人物移动
  731. // user.player.position = JSON.parse(JSON.stringify(playerPosition));
  732. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  733. // JSON.stringify(playerPosition),
  734. // );
  735. // }
  736. // else{
  737. // if(singleInfo != null){
  738. // return await this.moveDirect(
  739. // playerPosition,
  740. // closestDis,
  741. // breakPointId,
  742. // singleInfo.breakPointId,
  743. // appId,
  744. // userId,
  745. // traceId,
  746. // actionType,
  747. // );
  748. // }
  749. // else if (count == 1) {
  750. // return await this.moveDirect(
  751. // playerPosition,
  752. // closestDis,
  753. // breakPointId,
  754. // neighPoints[0].breakPointId,
  755. // appId,
  756. // userId,
  757. // traceId,
  758. // actionType,
  759. // );
  760. // }
  761. // else if (count == 0) {
  762. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  763. // JSON.stringify(user.player.position),
  764. // );
  765. // this.reply.actionResponses[0].actionType = actionType;
  766. // return this.reply;
  767. // }
  768. // }
  769. // //count == 2
  770. // //超出范围了
  771. // if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  772. // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  773. // if (neighPoints[0].angle == 0) {
  774. // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  775. // }
  776. // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  777. // if (neighPoints[1].angle == 0) {
  778. // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  779. // }
  780. // if (offsetAngle1 > offsetAngle2) {
  781. // chooseBreakPointId = neighPoints[1].breakPointId;
  782. // } else {
  783. // chooseBreakPointId = neighPoints[0].breakPointId;
  784. // }
  785. // return await this.moveCamera(
  786. // breakPointId,
  787. // chooseBreakPointId,
  788. // appId,
  789. // userId,
  790. // traceId,
  791. // actionType,
  792. // );
  793. // } else {
  794. // user.player.position = JSON.parse(JSON.stringify(playerPosition));
  795. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  796. // JSON.stringify(user.player.position),
  797. // );
  798. // this.reply.actionResponses[0].actionType = actionType;
  799. // return this.reply;
  800. // }
  801. // } catch (error) {
  802. // console.log('MoveService', error);
  803. // debugger;
  804. // return null;
  805. // }
  806. // }
  807. async joystick(actionRequest) {
  808. try {
  809. if (this.sendingFrameForJoystick) {
  810. return null;
  811. }
  812. const userId = actionRequest['user_id'];
  813. const traceId = actionRequest['trace_id'];
  814. const dir_action = actionRequest['dir_action'];
  815. const actionType = actionRequest['action_type'];
  816. const user = this.users[userId];
  817. const breakPointId = user.breakPointId;
  818. const appId = user.appId;
  819. //const step = 0.3;
  820. const step = 0.8;
  821. const closestDis = 80; //小于这个距离就跳到邻居呼吸点
  822. const distance = step * dir_action.speed_level;
  823. let angle = null;
  824. // console.log(
  825. // 'handlejoystick-angle->:dir_action.move_angle' + dir_action.move_angle,
  826. // );
  827. if (user.camera.angle.yaw < 0) {
  828. user.camera.angle.yaw = 360 + user.camera.angle.yaw;
  829. }
  830. let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  831. move_angle = move_angle % 360;
  832. // console.log(
  833. // 'handlejoystick-angle->:叠加后move_angle(也是user.angle)' + move_angle,
  834. // );
  835. //TODO 临时增加断言
  836. const playerPosition: Point = { x: 0, y: 0, z: 0 };
  837. playerPosition.x =
  838. user.player.position.x +
  839. distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  840. playerPosition.y =
  841. user.player.position.y +
  842. distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  843. // const offsetX = playerPosition.x - user.player.position.x;
  844. // const offsetY = playerPosition.y - user.player.position.y;
  845. console.log(
  846. 'handlejoysticktesttest:actionRequest-playerPosition' +
  847. JSON.stringify(playerPosition),
  848. );
  849. //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  850. let chooseBreakPointId = null;
  851. const breakPoint = this.breakPointInfo[breakPointId];
  852. const surroundPointIds = breakPoint.contact;
  853. const traceIds = [];
  854. user.player.angle.yaw = move_angle;
  855. traceIds.push(traceId);
  856. this.reply.traceIds = traceIds;
  857. this.reply['newUserStates'][0].userId = userId;
  858. this.reply['actionResponses'][0].traceId = traceId;
  859. this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  860. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  861. JSON.stringify(user.camera.angle),
  862. );
  863. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  864. JSON.stringify(user.camera.position),
  865. );
  866. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  867. JSON.stringify(breakPoint.position),
  868. );
  869. this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  870. this.reply['actionResponses'][0].traceId = traceId;
  871. this.reply.mediaSrc = null;
  872. console.log('joystickjoystick:' + this.cameraInfos.length);
  873. if (surroundPointIds.length == 1) {
  874. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  875. return await this.moveDirect(
  876. playerPosition,
  877. closestDis,
  878. breakPointId,
  879. surroundPointIds[0],
  880. appId,
  881. userId,
  882. traceId,
  883. actionType,
  884. );
  885. }
  886. let count = 0;
  887. const neighPoints = [];
  888. //人在哪个角度
  889. const _angle = this.getAngle(
  890. breakPoint.position,
  891. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  892. playerPosition,
  893. );
  894. // const offsetBreakPosition = {
  895. // x: breakPoint.position.x + offsetX,
  896. // y: breakPoint.position.y + offsetY,
  897. // };
  898. let closestNeighorId = null;
  899. let neighDis = null;
  900. let singleInfo = null;
  901. for (let i = 0; i < surroundPointIds.length; ++i) {
  902. const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  903. if (closestNeighorId == null) {
  904. neighDis = this.getDistance(playerPosition, neighPoint.position);
  905. closestNeighorId = surroundPointIds[i];
  906. } else if (
  907. neighDis > this.getDistance(playerPosition, neighPoint.position)
  908. ) {
  909. neighDis = this.getDistance(playerPosition, neighPoint.position);
  910. closestNeighorId = surroundPointIds[i];
  911. }
  912. neighPoint.breakPointId = surroundPointIds[i];
  913. angle = this.getAngle(
  914. breakPoint.position,
  915. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  916. neighPoint.position,
  917. );
  918. //if(angle<45&&angle!=0){
  919. if (
  920. // Math.abs(angle - move_angle) < 45 &&
  921. // Math.abs(angle - move_angle) != 0
  922. Math.abs(angle - _angle) < 45 &&
  923. Math.abs(angle - _angle) != 0
  924. ) {
  925. neighPoint.angle = angle;
  926. neighPoints.push(neighPoint);
  927. ++count;
  928. } else if (Math.abs(angle - move_angle) == 0) {
  929. neighPoint.angle = angle;
  930. neighPoints.push(neighPoint);
  931. ++count;
  932. break;
  933. }
  934. //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  935. if (angle == 0 && Math.abs(360 - _angle) < 45) {
  936. neighPoint.angle = angle;
  937. neighPoints.push(neighPoint);
  938. ++count;
  939. }
  940. if (
  941. Math.abs(angle - move_angle) < 45 ||
  942. Math.abs(angle + 360 - move_angle) < 45
  943. ) {
  944. if (singleInfo == null) {
  945. singleInfo = {
  946. angle: angle,
  947. breakPointId: surroundPointIds[i],
  948. };
  949. }
  950. }
  951. }
  952. if (count == 2) {
  953. //人物移动
  954. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  955. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  956. JSON.stringify(playerPosition),
  957. );
  958. } else {
  959. if (singleInfo != null) {
  960. // console.log(
  961. // 'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
  962. // );
  963. //console.log('handlejoysticktesttest校验0角度:'+_angle+','+move_angle) //这个没办法,得换方案
  964. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  965. return await this.moveDirect(
  966. playerPosition,
  967. closestDis,
  968. breakPointId,
  969. singleInfo.breakPointId,
  970. appId,
  971. userId,
  972. traceId,
  973. actionType,
  974. );
  975. } else if (count == 1) {
  976. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  977. return await this.moveDirect(
  978. playerPosition,
  979. closestDis,
  980. breakPointId,
  981. neighPoints[0].breakPointId,
  982. appId,
  983. userId,
  984. traceId,
  985. actionType,
  986. );
  987. } else if (count == 0) {
  988. this.reply['newUserStates'][0].playerState.player.position =
  989. JSON.parse(JSON.stringify(user.player.position));
  990. this.reply.actionResponses[0].actionType = actionType;
  991. //console.log('handlejoysticktesttest:actionRequest-停止'+new Date().getTime());
  992. return this.reply;
  993. }
  994. }
  995. //count == 2
  996. //超出范围了
  997. console.log(
  998. 'handlejoysticktesttest:距离:' +
  999. JSON.stringify(playerPosition) +
  1000. '->' +
  1001. JSON.stringify(breakPoint.position) +
  1002. ':' +
  1003. this.getDistance(playerPosition, breakPoint.position),
  1004. );
  1005. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  1006. // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  1007. // if (neighPoints[0].angle == 0) {
  1008. // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  1009. // }
  1010. // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  1011. // if (neighPoints[1].angle == 0) {
  1012. // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  1013. // }
  1014. // if (offsetAngle1 > offsetAngle2) {
  1015. // chooseBreakPointId = neighPoints[1].breakPointId;
  1016. // } else {
  1017. // chooseBreakPointId = neighPoints[0].breakPointId;
  1018. // }
  1019. if (closestNeighorId == null) {
  1020. debugger;
  1021. }
  1022. chooseBreakPointId = closestNeighorId;
  1023. return await this.moveCamera(
  1024. breakPointId,
  1025. chooseBreakPointId,
  1026. appId,
  1027. userId,
  1028. traceId,
  1029. actionType,
  1030. );
  1031. } else {
  1032. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1033. JSON.stringify(user.player.position),
  1034. );
  1035. this.reply.actionResponses[0].actionType = actionType;
  1036. const cameraInfo = this.getCameraInfo();
  1037. if (cameraInfo != null) {
  1038. console.log('joystick自由--->合并');
  1039. this.reply['newUserStates'][0].playerState.camera.position =
  1040. cameraInfo.camera_position;
  1041. this.reply['newUserStates'][0].playerState.camera.angle =
  1042. cameraInfo.camera_angle;
  1043. if (cameraInfo.mediaSrc) {
  1044. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1045. this.reply.isIDR = cameraInfo.isIDR;
  1046. }
  1047. user.camera.position = JSON.parse(
  1048. JSON.stringify(cameraInfo.camera_position),
  1049. );
  1050. // console.log(
  1051. // 'handlejoystick-angle->:自由并过渡更新user.angle' +
  1052. // cameraInfo.camera_angle.yaw,
  1053. // );
  1054. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1055. this.sendingFrameForJoystick = true;
  1056. } else {
  1057. //console.log('joystick自由--->不合并');
  1058. }
  1059. //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
  1060. console.log(
  1061. 'handlejoysticktesttest:自由:' +
  1062. this.reply.mediaSrc +
  1063. '->' +
  1064. JSON.stringify(user.player.position),
  1065. );
  1066. return this.reply;
  1067. }
  1068. } catch (error) {
  1069. console.log('MoveService', error);
  1070. return null;
  1071. }
  1072. }
  1073. //沿着最合适的neighBreakPointId走
  1074. async moveDirect(
  1075. playerPosition,
  1076. closestDis,
  1077. breakPointId,
  1078. neighBreakPointId,
  1079. appId,
  1080. userId,
  1081. traceId,
  1082. actionType,
  1083. ) {
  1084. const breakPoint = this.breakPointInfo[breakPointId];
  1085. const user = this.users[userId];
  1086. const player_Position = this.getTarget(
  1087. playerPosition,
  1088. breakPoint.position,
  1089. this.breakPointInfo[neighBreakPointId].position,
  1090. );
  1091. // console.log('handlejoysticktesttest校验1线段:'+breakPointId+','+neighBreakPointId)
  1092. // console.log('handlejoysticktesttest校验2计算后的:'+JSON.stringify(player_Position))
  1093. // console.log('handlejoysticktesttest校验3人物变化前:'+JSON.stringify(user.player.position))
  1094. // console.log('handlejoysticktesttest校验4人物本应该的坐标:'+JSON.stringify(playerPosition))
  1095. if (player_Position != null) {
  1096. playerPosition.x = player_Position.x;
  1097. playerPosition.y = player_Position.y;
  1098. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  1099. }
  1100. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  1101. //this.reply.moveOver = true;
  1102. return await this.moveCamera(
  1103. breakPointId,
  1104. neighBreakPointId,
  1105. appId,
  1106. userId,
  1107. traceId,
  1108. actionType,
  1109. );
  1110. } else {
  1111. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1112. JSON.stringify(user.player.position),
  1113. );
  1114. this.reply.actionResponses[0].actionType = actionType;
  1115. const cameraInfo = this.getCameraInfo();
  1116. if (cameraInfo != null) {
  1117. this.reply['newUserStates'][0].playerState.camera.position =
  1118. cameraInfo.camera_position;
  1119. this.reply['newUserStates'][0].playerState.camera.angle =
  1120. cameraInfo.camera_angle;
  1121. if (cameraInfo.mediaSrc) {
  1122. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1123. this.reply.isIDR = cameraInfo.isIDR;
  1124. }
  1125. user.camera.position = JSON.parse(
  1126. JSON.stringify(cameraInfo.camera_position),
  1127. );
  1128. console.log(
  1129. 'handlejoystick-angle->:moveDirect更新user.angle' +
  1130. cameraInfo.camera_angle.yaw,
  1131. );
  1132. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1133. this.sendingFrameForJoystick = true;
  1134. }
  1135. return this.reply;
  1136. }
  1137. }
  1138. async moveCamera(
  1139. breakPointId,
  1140. chooseBreakPointId,
  1141. appId,
  1142. userId,
  1143. traceId,
  1144. actionType,
  1145. ) {
  1146. const user = this.users[userId];
  1147. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1148. JSON.stringify(user.player.position),
  1149. );
  1150. if (chooseBreakPointId == null) {
  1151. this.reply.actionResponses[0].actionType = actionType;
  1152. return this.reply;
  1153. }
  1154. //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
  1155. //相机纠正加过渡
  1156. else {
  1157. if (chooseBreakPointId == user.breakPointId) {
  1158. return this.reply;
  1159. }
  1160. const angle = user.camera.angle.yaw % 45; //纠正需要
  1161. //通过user.camera.angle矫正相机的angle
  1162. const checkReplys = await this.modeifyCameraAngle(
  1163. angle,
  1164. userId,
  1165. traceId,
  1166. actionType,
  1167. );
  1168. for (let i = 0; i < checkReplys.length; ++i) {
  1169. let isIDR = false;
  1170. if (i == 0 || i == checkReplys.length - 1) {
  1171. isIDR = true;
  1172. }
  1173. this.addCameraInfo(
  1174. checkReplys[i]['newUserStates'][0].playerState.camera.position,
  1175. checkReplys[i]['newUserStates'][0].playerState.camera.angle,
  1176. checkReplys[i].mediaSrc,
  1177. isIDR,
  1178. );
  1179. }
  1180. //replys.push(checkReplys);
  1181. //读redis里的数据,按照frame_index的大小排序
  1182. /*
  1183. const key =
  1184. 'moveframe:app_id:' +
  1185. appId +
  1186. ':start_break_point_id:' +
  1187. breakPointId +
  1188. ':end_break_point_id:' +
  1189. chooseBreakPointId +
  1190. ':angle:' +
  1191. Math.floor(user.camera.angle.yaw / 45);
  1192. const moveFramesRes = await this.cacheService.get(key);
  1193. const moveFrames = JSON.parse(moveFramesRes);
  1194. */
  1195. const index = this.getMoveIndex(user.camera.angle.yaw);
  1196. const moveFrames = await this.getMoveFrames(
  1197. appId,
  1198. breakPointId,
  1199. chooseBreakPointId,
  1200. index,
  1201. );
  1202. this.setCameraInfo(appId, moveFrames, breakPointId, chooseBreakPointId);
  1203. user.breakPointId = chooseBreakPointId;
  1204. const cameraInfo = this.getCameraInfo();
  1205. if (cameraInfo != null) {
  1206. this.reply['newUserStates'][0].playerState.camera.position =
  1207. cameraInfo.camera_position;
  1208. this.reply['newUserStates'][0].playerState.camera.angle =
  1209. cameraInfo.camera_angle;
  1210. user.camera.position = JSON.parse(
  1211. JSON.stringify(cameraInfo.camera_position),
  1212. );
  1213. // console.log(
  1214. // 'handlejoystick-angle->:刚刚过渡更新user.angle' +
  1215. // cameraInfo.camera_angle.yaw,
  1216. // );
  1217. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1218. if (cameraInfo.mediaSrc) {
  1219. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1220. this.reply.isIDR = cameraInfo.isIDR;
  1221. }
  1222. this.sendingFrameForJoystick = true;
  1223. }
  1224. //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
  1225. console.log(
  1226. 'handlejoysticktesttest:actionRequest-镜头过渡:从' +
  1227. breakPointId +
  1228. '到' +
  1229. chooseBreakPointId +
  1230. ',' +
  1231. JSON.stringify(user.player.position),
  1232. );
  1233. return this.reply;
  1234. }
  1235. }
  1236. setCameraInfo(appId, moveFrames, startBreakPointId, endBreakPointId) {
  1237. for (let i = 0; i < moveFrames.length; i += moveInterval) {
  1238. moveFrames[i].endBreakPointId = endBreakPointId;
  1239. moveFrames[i].mediaSrc =
  1240. '/' +
  1241. appId +
  1242. '/' +
  1243. startBreakPointId +
  1244. '/' +
  1245. moveFrames[i].file_name.substring(
  1246. 0,
  1247. moveFrames[i].file_name.indexOf('.'),
  1248. ) +
  1249. '/' +
  1250. moveFrames[i].file_name +
  1251. '?m=' +
  1252. new Date().getTime();
  1253. if (startBreakPointId > endBreakPointId) {
  1254. moveFrames[i].mediaSrc =
  1255. '/' +
  1256. appId +
  1257. '/' +
  1258. endBreakPointId +
  1259. '/' +
  1260. moveFrames[i].file_name.substring(
  1261. 0,
  1262. moveFrames[i].file_name.indexOf('.'),
  1263. ) +
  1264. '/' +
  1265. moveFrames[i].file_name +
  1266. '?m=' +
  1267. new Date().getTime();
  1268. }
  1269. if (i == 0 || i == moveFrames.length - 1) {
  1270. moveFrames[i].isIDR = true;
  1271. } else {
  1272. moveFrames[i].isIDR = false;
  1273. }
  1274. this.cameraInfos.push(moveFrames[i]);
  1275. }
  1276. }
  1277. addCameraInfo(cameraPosition, cameraAngle, mediaSrc, isIDR) {
  1278. this.cameraInfos.push({
  1279. camera_position: cameraPosition,
  1280. camera_angle: cameraAngle,
  1281. mediaSrc: mediaSrc,
  1282. isIDR: isIDR,
  1283. });
  1284. }
  1285. getCameraInfo() {
  1286. if (this.cameraInfos.length > 0) {
  1287. const item = this.cameraInfos.shift();
  1288. return item;
  1289. } else {
  1290. return null;
  1291. }
  1292. }
  1293. complementFrame(userId) {
  1294. if (this.cameraInfos.length > 0) {
  1295. //console.log('handlejoysticktesttest:complementFrame-1 继续'+userId+','+new Date().getTime())
  1296. const user = this.users[userId];
  1297. const cameraInfo = this.cameraInfos.shift();
  1298. this.reply.traceIds = [];
  1299. this.reply['newUserStates'][0].userId = userId;
  1300. this.reply['actionResponses'][0].traceId = null;
  1301. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1302. user.player.angle.yaw;
  1303. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1304. JSON.stringify(user.player.position),
  1305. );
  1306. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1307. JSON.stringify(cameraInfo.camera_angle),
  1308. );
  1309. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1310. JSON.stringify(cameraInfo.camera_position),
  1311. );
  1312. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1313. JSON.stringify(user.player.position),
  1314. );
  1315. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1316. this.reply['actionResponses'][0].traceId = null;
  1317. if (cameraInfo.mediaSrc) {
  1318. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1319. this.reply.isIDR = cameraInfo.isIDR;
  1320. }
  1321. user.camera.position = JSON.parse(
  1322. JSON.stringify(cameraInfo.camera_position),
  1323. );
  1324. user.camera.angle = JSON.parse(JSON.stringify(cameraInfo.camera_angle));
  1325. this.sendingFrameForJoystick = true;
  1326. return this.reply;
  1327. } else if (this.cameraInfos.length == 0) {
  1328. if (this.reply['newUserStates'][0].renderInfo.isMoving == 1) {
  1329. //console.log('handlejoysticktesttest:complementFrame-2 停止1'+new Date().getTime())
  1330. return this.stopJoystick(userId);
  1331. } else {
  1332. //console.log('handlejoysticktesttest:complementFrame-2 停止2'+new Date().getTime())
  1333. return null;
  1334. }
  1335. }
  1336. }
  1337. stopJoystick(userId) {
  1338. //console.log('handlejoysticktesttest:stopJoystick');
  1339. this.reply.traceIds = [];
  1340. const user = this.users[userId];
  1341. this.reply['newUserStates'][0].userId = userId;
  1342. this.reply['actionResponses'][0].traceId = null;
  1343. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1344. user.player.angle.yaw;
  1345. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1346. JSON.stringify(user.player.position),
  1347. );
  1348. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1349. JSON.stringify(user.camera.angle),
  1350. );
  1351. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1352. JSON.stringify(user.camera.position),
  1353. );
  1354. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1355. JSON.stringify(user.player.position),
  1356. );
  1357. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1358. this.reply['actionResponses'][0].traceId = null;
  1359. this.reply.mediaSrc = null;
  1360. return this.reply;
  1361. }
  1362. async modeifyCameraAngle(angle, userId, traceId, actionType) {
  1363. const checkReplys = [];
  1364. if (angle > 22) {
  1365. angle = 45 - angle;
  1366. for (let i = 0; i < angle; ++i) {
  1367. // console.warn('矫正一次:' + i);
  1368. const reply = await this.rotateService.rotateForAngle(userId, 1);
  1369. // console.warn(
  1370. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1371. // );
  1372. reply.traceIds = [];
  1373. reply.traceIds.push(traceId);
  1374. const actionResponse = this.rotateService.createActionResponse(
  1375. actionType,
  1376. traceId,
  1377. );
  1378. reply.actionResponses = [];
  1379. reply.actionResponses.push(actionResponse);
  1380. checkReplys.push(reply);
  1381. }
  1382. } else if (angle != 0) {
  1383. for (let i = angle; i > -1; --i) {
  1384. // console.warn('矫正一次:' + i);
  1385. const reply = await this.rotateService.rotateForAngle(userId, -1);
  1386. // console.warn(
  1387. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1388. // );
  1389. reply.traceIds = [];
  1390. reply.traceIds.push(traceId);
  1391. const actionResponse = this.rotateService.createActionResponse(
  1392. actionType,
  1393. traceId,
  1394. );
  1395. reply.actionResponses = [];
  1396. reply.actionResponses.push(actionResponse);
  1397. checkReplys.push(reply);
  1398. }
  1399. }
  1400. return checkReplys;
  1401. }
  1402. getTarget(position, position1, position2) {
  1403. const line = this.createLine1(position1, position2);
  1404. const join = this.getJoinLinePoint(position, line);
  1405. // if (this.isContainForSegment(join, position1, position2)) {
  1406. // return join;
  1407. // } else {
  1408. // return null;
  1409. // }
  1410. return join;
  1411. }
  1412. getDistance(position1, position2) {
  1413. return Math.sqrt(
  1414. (position1.x - position2.x) * (position1.x - position2.x) +
  1415. (position1.y - position2.y) * (position1.y - position2.y),
  1416. );
  1417. }
  1418. isContainForSegment(point, startPoint, endPoint) {
  1419. const minDis = 0.1;
  1420. const dis1 =
  1421. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  1422. const dis2 = this.getDistance(startPoint, endPoint);
  1423. if (Math.abs(dis1 - dis2) < minDis) {
  1424. return true;
  1425. } else {
  1426. return false;
  1427. }
  1428. }
  1429. //两条线的交点
  1430. getIntersectionPoint(parameter1, parameter2) {
  1431. if (this.isParallel(parameter1, parameter2)) {
  1432. return null;
  1433. }
  1434. if (
  1435. typeof parameter1.a == 'undefined' &&
  1436. typeof parameter2.a != 'undefined'
  1437. ) {
  1438. if (parameter1.x) {
  1439. return {
  1440. x: parameter1.x,
  1441. y: parameter2.a * parameter1.x + parameter2.b,
  1442. };
  1443. } else if (parameter1.y) {
  1444. return {
  1445. x: (parameter1.y - parameter2.b) / parameter2.a,
  1446. y: parameter1.y,
  1447. };
  1448. }
  1449. } else if (
  1450. typeof parameter2.a == 'undefined' &&
  1451. typeof parameter1.a != 'undefined'
  1452. ) {
  1453. if (parameter2.x) {
  1454. return {
  1455. x: parameter2.x,
  1456. y: parameter1.a * parameter2.x + parameter1.b,
  1457. };
  1458. } else if (parameter2.y) {
  1459. return {
  1460. x: (parameter2.y - parameter1.b) / parameter1.a,
  1461. y: parameter2.y,
  1462. };
  1463. }
  1464. } else if (
  1465. typeof parameter2.a == 'undefined' &&
  1466. typeof parameter1.a == 'undefined'
  1467. ) {
  1468. if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
  1469. return { x: parameter1.x, y: parameter2.y };
  1470. } else if (
  1471. parameter1.hasOwnProperty('y') &&
  1472. parameter2.hasOwnProperty('x')
  1473. ) {
  1474. return { x: parameter2.x, y: parameter1.y };
  1475. } else {
  1476. return null;
  1477. }
  1478. }
  1479. if (parameter1.a == parameter2.a) {
  1480. return null;
  1481. }
  1482. const joinpointx =
  1483. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  1484. const joinpointy =
  1485. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  1486. (parameter1.a - parameter2.a);
  1487. const point = { x: joinpointx, y: joinpointy };
  1488. return point;
  1489. }
  1490. // 垂直线
  1491. getVerticalLine(line, point) {
  1492. if (typeof line.a === 'undefined') {
  1493. if (line.hasOwnProperty('x')) {
  1494. return { y: point.y };
  1495. } else if (line.hasOwnProperty('y')) {
  1496. return { x: point.x };
  1497. } else {
  1498. return null;
  1499. }
  1500. } else if (line.a == 0) {
  1501. return { x: point.x };
  1502. } else {
  1503. const tl = {} as any as VerticalLineType;
  1504. tl.a = -1 / line.a;
  1505. const result = this.createLine2(tl, point);
  1506. return result;
  1507. }
  1508. }
  1509. // 经过point且与line垂直的直线,该直线与line的交点
  1510. getJoinLinePoint(point, line) {
  1511. const verticalLine = this.getVerticalLine(line, point);
  1512. const join = this.getIntersectionPoint(line, verticalLine);
  1513. return join;
  1514. }
  1515. // 与lineA平行并且point在线上
  1516. createLine2(lineA, point) {
  1517. const parameter = {} as any as {
  1518. x: number;
  1519. y: number;
  1520. a: number;
  1521. b: number;
  1522. };
  1523. if (typeof lineA.a === 'undefined') {
  1524. if (typeof lineA.x !== 'undefined') {
  1525. parameter.x = point.x;
  1526. } else if (typeof lineA.y !== 'undefined') {
  1527. parameter.y = point.y;
  1528. }
  1529. } else {
  1530. parameter.a = lineA.a;
  1531. parameter.b = point.y - point.x * lineA.a;
  1532. }
  1533. return parameter;
  1534. }
  1535. createLine1(point1, point2) {
  1536. if (point1.x == point2.x && point1.y == point2.y) {
  1537. return null;
  1538. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  1539. return { x: point1.x };
  1540. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  1541. return { y: point1.y };
  1542. }
  1543. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  1544. const parameterb =
  1545. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  1546. if (this.getFixed(parametera) == 0) {
  1547. return { y: this.getFixed(parameterb) };
  1548. }
  1549. const parameter = {
  1550. a: this.getFixed(parametera),
  1551. b: this.getFixed(parameterb),
  1552. };
  1553. return parameter;
  1554. }
  1555. //返回true表示平行
  1556. isParallel(line1, line2) {
  1557. if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
  1558. if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
  1559. return true;
  1560. } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
  1561. return true;
  1562. } else {
  1563. return false;
  1564. }
  1565. } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
  1566. return false;
  1567. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  1568. return true;
  1569. } else {
  1570. return false;
  1571. }
  1572. }
  1573. getFixed(num) {
  1574. const decimal = 2;
  1575. return parseFloat(num.toFixed(decimal));
  1576. }
  1577. isPointInPoly(position, breakPointIds) {
  1578. const x = position.x;
  1579. const y = position.y;
  1580. let inside = false;
  1581. for (
  1582. let i = 0, j = breakPointIds.length - 1;
  1583. i < breakPointIds.length;
  1584. j = i++
  1585. ) {
  1586. const pt1 = this.breakPointInfo[breakPointIds[i]];
  1587. const pt2 = this.breakPointInfo[breakPointIds[j]];
  1588. const xi = pt1.x;
  1589. const yi = pt1.y;
  1590. const xj = pt2.x;
  1591. const yj = pt2.y;
  1592. const intersect =
  1593. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  1594. if (intersect) inside = !inside;
  1595. }
  1596. return inside;
  1597. }
  1598. getMoveIndex(angle) {
  1599. if (angle < 0) {
  1600. angle = 360 + angle;
  1601. }
  1602. for (let i = 0; i < 8; ++i) {
  1603. if (angle < 45 * i + 45 / 2) {
  1604. return i;
  1605. }
  1606. }
  1607. //超过了337
  1608. return 0;
  1609. }
  1610. }