Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: samples/pop_pop_win/lib/src/game_storage.dart

Issue 786443002: Delete pop-pop-win from the repo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 library pop_pop_win.game_storage;
5
6 import 'dart:async';
7
8 import 'game.dart';
9 import 'platform.dart';
10
11 class GameStorage {
12 static const _gameCountKey = 'gameCount';
13 final StreamController _bestTimeUpdated = new StreamController();
14 final Map<String, String> _cache = new Map<String, String>();
15
16 Future<int> get gameCount => _getIntValue(_gameCountKey);
17
18 Stream get bestTimeUpdated => _bestTimeUpdated.stream;
19
20 void recordState(GameState state) {
21 assert(state != null);
22 _incrementIntValue(state.name);
23 }
24
25 Future<bool> updateBestTime(Game game) {
26 assert(game != null);
27 assert(game.state == GameState.won);
28
29 var w = game.field.width;
30 var h = game.field.height;
31 var m = game.field.bombCount;
32 var duration = game.duration.inMilliseconds;
33
34 var key = _getKey(w, h, m);
35
36 return _getIntValue(key, null).then((int currentScore) {
37 if (currentScore == null || currentScore > duration) {
38 _setIntValue(key, duration);
39 _bestTimeUpdated.add(null);
40 return true;
41 } else {
42 return false;
43 }
44 });
45 }
46
47 Future<int> getBestTimeMilliseconds(int width, int height, int bombCount) {
48 final key = _getKey(width, height, bombCount);
49 return _getIntValue(key, null);
50 }
51
52 Future reset() {
53 _cache.clear();
54 return targetPlatform.clearValues();
55 }
56
57 Future<int> _getIntValue(String key, [int defaultValue = 0]) {
58 assert(key != null);
59 if (_cache.containsKey(key)) {
60 return new Future.value(_parseValue(_cache[key], defaultValue));
61 }
62
63 return targetPlatform.getValue(key).then((String strValue) {
64 _cache[key] = strValue;
65 return _parseValue(strValue, defaultValue);
66 });
67 }
68
69 Future _setIntValue(String key, int value) {
70 assert(key != null);
71 _cache.remove(key);
72 String val = (value == null) ? null : value.toString();
73 return targetPlatform.setValue(key, val);
74 }
75
76 Future _incrementIntValue(String key) {
77 return _getIntValue(key).then((int val) {
78 return _setIntValue(key, val + 1);
79 });
80 }
81
82 static String _getKey(int w, int h, int m) => "w$w-h$h-m$m";
83
84 static int _parseValue(String value, int defaultValue) {
85 if (value == null) {
86 return defaultValue;
87 } else {
88 return int.parse(value);
89 }
90 }
91 }
OLDNEW
« no previous file with comments | « samples/pop_pop_win/lib/src/game_manager.dart ('k') | samples/pop_pop_win/lib/src/platform.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698