Skip to content

Commit

Permalink
re-add motd & featured content
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkzy committed Jan 17, 2024
1 parent f8eb92f commit a51fa41
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/client/component/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace download
}, scheduler::pipeline::lui);
}

console::debug("Download progress: %lli/%lli\n", progress, total);
//console::debug("Download progress: %lli/%lli\n", progress, total);
if (download_aborted())
{
return -1;
Expand Down
118 changes: 103 additions & 15 deletions src/client/component/motd.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,125 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"

#include "console.hpp"
#include "materials.hpp"
#include "motd.hpp"
#include "scheduler.hpp"

#include "game/game.hpp"

#include <utils/http.hpp>
#include <utils/string.hpp>

#define WEBSITE_DATA_URL "https://raw.githubusercontent.com/h1-mod/website/publish/data"

namespace motd
{
class component final : public component_interface
namespace
{
public:
void post_load() override
bool waiting = true;
int index = 0;

featured_content_t featured_content;

//std::optional<utils::http::result> motd_image_data;

void get_featured_content(const std::string& content_name, const int content_index)
{
const auto name = utils::string::va(content_name.data(), content_index);
const auto url = utils::string::va(WEBSITE_DATA_URL "/%s", name);
const auto result = utils::http::get_data(url, {}, {}, {});
if (result.has_value())
{
featured_content.insert_or_assign(name, result.value());
}
};

bool handle_featured_content()
{
if (index > 3)
{
return scheduler::cond_end;
}

scheduler::once([&]()
{
if (index > 3)
{
return;
}

const auto name = (index == 0 ? "motd.json" : "featured%d.json");
get_featured_content(name, index);

++index;
}, scheduler::async);

return scheduler::cond_continue;
}

/*
void download_motd_image()
{
motd_image_data = utils::http::get_data(WEBSITE_DATA_URL "/motd.png", {}, {}, {});
waiting = false;
}
bool setup_motd_image()
{
std::thread([]
{
//auto data = utils::http::get_data("https://h1.gg/data/motd.png");
//if (data.has_value())
//{
// materials::add("motd_image", data.value().buffer);
//}
}).detach();
if (waiting)
{
return scheduler::cond_continue;
}
if (!motd_image_data.has_value())
{
printf("motd image doesn't have a value\n");
return scheduler::cond_end;
}
printf("motd image loading...");
const auto material = materials::create_material("motd_image");
try
{
if (!materials::setup_material_image(material, motd_image_data.value().buffer))
{
materials::free_material(material);
}
printf("motd image loaded");
}
catch (const std::exception& e)
{
materials::free_material(material);
console::error("Failed to load MOTD image: %s\n", e.what());
}
return scheduler::cond_end;
}
*/
}

featured_content_t& get_featured_content()
{
return featured_content;
}

void post_unpack() override
class component final : public component_interface
{
public:
void post_load() override
{
if (game::environment::is_sp())
if (!game::environment::is_mp())
{
return;
}

//scheduler::once(download_motd_image, scheduler::async);
//scheduler::schedule(setup_motd_image, scheduler::main);
scheduler::schedule(handle_featured_content, scheduler::async);
}
};
}

REGISTER_COMPONENT(motd::component)
REGISTER_COMPONENT(motd::component)
10 changes: 10 additions & 0 deletions src/client/component/motd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <utils/http.hpp>

namespace motd
{
using featured_content_t = std::unordered_map<std::string, utils::http::result>;

featured_content_t& get_featured_content();
}
10 changes: 5 additions & 5 deletions src/client/component/party.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ namespace party

if (mapname.contains('.') || mapname.contains("::"))
{
throw std::runtime_error(utils::string::va("Invalid server mapname value %s\n", mapname.data()));
throw std::runtime_error(utils::string::va("Invalid server mapname value '%s'", mapname.data()));
}

const auto check_file = [&](const usermap_file& file)
Expand All @@ -261,10 +261,10 @@ namespace party
{
if (!file.optional)
{
std::string missing_value = "Server %s is empty";
std::string missing_value = "Server '%s' is empty";
if (file.name == "usermap_hash"s)
{
missing_value += " (or you are missing content for map '%s')\n";
missing_value += " (or you are missing content for map '%s')";
}
throw std::runtime_error(utils::string::va(missing_value.data(), file.name.data(), mapname.data()));
}
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace party

if (!server_fs_game.starts_with("mods/") || server_fs_game.contains('.') || server_fs_game.contains("::"))
{
throw std::runtime_error(utils::string::va("Invalid server fs_game value %s\n", server_fs_game.data()));
throw std::runtime_error(utils::string::va("Invalid server fs_game value '%s'", server_fs_game.data()));
}

auto needs_restart = false;
Expand All @@ -321,7 +321,7 @@ namespace party
}

throw std::runtime_error(
utils::string::va("Connection failed: Server %s is empty.", file.name.data()));
utils::string::va("Server '%s' is empty", file.name.data()));
}

