Skip to content

Commit

Permalink
controller tui command decipher (in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 23, 2024
1 parent 5b2ee64 commit 993fbca
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 229 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/building.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
os: ubuntu-latest
compiler: clang++

- name: MacOS GCC
os: macos-latest
compiler: g++

#- name: MacOS Clang
# os: macos-latest
# compiler: clang++
Expand All @@ -30,6 +34,10 @@ jobs:
os: windows-latest
compiler: g++

- name: Windows Clang
os: windows-latest
compiler: clang++

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(PROJECT_COPYRIGHT "Copyright (c) 2022-2024 r3w0p")
set(PROJECT_DESCRIPTION "A TUI version of the Caravan card game from Fallout: New Vegas.")
set(PROJECT_DESCRIPTION "A command-line version of the Caravan card game from Fallout: New Vegas.")
set(PROJECT_URL "https://github.com/r3w0p/caravan")

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-static")
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

| v1.0.0 | GPL-3.0 | (c) 2022-2024 r3w0p |

A CLI version of the Caravan card game from Fallout: New Vegas.
A command-line version of the Caravan card game from Fallout: New Vegas.


===========
Expand Down
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_user_input(std::string input) override; // from ViewSubscriber
void on_view_user_input(std::string input, bool confirmed) override; // from ViewSubscriber
};

#endif //CARAVAN_CONTROLLER_TUI_H
2 changes: 1 addition & 1 deletion include/caravan/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ViewSubscriber {
public:
virtual void on_view_user_input(std::string input) = 0;
virtual void on_view_user_input(std::string input, bool confirmed) = 0;
};

class View : public Publisher<ViewSubscriber> {
Expand Down
222 changes: 213 additions & 9 deletions src/caravan/controller/controller_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,230 @@

#include "caravan/controller/controller_tui.h"

void ControllerTUI::on_view_user_input(std::string input) {
void ControllerTUI::on_view_user_input(std::string input, bool confirmed) {
if(closed) return;

// TODO A future feature that highlights parts of the board as a command is typed,
// so that the user has a better idea of what their command will do.
if(!confirmed) return;

// Decipher input
GameCommand command;

/*
* EXIT
*/
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')
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;
}

/*
* FIRST
* - OPTION TYPE
*/

if (input.size() < 1)
throw CaravanFatalException("A command type has not been entered.");

switch (input.at(0)) {
case 'P':
case 'p':
command.type = OPTION_PLAY;
/*
* P2F
* "Play numeral card at hand pos 2 onto caravan F"
*
* P4F8
* "Play face card at hand pos 4 onto caravan F, slot 8"
*/
break;
case 'D':
case 'd':
command.type = OPTION_DISCARD;
/*
* D3
* "Discard card at hand pos 3"
*/
break;
case 'C':
case 'c':
/*
* CE
* "Clear caravan E"
*/
command.type = OPTION_CLEAR;
break;
default:
throw CaravanGameException("Invalid option '" + std::to_string(input.at(0)) + "', must be one of: (P)lay, (D)iscard, (C)lear.");
}

/*
* SECOND
* - HAND POSITION or
* - CARAVAN NAME
*/

if (command.type == OPTION_PLAY or command.type == OPTION_DISCARD) {

if (input.size() < 2)
throw CaravanFatalException("A hand position has not been entered.");

switch (input.at(1)) {
case '1':
command.pos_hand = 1;
break;
case '2':
command.pos_hand = 2;
break;
case '3':
command.pos_hand = 3;
break;
case '4':
command.pos_hand = 4;
break;
case '5':
command.pos_hand = 5;
break;
case '6':
command.pos_hand = 6;
break;
case '7':
command.pos_hand = 7;
break;
case '8':
command.pos_hand = 8;
break;
default:
throw CaravanGameException("Invalid hand position '" + std::to_string(input.at(1)) + "'.");
}

} else if (command.type == OPTION_CLEAR) {

} else if(input.size() >= 2) {
if (input.size() < 2)
throw CaravanFatalException("A caravan name has not been entered.");

} else return;
switch (input.at(1)) {
case 'A':
case 'a':
command.caravan_name = CARAVAN_A;
break;
case 'B':
case 'b':
command.caravan_name = CARAVAN_B;
break;
case 'C':
case 'c':
command.caravan_name = CARAVAN_C;
break;
case 'D':
case 'd':
command.caravan_name = CARAVAN_D;
break;
case 'E':
case 'e':
command.caravan_name = CARAVAN_E;
break;
case 'F':
case 'f':
command.caravan_name = CARAVAN_F;
break;
default:
throw CaravanGameException("Invalid caravan name '" + std::to_string(input.at(1)) + "', must be between: A-F.");
}
}

// Discard/clear does not require caravan name/pos
if (command.type == OPTION_DISCARD or command.type == OPTION_CLEAR)
return command; // TODO fix

/*
* THIRD
* - CARAVAN NAME
*/

if (input.size() < 3)
throw CaravanFatalException("A caravan name has not been entered.");

switch (input.at(2)) {
case 'A':
case 'a':
command.caravan_name = CARAVAN_A;
break;
case 'B':
case 'b':
command.caravan_name = CARAVAN_B;
break;
case 'C':
case 'c':
command.caravan_name = CARAVAN_C;
break;
case 'D':
case 'd':
command.caravan_name = CARAVAN_D;
break;
case 'E':
case 'e':
command.caravan_name = CARAVAN_E;
break;
case 'F':
case 'f':
command.caravan_name = CARAVAN_F;
break;
default:
throw CaravanGameException(
"Invalid caravan name '" + std::to_string(input.at(2)) + "', must be between: A-F.");
}

/*
* FOURTH
* - CARAVAN POSITION (used when selecting Face card only)
*/

// Caravan position only specified here when playing a face card
if ((char) input.at(3) == '\0') {
command.pos_caravan = 0;
return command; // TODO fix
}

switch (input.at(3)) {
case '1':
command.pos_caravan = 1;
break;
case '2':
command.pos_caravan = 2;
break;
case '3':
command.pos_caravan = 3;
break;
case '4':
command.pos_caravan = 4;
break;
case '5':
command.pos_caravan = 5;
break;
case '6':
command.pos_caravan = 6;
break;
case '7':
command.pos_caravan = 7;
break;
case '8':
command.pos_caravan = 8;
break;
case '9':
command.pos_caravan = 9;
break;
default:
throw CaravanGameException("Invalid caravan position '" + std::to_string(input.at(3)) + "'.");
}

// Pass to subscribers
// Pass to subscribers (i.e., Model)
for(ControllerSubscriber *s : subscribers) {
s->on_controller_command(command);
}
Expand Down
Loading

0 comments on commit 993fbca

Please sign in to comment.