Skip to content

Commit

Permalink
add utility.hpp to store utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leeendl committed Nov 22, 2023
1 parent 53eccb4 commit 794648e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
62 changes: 11 additions & 51 deletions include/image.hpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,39 @@
#include <opencv2/opencv.hpp>
#include <dpp/message.h>
#include <filesystem>
#include <random>
#define cache_channel 1176057541013282896

class image {
dpp::snowflake id{};
cv::Mat img{};
public:
bool update(cv::Mat new_img = cv::Mat()) {
cv::String path() {
return cv::String(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id)));
}
std::string raw() {
return dpp::utility::read_file(this->path().c_str());
}
bool image_write(cv::Mat new_img = cv::Mat()) {
if (not new_img.empty()) this->img = new_img;
return cv::imwrite(cv::String(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id))), this->img);
return cv::imwrite(this->path(), this->img);
}
/* @param dem y, x e.g. { 100, 100 } */
image(dpp::snowflake id, std::vector<int> dem) {
this->id = id;
update(cv::Mat::zeros(dem[0], dem[1], CV_8UC3) + cv::Scalar(0, 0, 0, 240));
}
std::string raw() {
return dpp::utility::read_file(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id)));
}
std::string path() {
return std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id));
image_write(cv::Mat::zeros(dem[0], dem[1], CV_8UC3) + cv::Scalar(0, 0, 0, 240));
}
image& background(std::string file_name) {
cv::Mat image = cv::imread(cv::String(this->path()));
cv::Mat image = cv::imread(this->path());
cv::Mat background = cv::imread(cv::String(std::format(".\\cache\\{0}.jpg", file_name)));
cv::resize(image, image, background.size());
cv::addWeighted(image, 0.5, background, (1.0 - 0.5), 0.0, this->img);
update();
}
image& add_line(std::vector<int> pt1, std::vector<int> pt2, std::vector<double> RGBA, int thickness = 1) {
if (RGBA.size() == 3) RGBA[4] = 240;
cv::line(this->img, cv::Point(pt1[0], pt1[1]), cv::Point(pt2[0], pt2[1]), cv::Scalar(RGBA[0], RGBA[1], RGBA[2], RGBA[3]), thickness);
update();
}
~image() {
/* this deletes the physical copy and keeps the cloud copy (via discord) */
std::filesystem::remove(std::format(".\\cache\\{0}.jpg", static_cast<uint64_t>(this->id)));
std::filesystem::remove(std::filesystem::path(this->path()));
}
};


std::unique_ptr<std::vector<std::string>> index(const std::string& source, const char& find)
{
std::unique_ptr<std::vector<std::string>> i = std::make_unique<std::vector<std::string>>();
std::string_view preview(source);
size_t pos = 0;
while ((pos = preview.find(find)) not_eq -1) {
if (pos not_eq 0) i->emplace_back(std::string(preview.substr(0, pos)));
preview.remove_prefix(pos + 1);
}
if (not preview.empty()) i->emplace_back(std::string(preview));
return std::move(i);
}
template<typename T> T rand(T min, T max) {
std::uniform_int_distribution<T> integer(min, max);
std::default_random_engine random;
return integer(random);
}
time_t string_to_time(std::string str) {
try
{
time_t t = time(0);
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
std::unique_ptr<std::vector<std::string>> is = index(str, ','); // TODO: make it so ',' are not needed. 1h, 30m -> 1h 30m
for (std::string& i : std::move(*is)) {
if (std::ranges::find(i | std::views::reverse, 's') not_eq i.rend()) t += std::stoull(dpp::rtrim(i));
if (std::ranges::find(i | std::views::reverse, 'm') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60;
if (std::ranges::find(i | std::views::reverse, 'h') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60;
if (std::ranges::find(i | std::views::reverse, 'd') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60 * 24;
}
return t;
}
catch (...) {
return time(0);
}
}
38 changes: 38 additions & 0 deletions include/utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <random> // random engine
#include <dpp/stringops.h> // dpp::rtrim()
#include <ranges> // std::ranges::

std::unique_ptr<std::vector<std::string>> index(const std::string& source, const char& find)
{
std::unique_ptr<std::vector<std::string>> i = std::make_unique<std::vector<std::string>>();
std::string_view preview(source);
size_t pos = 0;
while ((pos = preview.find(find)) not_eq -1) {
if (pos not_eq 0) i->emplace_back(std::string(preview.substr(0, pos)));
preview.remove_prefix(pos + 1);
}
if (not preview.empty()) i->emplace_back(std::string(preview));
return std::move(i);
}
template<typename T> T rand(T min, T max) {
static thread_local std::default_random_engine random(std::random_device{}());
return std::uniform_int_distribution<T>(min, max)(random);
}
time_t string_to_time(std::string str) {
try
{
time_t t = time(0);
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
std::unique_ptr<std::vector<std::string>> is = index(str, ','); // TODO: make it so ',' are not needed. 1h, 30m -> 1h 30m
for (std::string& i : std::move(*is)) {
if (std::ranges::find(i | std::views::reverse, 's') not_eq i.rend()) t += std::stoull(dpp::rtrim(i));
if (std::ranges::find(i | std::views::reverse, 'm') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60;
if (std::ranges::find(i | std::views::reverse, 'h') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60;
if (std::ranges::find(i | std::views::reverse, 'd') not_eq i.rend()) t += std::stoull(dpp::rtrim(i)) * 60 * 60 * 24;
}
return t;
}
catch (...) {
return time(0);
}
}
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <image.hpp>
#include <utility.hpp>
using namespace std::chrono;
std::unique_ptr<dpp::cluster> bot = std::make_unique<dpp::cluster>("MTAwNDUxNDkzNTA1OTAwNTQ3MA.______.", dpp::i_all_intents);
std::unordered_map<dpp::snowflake, std::future<void>> cmd_sender;
Expand Down
1 change: 1 addition & 0 deletions neko.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<ClInclude Include="include\dpp\webhook.h" />
<ClInclude Include="include\dpp\wsclient.h" />
<ClInclude Include="include\image.hpp" />
<ClInclude Include="include\utility.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
1 change: 1 addition & 0 deletions neko.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,6 @@
<Filter>dpp</Filter>
</ClInclude>
<ClInclude Include="include\image.hpp" />
<ClInclude Include="include\utility.hpp" />
</ItemGroup>
</Project>

0 comments on commit 794648e

Please sign in to comment.