gameInit.dart 751 B

1234567891011121314151617181920212223242526
  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.scores > store.state.status.total) {
  10. prefs.setInt(key, store.state.status.scores);
  11. }
  12. var state = GameState.initial(mode);
  13. state.status = GameStatus(
  14. adds: 0,
  15. end: false,
  16. moves: 0,
  17. total: prefs.getInt(key) ?? 0,
  18. scores: 0,
  19. );
  20. store.dispatch(UpdateStateAction(state));
  21. }