OLD | NEW |
1 part of pop_pop_win.html; | 1 library pop_pop_win.game_storage; |
2 | 2 |
| 3 import 'dart:async'; |
| 4 |
| 5 import 'game.dart'; |
| 6 import 'platform.dart'; |
| 7 |
3 class GameStorage { | 8 class GameStorage { |
4 static const _gameCountKey = 'gameCount'; | 9 static const _gameCountKey = 'gameCount'; |
5 final EventHandle _bestTimeUpdated = new EventHandle(); | 10 final StreamController _bestTimeUpdated = new StreamController(); |
6 final Map<String, String> _cache = new Map<String, String>(); | 11 final Map<String, String> _cache = new Map<String, String>(); |
7 | 12 |
8 Future<int> get gameCount => _getIntValue(_gameCountKey); | 13 Future<int> get gameCount => _getIntValue(_gameCountKey); |
9 | 14 |
10 Stream get bestTimeUpdated => _bestTimeUpdated.stream; | 15 Stream get bestTimeUpdated => _bestTimeUpdated.stream; |
11 | 16 |
12 void recordState(GameState state) { | 17 void recordState(GameState state) { |
13 assert(state != null); | 18 assert(state != null); |
14 _incrementIntValue(state.name); | 19 _incrementIntValue(state.name); |
15 } | 20 } |
16 | 21 |
17 Future<bool> updateBestTime(Game game) { | 22 Future<bool> updateBestTime(Game game) { |
18 assert(game != null); | 23 assert(game != null); |
19 assert(game.state == GameState.won); | 24 assert(game.state == GameState.won); |
20 | 25 |
21 final w = game.field.width; | 26 var w = game.field.width; |
22 final h = game.field.height; | 27 var h = game.field.height; |
23 final m = game.field.bombCount; | 28 var m = game.field.bombCount; |
24 final duration = game.duration.inMilliseconds; | 29 var duration = game.duration.inMilliseconds; |
25 | 30 |
26 final key = _getKey(w, h, m); | 31 var key = _getKey(w, h, m); |
27 | 32 |
28 return _getIntValue(key, null) | 33 return _getIntValue(key, null).then((int currentScore) { |
29 .then((int currentScore) { | 34 if (currentScore == null || currentScore > duration) { |
30 if(currentScore == null || currentScore > duration) { | 35 _setIntValue(key, duration); |
31 _setIntValue(key, duration); | 36 _bestTimeUpdated.add(null); |
32 _bestTimeUpdated.add(null); | 37 return true; |
33 return true; | 38 } else { |
34 } else { | 39 return false; |
35 return false; | 40 } |
36 } | 41 }); |
37 }); | |
38 } | 42 } |
39 | 43 |
40 Future<int> getBestTimeMilliseconds(int width, int height, int bombCount) { | 44 Future<int> getBestTimeMilliseconds(int width, int height, int bombCount) { |
41 final key = _getKey(width, height, bombCount); | 45 final key = _getKey(width, height, bombCount); |
42 return _getIntValue(key, null); | 46 return _getIntValue(key, null); |
43 } | 47 } |
44 | 48 |
45 Future reset() { | 49 Future reset() { |
46 _cache.clear(); | 50 _cache.clear(); |
47 return targetPlatform.clearValues(); | 51 return targetPlatform.clearValues(); |
48 } | 52 } |
49 | 53 |
50 Future<int> _getIntValue(String key, [int defaultValue = 0]) { | 54 Future<int> _getIntValue(String key, [int defaultValue = 0]) { |
51 assert(key != null); | 55 assert(key != null); |
52 if (_cache.containsKey(key)) { | 56 if (_cache.containsKey(key)) { |
53 return new Future.value(_parseValue(_cache[key], defaultValue)); | 57 return new Future.value(_parseValue(_cache[key], defaultValue)); |
54 } | 58 } |
55 | 59 |
56 return targetPlatform.getValue(key) | 60 return targetPlatform.getValue(key).then((String strValue) { |
57 .then((String strValue) { | 61 _cache[key] = strValue; |
58 _cache[key] = strValue; | 62 return _parseValue(strValue, defaultValue); |
59 return _parseValue(strValue, defaultValue); | 63 }); |
60 }); | |
61 } | 64 } |
62 | 65 |
63 Future _setIntValue(String key, int value) { | 66 Future _setIntValue(String key, int value) { |
64 assert(key != null); | 67 assert(key != null); |
65 _cache.remove(key); | 68 _cache.remove(key); |
66 String val = (value == null) ? null : value.toString(); | 69 String val = (value == null) ? null : value.toString(); |
67 return targetPlatform.setValue(key, val); | 70 return targetPlatform.setValue(key, val); |
68 } | 71 } |
69 | 72 |
70 Future _incrementIntValue(String key) { | 73 Future _incrementIntValue(String key) { |
71 return _getIntValue(key) | 74 return _getIntValue(key).then((int val) { |
72 .then((int val) { | 75 return _setIntValue(key, val + 1); |
73 return _setIntValue(key, val + 1); | 76 }); |
74 }); | |
75 } | 77 } |
76 | 78 |
77 static String _getKey(int w, int h, int m) => "w$w-h$h-m$m"; | 79 static String _getKey(int w, int h, int m) => "w$w-h$h-m$m"; |
78 | 80 |
79 static int _parseValue(String value, int defaultValue) { | 81 static int _parseValue(String value, int defaultValue) { |
80 if(value == null) { | 82 if (value == null) { |
81 return defaultValue; | 83 return defaultValue; |
82 } else { | 84 } else { |
83 return int.parse(value); | 85 return int.parse(value); |
84 } | 86 } |
85 } | 87 } |
86 | |
87 } | 88 } |
OLD | NEW |