summaryrefslogtreecommitdiff
path: root/referee.cpp
blob: ffcbd149a124bd3559927c828f88cc5da8dca1fb (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
#include <iostream>
#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 and will be treated as
// such for the duration of the competition.
#define FEATURE_TIMEOUT 200


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

	proc.run();

	if (referee_verbose) {
		cout << "REF(" << proc.getPid() << ") starting for:";
		for (const string &p : players) cout << " " << p;
		cout << endl;
	}

	queryFeatures();

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

	readWriteLines();
}

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

bool Referee::moveValid(int player, const string_view line) {
	string s = to_string(player) + " ";
	s.append(line);

	if (referee_verbose) {
		cout << "REF(" << proc.getPid() << ") write <" << s << ">" << endl;
	}

	proc.writeLine(s);

	optional<string> response = proc.readLine();
	if (response) {
		if (referee_verbose) {
			cout << "REF(" << proc.getPid() << ") read <" << *response << ">" << endl;
		}

		bool result;
		if (*response == "valid end") {
			isEnd = true;
			result = true;
		} else if (*response == "valid") {
			result = true;
		} else if (*response == "invalid") {
			result = false;
		} else {
			cout << "REF(" << proc.getPid() << ") Invalid validity judgement '" << *response << "'!" << endl;
			exit(1);
		}

		readWriteLines();

		if (isEnd) readScores();

		return result;
	} else {
		cout << "REF(" << proc.getPid() << ") EOF!" << endl;
		return false;
	}
}

vector<pair<int, string>> Referee::playerWriteLines() {
	return writeLinesList;
}

bool Referee::gameEnded() {
	return isEnd;
}

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

	while (true) {
		optional<string> line;
		if (first) {
			line = proc.readLineTimeout(FEATURE_TIMEOUT);
			if (!line) {
				cout << "Referee '" << refereeExecname << "' seems to follow the old protocol!" << endl;
				return;
			}

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

		if (!line) {
			cout << "REF(" << proc.getPid() << ") EOF!" << endl;
			exit(1);
		}

		if (referee_verbose) {
			cout << "REF(" << proc.getPid() << ") read <" << *line << ">" << endl;
		}

		if (*line == "feature_end") {
			return;
		} else if (line->substr(0, 8) == "feature ") {
			string featname = line->substr(8);
			if (featname == "write_lines") features.writeLines = true;
			else if (featname == "no_last_move") features.noLastMove = true;
			else {
				cerr << "REF(" << proc.getPid() << ") Referee requested unsupported feature '" << featname << "'" << endl;
				exit(1);
			}
		} else {
			cerr << "REF(" << proc.getPid() << ") Referee protocol violation in feature negotiation" << endl;
			exit(1);
		}
	}
}

void Referee::readWriteLines() {
	writeLinesList.clear();

	if (!features.writeLines) return;

	while (true) {
		optional<string> line = proc.readLine();
		if (!line) {
			cout << "REF(" << proc.getPid() << ") EOF!" << endl;
			break;
		}

		if (referee_verbose) {
			cout << "REF(" << proc.getPid() << ") read <" << *line << ">" << endl;
		}

		if (line == "write_end") break;

		size_t idx = line->find(' ');
		if (idx == string::npos) {
			cerr << "REF(" << proc.getPid() << ") Referee feature_write_lines protocol violation!" << endl;
			exit(1);
		}

		int player = stoi(line->substr(0, idx));
		writeLinesList.emplace_back(player, line->substr(idx + 1));
	}
}

void Referee::readScores() {
	optional<string> line = proc.readLine();
	if (!line) {
		cerr << "Referee stopped before providing scores" << endl;
		exit(1);
	}

	size_t cursor = 0;
	while (cursor < line->size()) {
		size_t idx = line->find(' ', cursor);
		if (idx == string::npos) idx = line->size();
		scores.push_back(stoi(line->substr(cursor, idx - cursor)));
		cursor = idx + 1;
	}
}

optional<vector<int>> Referee::getScores() {
	if (isEnd) return scores;
	else return nullopt;
}

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

const Features& Referee::getFeatures() const {
	return features;
}