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 @ApiClass(version: 'v1') | 12 @ApiClass(version: 'v1') |
15 class PiratesApi { | 13 class PiratesApi { |
16 final Map<int, Pirate> _alivePirates = {}; | 14 final Map<int, Pirate> _pirateCrew = {}; |
17 PirateShanghaier _shanghaier; | 15 final PirateShanghaier _shanghaier = new PirateShanghaier(properPirateNames); |
18 Map<String, List<String>> _properPirates; | |
19 | |
20 PiratesApi() { | |
21 var namesFile = new File( | |
22 Platform.script.resolve('../lib/server/piratenames.json').toFilePath()); | |
23 _properPirates = JSON.decode(namesFile.readAsStringSync()); | |
24 _shanghaier = new PirateShanghaier(_properPirates); | |
25 } | |
26 | 16 |
27 @ApiMethod(path: 'pirates') | 17 @ApiMethod(path: 'pirates') |
28 List<Pirate> listPirates() { | 18 List<Pirate> listPirates() { |
29 return _alivePirates.values.toList(); | 19 return _pirateCrew.values.toList(); |
30 } | 20 } |
31 | 21 |
32 @ApiMethod(path: 'shanghai') | 22 @ApiMethod(path: 'shanghai') |
33 Pirate shanghaiAPirate() { | 23 Pirate shanghaiAPirate() { |
34 var pirate = _shanghaier.shanghaiAPirate(); | 24 var pirate = _shanghaier.shanghaiAPirate(); |
35 if (pirate == null) { | 25 if (pirate == null) { |
36 throw new InternalServerError('Ran out of pirates!'); | 26 throw new InternalServerError('Ran out of pirates!'); |
37 } | 27 } |
38 _alivePirates[pirate.toString().hashCode] = pirate; | 28 _pirateCrew[pirate.toString().hashCode] = pirate; |
39 return pirate; | 29 return pirate; |
40 } | 30 } |
41 } | 31 } |
OLD | NEW |