OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pirate.server; | 5 library pirate.server; |
6 | 6 |
7 import 'dart:convert' show JSON; | |
8 import 'dart:io'; | |
9 import 'package:rpc/rpc.dart'; | 7 import 'package:rpc/rpc.dart'; |
10 | 8 |
11 import '../common/messages.dart'; | 9 import '../common/messages.dart'; |
12 import '../common/utils.dart'; | 10 import '../common/utils.dart'; |
13 | 11 |
14 class PiratesApi { | 12 class PiratesApi { |
15 final Map<int, Pirate> _alivePirates = {}; | 13 final Map<int, Pirate> _pirateCrew = {}; |
16 PirateShanghaier _shanghaier; | 14 final PirateShanghaier _shanghaier = new PirateShanghaier(properPirateNames); |
17 Map<String, List<String>> _properPirates; | |
18 | |
19 PiratesApi() { | |
20 var namesFile = new File( | |
21 Platform.script.resolve('../lib/server/piratenames.json').toFilePath()); | |
22 _properPirates = JSON.decode(namesFile.readAsStringSync()); | |
23 _shanghaier = new PirateShanghaier(_properPirates); | |
24 } | |
25 | 15 |
26 List<Pirate> listPirates() { | 16 List<Pirate> listPirates() { |
27 return _alivePirates.values.toList(); | 17 return _pirateCrew.values.toList(); |
28 } | 18 } |
29 | 19 |
30 Pirate shanghaiAPirate() { | 20 Pirate shanghaiAPirate() { |
31 var pirate = _shanghaier.shanghaiAPirate(); | 21 var pirate = _shanghaier.shanghaiAPirate(); |
32 if (pirate == null) { | 22 if (pirate == null) { |
33 throw new InternalServerError('Ran out of pirates!'); | 23 throw new InternalServerError('Ran out of pirates!'); |
34 } | 24 } |
35 _alivePirates[pirate.toString().hashCode] = pirate; | 25 _pirateCrew[pirate.toString().hashCode] = pirate; |
36 return pirate; | 26 return pirate; |
37 } | 27 } |
38 } | 28 } |
OLD | NEW |