Moved voting code from CurrentUser to VoteDialog.

This commit is contained in:
hiker
2014-04-05 21:59:56 +11:00
parent 85bd942de7
commit 4a3cf13bc4
4 changed files with 65 additions and 58 deletions

View File

@@ -262,45 +262,6 @@ namespace Online
//FIXME needs changes for actual valid joining
} // ServerJoinRequest::callback
// ------------------------------------------------------------------------
/** A request to the server, to perform a vote on an addon.
* \param addon_id the id of the addon to vote for.
* \param rating the voted rating.
*/
const CurrentUser::SetAddonVoteRequest*
CurrentUser::requestSetAddonVote(const std::string & addon_id,
float rating) const
{
assert(m_state == US_SIGNED_IN);
CurrentUser::SetAddonVoteRequest * request =
new CurrentUser::SetAddonVoteRequest();
request->setServerURL("client-user.php");
request->addParameter("action", "set-addon-vote");
request->addParameter("token", getToken());
request->addParameter("userid", getID());
request->addParameter("addonid", addon_id.substr(6));
request->addParameter("rating", rating);
request->queue();
return request;
} // requestSetAddonVote
// ------------------------------------------------------------------------
/** Callback for the request to vote for an addon. Updates the local
* average rating.
*/
void CurrentUser::SetAddonVoteRequest::callback()
{
if(isSuccess())
{
std::string addon_id;
getXMLData()->get("addon-id", &addon_id);
float average;
getXMLData()->get("new-average", &average);
addons_manager->getAddon(Addon::createAddonId(addon_id))
->setRating(average);
}
} // SetAddonVoteRequest::callback
// ------------------------------------------------------------------------
/** A request to the server, to fetch matching results for the supplied
* search term.

View File

@@ -80,13 +80,6 @@ namespace Online
ServerJoinRequest() : XMLRequest() {}
}; // ServerJoinRequest
// ----------------------------------------------------------------
class SetAddonVoteRequest : public XMLRequest {
virtual void callback ();
public:
SetAddonVoteRequest() : XMLRequest() {}
}; // SetAddonVoteRequest
// ----------------------------------------------------------------
class PollRequest : public XMLRequest {
virtual void callback ();
@@ -130,7 +123,6 @@ namespace Online
void requestSignOut();
ServerJoinRequest * requestServerJoin(uint32_t server_id, bool request_now = true);
const SetAddonVoteRequest * requestSetAddonVote(const std::string & addon_id, float rating) const;
void requestFriendRequest(const uint32_t friend_id) const;
void requestPasswordChange( const irr::core::stringw &current_password,
const irr::core::stringw &new_password,

View File

@@ -17,8 +17,7 @@
#include "states_screens/dialogs/vote_dialog.hpp"
#include <IGUIEnvironment.h>
#include "addons/addons_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "guiengine/engine.hpp"
#include "states_screens/state_manager.hpp"
@@ -27,6 +26,7 @@
#include "online/current_user.hpp"
#include "online/messages.hpp"
#include <IGUIEnvironment.h>
using namespace GUIEngine;
@@ -88,6 +88,49 @@ bool VoteDialog::onEscapePressed()
return false;
} // onEscapePressed
// ----------------------------------------------------------------------------
/** A request to the server, to perform a vote on an addon.
* \param rating the voted rating.
*/
void VoteDialog::sendVote()
{
/** A vote request. The callback will update the addon manager with the
* new average. The VoteDialog polls this request till it is finished
* to inform the user about the new average.
*/
class SetAddonVoteRequest : public XMLRequest
{
virtual void callback()
{
if (isSuccess())
{
std::string addon_id;
getXMLData()->get("addon-id", &addon_id);
float average;
getXMLData()->get("new-average", &average);
addons_manager->getAddon(Addon::createAddonId(addon_id))
->setRating(average);
} // isSuccess
} // callbac
public:
SetAddonVoteRequest() : XMLRequest() {}
}; // SetAddonVoteRequest
// ------------------------------------------------------------------------
m_perform_vote_request = new SetAddonVoteRequest();
CurrentUser::get()->setUserDetails(m_perform_vote_request);
m_perform_vote_request->addParameter("action", "set-addon-vote");
m_perform_vote_request->addParameter("addonid", m_addon_id.substr(6));
m_perform_vote_request->addParameter("rating", m_rating_widget->getRating());
m_perform_vote_request->queue();
m_rating_widget->setDeactivated();
m_cancel_widget->setDeactivated();
} // sendVote
// -----------------------------------------------------------------------------
/** Callback when a user event is triggered.
* \param event Information about the event that was triggered.
@@ -97,11 +140,7 @@ GUIEngine::EventPropagation VoteDialog::processEvent(const std::string& event)
if (event == m_rating_widget->m_properties[PROP_ID])
{
m_perform_vote_request = CurrentUser::get()
->requestSetAddonVote(m_addon_id,
m_rating_widget->getRating());
m_rating_widget->setDeactivated();
m_cancel_widget->setDeactivated();
sendVote();
return GUIEngine::EVENT_BLOCK;
}

View File

@@ -32,25 +32,40 @@
class VoteDialog : public GUIEngine::ModalDialog
{
private:
/** Stores the id of the addon being voted on. */
const std::string m_addon_id;
bool m_self_destroy;
Online::XMLRequest * m_fetch_vote_request;
const Online::CurrentUser::SetAddonVoteRequest * m_perform_vote_request;
/** True if the dialog should be removed (which needs to be done
* in the update call each frame). */
bool m_self_destroy;
/** The request to fetch the current vote, which is submitted
* immediately when this dialog is opened. */
Online::XMLRequest * m_fetch_vote_request;
/** The request to perform a vote. */
Online::XMLRequest* m_perform_vote_request;
/** Pointer to the info widget of this dialog. */
GUIEngine::LabelWidget * m_info_widget;
/** Pointer to the rating widget of this dialog */
GUIEngine::RatingBarWidget * m_rating_widget;
/** Pointer to the options widget, which contains the canel button. */
GUIEngine::RibbonWidget * m_options_widget;
/** Pointer to the cancel button. */
GUIEngine::IconButtonWidget * m_cancel_widget;
void updateFetchVote();
void sendVote();
public:
VoteDialog(const std::string & addon_id);
~VoteDialog();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);
virtual void onUpdate(float dt);
virtual bool onEscapePressed();
};
}; // VoteDialog
#endif