summaryrefslogtreecommitdiff
path: root/referee.h
blob: 1b9d50793d91e8b6787fd6619bc5de95f2372c80 (plain)
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
#pragma once

#include <vector>
#include <string>
#include <string_view>
#include <optional>
#include <utility>
#include <functional>
#include <variant>
#include "process.h"

using namespace std;


class Referee {
	Process proc;
	string refereeExecname;
	int numPlayers;

	string reftag;

	struct Features {
		// Referee supports current version of the protocol.
		bool version2;
	};
	Features features;

	void queryFeatures();

public:
	struct ReadEvent {
		int player;
		// call with the line read (no newline)
		function<void(const string&)> callback;
	};
	struct WriteEvent {
		int player;
		string line;
	};
	struct GamelogEvent {
		int player;
		string line;
	};
	struct EndEvent {
		vector<int> scores;
	};
	struct ErrorEvent {
		int player;
		string message;
	};
	using Event = variant<ReadEvent, WriteEvent, GamelogEvent, EndEvent, ErrorEvent>;

	Referee(const string_view execname, const vector<string> &players);
	~Referee();

	Event nextEvent();

	// Kills the referee process
	void terminate();
};

/*
Referee protocol:

At startup, the referee should write a number of lines to stdout of the form
'feature <name>', indicating that the referee supports the feature given by
<name>. The list of features should be ended with a line containing
'feature_end'. To use the version of the protocol described here, it is
mandatory to request 'feature version_2'. For supported features and their
effect, see below.

The referee then receives a line containing the number of players in the game,
followed by that number of lines containing the players' names.

Then, the referee has the following commands available:
- 'read <player>': read a line from the player with index <player>; this line
  is written directly to the referee afterwards. The first player is 0, the
  second player is 1, etc.
- 'write <player> <line>': write a line to the player with index <player>.
- 'gamelog <player> <line>': write a line in the game log indicating that the
  player with index <player> made the move given by <line>.
- 'end <score> <score> ...': signify that the game has ended. The final scores
  of the players are given after 'end'. These scores should be given as a
  space-separated list of integers, as many as there are players.
- 'error <player> <message>': indicate that the player with index <player> has
  produced erroneous output. This ends the game. The <message> will be
  presented to the user and written to the gamelog.

FEATURES
Currently, the following features are supported:
- 'version_2': must be used by all referees.
*/