A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
turings-tarpit/builder.cpp

170 lines
4.3 KiB

#include "builder.hpp"
#include "dbc.hpp"
#include "watcher.hpp"
#include "game_engine.hpp"
#include "coro.hpp"
#include <chrono> // for milliseconds
#include <efsw/efsw.hpp>
#include <fmt/chrono.h>
#include <fmt/color.h>
#include <fmt/core.h>
#include <fmt/core.h>
#include <fstream>
#include <git2.h>
#include <iostream>
#include <memory> // for allocator, shared_ptr
#include <regex>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string> // for operator+, to_string
#include <unistd.h>
#include <vector>
#include <nlohmann/json.hpp>
#include <fstream>
using namespace std;
using namespace fmt;
using namespace nlohmann;
#define BUF_MAX 1024
Builder::Builder(GUI &g, GameEngine &engine) : gui(g), game(engine) {
ifstream infile(".tarpit.json");
json config = json::parse(infile);
config["git_path"].template get_to<string>(git_path);
config["build_cmd"].template get_to<string>(build_cmd);
}
Task<unsigned> Builder::run_build() {
regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
char buffer[BUF_MAX]; // BUF_MAX is a define already?
ofstream stats_out;
co_await Pass{};
stats_out.open("stats.csv", ios::out | ios::app);
std::time_t tstamp = std::time(nullptr);
dbc::check(stats_out.good(), "Error opening stats.csv file.");
dbc::pre("simple test", [&]() { return stats_out.good(); });
co_yield 1;
// need to catch the error message when the command is bad
FILE *build_out = popen(build_cmd.c_str(), "r");
dbc::check(build_out != nullptr, "Failed to run command.");
co_yield 2;
game.start_round();
while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
co_yield 3;
string line(buffer); // yeah, that's probably a problem
cerr << buffer;
smatch err;
bool match = regex_match(line, err, err_re);
if(match) {
string file_name = err[1].str();
string lnumber = err[2].str();
string col = err[3].str();
string type = err[4].str();
string message = err[5].str();
string result = format("{:%FT%T},{},{},{},{},{}",
fmt::localtime(tstamp), file_name,
lnumber, col, type, message);
stats_out << result;
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
// refactor this
if(game.is_dead()) {
gui.you_died();
}
}
co_yield 4;
}
co_yield 3;
game.end_round();
int rc = pclose(build_out);
if(rc == 0) {
gui.build_works();
} else {
gui.output(format(">>>>>>>> DIED? {}", game.is_dead()));
gui.build_failed(!game.is_dead(), build_cmd);
}
stats_out.close();
dbc::post("a post test", [&]() { return !stats_out.is_open(); });
co_return 1000;
}
void Builder::run() {
git_repository* repo = nullptr;
try {
gui.output(format("Using build command: {}", build_cmd));
efsw::FileWatcher* fileWatcher = new efsw::FileWatcher();
dbc::check(fileWatcher != nullptr, "Failed to create filewatcher.");
git_libgit2_init();
int err = git_repository_open(&repo, git_path.c_str());
dbc::check(err == 0, git_error_last()->message);
UpdateListener* listener = new UpdateListener(repo);
dbc::check(listener != nullptr, "Failed to create listener.");
gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
auto build_task = run_build();
int rc = gui.main_loop(game, [&] {
fileWatcher->watch();
if(listener->changes) {
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
if(!build_task.done()) {
unsigned point = build_task();
} else {
if(game.is_dead()) {
gui.output("!!!! YOU DIED! !!!! Learn to code luser.");
game.reset()
}
listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
build_task.destroy();
build_task = run_build();
}
}
return 0;
});
dbc::check(rc == 0, "Invalid return from main_loop.");
fileWatcher->removeWatch(wid);
git_libgit2_shutdown();
} catch(dbc::Error &err) {
if(repo != nullptr) git_repository_free(repo);
git_libgit2_shutdown();
throw err;
}
}