import 'package:fixnum/fixnum.dart'; import 'package:grpc/grpc.dart'; import 'package:trackoffical_app/service/game/game_instance.dart'; import 'package:trackoffical_app/service/game/game_instance_guardian.dart'; import 'package:trackoffical_app/service/game/game_instance_orienteering_assistant.dart'; import 'package:trackoffical_app/service/game/game_instance_std/game_instance_std.dart'; import 'package:trackoffical_app/service/service.dart'; import 'package:trackoffical_app/view/ingame/game_orienteering_assistant/game_orienteering_assistant_view.dart'; import 'dart:async'; import '../../exception/exception.dart'; import '../../logger.dart'; import '../../model/game_state.dart'; import '../api.dart'; import '../database.dart'; class GameManagerService extends IService { static GameManagerService get to => Get.find(); final _api = ApiService.to; final _database = DatabaseService.to; Future checkUnFinished()async{ final save = await _database.getExistGameData(); if (save != null) { info('存在本地未完成游戏'); final instance = GameInstanceStd( gameState: save.toState(), ); return instance; } else { try { final online = await _api.getInGameData(); final gameState = GameState() ..pbGameData = online.data ..pbGameSave = online.save; info('存在线上未完成游戏'); final instance = GameInstanceStd( gameState: gameState, ); return instance; } on GrpcError catch (e) { if (e.code != StatusCode.notFound) { warn(e); } } catch (e) { warn(e); } } return null; } Future gameStart(int activityId, Int64 mapRouteId) async { info('项目Id[$activityId]准备开始'); if(statue.value== GameManagerServiceStatus.closing){ throw GameNotClosedError(); } final data = await ApiService.to.gameStart(activityId, mapRouteId); final instance = GameInstanceStd( gameState: data.toGameState(), ); instanceStart(instance); info('活动Id[$activityId],游戏Id[${instance.id}]已开始'); return instance; } Future instanceStop()async{ if(statue.value == GameManagerServiceStatus.closing){ warn('[${instance.runtimeType}]正在停止,忽略操作'); return; } if(instance==null){ return; } statue.value = GameManagerServiceStatus.closing; debug('[${instance.runtimeType}]正在停止'); await instance?.closeUsedByManager(); Get.delete(); debug('[${instance.runtimeType}]已停止'); instance=null; statue.value = GameManagerServiceStatus.idle; } Future guardianStart(int underGuardianId)async{ final instance = GameInstanceGuardian(underGuardianId: underGuardianId); await instanceStart(instance); } void orienteeringAssistantStart(){ final instance = GameInstanceOrienteeringAssistant(); Get.to(()=>gameOrienteeringAssistantView(instance)); instanceStart(instance); } Future instanceStart(GameInstance instance)async{ if(statue.value== GameManagerServiceStatus.closing){ throw GameNotClosedError(); } await instanceStop(); statue.value = GameManagerServiceStatus.preparing; this.instance=instance; instance.stop=(){ instanceStop(); }; statue.value = GameManagerServiceStatus.loading; await instance.init(); instance.loadProgress.value= 1; Get.put(instance, permanent: true); statue.value = GameManagerServiceStatus.playing; } GameInstance? instance; final statue = GameManagerServiceStatus.idle.obs; @override Future init() async{} } enum GameManagerServiceStatus { // 空闲 idle, // 准备进入游戏 preparing, loading, playing, // 结算中 closing, }