ActionsHandler.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import Actions from "./enum/Actions.js"
  2. import {eventsManager} from "./EventsManager.js"
  3. import util from "./util.js"
  4. import Person from "./enum/Person.js"
  5. import ClickType from "./enum/ClickType.js"
  6. import Logger from "./Logger.js"
  7. import Response from "./Response.js"
  8. import { VDecoder } from "./h264Decoder/VDecoder.js";
  9. const logger = new Logger('actions-handler')
  10. const QueueActions = [Actions.Transfer, Actions.ChangeSkin, Actions.GetOnVehicle, Actions.GetOffVehicle];
  11. export default class ActionsHandler {
  12. constructor(e) {
  13. this.currentActiveAction = null
  14. this.room = e
  15. //xst
  16. window.workerReady = false
  17. this.vDecoder = new VDecoder({
  18. maxChip: 100,
  19. });
  20. window.vDecoder = this.vDecoder
  21. this.init()
  22. }
  23. init(){
  24. this.vDecoder.on('ready', () => {
  25. console.log("ready");
  26. window.workerReady = true
  27. })
  28. }
  29. async avatarComponentsSync(e){
  30. const t = {
  31. action_type: Actions.SetPlayerState,
  32. set_player_state_action: {
  33. player_state: {
  34. avatar_components: JSON.stringify(e)
  35. }
  36. }
  37. };
  38. this.sendData({
  39. data: t
  40. })
  41. }
  42. async sendData(e) {
  43. //console.log('发送数据:'+JSON.stringify(e))
  44. await this.beforeSend(e);
  45. const t = util.uuid();
  46. this.room.networkController.sendRtcData(le(oe({}, e.data), {
  47. trace_id: t,
  48. user_id: this.room.options.userId
  49. }));
  50. if (e.track === !1)
  51. {
  52. return Promise.resolve(null);
  53. }
  54. const {sampleRate: r=1, timeout: n=2e3, tag: o, data: a, special: s} = e;
  55. return eventsManager.track({
  56. timeout: n,
  57. traceId: t,
  58. event: a.action_type,
  59. tag: o,
  60. extra: a
  61. }, {
  62. special: s,
  63. sampleRate: r,
  64. noReport: this.room.viewMode === "serverless" || this.room.options.viewMode === "serverless"
  65. }).finally(()=>{
  66. QueueActions.includes(e.data.action_type) && (this.currentActiveAction = void 0)
  67. }
  68. )
  69. }
  70. //async sendData(e) {
  71. //console.log('发送数据:'+JSON.stringify(e))
  72. //旋转
  73. //window.room.sceneManager.scene.activeCamera.rotation.y
  74. //心跳:要去掉
  75. //邻居点:要去掉
  76. //漫游
  77. //人物所在位置和角度,相机的位置和角度,相机的目标点,点击的坐标
  78. //}
  79. async beforeSend(e) {
  80. var o;
  81. const t = (o = this.room._userAvatar) == null ? void 0 : o.isMoving
  82. , r = e.data.action_type;
  83. if (QueueActions.includes(r)) {
  84. if (this.currentActiveAction)
  85. return logger.error(`${Actions[this.currentActiveAction]} still pending, reject ${Actions[r]}`),
  86. Promise.reject(new FrequencyLimitError(`${Actions[r]} action request frequency limit`));
  87. this.currentActiveAction = r
  88. }
  89. if (t && QueueActions.includes(e.data.action_type))
  90. try {
  91. await this.stopMoving()
  92. } catch (a) {
  93. this.currentActiveAction = void 0,
  94. logger.error("before action stopMoving failed", a)
  95. }
  96. }
  97. async moveTo(e) {
  98. const {point: t, extra: r="", motionType: n} = e
  99. , o = {
  100. action_type: Actions.Clicking,
  101. clicking_action: {
  102. clicking_point: t,
  103. clicking_type: ClickType.IgnoreView,
  104. extra: encodeURIComponent(r),
  105. attitude: n
  106. },
  107. clicking_state: this.room._currentClickingState
  108. };
  109. return this.sendData({
  110. data: o
  111. })
  112. }
  113. transfer(e) {
  114. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s, person: l, noMedia: u, timeout: c, tag: h, special: f} = e
  115. , d = {
  116. data: {
  117. action_type: Actions.Transfer,
  118. transfer_action: {
  119. render_type: t,
  120. player: r,
  121. camera: n,
  122. areaName: o,
  123. attitude: a,
  124. pathName: s,
  125. person: {
  126. type: l
  127. },
  128. noMedia: u,
  129. tiles: [0, 1, 2, 4]
  130. }
  131. },
  132. special: f,
  133. timeout: c || 4e3,
  134. tag: h
  135. };
  136. return this.sendData(d).then(_=>(typeof l != "undefined" && this.room.updateCurrentNetworkOptions({
  137. person: l,
  138. rotationRenderType: t
  139. }),
  140. _))
  141. }
  142. changeRotationRenderType(e) {
  143. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s} = e;
  144. return this.transfer({
  145. renderType: t,
  146. player: r,
  147. camera: n,
  148. areaName: o,
  149. attitude: a,
  150. pathName: s,
  151. tag: "changeToRotationVideo"
  152. })
  153. }
  154. requestPanorama(e, noMedia, timeout) {
  155. const {camera: camera, player: player, areaName: areaName, attitude: attitude, pathName: pathName, tag: tag} = e;
  156. return this.transfer({
  157. renderType: RenderType.ClientRotationPano,
  158. player: player,
  159. camera: camera,
  160. person: Person.First,
  161. areaName: areaName,
  162. attitude: attitude,
  163. pathName: pathName,
  164. noMedia: noMedia,
  165. timeout: timeout,
  166. tag: tag || "requestPanorama",
  167. special: !noMedia
  168. })
  169. }
  170. setMotionType(e) {
  171. return this.transfer({
  172. attitude: e,
  173. tag: "setMotionType"
  174. })
  175. }
  176. setNickName(e) {
  177. const t = {
  178. action_type: Actions.ChangeNickname,
  179. change_nickname_action: {
  180. nickname: e
  181. }
  182. };
  183. return this.sendData({
  184. data: t
  185. })
  186. }
  187. getReserveSeat({routeId: e, name: t}) {
  188. const r = {
  189. action_type: Actions.ReserveSeat,
  190. reserve_seat_action: {
  191. route_id: e,
  192. name: t
  193. }
  194. };
  195. return this.sendData({
  196. data: r
  197. })
  198. }
  199. getReserveStatus({routeId: e, name: t, need_detail: r}) {
  200. const n = {
  201. action_type: Actions.GetReserveStatus,
  202. get_reserve_status_action: {
  203. route_id: e,
  204. name: t,
  205. need_detail: r
  206. }
  207. };
  208. return this.sendData({
  209. data: n,
  210. timeout: 2e3
  211. }).then(o=>o.reserveDetail)
  212. }
  213. stopMoving() {
  214. const e = {
  215. action_type: Actions.StopMoving,
  216. stop_move_action: {}
  217. };
  218. return this.sendData({
  219. data: e
  220. })
  221. }
  222. getOnVehicle({routeId: e, name: t, camera: r}) {
  223. const n = {
  224. action_type: Actions.GetOnVehicle,
  225. get_on_vehicle_action: {
  226. route_id: e,
  227. name: t,
  228. camera: r
  229. }
  230. };
  231. return this.sendData({
  232. data: n
  233. })
  234. }
  235. getOffVehicle({renderType: e, player: t, camera: r}) {
  236. const n = {
  237. action_type: Actions.GetOffVehicle,
  238. get_off_vehicle_action: {
  239. render_type: e,
  240. player: t,
  241. camera: r
  242. }
  243. };
  244. return this.sendData({
  245. data: n
  246. })
  247. }
  248. confirmEvent(e) {
  249. const t = {
  250. action_type: Actions.ConfirmEvent,
  251. confirm_event_action: {
  252. id: e
  253. }
  254. };
  255. return this.sendData({
  256. data: t,
  257. track: !1
  258. })
  259. }
  260. echo(e) {
  261. const t = {
  262. action_type: Actions.Echo,
  263. echo_msg: {
  264. echoMsg: e
  265. }
  266. };
  267. return this.sendData({
  268. data: t,
  269. track: !1
  270. })
  271. }
  272. async changeSkin(e) {
  273. const t = e.special === void 0 ? e.renderType === RenderType.ClientRotationPano : e.special
  274. , {skinId: r, mode: n, landingType: o=LandingType.Stay, landingPoint: a, landingCamera: s, renderType: l, areaName: u, attitude: c, pathName: h, person: f, noMedia: d, timeout: _, roomTypeId: g=""} = e
  275. , m = this.room.skinList.filter(y=>y.id === r)[0];
  276. if (!m) {
  277. const y = `skin ${r} is invalid`;
  278. return logger.error(y),
  279. Promise.reject(new ParamError(y))
  280. }
  281. const v = {
  282. action_type: Actions.ChangeSkin,
  283. change_skin_action: {
  284. skinID: r,
  285. mode: n === ChangeMode.Preview ? ChangeMode.Preview : ChangeMode.Confirm,
  286. skin_data_version: r + m.versionId,
  287. landing_type: o,
  288. landing_point: a,
  289. landing_camera: s,
  290. render_wrapper: {
  291. render_type: l
  292. },
  293. areaName: u,
  294. attitude: c,
  295. noMedia: d,
  296. person: f,
  297. pathName: h,
  298. roomTypeId: g
  299. }
  300. };
  301. return this.sendData({
  302. data: v,
  303. timeout: _ || 6e3,
  304. special: t
  305. }).then(async y=>{
  306. if (l === RenderType.ClientRotationPano && y) {
  307. const b = await this.room.modelManager.findRoute(r, h)
  308. , {camera: T} = util.getRandomItem(b.birthPointList) || {};
  309. await this.room.panorama.handleReceivePanorama(y, T)
  310. }
  311. return this.handleChangeSkin(e)
  312. }
  313. ).catch(y=>d ? this.handleChangeSkin(e) : Promise.reject(y))
  314. }
  315. handleChangeSkin(e) {
  316. const {skinId: t, mode: r, renderType: n, areaName: o, attitude: a, pathName: s} = e;
  317. return this.room.sceneManager.staticmeshComponent.getCgMesh().show(),
  318. this.room.sceneManager.cameraComponent.switchToCgCamera(),
  319. this.room.engineProxy._updateSkinAssets(t).then(()=>{
  320. this.room.sceneManager.staticmeshComponent.getCgMesh().hide(),
  321. this.room.sceneManager.cameraComponent.switchToMainCamera(),
  322. this.room.pathManager.currentArea = o,
  323. logger.info("changeSkin _updateSkinAssets susccss"),
  324. this.room.updateCurrentNetworkOptions({
  325. pathName: s,
  326. attitude: a,
  327. areaName: o
  328. }),
  329. this.room.skinChangedHook(),
  330. this.room.emit("skinChanged", {
  331. skin: {
  332. id: t
  333. },
  334. mode: r
  335. }),
  336. n === RenderType.ClientRotationPano && this.room.sceneManager.cameraComponent.allowMainCameraController()
  337. }
  338. )
  339. }
  340. /***********************************************************************xst****************************************************************/
  341. /*
  342. rotate({pitch: e, yaw: t}) {
  343. var n;
  344. if (this.room.disableRotate || this.room.isPano || ((n = this.room._userAvatar) == null ? void 0 : n._isChangingComponentsMode))
  345. return;
  346. const r = {
  347. action_type: Actions.Rotation,
  348. rotation_action: {
  349. vertical_move: e,
  350. horizontal_move: -t
  351. }
  352. };
  353. //console.log('发送数据rotate:'+JSON.stringify(r.rotation_action))
  354. this.sendData({
  355. data: r,
  356. sampleRate: .02
  357. })
  358. }
  359. */
  360. /*
  361. rotate({type: type, angle: angle}) {
  362. let endRotation = window.room.sceneManager.cameraComponent.mainCamera.rotation.y
  363. if(endRotation<0){
  364. endRotation += 2*Math.PI
  365. }
  366. else if(endRotation>2*Math.PI){
  367. endRotation -= 2*Math.PI
  368. }
  369. let sfns = Math.round(endRotation/Math.PI * 180 /6)
  370. //每6°一帧
  371. for(let i=0;i<60;++i){
  372. if(Math.abs(Math.abs(angle) - i/60 * 2 * Math.PI)<0.01){
  373. //找到了
  374. endRotation += angle
  375. break;
  376. }
  377. }
  378. let efns = Math.round(endRotation/Math.PI * 180 /6)
  379. console.log('取帧:'+sfns+','+efns);
  380. this.fetchData({
  381. type:type,
  382. sFrame:sfns,
  383. eFrame:efns
  384. })
  385. this.room.sceneManager.materialComponent.initreceveFrames()
  386. // this.room.sceneManager.cameraComponent.mainCamera.rotation.y = endRotation
  387. // window.room.sceneManager.cameraComponent._cameraPose.rotation._y = endRotation
  388. // window.room.sceneManager.cameraComponent.mainCamera.rotation.y=endRotation
  389. let response = JSON.parse(JSON.stringify(Response))
  390. response.signal.newUserStates[0].playerState.player.position.x = this.room.avatarManager.avatars.get(nickname).position.x
  391. response.signal.newUserStates[0].playerState.player.position.y = this.room.avatarManager.avatars.get(nickname).position.y
  392. response.signal.newUserStates[0].playerState.player.position.z = this.room.avatarManager.avatars.get(nickname).position.z
  393. response.signal.newUserStates[0].playerState.player.angle.pitch = this.room.avatarManager.avatars.get(nickname).rotation.pitch
  394. response.signal.newUserStates[0].playerState.player.angle.yaw = this.room.avatarManager.avatars.get(nickname).rotation.yaw
  395. response.signal.newUserStates[0].playerState.player.angle.roll = this.room.avatarManager.avatars.get(nickname).rotation.roll
  396. response.signal.newUserStates[0].playerState.camera.position =
  397. response.signal.newUserStates[0].playerState.camera.angle =
  398. response.signal.newUserStates[0].playerState.cameraCenter.x = this.room.avatarManager.avatars.get(nickname).position.x
  399. response.signal.newUserStates[0].playerState.cameraCenter.y = this.room.avatarManager.avatars.get(nickname).position.y
  400. response.signal.newUserStates[0].playerState.cameraCenter.z = this.room.avatarManager.avatars.get(nickname).position.z
  401. }
  402. */
  403. fetchData({sFrame: sfns, eFrame: efns}){
  404. if(window.workerReady){
  405. this.vDecoder.fetch({
  406. path: "https://laser-data.oss-cn-shenzhen.aliyuncs.com/test-video/earth",
  407. range: [sfns, efns],
  408. });
  409. }
  410. else{
  411. console.error('还没准备好')
  412. }
  413. }
  414. /*******************************************************************************************************************************************/
  415. turnTo(e) {
  416. const {point: t, timeout: r=2e3, offset: n=8} = e || {}
  417. , o = {
  418. action_type: Actions.TurnTo,
  419. turn_to_action: {
  420. turn_to_point: t,
  421. offset: n
  422. }
  423. };
  424. return this.sendData({
  425. data: o,
  426. timeout: r
  427. })
  428. }
  429. rotateTo(e) {
  430. const {point: t, offset: r=0, speed: n=3} = e || {}
  431. , o = {
  432. action_type: Actions.RotateTo,
  433. rotate_to_action: {
  434. rotate_to_point: t,
  435. offset: r,
  436. speed: n
  437. }
  438. };
  439. console.log('发送数据rotateTo:'+JSON.stringify(o.rotate_to_action))
  440. return this.sendData({
  441. data: o
  442. })
  443. }
  444. broadcast(e) {
  445. const {data: t, msgType: r=MessageHandleType.MHT_FollowListMulticast, targetUserIds: n} = e;
  446. if (r === MessageHandleType.MHT_CustomTargetSync && !Array.isArray(n))
  447. return Promise.reject(new ParamError(`param targetUserIds is required when msgType is ${MessageHandleType[r]}`));
  448. const o = {
  449. action_type: Actions.Broadcast,
  450. broadcast_action: {
  451. data: JSON.stringify(t),
  452. user_id: this.room.options.userId,
  453. msgType: r
  454. }
  455. };
  456. return Array.isArray(n) && r === MessageHandleType.MHT_CustomTargetSync && (o.broadcast_action.target_user_ids = n),
  457. this.room.actionsHandler.sendData({
  458. data: o,
  459. tag: t.broadcastType
  460. })
  461. }
  462. getNeighborPoints(e) {
  463. const {point: t, containSelf: r=!1, searchRange: n=500} = e
  464. , o = {
  465. action_type: Actions.GetNeighborPoints,
  466. get_neighbor_points_action: {
  467. point: t,
  468. level: 1,
  469. containSelf: r,
  470. searchRange: n
  471. }
  472. };
  473. return this.sendData({
  474. data: o
  475. }).then(a=>a.nps)
  476. }
  477. playCG(e) {
  478. const t = {
  479. action_type: Actions.PlayCG,
  480. play_cg_action: {
  481. cg_name: e
  482. }
  483. };
  484. return this.sendData({
  485. data: t
  486. })
  487. }
  488. audienceToVisitor(e) {
  489. const {avatarId: t, avatarComponents: r, player: n, camera: o} = e
  490. , a = {
  491. action_type: Actions.AudienceChangeToVisitor,
  492. audienceChangeToVisitorAction: {
  493. avatarID: t,
  494. avatarComponents: r,
  495. player: n,
  496. camera: o
  497. }
  498. };
  499. return logger.debug("send data: audience to visitor"),
  500. this.sendData({
  501. data: a
  502. })
  503. }
  504. visitorToAudience(e) {
  505. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s, person: l, noMedia: u} = e
  506. , c = {
  507. action_type: Actions.VisitorChangeToAudience,
  508. visitorChangeToAudienceAction: {
  509. transferAction: {
  510. render_type: t,
  511. player: r,
  512. camera: n,
  513. areaName: o,
  514. attitude: a,
  515. pathName: s,
  516. person: {
  517. type: l
  518. },
  519. noMedia: u,
  520. tiles: [0, 1, 2, 4]
  521. }
  522. }
  523. };
  524. return logger.debug("send data: visitor to audience"),
  525. this.sendData({
  526. data: c
  527. })
  528. }
  529. removeVisitor(e) {
  530. const {removeType: t, userIDList: r, extraInfo: n=""} = e
  531. , o = {
  532. action_type: Actions.RemoveVisitor,
  533. removeVisitorAction: {
  534. removeVisitorEvent: t,
  535. userIDList: r,
  536. extraInfo: encodeURIComponent(n)
  537. }
  538. };
  539. return logger.debug("send data: remove visitor"),
  540. this.sendData({
  541. data: o
  542. })
  543. }
  544. getUserWithAvatar(e, t) {
  545. const r = {
  546. action_type: Actions.GetUserWithAvatar,
  547. getUserWithAvatarAction: {
  548. userType: e,
  549. roomID: t
  550. }
  551. };
  552. return logger.debug("send data: get user with avatar"),
  553. this.sendData({
  554. data: r
  555. }).then(n=>n.userWithAvatarList)
  556. }
  557. joystick(e) {
  558. const {degree: t, level: r=1} = e
  559. , n = util.uuid();
  560. let o = -t + 90 + 360;
  561. o >= 360 && (o -= 360);
  562. const a = {
  563. action_type: Actions.Joystick,
  564. dir_action: {
  565. move_angle: o,
  566. speed_level: r
  567. },
  568. trace_id: n,
  569. user_id: this.room.options.userId,
  570. packet_id: n
  571. };
  572. return this.sendData({
  573. data: a,
  574. track: !1
  575. })
  576. }
  577. }