summaryrefslogtreecommitdiff
path: root/referee.cpp
blob: 2701e782373eaea68680c74a403bccbce6b5ade5 (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
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
#include <iostream>
#include <tuple>
#include <cstdlib>
#include "referee.h"
#include "params.h"

// Timeout in milliseconds for the feature negotiation at the start of the
// protocol. If the referee hasn't sent anything after this timeout, we assume
// the referee isn't capable of sending feature lines.
#define FEATURE_TIMEOUT 200


Referee::Referee(const string_view execname, const vector<string> &players)
		: proc(execname), refereeExecname(execname), numPlayers(players.size()) {

	proc.run();

	reftag = "REF(" + to_string(proc.getPid()) + ") ";

	if (referee_verbose) {
		cout << reftag << "starting for:";
		for (const string &p : players) cout << " " << p;
		cout << endl;
	}

	queryFeatures();

	if (!features.version2) {
		cout << reftag << "does not support protocol version 2!" << endl;
		exit(1);
	}

	proc.writeLine(to_string(players.size()));
	for (const string &p : players) {
		proc.writeLine(p);
	}
}

Referee::~Referee() {
	if (referee_verbose) {
		cout << reftag << "stopping" << endl;
	}
	proc.wait();
	proc.terminate();
}

Referee::Event Referee::nextEvent() {
	optional<string> ocommand = proc.readLine();
	if (!ocommand) {
		cout << reftag << "Unexpected EOF before 'end' command!" << endl;
		exit(1);
	}

	string &command = *ocommand;

	if (referee_verbose) {
		cout << reftag << "read <" << command << ">" << endl;
	}

	auto getPlayer = [this](const string &command, const char *commandName, int prefixLen) {
		int player;
		try {
			player = stoi(command.substr(prefixLen));
		} catch (...) {
			cout << reftag << "Protocol violation in '" << commandName << "' command!" << endl;
			exit(1);
		}

		if (player < 0 || player >= numPlayers) {
			cout << reftag << "Invalid player index " << player << " mentioned in '" << commandName << "' command!" << endl;
			exit(1);
		}

		size_t afterIndex = command.find(' ', prefixLen);
		if (afterIndex != string::npos) afterIndex++;

		return make_pair(player, afterIndex);
	};

	if (command.substr(0, 5) == "read ") {
		int player = getPlayer(command, "read", 5).first;
		return ReadEvent{
			player,
			[this](const string &line) {
				if (referee_verbose) {
					cout << reftag << "write <" << line << ">" << endl;
				}

				if (!proc.writeLine(line)) {
					cout << reftag << "Referee exited unexpectedly after 'read' command!" << endl;
					exit(1);
				}
			}
		};
	} else if (command.substr(0, 6) == "write ") {
		int player;
		size_t afterIndex;
		tie(player, afterIndex) = getPlayer(command, "write", 6);

		if (afterIndex == string::npos) {
			cout << reftag << "Protocol violation in 'write' command: no line!" << endl;
			exit(1);
		}

		return WriteEvent{player, command.substr(afterIndex)};
	} else if (command.substr(0, 8) == "gamelog ") {
		int player;
		size_t afterIndex;
		tie(player, afterIndex) = getPlayer(command, "gamelog", 8);

		if (afterIndex == string::npos) {
			cout << reftag << "Protocol violation in 'gamelog' command: no line!" << endl;
			exit(1);
		}

		return GamelogEvent{player, command.substr(afterIndex)};
	} else if (command.substr(0, 4) == "end ") {
		size_t cursor = 4;

		vector<int> scores;
		while (cursor < command.size()) {
			size_t idx = command.find(' ', cursor);
			if (idx == string::npos) idx = command.size();

			string substr = command.substr(cursor, idx - cursor);

			int score;
			try {
				score = stoi(substr);
			} catch (...) {
				cout << reftag << "Protocol violation in 'end' command: invalid scores!" << endl;
				exit(1);
			}

			scores.push_back(score);

			cursor = idx + 1;
		}

		proc.terminate();
		return EndEvent{move(scores)};
	} else if (command.substr(0, 6) == "error ") {
		int player;
		size_t afterIndex;
		tie(player, afterIndex) = getPlayer(command, "error", 6);

		if (afterIndex == string::npos) {
			cout << reftag << "Protocol violation in 'error' command: no message!" << endl;
			exit(1);
		}

		proc.terminate();
		return ErrorEvent{player, command.substr(6)};
	} else {
		cout << reftag << "Protocol violation: unknown command '" << command << "'!" << endl;
		exit(1);
	}
}

void Referee::queryFeatures() {
	bool first = true;

	while (true) {
		optional<string> line;
		if (first) {
			line = proc.readLineTimeout(FEATURE_TIMEOUT);
			if (!line) {
				cout << "Referee '" << refereeExecname << "' does not support feature detection!" << endl;
				exit(1);
			}

			first = false;
		} else {
			line = proc.readLine();
		}

		if (!line) {
			cout << reftag << "EOF!" << endl;
			exit(1);
		}

		if (referee_verbose) {
			cout << reftag << "read <" << *line << ">" << endl;
		}

		if (*line == "feature_end") {
			return;
		} else if (line->substr(0, 8) == "feature ") {
			string featname = line->substr(8);
			if (featname == "version_2") features.version2 = true;
			else {
				cerr << reftag << "Referee requested unsupported feature '" << featname << "'" << endl;
				exit(1);
			}
		} else {
			cerr << reftag << "Referee protocol violation in feature negotiation" << endl;
			exit(1);
		}
	}
}

void Referee::terminate() {
	proc.terminate();
}