Skip to content

Commit

Permalink
Add add-callback hook to Db. Use callback to invoke MyScores add oper…
Browse files Browse the repository at this point in the history
…ation after journal-add.
  • Loading branch information
ebarlas committed May 3, 2024
1 parent 950baa7 commit af25368
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
33 changes: 21 additions & 12 deletions src/game/Db.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ namespace trippin {
}
};

explicit Db(const char *name, int version, std::function<bool(const T&)> outFn) :
explicit Db(
const char *name,
int version,
std::function<void(const T &)> addCallback,
std::function<bool(const T &)> dispatchFn) :
name(name),
outFn(outFn),
fileName(makeFileName(name, version)) {
fileName(makeFileName(name, version)),
addCallback(addCallback),
dispatchFn(dispatchFn) {
}

void start() {
auto t1 = std::thread(&Db<T>::runFilePersistenceLoop, this);
auto t1 = std::thread(&Db<T>::runJournalLoop, this);
t1.detach();
auto t2 = std::thread(&Db<T>::runDispatchLoop, this);
t2.detach();
Expand All @@ -43,7 +48,8 @@ namespace trippin {
std::string fileName;
Channel<Event> inChannel;
Channel<T> outChannel;
std::function<bool(const T&)> outFn;
std::function<void(const T &)> addCallback;
std::function<bool(const T &)> dispatchFn;

static std::string makeFileName(const char *name, int version) {
std::stringstream fileName;
Expand All @@ -54,12 +60,12 @@ namespace trippin {
void load() {
auto contents = readPrefFile(fileName.c_str());
if (contents) {
SDL_Log("initializing from journal file, type=%s, filename=%s", name, fileName.c_str());
SDL_Log("initializing from journal file, file=%s", fileName.c_str());
auto events = parseLines(*contents);
addInitialEvents(events);
SDL_Log("done initializing from journal file, type=%s, filename=%s", name, fileName.c_str());
SDL_Log("done initializing from journal file, file=%s", fileName.c_str());
} else {
SDL_Log("unable to initialize from journal file, type=%s, filename=%s", name, fileName.c_str());
SDL_Log("unable to initialize from journal file, filen=%s", fileName.c_str());
}
}

Expand Down Expand Up @@ -103,8 +109,8 @@ namespace trippin {
}
}

void runFilePersistenceLoop() {
SDL_Log("started file persistence thread, type=%s", name);
void runJournalLoop() {
SDL_Log("started journal thread, file=%s", fileName.c_str());
load();
unsigned long iter = 0;
while (true) {
Expand All @@ -113,9 +119,12 @@ namespace trippin {
break;
}
addNextEvent(*event);
if (event->add) {
addCallback(event->val);
}
iter++;
auto *op = event->add ? "add" : "ack";
SDL_Log("persisted event in journal, iter=%lu, type=%s, op=%s", iter, name, op);
SDL_Log("persisted event in journal, iter=%lu, op=%s, file=%s", iter, op, fileName.c_str());
}
}

Expand All @@ -127,7 +136,7 @@ namespace trippin {
if (!val) {
break;
}
while (!outFn(*val)) {
while (!dispatchFn(*val)) {
std::this_thread::sleep_for(std::chrono::seconds(5));
}
inChannel.put({*val, false});
Expand Down
24 changes: 15 additions & 9 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void trippin::Game::initAppId() {
}

void trippin::Game::initLogger() {
logger = std::make_unique<Logger>([this](const LogEvent& e){logDb->add(e);}, appId);
logger = std::make_unique<Logger>([this](const LogEvent &e) { logDb->add(e); }, appId);
SDL_DisplayMode displayMode;
SDL_GetDisplayMode(0, 0, &displayMode);
auto platform = SDL_GetPlatform();
Expand Down Expand Up @@ -95,26 +95,33 @@ void trippin::Game::initDbSynchronizer() {
stagingArea = std::make_unique<StagingArea>(*transport);
stagingArea->start();
myScores = std::make_unique<MyScores>(configuration.version.major, 10);
myScores->start();
scoreDb = std::make_unique<Db<Score>>("scores", configuration.version.major, [t = transport.get()](const Score &s) {
auto scoreAddCallback = [m = myScores.get()](const Score &s) {
m->addScore(s);
};
auto scoreDispatchFn = [t = transport.get()](const Score &s) {
auto id = s.id.c_str();
auto name = s.name.c_str();
if (t->addScore(s)) {
SDL_Log("added score, id=%s, game=%d, name=%s, score=%d", s.id.c_str(), s.game, s.name.c_str(), s.score);
SDL_Log("added score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score);
return true;
} else {
SDL_Log("failed to add score, id=%s, game=%d, name=%s, score=%d", s.id.c_str(), s.game, s.name.c_str(), s.score);
SDL_Log("failed to add score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score);
return false;
}
});
};
scoreDb = std::make_unique<Db<Score>>("scores", configuration.version.major, scoreAddCallback, scoreDispatchFn);
scoreDb->start();
logDb = std::make_unique<Db<LogEvent>>("logs", configuration.version.major, [t = transport.get()](const LogEvent &e) {
auto logAddCallback = [](const LogEvent &e) {};
auto logDispatchFn = [t = transport.get()](const LogEvent &e) {
if (t->addLogEvent(e)) {
SDL_Log("sent log event, index=%d", e.index);
return true;
} else {
SDL_Log("failed to send log event, index=%d", e.index);
return false;
}
});
};
logDb = std::make_unique<Db<LogEvent>>("logs", configuration.version.major, logAddCallback, logDispatchFn);
logDb->start();
}

Expand Down Expand Up @@ -779,7 +786,6 @@ void trippin::Game::handle(UserInput::Event &event) {
Score sc{appId, gameId, score, nameForm->getName(), convertInputEvents()};
stagingArea->addScore(sc);
scoreDb->add(sc);
myScores->addScore(sc);
state = State::START_MENU;
titleMenu->reset();
auto sum = std::accumulate(inputEvents.begin(), inputEvents.end(), 0, [](std::size_t sum, const auto &vec) {
Expand Down
23 changes: 2 additions & 21 deletions src/game/MyScores.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
#include <sstream>
#include <thread>
#include "MyScores.h"
#include "sprite/Files.h"
#include "SDL.h"

void trippin::MyScores::start() {
auto t = std::thread(&MyScores::run, this);
t.detach();
}

void trippin::MyScores::run() {
unsigned long count = 0;
while (true) {
auto score = channel.take();
if (!score) {
break;
}
addLatestScore(*score);
addTopScore(*score);
count++;
SDL_Log("added my score, count=%lu, score=%d, name=%s", count, score->score, score->name.c_str());
}
}

trippin::MyScores::MyScores(int version, unsigned long limit) :
version(version),
limit(limit),
Expand Down Expand Up @@ -56,7 +36,8 @@ static bool scoreOrder(const trippin::Score &left, const trippin::Score &right)
}

void trippin::MyScores::addScore(const trippin::Score &score) {
channel.put(score);
addLatestScore(score);
addTopScore(score);
}

void trippin::MyScores::addLatestScore(const trippin::Score &score) {
Expand Down
3 changes: 0 additions & 3 deletions src/game/MyScores.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace trippin {
class MyScores {
public:
MyScores(int version, unsigned long limit);
void start();
void addScore(const Score &score);
[[nodiscard]] std::vector<Score> getLatestScores() const;
[[nodiscard]] std::vector<Score> getTopScores() const;
Expand All @@ -21,13 +20,11 @@ namespace trippin {

const int version;
const unsigned long limit;
Channel<Score> channel;
Scores latestScores;
Scores topScores;

static std::string fileName(std::string_view type, int version);
static void loadScores(Scores &scores);
void run();
void addLatestScore(const trippin::Score &score);
void addTopScore(const trippin::Score &score);
void resizeAndStore(std::string &filename, std::vector<Score> &scores) const;
Expand Down
2 changes: 1 addition & 1 deletion src/net/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace trippin {
return e;
}

bool put(T elem) {
bool put(const T& elem) {
{
std::lock_guard lock(mutex);
if (closed) {
Expand Down

0 comments on commit af25368

Please sign in to comment.