Skip to content

Commit

Permalink
add json
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldomanek authored and michaeldomanek committed Apr 9, 2021
1 parent 6fbb24a commit 164353b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ Usage: ./robo-server [OPTIONS]
| -m,--max-players | UINT:INT in [2 - 4]=4 | the maximum players of the game |
| -p,--port | UINT=1113 | port to connect to |
| -s,--not-shoot-and-move | FLAG | Robot can not shoot while it is moving |
| -j,--json-config | TEXT:FILE | JSON Configuration for port, max-player, ... |

## verwendete Software
* [asio](https://think-async.com/Asio/)
* [CLI11](https://github.com/CLIUtils/CLI11)
* [fmt](https://github.com/fmtlib/fmt)
* [json](https://github.com/nlohmann/json)
* [spdlog](https://github.com/gabime/spdlog)

* [protocol-buffers](https://developers.google.com/protocol-buffers/)
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"max-players": 2,
"not-shoot-and-move": true
}
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ add_global_arguments('-I' + get_option('cli11_include_dir'), language : 'cpp')
# spdlog: https://github.com/gabime/spdlog
add_global_arguments('-I' + get_option('spdlog_include_dir'), language : 'cpp')

# json: https://github.com/nlohmann/json
add_global_arguments('-I' + get_option('json_include_dir'), language : 'cpp')

# begin protobuf: https://developers.google.com/protocol-buffers/
protoc = find_program('protoc', required : true)
protobuf_dep = dependency('protobuf', required : true)
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
option('asio_include_dir', type : 'string', value : '/home/michael/asio/asio/include/', description : 'the include dir of asio')
option('json_include_dir', type : 'string', value : '/home/michael/', description : 'the include dir of json')
option('cli11_include_dir', type : 'string', value : '/home/michael/', description : 'the dir containing CLI11.hpp')
option('criterion_include_dir', type : 'string', value : '', description : 'the dir containing criterion.hpp')
option('spdlog_include_dir', type : 'string', value : '/home/michael/spdlog/include/', description : 'the include dir of spdlog')
37 changes: 37 additions & 0 deletions src/server_src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "robotProperties.pb.h"
#include "grpcServer.h"

#include "json.hpp"
#include "asio.hpp"
#include "CLI11.hpp"
#include <SFML/Graphics.hpp>
Expand All @@ -26,6 +27,16 @@
using namespace std;
using namespace asio;
using namespace asio::ip;
using json = nlohmann::json;

auto throwValidationError(const CLI::App &app, const string &error) {
try {
spdlog::error(error);
throw CLI::ValidationError(error);
} catch (const CLI::Error &e) {
return app.exit(e);
}
}

int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
Expand Down Expand Up @@ -68,8 +79,34 @@ int main(int argc, char* argv[]) {
bool canNotShootAndMove{};
app.add_flag("-s,--not-shoot-and-move", canNotShootAndMove, "Robot can not shoot while it is moving");

string jsonConfigPath;
app.add_option("-j,--json-config", jsonConfigPath, "JSON Configuration for port, max-player and not-shoot-and-move")->check(CLI::ExistingFile);

CLI11_PARSE(app, argc, argv);

std::ifstream i(jsonConfigPath);
json jsonConfig;
i >> jsonConfig;

if (jsonConfig.contains("max-players")) {
if (jsonConfig["max-players"].is_number()) {
if(jsonConfig["max-players"] >= 2 and jsonConfig["max-players"] <= 4) {
maxPlayers = jsonConfig["max-players"];
} else {
return throwValidationError(app, "max-players must be between 2 and 4");
}
} else {
return throwValidationError(app, "max-players must be a number");
}
}
if (jsonConfig.contains("not-shoot-and-move")) {
if (jsonConfig["not-shoot-and-move"].is_boolean()) {
canNotShootAndMove = jsonConfig["not-shoot-and-move"];
} else {
return throwValidationError(app, "not-shoot-and-move must be a boolean flag");
}
}

auto file_logger = spdlog::rotating_logger_mt("file_logger", "../logs/server.log", 1048576 * 5, 3);
spdlog::set_default_logger(file_logger);
spdlog::set_pattern("[%Y %m %d %H:%M:%S,%e] [%l] %v");
Expand Down

0 comments on commit 164353b

Please sign in to comment.