Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network #120

Draft
wants to merge 49 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
4efe561
Create background structure
Alexeyha May 18, 2022
29a9863
Change camera position
Alexeyha May 18, 2022
6165a9c
Complete background
Alexeyha May 22, 2022
0c02c7b
Merge remote-tracking branch 'origin/main' into 104-create-background
Alexeyha May 27, 2022
b71d7d9
Merge main
Alexeyha May 27, 2022
ce4ef0f
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
628113e
Fix PauseMenu render
MhlvDenis May 28, 2022
6ee5a4b
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
84d9c73
Update config
Alexeyha May 29, 2022
703b6a6
Add scale to arguments
Alexeyha May 29, 2022
6a7fa64
Merge remote-tracking branch 'origin/115-network' into 115-network
MhlvDenis May 29, 2022
fe60003
Test for httplib
MhlvDenis May 29, 2022
cf3727a
Add simple client and server realisation
MhlvDenis May 29, 2022
ce97028
Merge pull request #119 from PlatformerTeam/104-create-background
Alexeyha May 30, 2022
2d1ea08
Chain server and window
MhlvDenis May 30, 2022
20f9518
Add server control buttons
MhlvDenis May 30, 2022
04e24ce
Add server logs
MhlvDenis May 30, 2022
87dc434
Add mutex for logs and db
MhlvDenis May 30, 2022
84d2adf
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
995cbe3
Test for httplib
MhlvDenis May 29, 2022
72b1e90
Add simple client and server realisation
MhlvDenis May 29, 2022
9b3b98f
Chain server and window
MhlvDenis May 30, 2022
205ff02
Add server control buttons
MhlvDenis May 30, 2022
acc028e
Add server logs
MhlvDenis May 30, 2022
2af5698
Add mutex for logs and db
MhlvDenis May 30, 2022
fe12638
Provide rebase
MhlvDenis May 30, 2022
9b6e2be
Merge remote-tracking branch 'origin/115-network' into 115-network
MhlvDenis May 30, 2022
8560e00
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
1f9e1fb
Test for httplib
MhlvDenis May 29, 2022
cf8a92f
Add simple client and server realisation
MhlvDenis May 29, 2022
cb7cdac
Chain server and window
MhlvDenis May 30, 2022
7a9ec4a
Add server control buttons
MhlvDenis May 30, 2022
3e4ee47
Add server logs
MhlvDenis May 30, 2022
4c81fe7
Add mutex for logs and db
MhlvDenis May 30, 2022
9ab7131
Provide rebase
MhlvDenis May 30, 2022
7a89383
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
9aaded5
Add httplib.h in submodule, move resources, add network directory and…
MhlvDenis May 28, 2022
0a95808
Add simple client and server realisation
MhlvDenis May 29, 2022
35b01e9
Add server control buttons
MhlvDenis May 30, 2022
765788e
Add server logs
MhlvDenis May 30, 2022
7021ea2
Add mutex for logs and db
MhlvDenis May 30, 2022
a5a02b2
Merge remote-tracking branch 'origin/115-network' into 115-network
MhlvDenis May 30, 2022
69c2c45
Implement LevelLoaderFromServer
MhlvDenis Jun 1, 2022
6e90660
Level-table in database
MhlvDenis Jun 2, 2022
6f710e2
Load levels from server
MhlvDenis Jun 2, 2022
7cef2f0
something
MhlvDenis Jun 3, 2022
7d466e6
Input levelname
MhlvDenis Jun 3, 2022
39d234d
Input levelname frontend
MhlvDenis Jun 4, 2022
33ef740
Input levelname with checking
MhlvDenis Jun 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "deps/libpqxx"]
path = deps/libpqxx
url = https://github.com/jtv/libpqxx
[submodule "deps/cpp-httplib"]
path = deps/cpp-httplib
url = https://github.com/yhirose/cpp-httplib
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ add_subdirectory(${DIRECTORY_PQXX})
message(STATUS "PostgreSQL include: ${INCLUDE_PQXX}")
message(STATUS "PostgreSQL library: ${LIBRARY_PQXX}")

# HTTP library
set(DIRECTORY_HTTP ${PROJECT_SOURCE_DIR}/deps/cpp-httplib)
set(INCLUDE_HTTP ${DIRECTORY_HTTP})

add_subdirectory(${DIRECTORY_HTTP})
message(STATUS "HTTP include: ${INCLUDE_HTTP}")

