From 7c6621108b54fd66fbb7aa87be067a34abcc3ced Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 30 Nov 2011 18:13:03 +0300 Subject: Add server side online players list support. --- src/gui/whoisonline.cpp | 211 +++++++++++++++++++++++++++++++++--------------- src/gui/whoisonline.h | 17 +++- 2 files changed, 160 insertions(+), 68 deletions(-) (limited to 'src/gui') diff --git a/src/gui/whoisonline.cpp b/src/gui/whoisonline.cpp index e0fa4ebcf..fde0f08da 100644 --- a/src/gui/whoisonline.cpp +++ b/src/gui/whoisonline.cpp @@ -41,6 +41,9 @@ #include "playerrelations.h" #include "main.h" +#include "net/net.h" +#include "net/playerhandler.h" + #include "gui/chatwindow.h" #include "utils/gettext.h" @@ -181,7 +184,113 @@ void WhoIsOnline::handleLink(const std::string& link, gcn::MouseEvent *event) } } -void WhoIsOnline::loadList() +void WhoIsOnline::updateWindow(std::vector &friends, + std::vector &neutral, + std::vector &disregard, + std::vector enemy, + int numOnline) +{ + //Set window caption + setCaption(_("Who Is Online - ") + toString(numOnline)); + + //List the online people + sort(friends.begin(), friends.end(), nameCompare); + sort(neutral.begin(), neutral.end(), nameCompare); + sort(disregard.begin(), disregard.end(), nameCompare); + bool addedFromSection(false); + for (int i = 0; i < static_cast(friends.size()); i++) + { + mBrowserBox->addRow(friends.at(i)); + addedFromSection = true; + } + if (addedFromSection == true) + { + mBrowserBox->addRow("---"); + addedFromSection = false; + } + for (int i = 0; i < static_cast(enemy.size()); i++) + { + mBrowserBox->addRow(enemy.at(i)); + addedFromSection = true; + } + if (addedFromSection == true) + { + mBrowserBox->addRow("---"); + addedFromSection = false; + } + for (int i = 0; i < static_cast(neutral.size()); i++) + { + mBrowserBox->addRow(neutral.at(i)); + addedFromSection = true; + } + if (addedFromSection == true && !disregard.empty()) + { + mBrowserBox->addRow("---"); + addedFromSection = false; + } + for (int i = 0; i < static_cast(disregard.size()); i++) + { + mBrowserBox->addRow(disregard.at(i)); + } + + if (mScrollArea->getVerticalMaxScroll() < + mScrollArea->getVerticalScrollAmount()) + { + mScrollArea->setVerticalScrollAmount( + mScrollArea->getVerticalMaxScroll()); + } +} + +void WhoIsOnline::loadList(std::vector &list) +{ + mBrowserBox->clearRows(); + int numOnline = list.size(); + std::vector friends; + std::vector neutral; + std::vector disregard; + std::vector enemy; + + mOnlinePlayers.clear(); + mShowLevel = config.getBoolValue("showlevel"); + + std::vector::const_iterator it = list.begin(); + std::vector::const_iterator it_end = list.end(); + for (; it != it_end; ++ it) + { + std::string nick = *it; + mOnlinePlayers.insert(nick); + + switch (player_relations.getRelation(nick)) + { + case PlayerRelation::NEUTRAL: + default: + neutral.push_back(prepareNick(nick, 0, "0")); + break; + + case PlayerRelation::FRIEND: + friends.push_back(prepareNick(nick, 0, "2")); + break; + + case PlayerRelation::DISREGARDED: + case PlayerRelation::BLACKLISTED: + disregard.push_back(prepareNick(nick, 0, "8")); + break; + + case PlayerRelation::ENEMY2: + enemy.push_back(prepareNick(nick, 0, "1")); + break; + + case PlayerRelation::IGNORED: + case PlayerRelation::ERASED: + //Ignore the ignored. + break; + } + } + + updateWindow(friends, neutral, disregard, enemy, numOnline); +} + +void WhoIsOnline::loadWebList() { if (!mMemoryBuffer) return; @@ -303,59 +412,11 @@ void WhoIsOnline::loadList() line = strtok(nullptr, "\n"); } - //Set window caption - setCaption(_("Who Is Online - ") + toString(numOnline)); - - //List the online people - sort(friends.begin(), friends.end(), nameCompare); - sort(neutral.begin(), neutral.end(), nameCompare); - sort(disregard.begin(), disregard.end(), nameCompare); - bool addedFromSection(false); - for (int i = 0; i < static_cast(friends.size()); i++) - { - mBrowserBox->addRow(friends.at(i)); - addedFromSection = true; - } - if (addedFromSection == true) - { - mBrowserBox->addRow("---"); - addedFromSection = false; - } - for (int i = 0; i < static_cast(enemy.size()); i++) - { - mBrowserBox->addRow(enemy.at(i)); - addedFromSection = true; - } - if (addedFromSection == true) - { - mBrowserBox->addRow("---"); - addedFromSection = false; - } - for (int i = 0; i < static_cast(neutral.size()); i++) - { - mBrowserBox->addRow(neutral.at(i)); - addedFromSection = true; - } - if (addedFromSection == true && !disregard.empty()) - { - mBrowserBox->addRow("---"); - addedFromSection = false; - } - for (int i = 0; i < static_cast(disregard.size()); i++) - { - mBrowserBox->addRow(disregard.at(i)); - } + updateWindow(friends, neutral, disregard, enemy, numOnline); // Free the memory buffer now that we don't need it anymore free(mMemoryBuffer); mMemoryBuffer = nullptr; - - if (mScrollArea->getVerticalMaxScroll() < - mScrollArea->getVerticalScrollAmount()) - { - mScrollArea->setVerticalScrollAmount( - mScrollArea->getVerticalMaxScroll()); - } } size_t WhoIsOnline::memoryWrite(void *ptr, size_t size, @@ -461,15 +522,22 @@ int WhoIsOnline::downloadThread(void *ptr) void WhoIsOnline::download() { - mDownloadComplete = true; - if (mThread && SDL_GetThreadID(mThread)) - SDL_WaitThread(mThread, nullptr); + if (serverVersion < 3) + { + mDownloadComplete = true; + if (mThread && SDL_GetThreadID(mThread)) + SDL_WaitThread(mThread, nullptr); - mDownloadComplete = false; - mThread = SDL_CreateThread(WhoIsOnline::downloadThread, this); + mDownloadComplete = false; + mThread = SDL_CreateThread(WhoIsOnline::downloadThread, this); - if (mThread == nullptr) - mDownloadStatus = UPDATE_ERROR; + if (mThread == nullptr) + mDownloadStatus = UPDATE_ERROR; + } + else + { + Net::getPlayerHandler()->requestOnlineList(); + } } void WhoIsOnline::logic() @@ -513,7 +581,7 @@ void WhoIsOnline::logic() case UPDATE_LIST: if (mDownloadComplete == true) { - loadList(); + loadWebList(); mDownloadStatus = UPDATE_COMPLETE; mUpdateButton->setEnabled(true); mUpdateTimer = 0; @@ -532,18 +600,27 @@ void WhoIsOnline::action(const gcn::ActionEvent &event) { if (event.getId() == "update") { - if (mDownloadStatus == UPDATE_COMPLETE) + if (serverVersion < 3) { - mUpdateTimer = cur_time - 20; - if (mUpdateButton) - mUpdateButton->setEnabled(false); - setCaption(_("Who Is Online - Update")); - if (mThread && SDL_GetThreadID(mThread)) + if (mDownloadStatus == UPDATE_COMPLETE) { - SDL_WaitThread(mThread, nullptr); - mThread = nullptr; + mUpdateTimer = cur_time - 20; + if (mUpdateButton) + mUpdateButton->setEnabled(false); + setCaption(_("Who Is Online - Update")); + if (mThread && SDL_GetThreadID(mThread)) + { + SDL_WaitThread(mThread, nullptr); + mThread = nullptr; + } + mDownloadComplete = true; } - mDownloadComplete = true; + } + else + { + mUpdateTimer = cur_time - 20; + Net::getPlayerHandler()->requestOnlineList(); + setCaption(_("Who Is Online - Update")); } } } diff --git a/src/gui/whoisonline.h b/src/gui/whoisonline.h index 72063b183..112ad35ba 100644 --- a/src/gui/whoisonline.h +++ b/src/gui/whoisonline.h @@ -65,7 +65,9 @@ class WhoIsOnline : public Window, /** * Loads and display online list from the memory buffer. */ - void loadList(); + void loadWebList(); + + void loadList(std::vector &list); void handleLink(const std::string& link, gcn::MouseEvent *event); @@ -83,6 +85,10 @@ class WhoIsOnline : public Window, void optionChanged(const std::string &name); + void updateList(std::vector &list); + + void readFromWeb(); + private: void download(); @@ -101,6 +107,13 @@ private: const std::string prepareNick(std::string nick, int level, std::string color) const; + + void updateWindow(std::vector &friends, + std::vector &neutral, + std::vector &disregard, + std::vector enemy, + int numOnline); + enum DownloadStatus { UPDATE_ERROR = 0, @@ -137,4 +150,6 @@ private: bool mUpdateOnlineList; }; +extern WhoIsOnline *whoIsOnline; + #endif -- cgit v1.2.3-60-g2f50