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 final port2 = await player2.portToPlayer; | |
44 player1.enemy = new FlakyProxy(port2).sendPort; | |
45 final port1 = await player1.portToPlayer; | |
46 player2.enemy = new FlakyProxy(port1).sendPort; | |
47 } | |
48 | |
49 /** | |
50 * Create and connect players, providing a port for communicating progress to | |
51 * the test. | |
52 */ | |
53 void createPlayersForTest(SendPort testPort) { | |
54 Player player1 = new Player(); | |
55 Player player2 = new Player(); | |
56 | |
57 player1._portForTest = testPort; | |
58 player2._portForTest = testPort; | |
59 | |
60 player1.setup(window, 1); | |
61 player2.setup(window, 2); | |
62 | |
63 player1.enemy = await player2.portToPlayer; | |
64 player2.enemy = await player1.portToPlayer; | |
65 } | |
66 | |
67 /** | |
68 * A proxy between ports that randomly drops messages to simulate isolates | |
69 * across the network. | |
70 */ | |
71 class FlakyProxy { | |
72 ReceivePort proxy; | |
73 | |
74 SendPort _target; | |
75 | |
76 SendPort get sendPort() => proxy.toSendPort(); | |
77 | |
78 FlakyProxy(this._target) { | |
79 proxy = new ReceivePort(); | |
80 | |
81 proxy.receive((message, SendPort reply) { | |
82 window.setTimeout(() { | |
83 if (randomlyFail()) { | |
84 reply.send(const [false, "There was an error"], null); | |
85 } else { | |
86 _target.send(message, reply); | |
87 } | |
88 }, 200); | |
89 }); | |
90 } | |
91 | |
92 // TODO(sigmund): introduce UI to control flakiness, then do: | |
93 // => Math.random() > 0.9; | |
94 bool randomlyFail() => false; | |
95 } | |
OLD | NEW |