OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 | |
5 /** Entrypoint to set up the dartcombat sample. */ | |
6 void setUpGame() { | |
7 setupUI(); | |
8 createPlayers(); | |
9 } | |
10 | |
11 /** Sets up the UI creating the board for each player. */ | |
12 void setupUI() { | |
13 // Note: we set up the UI programatically to make testing easier. | |
14 var div = new Element.tag("div"); | |
15 div.innerHTML = """ | |
16 <div class='hbox'> | |
17 <div class='vbox'> | |
18 Player 1 board: | |
19 <div class='own' id='p1own'></div> | |
20 Known of enemy's board: | |
21 <div class='enemy' id='p1enemy'></div> | |
22 </div> | |
23 <div style='width:20%'></div> | |
24 <div class='vbox'> | |
25 Player 2 board: | |
26 <div class='own' id='p2own'></div> | |
27 Known of enemy's board: | |
28 <div class='enemy' id='p2enemy'></div> | |
29 </div> | |
30 </div> | |
31 """; | |
32 document.body.nodes.add(div); | |
33 } | |
34 | |
35 /** Create and connect players. */ | |
36 void createPlayers() { | |
37 Player player1 = new Player(); | |
38 player1.setup(window, 1); | |
39 | |
40 Player player2 = new Player(); | |
41 player2.setup(window, 2); | |
42 | |
43 player2.portToPlayer.then((SendPort port) { | |
44 player1.enemy = new FlakyProxy(port).sendPort; | |
45 }); | |
46 | |
47 player1.portToPlayer.then((SendPort port) { | |
48 player2.enemy = new FlakyProxy(port).sendPort; | |
49 }); | |
50 } | |
51 | |
52 /** | |
53 * Create and connect players, providing a port for communicating progress to | |
54 * the test. | |
55 */ | |
56 void createPlayersForTest(SendPort testPort) { | |
57 Player player1 = new Player(); | |
58 Player player2 = new Player(); | |
59 | |
60 player1._portForTest = testPort; | |
61 player2._portForTest = testPort; | |
62 | |
63 player1.setup(window, 1); | |
64 player2.setup(window, 2); | |
65 | |
66 player2.portToPlayer.then((SendPort port) { player1.enemy = port; }); | |
67 player1.portToPlayer.then((SendPort port) { player2.enemy = port; }); | |
68 } | |
69 | |
70 /** | |
71 * A proxy between ports that randomly drops messages to simulate isolates | |
72 * across the network. | |
73 */ | |
74 class FlakyProxy { | |
75 ReceivePort proxy; | |
76 | |
77 SendPort _target; | |
78 | |
79 SendPort get sendPort() => proxy.toSendPort(); | |
80 | |
81 FlakyProxy(this._target) { | |
82 proxy = new ReceivePort(); | |
83 | |
84 proxy.receive((message, SendPort reply) { | |
85 window.setTimeout(() { | |
86 if (randomlyFail()) { | |
87 reply.send(const [false, "There was an error"], null); | |
88 } else { | |
89 _target.send(message, reply); | |
90 } | |
91 }, 200); | |
92 }); | |
93 } | |
94 | |
95 // TODO(sigmund): introduce UI to control flakiness, then do: | |
96 // => Math.random() > 0.9; | |
97 bool randomlyFail() => false; | |
98 } | |
OLD | NEW |