# Core library
set(DIRECTORY_CORE ${PROJECT_SOURCE_DIR}/core)
set(INCLUDE_CORE ${DIRECTORY_CORE})
Expand All @@ -100,11 +107,21 @@ set(DIRECTORY_GAME ${PROJECT_SOURCE_DIR}/game)
set(EXECUTABLE_GAME example)
set(EXECUTABLE_GAME_RUNNER game_runner_example)
set(EXECUTABLE_GAME_DATABASE game_database_example)
set(EXECUTABLE_LOADER_FROM_SERVER game_loader_from_server_example)

add_subdirectory(${DIRECTORY_GAME})
message(STATUS "Game executable: ${EXECUTABLE_GAME}")
message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}")

# Network binaries
set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network)
set(EXECUTABLE_SIMPLE_SERVER simple-server)
set(EXECUTABLE_SIMPLE_CLIENT simple-client)

add_subdirectory(${DIRECTORY_NETWORK})
message(STATUS "Server executable: ${EXECUTABLE_SERVER}")
message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}")

# Tests
set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test)
set(EXECUTABLE_TEST test)
Expand Down
5 changes: 5 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ set(
runner/GameRunner.hpp runner/GameRunner.cpp
loader/LevelLoader.hpp
loader/LevelLoaderFromFile.cpp loader/LevelLoaderFromFile.hpp
loader/LevelLoaderFromServer.hpp loader/LevelLoaderFromServer.cpp

visual/image/Image.hpp
visual/image/static/RenderableStatic.cpp visual/image/static/RenderableStatic.hpp
visual/image/shape/square/RenderableSquare.cpp visual/image/shape/square/RenderableSquare.hpp
visual/image/animated/RenderableAnimatedOneFile.cpp visual/image/animated/RenderableAnimatedOneFile.hpp
visual/image/animated/RenderableAnimatedSeveralFiles.cpp visual/image/animated/RenderableAnimatedSeveralFiles.hpp
visual/image/background/RenderableBackground.cpp visual/image/background/RenderableBackground.hpp

visual/image/shape/Shape.cpp visual/image/shape/Shape.hpp
visual/image/shape/square/Square.cpp visual/image/shape/square/Square.hpp
visual/image/static/StaticImage.cpp visual/image/static/StaticImage.hpp
visual/image/animated/AnimatedImageOneFile.cpp visual/image/animated/AnimatedImageOneFile.hpp
visual/image/animated/AnimatedImageSeveralFiles.cpp visual/image/animated/AnimatedImageSeveralFiles.hpp
visual/image/background/BackgroundImage.cpp visual/image/background/BackgroundImage.hpp

visual/image/storage/ImageStorage.cpp visual/image/storage/ImageStorage.hpp
visual/Renderable.hpp
visual/Camera.cpp visual/Camera.hpp
Expand Down
37 changes: 36 additions & 1 deletion core/database/database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace mad::core {
"levels_completed SMALLINT NOT NULL);";
w.exec(m_query);

m_query = "CREATE TABLE IF NOT EXISTS levels("
"id SERIAL PRIMARY KEY,"
"name TEXT NOT NULL UNIQUE);";
w.exec(m_query);

w.commit();

SPDLOG_DEBUG("Tables created successfully");
Expand All @@ -40,10 +45,17 @@ namespace mad::core {
return !rows_found.empty();
}

bool Database::is_level_exists(const std::string &levelname) {
pqxx::work W(m_connector);
m_query = "SELECT * FROM levels WHERE name = '" + W.esc(levelname) + "';";
pqxx::result rows_found = W.exec(m_query);
return !rows_found.empty();
}

void Database::registry_user(const std::string &username) {
pqxx::work W(m_connector);

m_query = "SELECT id FROM users";
m_query = "SELECT id FROM users;";
pqxx::result total_rows = W.exec(m_query);
std::size_t id = total_rows.size();

Expand All @@ -56,6 +68,15 @@ namespace mad::core {
W.commit();
}

void Database::append_level(const std::string &levelname) {
pqxx::work W(m_connector);

m_query = "INSERT INTO levels(name) VALUES('" + levelname + "');";
W.exec(m_query);

W.commit();
}

std::size_t Database::get_id(const std::string &username) {
pqxx::work W(m_connector);
m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';";
Expand Down Expand Up @@ -99,4 +120,18 @@ namespace mad::core {
void Database::reset_progress(const std::string &username) {
reset_progress(get_id(username));
}

std::string Database::get_levelname(std::size_t id) {
pqxx::work W(m_connector);
m_query = "SELECT * FROM levels WHERE id = " + std::to_string(id) + ";";
auto found_row = W.exec1(m_query);
return found_row["name"].as<std::string>();
}

std::size_t Database::get_levels_total() {
pqxx::work W(m_connector);
m_query = "SELECT id FROM levels;";
auto total_rows = W.exec(m_query);
return total_rows.size();
}
}
11 changes: 10 additions & 1 deletion core/database/database/Database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <pqxx/pqxx>
#include <cstddef>
#include <mutex>


namespace mad::core {
Expand All @@ -13,13 +14,21 @@ namespace mad::core {

bool is_user_exists(const std::string &username);

void registry_user(const std::string &username);
bool is_level_exists(const std::string &levelname);

std::size_t get_id(const std::string &username);

std::size_t get_progress(std::size_t id);
std::size_t get_progress(const std::string &username);

std::string get_levelname(std::size_t id);

std::size_t get_levels_total();

void registry_user(const std::string &username);

void append_level(const std::string &levelname);

void increment_progress(std::size_t id);
void increment_progress(const std::string &username);

Expand Down
41 changes: 33 additions & 8 deletions core/loader/LevelLoaderFromFile.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "LevelLoaderFromFile.hpp"
#include <event/management/condition/CollisionCondition.hpp>
#include "event/management/condition/KeyDownCondition.hpp"
#include "event/management/condition/KeyPressedCondition.hpp"
#include "event/management/condition/TimerCondition.hpp"
#include "event/management/condition/TrueCondition.hpp"
#include "event/management/controller/statemachine/StateMachine.hpp"
#include <event/management/condition/TimerCondition.hpp>
#include <event/management/controller/statemachine/StateMachine.hpp>

#include <common/Error.hpp>

Expand All @@ -25,7 +22,7 @@ namespace mad::core {

Vec2d camera_position = {m_config_json["camera"]["position"]["x"],
m_config_json["camera"]["position"]["y"]};
auto camera = std::make_shared<mad::core::Camera>(camera_position, world, true);
auto camera = std::make_shared<mad::core::Camera>(camera_position, world);

auto keys = create_world(world);

Expand Down Expand Up @@ -82,6 +79,9 @@ namespace mad::core {
float current_position_y = object_size / 2;
std::unordered_map<LevelLoaderFromFile::IdKeys, Entity::Id> keys;
std::string map_line;

create_background(world);

while (std::getline(m_level_map, map_line)) {
for (char object: map_line) {
switch (m_objects[object]) {
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace mad::core {
void LevelLoaderFromFile::create_block(std::shared_ptr<LocalWorld> world,
Vec2d position, float block_size, bool is_stable) {

std::filesystem::path source("../../game/resources/static/");
std::filesystem::path source("../../resources/static/");
if (is_stable) {
source /= static_cast<std::string>(m_config_json["texture"]["stable"]);
} else {
Expand Down Expand Up @@ -203,7 +203,7 @@ namespace mad::core {
}

Entity::Id LevelLoaderFromFile::create_finish_block(std::shared_ptr<LocalWorld> world, Vec2d position, float block_size) {
std::filesystem::path source("../../game/resources/static/");
std::filesystem::path source("../../resources/static/");
source /= static_cast<std::string>(m_config_json["texture"]["finish"]);

auto image_storage = std::make_shared<ImageStorage>(
Expand All @@ -223,4 +223,29 @@ namespace mad::core {
);
}

void LevelLoaderFromFile::create_background(std::shared_ptr<LocalWorld> world) {
std::filesystem::path source(m_config_json["background"]["source"]);

std::shared_ptr<ImageStorage> image_storage;
std::vector<float> parallax_ratios = m_config_json["background"]["parallax_ratios"];

image_storage = std::make_shared<ImageStorage>(
std::unordered_map<ImageStorage::TypeAction, std::shared_ptr<Image>>(
{{ImageStorage::TypeAction::Idle,
std::make_shared<BackgroundImage>(
source,
parallax_ratios,
m_config_json["background"]["scale"]
)
}}
)
);
world->create_viewable_entity(
-1,
{0, 0},
0,
image_storage
);
}

}
4 changes: 3 additions & 1 deletion core/loader/LevelLoaderFromFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ namespace mad::core {

Entity::Id create_hero(std::shared_ptr<LocalWorld> world, Vec2d position);

void create_background(std::shared_ptr<LocalWorld> world);

Entity::Id create_finish_block(std::shared_ptr<LocalWorld> world, Vec2d position, float block_size);

private:
Expand All @@ -116,7 +118,7 @@ namespace mad::core {
Hero,
Enemy1,
Enemy2,
Empty,
Empty
};

std::filesystem::path m_level_directory;
Expand Down
Loading