map_watch.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:app_business/service/api.dart';
  2. import 'package:track_common/model.dart';
  3. import 'package:track_common/model/event_state.dart';
  4. import 'package:track_common/service/map_watch.dart';
  5. import '../generated/base.pb.dart' as pb;
  6. import '../model/game_map.dart';
  7. class MapWatchImpl extends MapWatch {
  8. MapWatchImpl({required super.id});
  9. ApiService get _api => Get.find();
  10. @override
  11. Future<List<EventInfo>> getEventList(int mapId) async {
  12. final r = await _api.stub
  13. .toUserDetailQueryV2(pb.ToUserDetailQueryRequestV2()..mapId = mapId);
  14. final eventList = <EventInfo>[];
  15. for (var one in r.list) {
  16. eventList.add(EventInfo()
  17. ..id = one.actId
  18. ..players = one.userList.map((e) => toAppPlayer(e)).toList());
  19. }
  20. return eventList;
  21. }
  22. PlayerInfo toAppPlayer(pb.ToOrienteerInGameInfo e) {
  23. final save = e.gameSaveInfo;
  24. final startAt = save.hasStartAt() ? save.startAt.toModel() : DateTime.now();
  25. final out = PlayerInfo()
  26. ..id = e.userId
  27. ..startAt = startAt
  28. ..distance = e.gpsInfo.distance.meter
  29. ..duration = Duration.zero
  30. ..positionHistory = e.gpsInfo.gameGpsInfos
  31. .map((e) => Position()
  32. ..latitude = e.latitude
  33. ..longitude = e.longitude
  34. ..altitude = e.altitude
  35. ..timestamp = e.gpsTime.toModel())
  36. .toList()
  37. ..heartRatePercent = e.otherInfo.heartRatePercent
  38. ..stepCount = e.otherInfo.stepNum
  39. ..cpListChecked = save.checkedSortedList.isEmpty
  40. ? []
  41. : save.checkedSortedList
  42. .map((e) => ControlPoint()
  43. ..isSuccess = e.isCheckSuccess
  44. ..intId = e.controlPointId.toInt64())
  45. .toList();
  46. if (save.hasStartAt()) {
  47. out.hrHistory = e.hrInfo.hrInfo
  48. .map((e) => HeartRate(
  49. startAt.add(Duration(milliseconds: e.timeStampMs.toInt())), e.hr))
  50. .toList();
  51. out.duration = DateTime.now().difference(startAt);
  52. }
  53. return out;
  54. }
  55. @override
  56. Future<EventInfoExt> getEventInfoExt(int id) async {
  57. final r = await _api.stub.toActionBasicQuery(IdRequest()..id = Int64(id));
  58. final event = EventInfoExt()
  59. ..name = r.actName
  60. ..cpAllCount = r.totalControlNum
  61. ..startAt = r.matchBt
  62. ..endAt = r.matchEt
  63. ..state = switch (r.state) {
  64. 0 => EventState.idle,
  65. 1 => EventState.start,
  66. _ => EventState.finish,
  67. };
  68. return event;
  69. }
  70. @override
  71. Future<Iterable<PlayerInfoExt>> getPlayerInfoExt(
  72. int eventId, Iterable<int> userIdList) async {
  73. final res = await _api.stub
  74. .toUserInActionBasicQueryV2(pb.ToUserInActionBasicQueryV2Request()
  75. ..actId = eventId
  76. ..userIdList.addAll(userIdList.toList()));
  77. return res.list.map((r) {
  78. final player = PlayerInfoExt()
  79. ..id = r.userId
  80. ..name = r.baseInfo.name
  81. ..routeName = r.courseBaseInfo.courseName
  82. ..cpListWant = r.courseBaseInfo.controlPointSortedList
  83. .map((e) => e.toModel())
  84. .toList();
  85. for (var (i, cp) in player.cpListWant.indexed) {
  86. cp.sn = i.toString();
  87. }
  88. return player;
  89. });
  90. }
  91. }
  92. class MapWatchServiceImpl extends MapWatchService {
  93. ApiService get _api => Get.find();
  94. @override
  95. Future<MapWatch> newInstanceByMap(MapInfo info) async {
  96. final r =
  97. await _api.stub.toMapDetailV2(pb.IdRequest()..id = Int64(info.id));
  98. final instance = MapWatchImpl(id: info.id)
  99. ..name = r.mapName
  100. ..plugMap.gameMap = r.zipImage.toGameMap();
  101. return instance;
  102. }
  103. }