aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
Diffstat (limited to 'frontend')
-rw-r--r--frontend/CMakeLists.txt4
-rw-r--r--frontend/Player.cpp115
-rw-r--r--frontend/Player.h46
-rw-r--r--frontend/cli.h8
-rw-r--r--frontend/cmd.cpp17
-rw-r--r--frontend/cmd.h7
-rw-r--r--frontend/main.cpp34
-rw-r--r--frontend/print.cpp6
-rw-r--r--frontend/print.h8
-rw-r--r--frontend/rl.cpp (renamed from frontend/cli.cpp)15
-rw-r--r--frontend/rl.h6
-rw-r--r--frontend/strings.h4
12 files changed, 212 insertions, 58 deletions
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
index a773577..9a383ce 100644
--- a/frontend/CMakeLists.txt
+++ b/frontend/CMakeLists.txt
@@ -1,8 +1,8 @@
target_sources(main PUBLIC
main.cpp
- cli.cpp
+ rl.cpp
strings.cpp
- cmd.cpp
print.cpp
+ Player.cpp
)
diff --git a/frontend/Player.cpp b/frontend/Player.cpp
new file mode 100644
index 0000000..d1f3052
--- /dev/null
+++ b/frontend/Player.cpp
@@ -0,0 +1,115 @@
+#include "strings.h"
+#include "Player.h"
+#include "print.h"
+
+using namespace std;
+
+static string argv_pop(vector<string> & argv) {
+ string el = argv.front();
+ argv.erase(argv.begin());
+ return el;
+}
+
+Player::Player(Dungeon & dungeon) : dungeon(dungeon) {
+ this->cmds.clear();
+ cmdset_default();
+}
+
+void Player::cmdset_default() {
+ this->cmds["Kijk"] = &Player::cmd_query;
+ this->cmds["Zoek"] = &Player::cmd_search;
+ this->cmds["Ga"] = &Player::cmd_go;
+ this->cmds["Pak"] = &Player::cmd_get;
+ this->cmds["Leg"] = &Player::cmd_put;
+ this->cmds["Bekijk"] = &Player::cmd_view;
+ this->cmds["Sla"] = &Player::cmd_hit;
+ this->cmds["Draag"] = &Player::cmd_equip;
+ this->cmds["Wacht"] = &Player::cmd_wait;
+ this->cmds["Consumeer"] = &Player::cmd_use;
+ this->cmds["Help"] = &Player::cmd_help;
+ this->cmds["Godmode"] = &Player::cmd_cheat;
+ this->cmds["Quit"] = &Player::cmd_quit;
+}
+
+void Player::cmdset_death() {
+ this->cmds["Help"] = &Player::cmd_help;
+ this->cmds["Quit"] = &Player::cmd_quit;
+ this->cmds["Opnieuw"] = &Player::cmd_restart;
+}
+
+FollowupAction Player::cmd(Argv argv) {
+ if (argv.size() == 0) return FollowupAction::NONE;
+ string cmd = argv_pop(argv);
+
+ if (this->cmds.contains(cmd))
+ return (this->*cmds.at(cmd))(argv);
+
+ print_string(strings::UNKNOWN_CMD);
+
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_query(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_search(Argv argv) {
+ return FollowupAction::UPDATE;
+}
+
+FollowupAction Player::cmd_go(Argv argv) {
+ return FollowupAction::UPDATE;
+}
+
+FollowupAction Player::cmd_get(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_put(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_view(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_hit(Argv argv) {
+ return FollowupAction::UPDATE;
+}
+
+FollowupAction Player::cmd_equip(Argv argv) {
+ return FollowupAction::UPDATE;
+}
+
+FollowupAction Player::cmd_wait(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_use(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_help(Argv argv) {
+ lprtf("De beschikbare commando's zijn: ");
+ bool first = true;
+ for (auto & [ key, _ ] : this->cmds) {
+ if (!first) lprtf(", ");
+ lprtf("%s", key.c_str());
+ first = false;
+ }
+ lprtf(".\n");
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_cheat(Argv argv) {
+ return FollowupAction::NONE;
+}
+
+FollowupAction Player::cmd_quit(Argv argv) {
+ return FollowupAction::EXIT;
+}
+
+FollowupAction Player::cmd_restart(Argv argv) {
+ return FollowupAction::RESTART;
+}
+
diff --git a/frontend/Player.h b/frontend/Player.h
index a99736d..c5fbb1e 100644
--- a/frontend/Player.h
+++ b/frontend/Player.h
@@ -1,9 +1,55 @@
#pragma once
+#include <vector>
+#include <unordered_map>
+#include <string>
+
+class Dungeon;
+
+enum FollowupAction {
+ NONE,
+ UPDATE,
+ EXIT,
+ RESTART,
+};
+
class Player {
+ typedef std::vector<std::string> & Argv;
+ typedef FollowupAction Cmd(Argv);
+
private:
unsigned int health_points = 0;
unsigned int attack_chance = 0;
+public:
+ Player(Dungeon & dungeon);
+ virtual ~Player() = default;
+
+public:
+ FollowupAction cmd(Argv argv);
+
+private:
+ void cmdset_default();
+ void cmdset_death();
+
+private:
+ std::unordered_map<std::string, FollowupAction(Player::*)(Argv)> cmds;
+ Cmd cmd_query;
+ Cmd cmd_search;
+ Cmd cmd_go;
+ Cmd cmd_get;
+ Cmd cmd_put;
+ Cmd cmd_view;
+ Cmd cmd_hit;
+ Cmd cmd_equip;
+ Cmd cmd_wait;
+ Cmd cmd_use;
+ Cmd cmd_help;
+ Cmd cmd_cheat;
+ Cmd cmd_quit;
+ Cmd cmd_restart;
+
+private:
+ Dungeon & dungeon;
};
diff --git a/frontend/cli.h b/frontend/cli.h
deleted file mode 100644
index a069d7a..0000000
--- a/frontend/cli.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#pragma once
-
-#include <string>
-
-void cli_main();
-
-std::string cli_readline();
-
diff --git a/frontend/cmd.cpp b/frontend/cmd.cpp
deleted file mode 100644
index c5b03ea..0000000
--- a/frontend/cmd.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <functional>
-#include <unordered_map>
-
-#include "cmd.h"
-
-using namespace std;
-
-static const unordered_map<string, function<void(const vector<string> &)>> handlers = {
- {"gert", [] (const vector<string> & argv) { } },
-};
-
-void cmd_handle(const vector<string> & argv) {
- string cmd = argv.front();
- vector<string> args = argv;
-
-}
-
diff --git a/frontend/cmd.h b/frontend/cmd.h
deleted file mode 100644
index 2718463..0000000
--- a/frontend/cmd.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-#include <vector>
-#include <string>
-
-void cmd_handle(const std::vector<std::string> & argv);
-
diff --git a/frontend/main.cpp b/frontend/main.cpp
index 2d66888..776525c 100644
--- a/frontend/main.cpp
+++ b/frontend/main.cpp
@@ -3,25 +3,51 @@
#include <memory>
#include "backend/Dungeon.h"
+#include "Player.h"
-#include "cli.h"
+#include "rl.h"
#include "strings.h"
#include "print.h"
using namespace std;
-int main() {
+FollowupAction game_main() {
auto dungeon = make_unique<Dungeon>();
print_string(strings::INTRO);
- string filename = cli_readline();
+ string filename = rl();
if (filename.size() == 0) {
lprtf("TODO: generate dungeon\n");
} else {
lprtf("TODO: load %s\n", filename.c_str());
}
- cli_main();
+ Player player { *dungeon };
+
+ while (1) {
+ string line = rl();
+ if (line.length() == 0) continue;
+ vector<string> argv = split_string(line, " ");
+ FollowupAction action = player.cmd(argv);
+
+ switch (action) {
+ case NONE: break;
+ case UPDATE: {
+ printf("TODO: update!\n");
+ break;
+ }
+ default: return action;
+ }
+ }
+}
+
+int main() {
+ FollowupAction action;
+
+ do {
+ action = game_main();
+ if (action == EXIT) break;
+ } while (action == RESTART);
return EXIT_SUCCESS;
}
diff --git a/frontend/print.cpp b/frontend/print.cpp
index 396e9ad..0c60892 100644
--- a/frontend/print.cpp
+++ b/frontend/print.cpp
@@ -57,15 +57,15 @@ SessionLog::SessionLog() {
this->file = { file, [] (FILE * file) { fclose(file); } };
}
-void SessionLog::append(const string & str) {
+void SessionLog::append(const string & str) const {
this->append(str.data(), str.size());
}
-void SessionLog::append(const char * str) {
+void SessionLog::append(const char * str) const {
this->append(str, strlen(str));
}
-void SessionLog::append(const char * buf, size_t buf_size) {
+void SessionLog::append(const char * buf, size_t buf_size) const {
if (this->file == nullptr) return;
if (buf_size == 0) return;
fwrite(buf, 1, buf_size, this->file.get());
diff --git a/frontend/print.h b/frontend/print.h
index 6b526ae..b8034f2 100644
--- a/frontend/print.h
+++ b/frontend/print.h
@@ -16,12 +16,12 @@ private:
virtual ~SessionLog() = default;
public:
- virtual void append(const std::string & str);
- virtual void append(const char * str);
- virtual void append(const char * buf, size_t buf_size);
+ virtual void append(const std::string & str) const;
+ virtual void append(const char * str) const;
+ virtual void append(const char * buf, size_t buf_size) const;
private:
std::unique_ptr<FILE, std::function<void(FILE*)>> file = nullptr;
- static constexpr bool enable = true;
+ static constexpr const bool enable = false;
};
diff --git a/frontend/cli.cpp b/frontend/rl.cpp
index de78bc1..3e1cc37 100644
--- a/frontend/cli.cpp
+++ b/frontend/rl.cpp
@@ -4,14 +4,12 @@
#include <readline/readline.h>
#include <readline/history.h>
-#include "cli.h"
+#include "rl.h"
#include "frontend/print.h"
-#include "strings.h"
-#include "cmd.h"
using namespace std;
-string cli_readline() {
+string rl() {
const char * PROMPT = "> ";
char * input = readline(PROMPT);
@@ -27,12 +25,3 @@ string cli_readline() {
return out;
}
-void cli_main() {
- while (1) {
- string line = cli_readline();
- if (line.length() == 0) continue;
- vector<string> argv = split_string(line, " ");
- cmd_handle(argv);
- }
-}
-
diff --git a/frontend/rl.h b/frontend/rl.h
new file mode 100644
index 0000000..048460b
--- /dev/null
+++ b/frontend/rl.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <string>
+
+std::string rl();
+
diff --git a/frontend/strings.h b/frontend/strings.h
index 452ad66..678667e 100644
--- a/frontend/strings.h
+++ b/frontend/strings.h
@@ -9,6 +9,10 @@ static constexpr const char * INTRO = R"(Voer de naam van een (.xml) bestand in
om hieruit een kerker te laden, of druk direct op ENTER om een kerker te
genereren.)";
+static constexpr const char * UNKNOWN_CMD = R"(De ingevoerde actie is onbekend
+of op dit moment niet mogelijk. De actie `Help` laat een opsomming van
+bestaande acties te zien.)";
+
}
void print_string(const char *);