ActionsHandler.js 16 KB

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