-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from cavoke-project/feature/game-submit
[server] add basic game submissions
- Loading branch information
Showing
9 changed files
with
1,607 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,3 +178,4 @@ local_server/ | |
# Drogon logs | ||
*.log | ||
/games/codenames/client/client.zip | ||
uploads/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "gamesubmissions_controller.h" | ||
#include "filters/AuthFilter.h" | ||
|
||
namespace cavoke::server::controllers { | ||
|
||
void GameSubmissionsController::submit_game( | ||
const drogon::HttpRequestPtr &req, | ||
std::function<void(const drogon::HttpResponsePtr &)> &&callback) { | ||
// TODO: Auth. Currently problems with authenticating the form request. | ||
// parse web form | ||
drogon::MultiPartParser mpp; | ||
int res = mpp.parse(req); | ||
if (res == -1) { | ||
return CALLBACK_STATUS_CODE(k400BadRequest); | ||
} | ||
// form files | ||
auto files = mpp.getFilesMap(); | ||
// form params | ||
nlohmann::json j = mpp.getParameters(); | ||
if (!j.contains("git_repo")) { | ||
j["git_repo"] = "<empty>"; | ||
} | ||
// serialize | ||
GameSubmissionReq req_data; | ||
try { | ||
req_data = j.get<GameSubmissionReq>(); | ||
} catch (...) { | ||
return CALLBACK_STATUS_CODE(k400BadRequest); | ||
} | ||
// validation | ||
if (req_data.package_type != "Git Repository" && | ||
req_data.package_type != "Zip Archive") { | ||
return CALLBACK_STATUS_CODE(k400BadRequest); | ||
} | ||
|
||
auto id = drogon::utils::getUuid(); | ||
auto submission = req_data.to_orm(id); | ||
if (req_data.package_type == "Zip Archive") { | ||
try { | ||
files.at("client_zip").saveAs(id + "-client.zip"); | ||
files.at("server_zip").saveAs(id + "-server.zip"); | ||
} catch (...) { | ||
return CALLBACK_STATUS_CODE(k400BadRequest); | ||
} | ||
} | ||
// save to db | ||
mp_submissions.insert(submission); | ||
|
||
LOG_INFO << "New game submission! Game: " << req_data.display_name; | ||
auto resp = newStatusCodeResponse(drogon::k200OK); | ||
resp->setBody( | ||
"Thanks for your game submission! We will review it and publish your " | ||
"game shortly."); | ||
return callback(resp); | ||
} | ||
} // namespace cavoke::server::controllers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef CAVOKE_GAMESUBMISSIONS_CONTROLLER_H | ||
#define CAVOKE_GAMESUBMISSIONS_CONTROLLER_H | ||
|
||
#include <drogon/HttpController.h> | ||
#include <nlohmann/json.hpp> | ||
#include "sql-models/Gamesubmissions.h" | ||
#include "utils.h" | ||
|
||
namespace cavoke::server::controllers { | ||
|
||
class GameSubmissionsController | ||
: public drogon::HttpController<GameSubmissionsController, true> { | ||
public: | ||
METHOD_LIST_BEGIN | ||
ADD_METHOD_TO(GameSubmissionsController::submit_game, | ||
"/submit_game", | ||
drogon::Post, | ||
drogon::Options); | ||
METHOD_LIST_END | ||
|
||
struct GameSubmissionReq { | ||
std::string game_id; | ||
std::string display_name; | ||
std::string description; | ||
std::string package_type; | ||
std::string git_repo; | ||
|
||
auto to_orm(const std::string &id) { | ||
drogon_model::cavoke_orm::Gamesubmissions res; | ||
res.setId(id); | ||
res.setGameId(game_id); | ||
res.setPackageType(package_type); | ||
res.setGitRepo(git_repo); | ||
res.setDisplayName(display_name); | ||
return res; | ||
} | ||
}; | ||
|
||
protected: | ||
void submit_game( | ||
const drogon::HttpRequestPtr &req, | ||
std::function<void(const drogon::HttpResponsePtr &)> &&callback); | ||
|
||
private: | ||
MAPPER_TYPE(drogon_model::cavoke_orm::Gamesubmissions) | ||
mp_submissions = MAPPER_FOR(drogon_model::cavoke_orm::Gamesubmissions); | ||
}; | ||
|
||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GameSubmissionsController::GameSubmissionReq, | ||
game_id, | ||
display_name, | ||
description, | ||
package_type, | ||
git_repo); | ||
|
||
} // namespace cavoke::server::controllers | ||
|
||
#endif // CAVOKE_GAMESUBMISSIONS_CONTROLLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.