gameInit.dart 793 B

123456789101112131415161718192021222324252627
  1. import 'package:flutter_2048/reducers/updateState.dart';
  2. import 'package:flutter_2048/store/game_state.dart';
  3. import 'package:flutter_2048/model/game_status.dart';
  4. import 'package:redux/redux.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. gameInit(Store<GameState> store, int mode) async {
  7. SharedPreferences prefs = await SharedPreferences.getInstance();
  8. var key = 'total_' + mode.toString();
  9. if (store.state.status.total != null &&
  10. store.state.status.scores > store.state.status.total) {
  11. prefs.setInt(key, store.state.status.scores);
  12. }
  13. var state = GameState.initial(mode);
  14. state.status = GameStatus(
  15. adds: 0,
  16. end: false,
  17. moves: 0,
  18. total: prefs.getInt(key) ?? 0,
  19. scores: 0,
  20. );
  21. store.dispatch(UpdateStateAction(state));
  22. }