SuperTuxKart
ranking_callback.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2018 SuperTuxKart-Team
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 
19 #ifndef HEADER_RANKING_CALLBACK_HPP
20 #define HEADER_RANKING_CALLBACK_HPP
21 
22 #include "config/player_manager.hpp"
23 #include "io/xml_node.hpp"
24 #include "online/xml_request.hpp"
25 #include "utils/string_utils.hpp"
26 #include "utils/translation.hpp"
27 
28 #include <memory>
29 
31 {
32 private:
33  core::stringw m_name;
34 
35  core::stringw m_ranking_result;
36  // ----------------------------------------------------------------
40  virtual void callback()
41  {
42  // I18N: In the network player dialog, indiciating a network
43  // player has no ranking
44  m_ranking_result = _("%s has no ranking yet.", m_name);
45  if (isSuccess())
46  {
47  int rank = -1;
48  float score = 0.0f;
49  getXMLData()->get("rank", &rank);
50  getXMLData()->get("scores", &score);
51  if (rank > 0)
52  {
53  // I18N: In the network player dialog show rank and
54  // score of a player
55  m_ranking_result =
56  _("%s is number %d in the rankings with a score of %f.",
57  m_name, rank, score);
58  }
59  } // callback
60  }
61 public:
62  RankingCallback(const core::stringw& name, uint32_t online_id)
63  : XMLRequest()
64  {
65  m_name = name;
66  }
67  const core::stringw& getRankingResult() const { return m_ranking_result; }
68  static std::shared_ptr<RankingCallback> getRankingCallback(
69  const core::stringw& name, uint32_t online_id)
70  {
71  auto rc = std::make_shared<RankingCallback>(name, online_id);
72  PlayerManager::setUserDetails(rc, "get-ranking");
73  rc->addParameter("id", online_id);
74  return rc;
75  }
76 }; // RankingCallback
77 
78 #endif
A http request expecting a xml return value.
Definition: xml_request.hpp:36
bool isSuccess() const
Returns whether the request was successfully executed on the server.
Definition: xml_request.hpp:83
const XMLNode * getXMLData() const
Get the downloaded XML tree.
Definition: xml_request.hpp:61
XMLRequest(int priority=1)
Creates a HTTP(S) request that will automatically parse the answer into a XML structure.
Definition: xml_request.cpp:35
static void setUserDetails(std::shared_ptr< Online::HTTPRequest > request, const std::string &action, const std::string &php_name="")
Adds the login credential to a http request.
Definition: player_manager.cpp:53
Definition: ranking_callback.hpp:31
virtual void callback()
Callback for the request to update rank of a player.
Definition: ranking_callback.hpp:40
int get(const std::string &attribute, std::string *value) const
If 'attribute' was defined, set 'value' to the value of the attribute and return 1,...
Definition: xml_node.cpp:176