map_watch.dart 3.2 KB

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