Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #27 from The-B1T-Foundation/added_math_game
Browse files Browse the repository at this point in the history
Added math game
  • Loading branch information
b1tflyyyy authored Apr 18, 2024
2 parents 8c1faa6 + 8308303 commit 3541f53
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 6 deletions.
4 changes: 4 additions & 0 deletions source/business_logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ set(MODEL_HEADERS
model/programmer_game/programmer_game_expression_model.hpp
model/config/db_config/db_config_model.hpp
model/config/tg_config/tg_config_model.hpp
model/math_problem_game/math_problem_game_expression_model.hpp
)

set(MODEL_SOURCES
model/user/user_model.cpp
model/programmer_game/programmer_game_expression_model.cpp
model/config/db_config/db_config_model.cpp
model/config/tg_config/tg_config_model.cpp
model/math_problem_game/math_problem_game_expression_model.cpp
)

set(CONTROLLER_HEADERS
Expand All @@ -48,6 +50,7 @@ set(CONTROLLER_HEADERS
controller/config/tg_config/tg_config_controller.hpp
controller/db/task_db/task_db_controller.hpp
controller/db/stats_db/stats_db_controller.hpp
controller/math_problem_game/math_problem_game_controller.hpp
)

set(CONTROLLER_SOURCES
Expand All @@ -59,6 +62,7 @@ set(CONTROLLER_SOURCES
controller/config/tg_config/tg_config_controller.cpp
controller/db/task_db/task_db_controller.cpp
controller/db/stats_db/stats_db_controller.cpp
controller/math_problem_game/math_problem_game_controller.cpp
)

add_library(${LIBRARY_NAME} STATIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AState_DB_Controller : public ABase_DB_Controller
{
Default,
Programmer_Game,
Math_Game,
};

public:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "math_problem_game_controller.hpp"

// ---------------------------------------------------------------------------------------------------------------------
math_game::SMath_Expression AMath_Problem_Game_Controller::Generate_Problem()
{
using namespace math_game;

std::random_device rd{};
std::mt19937 gen{ rd() };
std::uniform_int_distribution<> distribution{ 0, 10 };

const auto rnd_num{ static_cast<std::int32_t>(distribution(gen)) };
const auto first_num{ static_cast<std::int32_t>(distribution(gen)) };
const auto second_num{ static_cast<std::int32_t>(distribution(gen)) };
const auto third_num{ static_cast<std::int32_t>(distribution(gen)) };

// 1'st type of math problem: a + b * c
if (rnd_num >= 0 && rnd_num < 3)
{
return SMath_Expression{std::format("{} + {} * {}", first_num, second_num, third_num), std::to_string(first_num + second_num * third_num) };
}
// 2'nd type of math problem: (a - b) * c
else if (rnd_num >= 3 && rnd_num < 6)
{
return SMath_Expression{std::format("({} - {}) * {}", first_num, second_num, third_num), std::to_string((first_num - second_num) * third_num) };
}

// 3'd type of math problem: a * b - c
return SMath_Expression{std::format("{} * {} - {}", first_num, second_num, third_num), std::to_string(first_num * second_num - third_num) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#pragma once

#include <random>
#include <format>

#include <model/math_problem_game/math_problem_game_expression_model.hpp>

class AMath_Problem_Game_Controller
{
public:
constexpr AMath_Problem_Game_Controller() = default;
constexpr ~AMath_Problem_Game_Controller() = default;

static math_game::SMath_Expression Generate_Problem();
constexpr static std::int64_t Score{ 5 };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "math_problem_game_expression_model.hpp"

namespace math_game
{
// -----------------------------------------------------------------------------------------------------------------
SMath_Expression::SMath_Expression(std::string_view expression, std::string_view result)
{
std::memcpy(Expression, expression.data(), expression.size());
std::memcpy(Result, result.data(), expression.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#pragma once

#include <string>
#include <cstring>

namespace math_game
{
struct SMath_Expression
{
public:
explicit SMath_Expression(std::string_view expression, std::string_view result);
constexpr ~SMath_Expression() = default;

public:
constexpr static std::size_t SIZE{ 18 };
char Result[SIZE]{ };
char Expression[SIZE]{ };
};
}
27 changes: 25 additions & 2 deletions source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ void AMessage_Handler::Handle_Game_Commands(const TgBot::Message::Ptr& message)
State_DB_Controller.Set_State(message->from->id, AState_DB_Controller::EState_Type::Programmer_Game);
Task_DB_Controller.Set_Answer(message->from->id, expression.Result);
}
else if (message->text == SMessage_Commands::Math_Game)
{
auto expression{ AMath_Problem_Game_Controller::Generate_Problem() };
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Math_Game_Msg(expression));

State_DB_Controller.Set_State(message->from->id, AState_DB_Controller::EState_Type::Math_Game);
Task_DB_Controller.Set_Answer(message->from->id, expression.Result);
}

Task_DB_Controller.Set_Time_Stamp(message->from->id, current_time);
}
Expand All @@ -106,15 +114,30 @@ void AMessage_Handler::Handle_Game_State(std::int64_t user_id, AState_DB_Control
{
if (auto answer{ Task_DB_Controller.Get_Answer(user_id) }; answer != message)
{
Stats_DB_Controller.Set_Score(user_id, (current_score -= AProgrammer_Game_Controller::Score));
Stats_DB_Controller.Set_Score(user_id, (current_score - AProgrammer_Game_Controller::Score));
TG_Bot.getApi().sendMessage(user_id, AMessage_Reply::Get_Incorrect_Answer_Msg(AProgrammer_Game_Controller::Score, answer));
}
else
{
Stats_DB_Controller.Set_Score(user_id, (current_score += AProgrammer_Game_Controller::Score));
Stats_DB_Controller.Set_Score(user_id, (current_score + AProgrammer_Game_Controller::Score));
TG_Bot.getApi().sendMessage(user_id, AMessage_Reply::Get_Correct_Answer_Msg(AProgrammer_Game_Controller::Score));
}

break;
}
case AState_DB_Controller::EState_Type::Math_Game:
{
if (auto answer{ Task_DB_Controller.Get_Answer(user_id) }; answer != message)
{
Stats_DB_Controller.Set_Score(user_id, (current_score - AMath_Problem_Game_Controller::Score));
TG_Bot.getApi().sendMessage(user_id, AMessage_Reply::Get_Incorrect_Answer_Msg(AMath_Problem_Game_Controller::Score, answer));
}
else
{
Stats_DB_Controller.Set_Score(user_id, (current_score + AMath_Problem_Game_Controller::Score));
TG_Bot.getApi().sendMessage(user_id, AMessage_Reply::Get_Correct_Answer_Msg(AMath_Problem_Game_Controller::Score));
}

break;
}
}
Expand Down
6 changes: 5 additions & 1 deletion source/view/handlers/message_handler/message_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
#include <controller/db/task_db/task_db_controller.hpp>
#include <controller/db/stats_db/stats_db_controller.hpp>

#include <controller/programmer_game/programmer_game_controller.hpp>
#include <controller/math_problem_game/math_problem_game_controller.hpp>

#include <model/user/user_model.hpp>

#include <message_commands.hpp>
#include <message_reply.hpp>
#include <controller/programmer_game/programmer_game_controller.hpp>


class AMessage_Handler
{
Expand Down
3 changes: 2 additions & 1 deletion source/view/texts/message_commands/message_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@

constinit std::string_view SMessage_Commands::Start{ "/start" };
constinit std::string_view SMessage_Commands::Profile{ "/profile" };
constinit std::string_view SMessage_Commands::Programmer_Game{ "/pr_game" };
constinit std::string_view SMessage_Commands::Programmer_Game{ "/pr_game" };
constinit std::string_view SMessage_Commands::Math_Game{ "/math_game" };
1 change: 1 addition & 0 deletions source/view/texts/message_commands/message_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ struct SMessage_Commands
constinit static std::string_view Start;
constinit static std::string_view Profile;
constinit static std::string_view Programmer_Game;
constinit static std::string_view Math_Game;
};
8 changes: 7 additions & 1 deletion source/view/texts/message_reply/message_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ std::string AMessage_Reply::Get_Profile_Msg(const AUser& user, std::int64_t scor
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Programmer_Game_Msg(programmer_game::SExpression& expression)
std::string AMessage_Reply::Get_Programmer_Game_Msg(const programmer_game::SExpression& expression)
{
return std::format("{}\n{}\n{}\nAnswer: ?", expression.First_Operand, expression.Operation, expression.Second_Operand);
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Math_Game_Msg(const math_game::SMath_Expression& expression)
{
return std::format("Try to solve: {}", expression.Expression);
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Waiting_Time_Msg(std::int64_t time)
{
Expand Down
5 changes: 4 additions & 1 deletion source/view/texts/message_reply/message_reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include <format>

#include <emoji.hpp>

#include <model/programmer_game/programmer_game_expression_model.hpp>
#include <model/math_problem_game/math_problem_game_expression_model.hpp>
#include <model/user/user_model.hpp>

class AMessage_Reply
Expand All @@ -39,7 +41,8 @@ class AMessage_Reply

static std::string Get_Hello_Msg(std::string_view username);
static std::string Get_Profile_Msg(const AUser& user, std::int64_t score);
static std::string Get_Programmer_Game_Msg(programmer_game::SExpression& expression);
static std::string Get_Programmer_Game_Msg(const programmer_game::SExpression& expression);
static std::string Get_Math_Game_Msg(const math_game::SMath_Expression& expression);
static std::string Get_Waiting_Time_Msg(std::int64_t time);
static std::string Get_Correct_Answer_Msg(std::int64_t score);
static std::string Get_Incorrect_Answer_Msg(std::int64_t score, std::string_view correct_answer);
Expand Down

0 comments on commit 3541f53

Please sign in to comment.