Skip to content

Commit

Permalink
more pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 20, 2024
1 parent 6276b8c commit 4351d3c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 29 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ include_directories(include)

add_library(controller
"include/caravan/controller/controller.h"
"include/caravan/controller/controller_tui.h"

"src/caravan/controller/controller.cpp"
"src/caravan/controller/controller_tui.cpp"
)

add_library(core
Expand Down Expand Up @@ -76,10 +77,10 @@ add_library(user
)

add_library(view
"include/caravan/view/tui.h"
"include/caravan/view/view.h"
"include/caravan/view/view_tui.h"

"src/caravan/view/tui.cpp"
"src/caravan/view/view_tui.cpp"
)

target_link_libraries(view
Expand Down
15 changes: 4 additions & 11 deletions include/caravan/controller/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@
#include "caravan/user/user.h"
#include "caravan/view/view.h"


class Controller {
protected:
Game *game_ptr;
View *view_ptr;
User *user_a_ptr;
User *user_b_ptr;
class ControllerSubscriber {
public:
explicit Controller(Game *g, View *v, User *ua, User *ub) :
game_ptr(g), view_ptr(v), user_a_ptr(ua), user_b_ptr(ub) {};

void run();
virtual void on_user_input(std::string input, bool complete) = 0; // TODO
};

class Controller : public ViewSubscriber, Publisher<ControllerSubscriber> {};

#endif //CARAVAN_CONTROLLER_H
17 changes: 17 additions & 0 deletions include/caravan/controller/controller_tui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#ifndef CARAVAN_CONTROLLER_TUI_H
#define CARAVAN_CONTROLLER_TUI_H

#include "caravan/controller/controller.h"

class ControllerTUI : public Controller {
public:
explicit ControllerTUI() = default;

void on_view_update() override; // from ViewSubscriber
};

#endif //CARAVAN_CONTROLLER_TUI_H
4 changes: 3 additions & 1 deletion include/caravan/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class View : Publisher<ViewSubscriber> {
User *user_bottom_ptr;
bool closed;
public:
using Publisher<ViewSubscriber>::subscribe;

explicit View(User *utop, User *ubottom) :
user_top_ptr(utop), user_bottom_ptr(ubottom), closed(false) {}
user_top_ptr(utop), user_bottom_ptr(ubottom), closed(false) {};

virtual void run() = 0;
void close() { closed = true; };
Expand Down
10 changes: 4 additions & 6 deletions include/caravan/view/tui.h → include/caravan/view/view_tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#ifndef CARAVAN_VIEW_TUI_H
#define CARAVAN_VIEW_TUI_H

#include <string>
#include "caravan/model/game.h"
#include "caravan/user/user.h"
#include "caravan/view/view.h"
#include "caravan/core/common.h"

class TUI : public View {
class ViewTUI : public View {
public:
explicit TUI(User *utop, User *ubottom) :
View(utop, ubottom) {};
using View::subscribe;
explicit ViewTUI(User *utop, User *ubottom) : View(utop, ubottom) {};

void run() override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#include "caravan/controller/controller.h"
#include <string>
#include "caravan/controller/controller_tui.h"

void ControllerTUI::on_view_update() {

void Controller::run() {
// TODO
}
15 changes: 13 additions & 2 deletions src/caravan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#include "caravan/view/tui.h"
#include "caravan/view/view_tui.h"
#include "caravan/user/bot_easy.h"
#include "caravan/controller/controller_tui.h"


int main() {
auto *utop = new UserHuman(PLAYER_TOP);
auto *ubottom = new UserHuman(PLAYER_BOTTOM);

auto *v = new TUI(utop, ubottom);
auto *v = new ViewTUI(utop, ubottom);
auto *c = new ControllerTUI();
// auto *m = ...

// TODO Subscribe
v->subscribe(c);

v->run();

v->close();
// TODO c->close();
// TODO m->close();

delete v;
delete c;
delete utop;
delete ubottom;
}
4 changes: 2 additions & 2 deletions src/caravan/view/tui.cpp → src/caravan/view/view_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "caravan/user/user.h"
#include "caravan/view/view.h"
#include "caravan/view/tui.h"
#include "caravan/view/view_tui.h"

#include <memory>
#include <string>
Expand All @@ -15,7 +15,7 @@
#include "ftxui/dom/elements.hpp"


void TUI::run() {
void ViewTUI::run() {
using namespace ftxui;

// Input data
Expand Down

0 comments on commit 4351d3c

Please sign in to comment.