diff options
author | Hello=) <hello@themanaworld.org> | 2024-03-28 03:58:45 +0300 |
---|---|---|
committer | Hello=) <hello@themanaworld.org> | 2024-03-28 03:58:45 +0300 |
commit | db0b1da0060f5eb4b2af040c22ea9373d36970af (patch) | |
tree | dc2bdc0478c341bf4368fee4993c20e52f2d41c1 /src/onlinelist.cpp | |
download | guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.gz guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.bz2 guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.tar.xz guildbot-db0b1da0060f5eb4b2af040c22ea9373d36970af.zip |
Initial commit of Guild Bot, as seen in:
$ sha256sum guildsrc.zip
ef61469ab59ce3f5dd3289505e89bd5b84e3a82b89cda90d25ae3c41f7464beb guildsrc.zip
Verification shown its source 100% match to currently running version on TMWA.
The following changes were made VS version found in guildsrc.zip:
1) src/build and src/dist content completely removed.
Rationale: build-time artifacts; binaries contained login/password.
2) Executable flag (+x) removed from src/automation.cpp and src/main.cpp.
Rationale: executable CPP sources are weird thing. No change to file content.
3) src/main.cpp changes: 3 lines:
std::string main_username = "test";
std::string main_pw = "test";
std::string main_host = "127.0.0.1";
Rationale: avoid leaking real production credentials and ensure experimenters
dont hammer real TMW prod server by experiments.
Effort been made to preserve original file timestamps, etc. However toplevel src/
dir date adjusted to date of mentioned changes.
Diffstat (limited to 'src/onlinelist.cpp')
-rw-r--r-- | src/onlinelist.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/onlinelist.cpp b/src/onlinelist.cpp new file mode 100644 index 0000000..1c326af --- /dev/null +++ b/src/onlinelist.cpp @@ -0,0 +1,145 @@ +/* + * 4144's whoisonline code. + * + */ + +#include "onlinelist.h" + +#include <iostream> +#include <fstream> +#include <SDL.h> +#include <SDL_thread.h> +#include <vector> +#include <algorithm> + +#include "automation.h" +#include "game.h" +#include "main.h" +#include "utils/stringutils.h" + +OnlineList::OnlineList(): + mThread(NULL), + mDownloadStatus(UPDATE_LIST), + mDownloadComplete(true), + mAllowUpdate(true), + mOnlinePlayers() +{ + download(); +} + +OnlineList::~OnlineList() +{ + if (mThread && SDL_GetThreadID(mThread)) + SDL_WaitThread(mThread, NULL); +} + + +int OnlineList::downloadThread(void *ptr) +{ + OnlineList *wio = reinterpret_cast<OnlineList *>(ptr); + + std::string namelist = "online.txt"; + std::ifstream listFile (namelist.c_str(), std::ifstream::in); + + if (!wio->mAllowUpdate) + { + return 0; + } + + if (listFile.is_open()) + { + wio->mOnlinePlayers.clear(); + const std::string gmText = "(GM)"; + bool listStarted(false); + + while (!listFile.eof()) + { + std::string line; + getline(listFile, line); + if (!line.empty()) + { + std::string lineStr = line; + utils_trim(lineStr); + std::string::size_type pos = lineStr.find(gmText, 0); + if (pos != std::string::npos && pos == lineStr.length() - gmText.length()) + { + lineStr = lineStr.substr(0, pos-1); + utils_trim(lineStr); + + } + + if (listStarted == true) + { + size_t found = lineStr.find(" users are online."); //Dodgy hack! + if (found == std::string::npos) + wio->mOnlinePlayers.insert(lineStr); + } + else if (lineStr == "------------------------------") + listStarted = true; + } + } + listFile.close(); + } + else + { + wio->mDownloadStatus = UPDATE_ERROR; + return 0; + } + + wio->mDownloadComplete = true; + return 0; +} + +void OnlineList::download() +{ + mDownloadComplete = true; + if (mThread && SDL_GetThreadID(mThread)) + SDL_WaitThread(mThread, NULL); + + mDownloadComplete = false; + mThread = SDL_CreateThread(OnlineList::downloadThread, this); + + if (mThread == NULL) + mDownloadStatus = UPDATE_ERROR; +} + +void OnlineList::logic() +{ + + if (!mAllowUpdate) + return; + + if (mUpdateTimer == 0) + mUpdateTimer = cur_time; + + double timeDiff = difftime(cur_time, mUpdateTimer); + int timeLimit = 2; // Downloads the online list every 20 seconds. + + if (timeDiff >= timeLimit && mDownloadStatus != UPDATE_LIST) + { + mUpdateTimer = 0; + mDownloadStatus = UPDATE_LIST; + download(); + } + + switch (mDownloadStatus) + { + case UPDATE_ERROR: + mDownloadStatus = UPDATE_COMPLETE; + break; + case UPDATE_LIST: + if (mDownloadComplete == true) + { + mDownloadStatus = UPDATE_COMPLETE; + mUpdateTimer = 0; + + if (mOnlinePlayers.size() > 0) + Automation::getAutomationHandler()->updateOnline(mOnlinePlayers); + + } + break; + default: + break; + } +} + |