diff options
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 81 |
1 files changed, 36 insertions, 45 deletions
@@ -257,78 +257,69 @@ static void playMatch(Player &p1, Player &p2, int index, const Params ¶ms) { procs[0].run(); procs[1].run(); - const auto handleWriteLines = [&referee, &procs]() { - for (const auto &p : referee.playerWriteLines()) { - assert(0 <= p.first && p.first < 2); - procs[p.first].writeLine(p.second); - } - }; - - // Received upon initialisation of referee - handleWriteLines(); - - string lastMove = "Start"; - while (true) { - for (int i = 0; i < 2; i++) { - if (!referee.getFeatures().noLastMove) { - if (!procs[i].writeLine(lastMove)) { - cout << "ERROR writing move to player " << i+1 - << " (game " << gameCodeName(p1, p2, index) << ")" << endl; - throw StopCompetitionError(); - } - } + auto event = referee.nextEvent(); + + if (auto readEvent = get_if<Referee::ReadEvent>(&event)) { + Process &proc = procs[readEvent->player]; - procs[i].unStop(); + proc.unStop(); int64_t start = gettimestamp(); - optional<string> oline = procs[i].readLine(); + + optional<string> oline = proc.readLine(); if (!oline) { - cout << "ERROR reading move from player " << i+1 + cout << "ERROR reading move from player " << readEvent->player + 1 << " (game " << gameCodeName(p1, p2, index) << ")" << endl; - cout << "(process exit code: " << procs[i].waitPoll() << ")" << endl; + cout << "(process exit code: " << proc.waitPoll() << ")" << endl; throw StopCompetitionError(); } - lastMove = *oline; - (i == 0 ? mres.ms1 : mres.ms2) += gettimestamp() - start; - procs[i].stop(); + (readEvent->player == 0 ? mres.ms1 : mres.ms2) += gettimestamp() - start; + proc.stop(); - gamelog << "P" << i+1 << ": " << lastMove << endl; + readEvent->callback(*oline); if (mres.ms1 / 1000 > timeout_msec || mres.ms2 / 1000 > timeout_msec) { mres.status = MatchResult::Status::timeout; mres.sc1 = mres.sc2 = 0; referee.terminate(); - goto match_done; + break; } - if (!referee.moveValid(i, lastMove)) { - cout << "ERROR in player " << i+1 << ": invalid move '" << lastMove << "'" - << " (game " << gameCodeName(p1, p2, index) << ")" << endl; + } else if (auto writeEvent = get_if<Referee::WriteEvent>(&event)) { + if (!procs[writeEvent->player].writeLine(writeEvent->line)) { + cout << "ERROR writing move to player " << writeEvent->player + 1 + << " (game " << gameCodeName(p1, p2, index) << ")" << endl; throw StopCompetitionError(); } - handleWriteLines(); + } else if (auto gamelogEvent = get_if<Referee::GamelogEvent>(&event)) { + gamelog << "P" << gamelogEvent->player + 1 << ": " + << gamelogEvent->line << endl; - if (referee.gameEnded()) { - mres.status = MatchResult::Status::ok; + } else if (auto endEvent = get_if<Referee::EndEvent>(&event)) { + mres.status = MatchResult::Status::ok; - optional<vector<int>> oscores = referee.getScores(); - assert(oscores); - assert(oscores->size() == 2); - mres.sc1 = oscores->at(0); - mres.sc2 = oscores->at(1); + assert(endEvent->scores.size() == 2); + mres.sc1 = endEvent->scores[0]; + mres.sc2 = endEvent->scores[1]; + break; - goto match_done; - } + } else if (auto errorEvent = get_if<Referee::ErrorEvent>(&event)) { + cout << "ERROR in player " << errorEvent->player + 1 << ": " << errorEvent->message + << " (game " << gameCodeName(p1, p2, index) << ")" << endl; + gamelog << endl + << "ERROR in P" << errorEvent->player + 1 << ": " << errorEvent->message << endl; + throw StopCompetitionError(); + + } else { + assert(false); } } -match_done: for (int i = 0; i < 2; i++) { - bool success = procs[i].writeLine("Quit"); procs[i].unStop(); - if (success) usleep(10000); + if (procs[i].waitPoll() == -1) usleep(10000); int ret = procs[i].terminate(); if (ret != 0 && ret != 1009) { cout << "Player " << i+1 << " exited with code " << ret << endl; |