Rewrite RankingCallback for std::shared_ptr Request
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include "network/protocols/client_lobby.hpp"
|
||||
#include "network/stk_host.hpp"
|
||||
#include "states_screens/dialogs/general_text_field_dialog.hpp"
|
||||
#include "states_screens/dialogs/ranking_callback.hpp"
|
||||
#include "states_screens/state_manager.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
@@ -68,8 +69,9 @@ void NetworkPlayerDialog::beforeAddingWidgets()
|
||||
assert(m_info_widget != NULL);
|
||||
if (m_online_id != 0)
|
||||
{
|
||||
updatePlayerRanking(m_name, m_online_id, m_info_widget,
|
||||
m_fetched_ranking);
|
||||
m_ranking_callback =
|
||||
RankingCallback::getRankingCallback(m_name, m_online_id);
|
||||
m_ranking_callback->queue();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -171,13 +173,18 @@ void NetworkPlayerDialog::init()
|
||||
// -----------------------------------------------------------------------------
|
||||
void NetworkPlayerDialog::onUpdate(float dt)
|
||||
{
|
||||
if (*m_fetched_ranking == false)
|
||||
if (m_ranking_callback && !m_ranking_callback->isDone())
|
||||
{
|
||||
// I18N: In the network player dialog, showing when waiting for
|
||||
// the result of the ranking info of a player
|
||||
core::stringw msg = _("Fetching ranking info for %s", m_name);
|
||||
m_info_widget->setText(StringUtils::loadingDots(msg.c_str()), false);
|
||||
}
|
||||
else if (m_ranking_callback && m_ranking_callback->isDone())
|
||||
{
|
||||
m_info_widget->setText(m_ranking_callback->getRankingResult(), false);
|
||||
m_ranking_callback = nullptr;
|
||||
}
|
||||
|
||||
// It's unsafe to delete from inside the event handler so we do it here
|
||||
if (m_open_report_textbox)
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#define HEADER_NETWORK_USER_DIALOG_HPP
|
||||
|
||||
#include "guiengine/modaldialog.hpp"
|
||||
#include "states_screens/dialogs/ranking_callback.hpp"
|
||||
#include "utils/types.hpp"
|
||||
|
||||
#include <string>
|
||||
@@ -34,12 +33,14 @@ namespace GUIEngine
|
||||
class RibbonWidget;
|
||||
}
|
||||
|
||||
class RankingCallback;
|
||||
enum HandicapLevel : uint8_t;
|
||||
|
||||
/**
|
||||
* \brief Dialog that handle player in network lobby
|
||||
* \ingroup states_screens
|
||||
*/
|
||||
class NetworkPlayerDialog : public GUIEngine::ModalDialog,
|
||||
public RankingCallback
|
||||
class NetworkPlayerDialog : public GUIEngine::ModalDialog
|
||||
{
|
||||
private:
|
||||
const uint32_t m_host_id;
|
||||
@@ -58,7 +59,7 @@ private:
|
||||
|
||||
bool m_self_destroy, m_open_report_textbox;
|
||||
|
||||
std::shared_ptr<bool> m_fetched_ranking;
|
||||
std::shared_ptr<RankingCallback> m_ranking_callback;
|
||||
|
||||
GUIEngine::RibbonWidget* m_options_widget;
|
||||
|
||||
@@ -86,8 +87,7 @@ public:
|
||||
m_local_id(local_id), m_handicap(h),
|
||||
m_name(name), m_country_code(country_code),
|
||||
m_allow_change_team(allow_change_team), m_self_destroy(false),
|
||||
m_open_report_textbox(false),
|
||||
m_fetched_ranking(std::make_shared<bool>(false))
|
||||
m_open_report_textbox(false)
|
||||
{
|
||||
loadFromFile("online/user_info_dialog.stkgui");
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "guiengine/widgets/list_widget.hpp"
|
||||
#include "guiengine/widgets/ribbon_widget.hpp"
|
||||
#include "online/online_profile.hpp"
|
||||
#include "states_screens/dialogs/ranking_callback.hpp"
|
||||
#include "states_screens/state_manager.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
@@ -42,8 +43,7 @@ std::vector<std::tuple<int, core::stringw, float> >
|
||||
PlayerRankingsDialog::PlayerRankingsDialog(uint32_t online_id,
|
||||
const core::stringw& name)
|
||||
: ModalDialog(0.8f,0.9f), m_online_id(online_id),
|
||||
m_name(name), m_self_destroy(false),
|
||||
m_fetched_ranking(std::make_shared<bool>(false))
|
||||
m_name(name), m_self_destroy(false)
|
||||
{
|
||||
loadFromFile("online/player_rankings_dialog.stkgui");
|
||||
m_top_ten = getWidget<ListWidget>("top-ten");
|
||||
@@ -68,8 +68,9 @@ void PlayerRankingsDialog::beforeAddingWidgets()
|
||||
|
||||
m_ranking_info = getWidget<LabelWidget>("cur-rank");
|
||||
assert(m_ranking_info != NULL);
|
||||
updatePlayerRanking(m_name, m_online_id, m_ranking_info,
|
||||
m_fetched_ranking);
|
||||
m_ranking_callback =
|
||||
RankingCallback::getRankingCallback(m_name, m_online_id);
|
||||
m_ranking_callback->queue();
|
||||
} // beforeAddingWidgets
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -135,11 +136,16 @@ void PlayerRankingsDialog::fillTopTenList()
|
||||
// -----------------------------------------------------------------------------
|
||||
void PlayerRankingsDialog::onUpdate(float dt)
|
||||
{
|
||||
if (*m_fetched_ranking == false)
|
||||
if (m_ranking_callback && !m_ranking_callback->isDone())
|
||||
{
|
||||
core::stringw msg = _("Fetching ranking info for %s", m_name);
|
||||
m_ranking_info->setText(StringUtils::loadingDots(msg.c_str()), false);
|
||||
}
|
||||
else if (m_ranking_callback && m_ranking_callback->isDone())
|
||||
{
|
||||
m_ranking_info->setText(m_ranking_callback->getRankingResult(), false);
|
||||
m_ranking_callback = nullptr;
|
||||
}
|
||||
|
||||
// It's unsafe to delete from inside the event handler so we do it here
|
||||
if (m_self_destroy)
|
||||
@@ -171,9 +177,9 @@ GUIEngine::EventPropagation
|
||||
return GUIEngine::EVENT_BLOCK;
|
||||
|
||||
timer = StkTime::getMonoTimeMs();
|
||||
*m_fetched_ranking = false;
|
||||
updatePlayerRanking(m_name, m_online_id, m_ranking_info,
|
||||
m_fetched_ranking);
|
||||
m_ranking_callback =
|
||||
RankingCallback::getRankingCallback(m_name, m_online_id);
|
||||
m_ranking_callback->queue();
|
||||
updateTopTenList();
|
||||
return GUIEngine::EVENT_BLOCK;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#define HEADER_PLAYER_RANKINGS_DIALOG_HPP
|
||||
|
||||
#include "guiengine/modaldialog.hpp"
|
||||
#include "states_screens/dialogs/ranking_callback.hpp"
|
||||
#include "utils/types.hpp"
|
||||
|
||||
#include <irrString.h>
|
||||
@@ -36,12 +35,12 @@ namespace GUIEngine
|
||||
class RibbonWidget;
|
||||
}
|
||||
|
||||
class RankingCallback;
|
||||
/**
|
||||
* \brief Dialog that handle user in network lobby
|
||||
* \ingroup states_screens
|
||||
*/
|
||||
class PlayerRankingsDialog : public GUIEngine::ModalDialog,
|
||||
public RankingCallback
|
||||
class PlayerRankingsDialog : public GUIEngine::ModalDialog
|
||||
{
|
||||
private:
|
||||
const uint32_t m_online_id;
|
||||
@@ -50,7 +49,7 @@ private:
|
||||
|
||||
bool m_self_destroy;
|
||||
|
||||
std::shared_ptr<bool> m_fetched_ranking;
|
||||
std::shared_ptr<RankingCallback> m_ranking_callback;
|
||||
|
||||
GUIEngine::RibbonWidget* m_options_widget;
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#define HEADER_RANKING_CALLBACK_HPP
|
||||
|
||||
#include "config/player_manager.hpp"
|
||||
#include "guiengine/widgets/label_widget.hpp"
|
||||
#include "io/xml_node.hpp"
|
||||
#include "online/xml_request.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
@@ -28,73 +27,52 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
class RankingCallback
|
||||
class RankingCallback : public Online::XMLRequest
|
||||
{
|
||||
protected:
|
||||
// ------------------------------------------------------------------------
|
||||
void updatePlayerRanking(const core::stringw& name, uint32_t online_id,
|
||||
GUIEngine::LabelWidget* info,
|
||||
std::shared_ptr<bool> done)
|
||||
private:
|
||||
core::stringw m_name;
|
||||
|
||||
core::stringw m_ranking_result;
|
||||
// ----------------------------------------------------------------
|
||||
/** Callback for the request to update rank of a player. Shows his
|
||||
* rank and score.
|
||||
*/
|
||||
virtual void callback()
|
||||
{
|
||||
// --------------------------------------------------------------------
|
||||
class UpdatePlayerRankingRequest : public Online::XMLRequest
|
||||
// I18N: In the network player dialog, indiciating a network
|
||||
// player has no ranking
|
||||
m_ranking_result = _("%s has no ranking yet.", m_name);
|
||||
if (isSuccess())
|
||||
{
|
||||
private:
|
||||
std::weak_ptr<bool> m_done;
|
||||
|
||||
core::stringw m_name;
|
||||
|
||||
GUIEngine::LabelWidget* m_info;
|
||||
// ----------------------------------------------------------------
|
||||
/** Callback for the request to update rank of a player. Shows his
|
||||
* rank and score.
|
||||
*/
|
||||
virtual void callback()
|
||||
int rank = -1;
|
||||
float score = 0.0f;
|
||||
getXMLData()->get("rank", &rank);
|
||||
getXMLData()->get("scores", &score);
|
||||
if (rank > 0)
|
||||
{
|
||||
auto done = m_done.lock();
|
||||
// Dialog deleted
|
||||
if (!done)
|
||||
return;
|
||||
// I18N: In the network player dialog, indiciating a network
|
||||
// player has no ranking
|
||||
core::stringw result = _("%s has no ranking yet.", m_name);
|
||||
if (isSuccess())
|
||||
{
|
||||
int rank = -1;
|
||||
float score = 0.0f;
|
||||
getXMLData()->get("rank", &rank);
|
||||
getXMLData()->get("scores", &score);
|
||||
if (rank > 0)
|
||||
{
|
||||
// I18N: In the network player dialog show rank and
|
||||
// score of a player
|
||||
result = _("%s is number %d in the rankings with a score of %f.",
|
||||
m_name, rank, score);
|
||||
}
|
||||
}
|
||||
*done = true;
|
||||
m_info->setText(result, false);
|
||||
|
||||
} // callback
|
||||
public:
|
||||
UpdatePlayerRankingRequest(const core::stringw& name,
|
||||
uint32_t online_id,
|
||||
GUIEngine::LabelWidget* info,
|
||||
std::shared_ptr<bool> done)
|
||||
: XMLRequest()
|
||||
{
|
||||
m_name = name;
|
||||
m_info = info;
|
||||
m_done = done;
|
||||
// I18N: In the network player dialog show rank and
|
||||
// score of a player
|
||||
m_ranking_result =
|
||||
_("%s is number %d in the rankings with a score of %f.",
|
||||
m_name, rank, score);
|
||||
}
|
||||
}; // UpdatePlayerRankingRequest
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
auto request = std::make_shared<UpdatePlayerRankingRequest>(
|
||||
name, online_id, info, done);
|
||||
PlayerManager::setUserDetails(request, "get-ranking");
|
||||
request->addParameter("id", online_id);
|
||||
request->queue();
|
||||
} // callback
|
||||
}
|
||||
};
|
||||
public:
|
||||
RankingCallback(const core::stringw& name, uint32_t online_id)
|
||||
: XMLRequest()
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
const core::stringw& getRankingResult() const { return m_ranking_result; }
|
||||
static std::shared_ptr<RankingCallback> getRankingCallback(
|
||||
const core::stringw& name, uint32_t online_id)
|
||||
{
|
||||
auto rc = std::make_shared<RankingCallback>(name, online_id);
|
||||
PlayerManager::setUserDetails(rc, "get-ranking");
|
||||
rc->addParameter("id", online_id);
|
||||
return rc;
|
||||
}
|
||||
}; // RankingCallback
|
||||
|
||||
#endif
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
#include "states_screens/dialogs/server_configuration_dialog.hpp"
|
||||
#include "states_screens/state_manager.hpp"
|
||||
#include "tracks/track.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
using namespace Online;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "states_screens/dialogs/player_rankings_dialog.hpp"
|
||||
#include "states_screens/dialogs/user_info_dialog.hpp"
|
||||
#include "states_screens/state_manager.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
#include <IGUIButton.h>
|
||||
|
||||
Reference in New Issue
Block a user