1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! One game with bots in it, from `hello` to `end`, for the arena and cups.
//!
//! The sim is never told who sits where: it takes one action per seat per
//! tick, and this file fills those slots from the seat controllers, the
//! game's AI and bots over the protocol. Everything here is the seat layer:
//! the clock, the fair cursor, the log, the trace and the standings'
//! numbers. The board ticks exactly as it would anywhere else, and the
//! replay records the actions actually committed, so a replay reproduces
//! the game whatever the bots did.
use super::cursor::SeatCursor;
use super::diff::{Before, Happened};
use super::listener::{BotId, GameLink, GameMsg, Listener, View, lock};
use super::lookahead;
use super::protocol::{self, Act, Clock, Outcome, SeatKind, Table, You};
pub use super::seat::SeatResult;
use super::seat::{BotDriver, Journal, Turn, act_of, outcome, placings, refusal};
use crate::sim::{
Board, BotLevel, Hand, Level, MAX_PLAYERS, PlayerAction, PlayerId, Replay, bot_action_with,
};
use serde_json::{Value, json};
use std::io::Write;
use std::sync::mpsc::{RecvTimeoutError, Sender};
use std::time::{Duration, Instant};
/// Who decides for a seat.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Seat {
/// The game's AI at a difficulty: a pure function of the board.
Ai(BotLevel),
/// A bot connected over the protocol.
Bot(BotId),
}
impl Seat {
pub fn kind(self) -> SeatKind {
match self {
Seat::Ai(_) => SeatKind::Ai,
Seat::Bot(_) => SeatKind::Bot,
}
}
}
/// The name the game's AI goes by at a level.
pub fn ai_name(level: BotLevel) -> &'static str {
match level {
BotLevel::Easy => "Easy AI",
BotLevel::Normal => "Normal AI",
BotLevel::Hard => "Hard AI",
}
}
/// One game, before it is played.
pub struct GameSpec {
pub game: u32,
/// What a bot asks for to fetch this game's replay (`t1-g17`).
pub replay_id: String,
pub board: Board,
pub seats: Vec<Seat>,
pub names: Vec<String>,
pub clock: Clock,
/// The fair cursor rule: every seat's hand walks at a person's pace.
pub fair_cursor: bool,
/// How long a bot that is not connected when the game starts is waited
/// for before its seat is played idle and scored as a forfeit.
pub forfeit_after: Duration,
/// How long a bot has to answer `hello` with `ready`.
pub ready_within: Duration,
}
pub struct GameResult {
pub seats: Vec<SeatResult>,
pub replay: Replay,
pub ticks: u64,
pub elapsed: Duration,
}
/// What a game writes as it goes, besides its result.
pub struct Sinks<'a> {
/// The decision log: every decision a bot made, with its tick, its
/// note, and whether it was refused or late.
pub log: &'a mut dyn Write,
/// `--trace`: every tick message and the actions that followed it.
pub trace: Option<&'a mut dyn Write>,
/// `--watch`: the game, tick by tick, to the window drawing it.
pub feed: Option<Sender<Feed>>,
/// Lines worth printing on the console as they happen (a bot dropped,
/// a bot forfeited).
pub console: &'a mut dyn FnMut(String),
/// Print every bot's notes on the console too: with `--watch` the
/// author reads them beside the window, as the move happens.
pub echo_notes: bool,
}
/// A game going out to a window as it is played.
pub enum Feed {
Start(Box<Replay>),
Tick([PlayerAction; MAX_PLAYERS]),
}
/// A game in play.
struct Match<'a, 's> {
listener: &'a Listener,
link: GameLink,
spec: GameSpec,
kinds: Vec<SeatKind>,
sinks: &'a mut Sinks<'s>,
/// One per seat; `None` for the game's AI.
bots: Vec<Option<BotDriver>>,
/// The AI's numbers (the bots keep their own).
ai: Vec<SeatResult>,
cursors: Vec<SeatCursor>,
last: Vec<Option<(&'static str, Outcome)>>,
replay: Replay,
}
impl Match<'_, '_> {
fn n(&self) -> usize {
self.spec.seats.len()
}
fn table(&self) -> Table<'_> {
Table {
game: self.spec.game,
names: &self.spec.names,
kinds: &self.kinds,
clock: self.spec.clock,
cursor: self.spec.fair_cursor,
}
}
fn hello(&self, seat: PlayerId, resumed: bool) -> Value {
protocol::hello(&self.spec.board, &self.table(), seat, resumed)
}
/// Hand one message to the seat it is for.
fn take(&mut self, msg: GameMsg, t: u64) -> bool {
let seat = match &msg {
GameMsg::Reply { seat, .. }
| GameMsg::Ready { seat }
| GameMsg::Garbled { seat, .. }
| GameMsg::Dropped { seat }
| GameMsg::Back { seat, .. } => usize::from(*seat),
};
let Some(Some(bot)) = self.bots.get_mut(seat) else {
return false;
};
let mut journal = Journal {
log: &mut *self.sinks.log,
console: &mut *self.sinks.console,
echo_notes: self.sinks.echo_notes,
game: self.spec.game,
};
bot.take(msg, t, &mut journal)
}
/// `hello` to every bot, then wait until each is ready or forfeits.
fn greet(&mut self) {
let n = self.n();
// The connection each seat's bot was greeted on. A bot heard coming
// back on that same connection (it came back as the route opened)
// has its `hello` already; one back on another is greeted afresh.
// A reconnect while the old connection is still open is heard only
// as `Back`, so `connected` alone cannot tell the two apart.
let mut greeted: Vec<Option<u64>> = vec![None; n];
// Each bot that has been connected at some point of the wait: one
// that came and went again did not stay away, and is not marked as
// never coming.
let mut came: Vec<bool> = vec![false; n];
// Read once, as the game starts: a forfeit for absence in another
// game while this one waits (a cup plays fixtures side by side)
// does not cut short a wait that began before it.
let mut absent: Vec<bool> = vec![false; n];
for seat in 0..n {
let Some(bot) = &self.bots[seat] else {
continue;
};
absent[seat] = self.listener.absent(bot.id);
came[seat] = bot.connected;
if bot.connected {
greeted[seat] = self
.listener
.greet(bot.id, &self.hello(seat as PlayerId, false));
}
}
loop {
let now = Instant::now();
let mut waiting = false;
for bot in self.bots.iter_mut().flatten() {
if bot.ready || bot.forfeit {
continue;
}
let seat = usize::from(bot.seat);
// A bot that has already forfeited a game for never coming,
// and has not connected since, is not waited for again: a
// cup with it in every game would otherwise stand still for
// the whole timeout, game after game.
let limit = if bot.connected {
self.spec.ready_within
} else if absent[seat] && !came[seat] {
Duration::ZERO
} else {
self.spec.forfeit_after
};
if now.duration_since(bot.waiting_since) < limit {
waiting = true;
continue;
}
bot.forfeit = true;
let why = if bot.connected {
"never said ready"
} else if came[seat] {
"left before the first tick and was not back in time"
} else {
self.listener.mark_absent(bot.id);
"never came"
};
let game = self.spec.game;
(self.sinks.console)(format!(" {} forfeits game {game}: {why}", bot.name));
let _ = writeln!(
self.sinks.log,
"g{game} P{} {}: forfeit, {why}",
bot.seat + 1,
bot.name
);
}
if !waiting {
return;
}
let msg = match self.link.recv_timeout(Duration::from_millis(50)) {
Ok(msg) => msg,
Err(RecvTimeoutError::Timeout) => continue,
Err(RecvTimeoutError::Disconnected) => return,
};
// Before the first tick a bot that comes back is greeted afresh
// and given its ten seconds from now.
if let GameMsg::Back { seat, serial } = msg
&& let Some(Some(bot)) = self.bots.get_mut(usize::from(seat))
&& !bot.forfeit
{
let at = usize::from(seat);
came[at] = true;
if greeted[at] == Some(serial) {
continue;
}
bot.connected = true;
// Whatever it said on the old connection, the new one is
// asked again.
bot.ready = false;
bot.waiting_since = Instant::now();
let id = bot.id;
greeted[at] = self.listener.greet(id, &self.hello(seat, false));
continue;
}
if let GameMsg::Dropped { seat } = msg
&& let Some(Some(bot)) = self.bots.get_mut(usize::from(seat))
{
bot.connected = false;
bot.ready = false;
continue;
}
self.take(msg, 0);
}
}
/// Send tick `t` to every bot awake for it. Returns how many answers
/// the tick waits for.
fn send_tick(&mut self, t: u64, cursors: &[Option<(u8, u8)>]) -> usize {
let now = Instant::now();
let mut expecting = 0;
for seat in 0..self.n() {
let Some(bot) = &mut self.bots[seat] else {
continue;
};
let id = bot.id;
let rehello = std::mem::take(&mut bot.rehello) && bot.playing();
let turn = bot.turn(t, now);
if rehello {
self.listener.send(id, &self.hello(seat as PlayerId, true));
}
match turn {
Turn::Skip => {}
Turn::Busy => expecting += 1,
Turn::Send => {
let you = You {
seat: seat as PlayerId,
last: self.last[seat],
};
let msg = protocol::tick(&self.spec.board, self.spec.game, &you, cursors);
if self.listener.send(id, &msg)
&& let Some(bot) = &mut self.bots[seat]
{
bot.sent(t, now);
expecting += 1;
}
}
}
}
expecting
}
/// Wait for this tick's answers: live, until the wall clock's next
/// tick; fast-forward, until every bot has answered or the deadline
/// has passed, whichever is first.
fn gather(&mut self, t: u64, until: Instant, mut expecting: usize) {
let live = self.spec.clock.live;
loop {
if !live && expecting == 0 {
return;
}
let now = Instant::now();
if now >= until {
return;
}
match self.link.recv_timeout(until - now) {
Ok(msg) => {
if self.take(msg, t) {
expecting = expecting.saturating_sub(1);
}
}
Err(RecvTimeoutError::Timeout | RecvTimeoutError::Disconnected) => return,
}
}
}
/// Every seat's action for this tick, and the act each one carried out.
fn commit(&mut self) -> ([PlayerAction; MAX_PLAYERS], Vec<Option<Act>>) {
let mut actions = [PlayerAction::None; MAX_PLAYERS];
let mut done = vec![None; self.n()];
let board = &self.spec.board;
for seat in 0..self.n() {
let player = seat as PlayerId;
let act = match (&mut self.bots[seat], self.spec.seats[seat]) {
(Some(bot), _) => {
let mut journal = Journal {
log: &mut *self.sinks.log,
console: &mut *self.sinks.console,
echo_notes: self.sinks.echo_notes,
game: self.spec.game,
};
bot.order(&mut journal)
}
(None, Seat::Ai(level)) => {
let hand = if self.spec.fair_cursor {
Hand::Fair
} else {
Hand::Instant
};
let action = bot_action_with(board, player, level, hand);
actions[seat] = action;
if let Some(act) = act_of(action) {
self.cursors[seat].at = act.target().unwrap_or(self.cursors[seat].at);
done[seat] = Some(act);
}
continue;
}
(None, Seat::Bot(_)) => Act::None,
};
let (action, landed) =
self.cursors[seat].step(board, player, act, self.spec.fair_cursor);
actions[seat] = action;
done[seat] = landed;
}
(actions, done)
}
fn result_mut(&mut self, seat: usize) -> &mut SeatResult {
match &mut self.bots[seat] {
Some(bot) => &mut bot.result,
None => &mut self.ai[seat],
}
}
/// One tick of the round.
fn tick(&mut self, wall: &mut Instant) {
let t = self.spec.board.ticks();
let n = self.n();
let cursors: Vec<Option<(u8, u8)>> = self.cursors.iter().map(|c| Some(c.at)).collect();
if self.bots.iter().any(Option::is_some) {
let cap = if self.spec.clock.live {
lookahead::CAP_LIVE
} else {
lookahead::CAP_FAST
};
*lock(&self.link.view) = Some(View {
board: self.spec.board.clone(),
cursors: cursors.clone(),
budget: vec![cap; n],
});
}
// Whatever arrived since the last tick (a late reply, a bot coming
// back) is taken first: it decides who is sent this one.
while let Some(msg) = self.link.try_recv() {
self.take(msg, t);
}
let sent_at = Instant::now();
let expecting = self.send_tick(t, &cursors);
let period = Duration::from_secs_f64(1.0 / f64::from(crate::sim::TICKS_PER_SECOND));
let until = if self.spec.clock.live {
*wall += period;
*wall
} else {
sent_at + Duration::from_millis(u64::from(self.spec.clock.deadline_ms))
};
self.gather(t, until, expecting);
// A live table that fell behind catches up rather than drifting.
if self.spec.clock.live && Instant::now() > *wall + period * 4 {
*wall = Instant::now();
}
let (actions, done) = self.commit();
let refusals: Vec<_> = (0..n)
.map(|seat| refusal(&self.spec.board, seat as PlayerId, actions[seat]))
.collect();
if let Some(trace) = self.sinks.trace.as_deref_mut() {
let you = You {
seat: 0,
last: None,
};
let acts: Vec<Value> = done
.iter()
.map(|d| d.unwrap_or(Act::None).to_json())
.collect();
let line = json!({
"state": protocol::tick(&self.spec.board, self.spec.game, &you, &cursors),
"actions": acts,
});
let _ = writeln!(trace, "{line}");
}
let before = Before::of(&self.spec.board);
self.spec.board.tick(&actions);
self.replay.record(actions);
if let Some(feed) = &self.sinks.feed {
let _ = feed.send(Feed::Tick(actions));
}
for seat in 0..n {
let Some(act) = done[seat] else {
continue;
};
let Some(outcome) = outcome(
act,
actions[seat],
refusals[seat],
&self.spec.board,
seat as PlayerId,
t,
) else {
continue;
};
self.last[seat] = Some((act.token(), outcome));
if let Outcome::Refused(why) = outcome {
self.result_mut(seat).rejected += 1;
if let Some(bot) = &self.bots[seat] {
let _ = writeln!(
self.sinks.log,
"g{} t{t} P{} {}: {} refused: {}",
self.spec.game,
seat + 1,
bot.name,
act.describe(),
why.token()
);
}
}
}
let mut happened = Vec::new();
before.diff(&self.spec.board, &mut happened);
for what in happened {
match what {
Happened::Banked { owner, .. } if usize::from(owner) < n => {
self.result_mut(usize::from(owner)).banked += 1;
}
Happened::Raided { owner, .. } if usize::from(owner) < n => {
self.result_mut(usize::from(owner)).raided += 1;
}
Happened::Banked { .. }
| Happened::Raided { .. }
| Happened::Eaten { .. }
| Happened::PostWorn { .. }
| Happened::PostGone { .. } => {}
}
}
}
/// Tell every bot how it went, and hand back the numbers.
fn finish(mut self, started: Instant) -> GameResult {
*lock(&self.link.view) = None;
// Replies still in flight for the last ticks are drained so their
// timing counts.
let t = self.spec.board.ticks();
while let Some(msg) = self.link.try_recv() {
self.take(msg, t);
}
let n = self.n();
let forfeits: Vec<bool> = (0..n)
.map(|seat| self.bots[seat].as_ref().is_some_and(|b| b.forfeit))
.collect();
let scores: Vec<u32> = self.spec.board.scores()[..n].to_vec();
let places = placings(&scores, &forfeits);
// Kept before anybody is told the game is over, so a bot that asks
// for the replay the moment it hears `end` finds it there.
let players = self.bots.iter().flatten().map(|b| b.id).collect();
self.listener
.keep_replay(self.spec.replay_id.clone(), players, self.replay.to_text());
for seat in 0..n {
if let Some(bot) = &mut self.bots[seat] {
bot.close();
self.listener.send(
bot.id,
&json!({
"type": "end", "game": self.spec.game, "scores": scores,
"placing": places[seat], "replay": self.spec.replay_id,
}),
);
}
let result = self.result_mut(seat);
result.score = scores[seat];
result.placing = places[seat];
}
let Match {
link,
spec,
bots,
ai,
replay,
..
} = self;
drop(link);
GameResult {
seats: bots
.into_iter()
.zip(ai)
.map(|(bot, ai)| bot.map_or(ai, |b| b.result))
.collect(),
replay,
ticks: spec.board.ticks(),
elapsed: started.elapsed(),
}
}
}
/// Play `spec` to the tide, with the listener carrying the bots' messages.
pub fn play(listener: &Listener, spec: GameSpec, sinks: &mut Sinks) -> GameResult {
let n = spec.seats.len();
let kinds: Vec<SeatKind> = spec.seats.iter().map(|s| s.kind()).collect();
let mut replay = Replay::new(Level::from_board("Arena", 3, spec.board.clone()));
for (i, name) in spec.names.iter().enumerate().take(MAX_PLAYERS) {
replay.names[i] = name.clone();
replay.kinds[i] = kinds[i];
}
if let Some(feed) = &sinks.feed {
let _ = feed.send(Feed::Start(Box::new(replay.clone())));
}
let deadline = Duration::from_millis(u64::from(spec.clock.deadline_ms));
// The route is opened before anybody is asked whether it is connected:
// a bot that comes back in between is then either seen connected or
// heard coming back over the route, never missed by both.
let routes = spec
.seats
.iter()
.enumerate()
.filter_map(|(seat, s)| match s {
Seat::Bot(id) => Some((*id, seat as PlayerId)),
Seat::Ai(_) => None,
})
.collect();
let link = listener.open_game(spec.game, routes);
let bots: Vec<Option<BotDriver>> = spec
.seats
.iter()
.enumerate()
.map(|(seat, s)| match s {
Seat::Bot(id) => Some(BotDriver::new(
*id,
seat as PlayerId,
spec.names[seat].clone(),
listener.connected(*id),
deadline,
)),
Seat::Ai(_) => None,
})
.collect();
let cursors = (0..n)
.map(|s| SeatCursor::home(&spec.board, s as PlayerId))
.collect();
let mut game = Match {
listener,
link,
spec,
kinds,
sinks,
bots,
ai: vec![SeatResult::default(); n],
cursors,
last: vec![None; n],
replay,
};
game.greet();
let started = Instant::now();
let mut wall = Instant::now();
while !game.spec.board.round_over() {
game.tick(&mut wall);
}
game.finish(started)
}