field_control_controller.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:application/service/api.dart';
  2. import 'package:application/service/map_watch.dart';
  3. import 'package:application/logger.dart';
  4. import 'package:get/get.dart';
  5. import '../home_controller.dart';
  6. export 'package:application/service/map_watch.dart';
  7. class ActiveInfo{
  8. var id = 0;
  9. var name = '';
  10. var cpAllCount = 0;
  11. var userList = <UserInfo>[];
  12. UserInfo? getUserById(int id){
  13. for (final one in userList){
  14. if(one.data.userId==id){
  15. return one;
  16. }
  17. }
  18. return null;
  19. }
  20. }
  21. class UserInfo{
  22. var data = ToOrienteerInGameInfo();
  23. }
  24. extension ActiveInfoExt on ToActionInfo{
  25. ActiveInfo into(){
  26. return ActiveInfo()
  27. ..id = actId
  28. ..name = actName
  29. ..cpAllCount=totalControlNum
  30. ..userList = userList.map((e) => e.into()).toList()
  31. ;
  32. }
  33. }
  34. extension UserInfoExt on ToOrienteerInGameInfo{
  35. UserInfo into(){
  36. return UserInfo()
  37. ..data = this
  38. ;
  39. }
  40. }
  41. class FieldControlController extends GetxController{
  42. HomeController get _home => Get.find();
  43. MapWatchService? get mapWatch => MapWatchService.instance;
  44. final activeList = <ActiveInfo>[].obs;
  45. @override
  46. void onInit() {
  47. super.onInit();
  48. workFlushData();
  49. }
  50. Future<void> workFlushData()async{
  51. await flushDataFirstTime();
  52. while(!isClosed){
  53. try{
  54. await flushData();
  55. }catch(e){
  56. error(e);
  57. }
  58. await 1.seconds.delay();
  59. }
  60. }
  61. Future<void> flushDataFirstTime()async{
  62. final r = await ApiService.to.stub.toUserDetailQueryV2(
  63. ToUserDetailQueryRequestV2(
  64. mapId: mapWatch?.id.toInt(),
  65. isFullQuery: true,
  66. ));
  67. activeList.value = r.list.map((e) => e.into()).toList();
  68. }
  69. ActiveInfo? getActiveById(int id){
  70. for (final one in activeList){
  71. if(one.id==id){
  72. return one;
  73. }
  74. }
  75. return null;
  76. }
  77. Future<void> flushData()async{
  78. final r = await ApiService.to.stub.toUserDetailQueryV2(ToUserDetailQueryRequestV2(
  79. mapId: mapWatch?.id.toInt()));
  80. final newList = <ActiveInfo>[];
  81. for(final one in r.list){
  82. final act = one.into();
  83. final old = getActiveById(act.id);
  84. if(old != null){
  85. act.name=old.name;
  86. act.cpAllCount=old.cpAllCount;
  87. for(final user in act.userList){
  88. final oUser = old.getUserById(user.data.userId);
  89. if(oUser != null){
  90. user.data = ToOrienteerInGameInfo(
  91. userId: user.data.userId,
  92. baseInfo: oUser.data.baseInfo,
  93. gpsInfo: user.data.gpsInfo,
  94. hrInfo: user.data.hrInfo,
  95. gameSaveInfo: user.data.gameSaveInfo,
  96. courseBaseInfo: oUser.data.courseBaseInfo,
  97. otherInfo: user.data.otherInfo,
  98. );
  99. }
  100. }
  101. }
  102. newList.add(act);
  103. }
  104. activeList.value = newList;
  105. }
  106. }