ActionsHandler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. const log$p = new Logger("actions-handler")
  2. , QueueActions = [Actions.Transfer, Actions.ChangeSkin, Actions.GetOnVehicle, Actions.GetOffVehicle];
  3. class ActionsHandler {
  4. constructor(e) {
  5. E(this, "room");
  6. E(this, "currentActiveAction");
  7. E(this, "avatarComponentsSync", e=>{
  8. const t = {
  9. action_type: Actions.SetPlayerState,
  10. set_player_state_action: {
  11. player_state: {
  12. avatar_components: JSON.stringify(e)
  13. }
  14. }
  15. };
  16. this.sendData({
  17. data: t
  18. })
  19. }
  20. );
  21. this.room = e
  22. }
  23. async sendData(e) {
  24. await this.beforeSend(e);
  25. const t = uuid$1();
  26. if (this.room.networkController.sendRtcData(le(oe({}, e.data), {
  27. trace_id: t,
  28. user_id: this.room.options.userId
  29. })),
  30. e.track === !1)
  31. return Promise.resolve(null);
  32. const {sampleRate: r=1, timeout: n=2e3, tag: o, data: a, special: s} = e;
  33. return eventsManager.track({
  34. timeout: n,
  35. traceId: t,
  36. event: a.action_type,
  37. tag: o,
  38. extra: a
  39. }, {
  40. special: s,
  41. sampleRate: r,
  42. noReport: this.room.viewMode === "serverless" || this.room.options.viewMode === "serverless"
  43. }).finally(()=>{
  44. QueueActions.includes(e.data.action_type) && (this.currentActiveAction = void 0)
  45. }
  46. )
  47. }
  48. async beforeSend(e) {
  49. var o;
  50. const t = (o = this.room._userAvatar) == null ? void 0 : o.isMoving
  51. , r = e.data.action_type;
  52. if (QueueActions.includes(r)) {
  53. if (this.currentActiveAction)
  54. return log$p.error(`${Actions[this.currentActiveAction]} still pending, reject ${Actions[r]}`),
  55. Promise.reject(new FrequencyLimitError(`${Actions[r]} action request frequency limit`));
  56. this.currentActiveAction = r
  57. }
  58. if (t && QueueActions.includes(e.data.action_type))
  59. try {
  60. await this.stopMoving()
  61. } catch (a) {
  62. this.currentActiveAction = void 0,
  63. log$p.error("before action stopMoving failed", a)
  64. }
  65. }
  66. async moveTo(e) {
  67. const {point: t, extra: r="", motionType: n} = e
  68. , o = {
  69. action_type: Actions.Clicking,
  70. clicking_action: {
  71. clicking_point: t,
  72. clicking_type: ClickType.IgnoreView,
  73. extra: encodeURIComponent(r),
  74. attitude: n
  75. },
  76. clicking_state: this.room._currentClickingState
  77. };
  78. return this.sendData({
  79. data: o
  80. })
  81. }
  82. transfer(e) {
  83. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s, person: l, noMedia: u, timeout: c, tag: h, special: f} = e
  84. , d = {
  85. data: {
  86. action_type: Actions.Transfer,
  87. transfer_action: {
  88. render_type: t,
  89. player: r,
  90. camera: n,
  91. areaName: o,
  92. attitude: a,
  93. pathName: s,
  94. person: {
  95. type: l
  96. },
  97. noMedia: u,
  98. tiles: [0, 1, 2, 4]
  99. }
  100. },
  101. special: f,
  102. timeout: c || 4e3,
  103. tag: h
  104. };
  105. return this.sendData(d).then(_=>(typeof l != "undefined" && this.room.updateCurrentNetworkOptions({
  106. person: l,
  107. rotationRenderType: t
  108. }),
  109. _))
  110. }
  111. changeRotationRenderType(e) {
  112. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s} = e;
  113. return this.transfer({
  114. renderType: t,
  115. player: r,
  116. camera: n,
  117. areaName: o,
  118. attitude: a,
  119. pathName: s,
  120. tag: "changeToRotationVideo"
  121. })
  122. }
  123. requestPanorama(e, t, r) {
  124. const {camera: n, player: o, areaName: a, attitude: s, pathName: l, tag: u} = e;
  125. return this.transfer({
  126. renderType: RenderType.ClientRotationPano,
  127. player: o,
  128. camera: n,
  129. person: Person.First,
  130. areaName: a,
  131. attitude: s,
  132. pathName: l,
  133. noMedia: t,
  134. timeout: r,
  135. tag: u || "requestPanorama",
  136. special: !t
  137. })
  138. }
  139. setMotionType(e) {
  140. return this.transfer({
  141. attitude: e,
  142. tag: "setMotionType"
  143. })
  144. }
  145. setNickName(e) {
  146. const t = {
  147. action_type: Actions.ChangeNickname,
  148. change_nickname_action: {
  149. nickname: e
  150. }
  151. };
  152. return this.sendData({
  153. data: t
  154. })
  155. }
  156. getReserveSeat({routeId: e, name: t}) {
  157. const r = {
  158. action_type: Actions.ReserveSeat,
  159. reserve_seat_action: {
  160. route_id: e,
  161. name: t
  162. }
  163. };
  164. return this.sendData({
  165. data: r
  166. })
  167. }
  168. getReserveStatus({routeId: e, name: t, need_detail: r}) {
  169. const n = {
  170. action_type: Actions.GetReserveStatus,
  171. get_reserve_status_action: {
  172. route_id: e,
  173. name: t,
  174. need_detail: r
  175. }
  176. };
  177. return this.sendData({
  178. data: n,
  179. timeout: 2e3
  180. }).then(o=>o.reserveDetail)
  181. }
  182. stopMoving() {
  183. const e = {
  184. action_type: Actions.StopMoving,
  185. stop_move_action: {}
  186. };
  187. return this.sendData({
  188. data: e
  189. })
  190. }
  191. getOnVehicle({routeId: e, name: t, camera: r}) {
  192. const n = {
  193. action_type: Actions.GetOnVehicle,
  194. get_on_vehicle_action: {
  195. route_id: e,
  196. name: t,
  197. camera: r
  198. }
  199. };
  200. return this.sendData({
  201. data: n
  202. })
  203. }
  204. getOffVehicle({renderType: e, player: t, camera: r}) {
  205. const n = {
  206. action_type: Actions.GetOffVehicle,
  207. get_off_vehicle_action: {
  208. render_type: e,
  209. player: t,
  210. camera: r
  211. }
  212. };
  213. return this.sendData({
  214. data: n
  215. })
  216. }
  217. confirmEvent(e) {
  218. const t = {
  219. action_type: Actions.ConfirmEvent,
  220. confirm_event_action: {
  221. id: e
  222. }
  223. };
  224. return this.sendData({
  225. data: t,
  226. track: !1
  227. })
  228. }
  229. echo(e) {
  230. const t = {
  231. action_type: Actions.Echo,
  232. echo_msg: {
  233. echoMsg: e
  234. }
  235. };
  236. return this.sendData({
  237. data: t,
  238. track: !1
  239. })
  240. }
  241. async changeSkin(e) {
  242. const t = e.special === void 0 ? e.renderType === RenderType.ClientRotationPano : e.special
  243. , {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
  244. , m = this.room.skinList.filter(y=>y.id === r)[0];
  245. if (!m) {
  246. const y = `skin ${r} is invalid`;
  247. return log$p.error(y),
  248. Promise.reject(new ParamError(y))
  249. }
  250. const v = {
  251. action_type: Actions.ChangeSkin,
  252. change_skin_action: {
  253. skinID: r,
  254. mode: n === ChangeMode.Preview ? ChangeMode.Preview : ChangeMode.Confirm,
  255. skin_data_version: r + m.versionId,
  256. landing_type: o,
  257. landing_point: a,
  258. landing_camera: s,
  259. render_wrapper: {
  260. render_type: l
  261. },
  262. areaName: u,
  263. attitude: c,
  264. noMedia: d,
  265. person: f,
  266. pathName: h,
  267. roomTypeId: g
  268. }
  269. };
  270. return this.sendData({
  271. data: v,
  272. timeout: _ || 6e3,
  273. special: t
  274. }).then(async y=>{
  275. if (l === RenderType.ClientRotationPano && y) {
  276. const b = await this.room.modelManager.findRoute(r, h)
  277. , {camera: T} = getRandomItem(b.birthPointList) || {};
  278. await this.room.panorama.handleReceivePanorama(y, T)
  279. }
  280. return this.handleChangeSkin(e)
  281. }
  282. ).catch(y=>d ? this.handleChangeSkin(e) : Promise.reject(y))
  283. }
  284. handleChangeSkin(e) {
  285. const {skinId: t, mode: r, renderType: n, areaName: o, attitude: a, pathName: s} = e;
  286. return this.room.sceneManager.staticmeshComponent.getCgMesh().show(),
  287. this.room.sceneManager.cameraComponent.switchToCgCamera(),
  288. this.room.engineProxy._updateSkinAssets(t).then(()=>{
  289. this.room.sceneManager.staticmeshComponent.getCgMesh().hide(),
  290. this.room.sceneManager.cameraComponent.switchToMainCamera(),
  291. this.room.pathManager.currentArea = o,
  292. log$p.info("changeSkin _updateSkinAssets susccss"),
  293. this.room.updateCurrentNetworkOptions({
  294. pathName: s,
  295. attitude: a,
  296. areaName: o
  297. }),
  298. this.room.skinChangedHook(),
  299. this.room.emit("skinChanged", {
  300. skin: {
  301. id: t
  302. },
  303. mode: r
  304. }),
  305. n === RenderType.ClientRotationPano && this.room.sceneManager.cameraComponent.allowMainCameraController()
  306. }
  307. )
  308. }
  309. rotate({pitch: e, yaw: t}) {
  310. var n;
  311. if (this.room.disableRotate || this.room.isPano || ((n = this.room._userAvatar) == null ? void 0 : n._isChangingComponentsMode))
  312. return;
  313. const r = {
  314. action_type: Actions.Rotation,
  315. rotation_action: {
  316. vertical_move: e,
  317. horizontal_move: -t
  318. }
  319. };
  320. this.sendData({
  321. data: r,
  322. sampleRate: .02
  323. })
  324. }
  325. turnTo(e) {
  326. const {point: t, timeout: r=2e3, offset: n=8} = e || {}
  327. , o = {
  328. action_type: Actions.TurnTo,
  329. turn_to_action: {
  330. turn_to_point: t,
  331. offset: n
  332. }
  333. };
  334. return this.sendData({
  335. data: o,
  336. timeout: r
  337. })
  338. }
  339. rotateTo(e) {
  340. const {point: t, offset: r=0, speed: n=3} = e || {}
  341. , o = {
  342. action_type: Actions.RotateTo,
  343. rotate_to_action: {
  344. rotate_to_point: t,
  345. offset: r,
  346. speed: n
  347. }
  348. };
  349. return this.sendData({
  350. data: o
  351. })
  352. }
  353. broadcast(e) {
  354. const {data: t, msgType: r=MessageHandleType.MHT_FollowListMulticast, targetUserIds: n} = e;
  355. if (r === MessageHandleType.MHT_CustomTargetSync && !Array.isArray(n))
  356. return Promise.reject(new ParamError(`param targetUserIds is required when msgType is ${MessageHandleType[r]}`));
  357. const o = {
  358. action_type: Actions.Broadcast,
  359. broadcast_action: {
  360. data: JSON.stringify(t),
  361. user_id: this.room.options.userId,
  362. msgType: r
  363. }
  364. };
  365. return Array.isArray(n) && r === MessageHandleType.MHT_CustomTargetSync && (o.broadcast_action.target_user_ids = n),
  366. this.room.actionsHandler.sendData({
  367. data: o,
  368. tag: t.broadcastType
  369. })
  370. }
  371. getNeighborPoints(e) {
  372. const {point: t, containSelf: r=!1, searchRange: n=500} = e
  373. , o = {
  374. action_type: Actions.GetNeighborPoints,
  375. get_neighbor_points_action: {
  376. point: t,
  377. level: 1,
  378. containSelf: r,
  379. searchRange: n
  380. }
  381. };
  382. return this.sendData({
  383. data: o
  384. }).then(a=>a.nps)
  385. }
  386. playCG(e) {
  387. const t = {
  388. action_type: Actions.PlayCG,
  389. play_cg_action: {
  390. cg_name: e
  391. }
  392. };
  393. return this.sendData({
  394. data: t
  395. })
  396. }
  397. audienceToVisitor(e) {
  398. const {avatarId: t, avatarComponents: r, player: n, camera: o} = e
  399. , a = {
  400. action_type: Actions.AudienceChangeToVisitor,
  401. audienceChangeToVisitorAction: {
  402. avatarID: t,
  403. avatarComponents: r,
  404. player: n,
  405. camera: o
  406. }
  407. };
  408. return log$p.debug("send data: audience to visitor"),
  409. this.sendData({
  410. data: a
  411. })
  412. }
  413. visitorToAudience(e) {
  414. const {renderType: t, player: r, camera: n, areaName: o, attitude: a, pathName: s, person: l, noMedia: u} = e
  415. , c = {
  416. action_type: Actions.VisitorChangeToAudience,
  417. visitorChangeToAudienceAction: {
  418. transferAction: {
  419. render_type: t,
  420. player: r,
  421. camera: n,
  422. areaName: o,
  423. attitude: a,
  424. pathName: s,
  425. person: {
  426. type: l
  427. },
  428. noMedia: u,
  429. tiles: [0, 1, 2, 4]
  430. }
  431. }
  432. };
  433. return log$p.debug("send data: visitor to audience"),
  434. this.sendData({
  435. data: c
  436. })
  437. }
  438. removeVisitor(e) {
  439. const {removeType: t, userIDList: r, extraInfo: n=""} = e
  440. , o = {
  441. action_type: Actions.RemoveVisitor,
  442. removeVisitorAction: {
  443. removeVisitorEvent: t,
  444. userIDList: r,
  445. extraInfo: encodeURIComponent(n)
  446. }
  447. };
  448. return log$p.debug("send data: remove visitor"),
  449. this.sendData({
  450. data: o
  451. })
  452. }
  453. getUserWithAvatar(e, t) {
  454. const r = {
  455. action_type: Actions.GetUserWithAvatar,
  456. getUserWithAvatarAction: {
  457. userType: e,
  458. roomID: t
  459. }
  460. };
  461. return log$p.debug("send data: get user with avatar"),
  462. this.sendData({
  463. data: r
  464. }).then(n=>n.userWithAvatarList)
  465. }
  466. joystick(e) {
  467. const {degree: t, level: r=1} = e
  468. , n = uuid$1();
  469. let o = -t + 90 + 360;
  470. o >= 360 && (o -= 360);
  471. const a = {
  472. action_type: Actions.Joystick,
  473. dir_action: {
  474. move_angle: o,
  475. speed_level: r
  476. },
  477. trace_id: n,
  478. user_id: this.room.options.userId,
  479. packet_id: n
  480. };
  481. return this.sendData({
  482. data: a,
  483. track: !1
  484. })
  485. }
  486. }