global.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. export default {
  2. _caller: null, // 调用方指针
  3. // *** 非指针复制 ***
  4. storePlayers: [{
  5. id: 0, // 玩家ID
  6. marker: null, // 玩家标识
  7. trail: null, // 玩家轨迹
  8. trailData: [], // 玩家轨迹信息(数据会追加并定时清理过期数据)
  9. interval_creatCircleMarker: null,
  10. interval_showTrail: null,
  11. }],
  12. map: null,
  13. map_layer: null,
  14. players: [], // 玩家信息(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  15. players_position: [], // 玩家当前位置信息(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  16. focusPlayerId: 0, // 当前选中的玩家ID
  17. checkPoints: [], // 检查点列表(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  18. routes: [], // 路线列表(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  19. init() {
  20. this._caller = null
  21. this.storePlayers.length = 0 // 清空数组
  22. this.map = null
  23. this.map_layer = null
  24. this.players.length = 0 // 清空数组
  25. this.players_position.length = 0 // 清空数组
  26. this.focusPlayerId = 0
  27. this.checkPoints.length = 0 // 清空数组
  28. this.routes.length = 0 // 清空数组
  29. },
  30. setCaller(data) {
  31. this._caller = data // 指针复制,是同一个对象
  32. },
  33. setPlayers(data) {
  34. this.players = data // 指针复制,是同一个对象
  35. },
  36. setPlayersPosition(data) {
  37. this.players_position = data // 指针复制,是同一个对象
  38. },
  39. // *** 非指针复制 ***
  40. // storePlayersTrail(data) {
  41. // this.storePlayers.trail.push(data)
  42. // },
  43. setCheckPoints(data) {
  44. this.checkPoints = data // 指针复制,是同一个对象
  45. },
  46. setRoutes(data) {
  47. this.routes = data // 指针复制,是同一个对象
  48. },
  49. getPlayerById(playerId) {
  50. if (!(playerId > 0)) return
  51. var player = null
  52. if (this.players != null && this.players.length > 0) {
  53. player = this.players.find(o => o.id === playerId)
  54. }
  55. // console.log("[getPlayerById]", playerId, player)
  56. return player
  57. },
  58. getPlayerPositionById(playerId) {
  59. if (!(playerId > 0)) return
  60. var player_position = null
  61. if (this.players_position != null && this.players_position.length > 0) {
  62. player_position = this.players_position.find(o => o.id === playerId)
  63. }
  64. // console.log("[getPlayerPositionById]", playerId, player_position)
  65. return player_position
  66. },
  67. getStorePlayersById(playerId) {
  68. if (!(playerId > 0)) return
  69. var storePlayer = null
  70. if (this.storePlayers != null && this.storePlayers.length > 0) {
  71. storePlayer = this.storePlayers.find(o => o.id === playerId)
  72. }
  73. // console.log("[getStorePlayersById]", playerId, storePlayer)
  74. return storePlayer
  75. },
  76. getCheckPointById(cpId) {
  77. if (!(cpId > 0)) return
  78. var checkPoint = null
  79. if (this.checkPoints != null && this.checkPoints.length > 0) {
  80. checkPoint = this.checkPoints.find(o => o.cp_id === cpId)
  81. }
  82. // console.log("[getCheckPointById]", cpId, checkPoint)
  83. return checkPoint
  84. },
  85. getRouteById(rtId) {
  86. if (!(rtId > 0)) return
  87. var route = null
  88. if (this.routes != null && this.routes.length > 0) {
  89. route = this.routes.find(o => o.id === rtId)
  90. }
  91. // console.log("[getCheckPointById]", rtId, route)
  92. return route
  93. },
  94. // 处理过期数据
  95. dealStaleData() {
  96. for (let i = 0; i < this.storePlayers.length; i++) {
  97. var storePlayer = this.storePlayers[i]
  98. if (storePlayer == null || !(storePlayer.id > 0)) {
  99. continue
  100. }
  101. // 查找最新的玩家记录列表
  102. var player = this.getPlayerById(storePlayer.id)
  103. if (player == null) { // 未找到 说明记录已过期
  104. // console.warn('[dealStaleData] 发现过期数据', storePlayer)
  105. if (storePlayer.marker != null) {
  106. storePlayer.marker.removeFrom(storePlayer.markerLayerGroup)
  107. storePlayer.marker = null
  108. }
  109. if (storePlayer.trail != null) {
  110. storePlayer.trail.removeFrom(storePlayer.trailLayerGroup)
  111. storePlayer.trail2.removeFrom(storePlayer.trailLayerGroup)
  112. storePlayer.trail = null
  113. storePlayer.trail2 = null
  114. storePlayer.trailData = []
  115. }
  116. if (storePlayer.interval_creatCircleMarker != null) {
  117. clearInterval(storePlayer.interval_creatCircleMarker)
  118. }
  119. if (storePlayer.interval_creatCircleMarker != null) {
  120. clearInterval(storePlayer.interval_showTrail)
  121. }
  122. // delete删除元素之后数组长度不变,只是被删除元素被置为empty
  123. // delete this.storePlayers[i]
  124. }
  125. }
  126. },
  127. }