Skip to content

Commit

Permalink
dfkjghjdfgjhf
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 21, 2024
1 parent 4351d3c commit 3f3074a
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/building.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
os: ubuntu-latest
compiler: clang++

#- name: MacOS Clang
# os: macos-latest
# compiler: clang++
- name: MacOS Clang
os: macos-latest
compiler: clang++

- name: Windows GCC
os: windows-latest
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ add_library(view
"include/caravan/view/view.h"
"include/caravan/view/view_tui.h"

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

Expand Down
13 changes: 11 additions & 2 deletions include/caravan/controller/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@

class ControllerSubscriber {
public:
virtual void on_user_input(std::string input, bool complete) = 0; // TODO
virtual void on_controller_command(GameCommand command) = 0; // TODO
};

class Controller : public ViewSubscriber, Publisher<ControllerSubscriber> {};
class Controller : public ViewSubscriber, public Publisher<ControllerSubscriber> {
protected:
bool closed;
public:
virtual ~Controller() = default;
explicit Controller() : closed(false) {};

void subscribe(ControllerSubscriber *sub) override;
void close() { closed = true; };
};

#endif //CARAVAN_CONTROLLER_H
2 changes: 1 addition & 1 deletion include/caravan/controller/controller_tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ControllerTUI : public Controller {
public:
explicit ControllerTUI() = default;

void on_view_update() override; // from ViewSubscriber
void on_view_user_input(std::string input) override; // from ViewSubscriber
};

#endif //CARAVAN_CONTROLLER_TUI_H
28 changes: 17 additions & 11 deletions include/caravan/core/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ typedef std::vector<Card> Deck;
typedef std::array<Card, TRACK_FACE_MAX> Faces;
typedef std::array<CaravanName, 3> PlayerCaravanNames;


typedef struct Slot {
Card card {};
Faces faces {};
Expand All @@ -91,25 +90,32 @@ typedef struct Slot {
typedef std::array<Slot, TRACK_NUMERIC_MAX> Track;

typedef struct GameConfig {
uint8_t pa_num_cards;
uint8_t pa_num_sample_decks;
bool pa_balanced_sample;
uint8_t pa_num_cards {0};
uint8_t pa_num_sample_decks {0};
bool pa_balanced_sample {false};

uint8_t pb_num_cards;
uint8_t pb_num_sample_decks;
bool pb_balanced_sample;
uint8_t pb_num_cards {0};
uint8_t pb_num_sample_decks {0};
bool pb_balanced_sample {false};

PlayerName pn_first;
PlayerName pn_first {NO_PLAYER};
} GameConfig;

typedef struct GameOption {
typedef struct GameOption { // TODO remove
OptionType type {};
uint8_t pos_hand {};
CaravanName caravan_name {};
uint8_t pos_caravan {};
Card card {};
} GameOption;

typedef struct GameCommand {
OptionType type {NO_OPTION};
uint8_t pos_hand {0};
CaravanName caravan_name {NO_CARAVAN};
uint8_t pos_caravan {0};
} GameCommand;

/*
* FUNCTIONS
*/
Expand All @@ -122,12 +128,12 @@ bool is_face_card(Card c);
* CLASSES
*/

template <typename T>
template <class T>
class Publisher {
protected:
std::vector<T*> subscribers;
public:
void subscribe(T *sub);
virtual void subscribe(T *sub) = 0;
};

#endif //CARAVAN_CORE_COMMON_H
1 change: 1 addition & 0 deletions include/caravan/user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class User {
protected:
PlayerName name;
public:
virtual ~User() = default;
explicit User(PlayerName pn) : name(pn) {};

PlayerName get_name() { return name; }
Expand Down
10 changes: 6 additions & 4 deletions include/caravan/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@

class ViewSubscriber {
public:
virtual void on_view_update() = 0; // TODO
virtual void on_view_user_input(std::string input) = 0;
};

class View : Publisher<ViewSubscriber> {
class View : public Publisher<ViewSubscriber> {
protected:
User *user_top_ptr;
User *user_bottom_ptr;
bool closed;
public:
using Publisher<ViewSubscriber>::subscribe;

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

virtual void run() = 0;
void subscribe(ViewSubscriber *sub) override;
void close() { closed = true; };
};



#endif //CARAVAN_VIEW_H
1 change: 0 additions & 1 deletion include/caravan/view/view_tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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

void run() override;
Expand Down
9 changes: 9 additions & 0 deletions src/caravan/controller/controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#include "caravan/controller/controller.h"

void Controller::subscribe(ControllerSubscriber *sub) {
subscribers.push_back(sub);
}
23 changes: 22 additions & 1 deletion src/caravan/controller/controller_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@

#include "caravan/controller/controller_tui.h"

void ControllerTUI::on_view_update() {
void ControllerTUI::on_view_user_input(std::string input) {
if(closed) return;

// Decipher input
GameCommand command;

if(
input.size() >= 4 and
(input.at(0) == 'E' or input.at(0) == 'e') and
(input.at(1) == 'X' or input.at(1) == 'x') and
(input.at(2) == 'I' or input.at(2) == 'i') and
(input.at(3) == 'T' or input.at(3) == 't')
) {
command.type = OPTION_EXIT;

} else if(input.size() >= 2) {

} else return;

// Pass to subscribers
for(ControllerSubscriber *s : subscribers) {
s->on_controller_command(command);
}
}
6 changes: 0 additions & 6 deletions src/caravan/core/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

#include "caravan/core/common.h"


bool is_numeral_card(Card c) {
return (c.rank >= ACE and c.rank <= TEN);
}

bool is_face_card(Card c) {
return (c.rank >= JACK and c.rank <= JOKER);
}

template<typename T>
void Publisher<T>::subscribe(T *sub) {
subscribers.push_back(sub);
}
32 changes: 22 additions & 10 deletions src/caravan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
#include "caravan/view/view_tui.h"
#include "caravan/user/bot_easy.h"
#include "caravan/controller/controller_tui.h"

#include <iostream>

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

ViewTUI *v;
Controller *c;
// Model *m;

try {
utop = new UserHuman(PLAYER_TOP);
ubottom = new UserHuman(PLAYER_BOTTOM);

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

auto *v = new ViewTUI(utop, ubottom);
auto *c = new ControllerTUI();
// auto *m = ...
// TODO Subscribe components
v->subscribe(c);

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

v->run();
} catch (CaravanFatalException &e) {
std::cout << e.what() << std::endl;
}

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

delete v;
Expand Down
9 changes: 9 additions & 0 deletions src/caravan/view/view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#include "caravan/view/view.h"

void View::subscribe(ViewSubscriber *sub) {
subscribers.push_back(sub);
}
5 changes: 4 additions & 1 deletion src/caravan/view/view_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"


void ViewTUI::run() {
// TODO on_model_exit, close the game

using namespace ftxui;

if(closed) return;

// Input data
std::string user_input;
std::string command;
Expand Down

0 comments on commit 3f3074a

Please sign in to comment.