const auto file_path = server_fs_game + "/mod" + file.extension;
Expand Down
71 changes: 14 additions & 57 deletions src/client/game/demonware/services/bdMarketingComms.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <std_include.hpp>

#include "component/motd.hpp"
#include "component/scheduler.hpp"

#include <utils/http.hpp>
#include "../services.hpp"

Expand All @@ -14,67 +17,21 @@ namespace demonware

void bdMarketingComms::getMessages(service_server* server, byte_buffer* /*buffer*/) const
{
/*auto reply = server->create_reply(this->task_id());
const int timeout = 7; // seconds
std::optional<utils::http::result> motd_content;
std::optional<utils::http::result> featured_content;
std::optional<utils::http::result> featured2_content;
auto get_motd = [&motd_content]()
{
motd_content = utils::http::get_data("https://h1.gg/data/motd.json", {}, {}, {}, timeout);
};
auto get_featured = [&featured_content]()
{
featured_content = utils::http::get_data("https://h1.gg/data/featured.json", {}, {}, {}, timeout);
};
auto get_featured2 = [&featured2_content]()
{
featured2_content = utils::http::get_data("https://h1.gg/data/featured2.json", {}, {}, {}, timeout);
};
std::thread get_motd_thread(get_motd);
std::thread get_featured_thread(get_featured);
std::thread get_featured2_thread(get_featured2);
get_motd_thread.join();
get_featured_thread.join();
get_featured2_thread.join();
if (motd_content.has_value())
{
const auto motd = new bdMarketingMessage;
motd->m_messageID = 1;
motd->m_languageCode = "en-US";
motd->m_content = motd_content.value().buffer.data();
motd->m_metadata = "{}";
reply->add(motd);
}
if (featured_content.has_value())
{
const auto featured = new bdMarketingMessage;
featured->m_messageID = 2;
featured->m_languageCode = "en-US";
featured->m_content = featured_content.value().buffer.data();
featured->m_metadata = "{}";
reply->add(featured);
}
auto reply = server->create_reply(this->task_id());

if (featured2_content.has_value())
int message_id = 1;
const auto featured_content = motd::get_featured_content();
for (const auto& [key, value] : featured_content)
{
const auto featured2 = new bdMarketingMessage;
featured2->m_messageID = 3;
featured2->m_languageCode = "en-US";
featured2->m_content = featured2_content.value().buffer.data();
featured2->m_metadata = "{}";
reply->add(featured2);
const auto marketing_message = new bdMarketingMessage;
marketing_message->m_messageID = message_id;
marketing_message->m_languageCode = "en-US";
marketing_message->m_content = value.buffer;
marketing_message->m_metadata = "{}";
reply->add(marketing_message);
++message_id;
}

reply->send();*/
auto reply = server->create_reply(this->task_id());
reply->send();
}

Expand Down

0 comments on commit a51fa41

Please sign in to comment.