Skip to content

Commit 4940952

Browse files
committed
Initial commit on public values
For now they are not refreshing themselves. On player list update it refreshes, and on command usage it updates the file itself (like it was before), but doesn't refresh on the screen. Likely will be done, but later.
1 parent 1c42811 commit 4940952

File tree

7 files changed

+201
-1
lines changed

7 files changed

+201
-1
lines changed

src/network/protocols/server_lobby.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "utils/lobby_queues.hpp"
6262
#include "utils/map_vote_handler.hpp"
6363
#include "utils/name_decorators/generic_decorator.hpp"
64+
#include "utils/public_player_value_storage.hpp"
6465
#include "utils/team_manager.hpp"
6566
#include "utils/tournament.hpp"
6667
#include "utils/translation.hpp"
@@ -2911,6 +2912,11 @@ void ServerLobby::updatePlayerList(bool update_when_reset_server)
29112912
prefix = TeamUtils::getTeamByIndex(team).getEmoji() + " " + prefix;
29122913
}
29132914

2915+
PublicPlayerValueStorage::tryUpdate();
2916+
std::string public_values = PublicPlayerValueStorage::get(utf8_profile_name);
2917+
if (!public_values.empty())
2918+
prefix = "{" + public_values + "} " + prefix;
2919+
29142920
profile_name = StringUtils::utf8ToWide(prefix) + profile_name;
29152921

29162922
packet.host_id = profile->getHostId();

src/utils/command_manager/map_file_resource.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "utils/command_manager/map_file_resource.hpp"
2020

2121
#include "utils/string_utils.hpp"
22+
#include "utils/public_player_value_storage.hpp"
23+
#include <optional>
2224

2325
namespace
2426
{
@@ -53,7 +55,33 @@ void MapFileResource::fromXmlNode(const XMLNode* node)
5355
readChar(node, "right-quote", &m_right_quote);
5456
readChar(node, "escape", &m_escape);
5557
node->get("index", &m_index);
56-
} // MapFileResource
58+
node->get("public", &m_public);
59+
if (m_public >= 0)
60+
{
61+
PublicPlayerValueStorage::add(
62+
std::make_shared<PublicPlayerValue>(
63+
[this](const std::string& name) -> std::optional<std::string> {
64+
auto it = m_indexed.find(name);
65+
if (it == m_indexed.end() || it->second.empty())
66+
return std::nullopt;
67+
68+
// We don't care about non-first rows for now.
69+
auto& vec = it->second[0]->cells;
70+
if (m_public >= (int)vec.size())
71+
return std::nullopt;
72+
73+
return { vec[m_public] };
74+
},
75+
[this]() {
76+
tryUpdate();
77+
}
78+
)
79+
);
80+
81+
// We usually don't call it immediately, but if we have it published, we have to call
82+
tryUpdate();
83+
}
84+
} // fromXmlNode
5785
//-----------------------------------------------------------------------------
5886

5987
void MapFileResource::onContentChange()

src/utils/command_manager/map_file_resource.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ struct MapFileResource: public FileResource
4040
// Except the lack of practical need, of course.
4141
int m_index = 0;
4242

43+
int m_public = -1;
44+
4345
struct Row
4446
{
4547
std::vector<std::string> cells;

src/utils/public_player_value.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// SuperTuxKart - a fun racing game with go-kart
3+
// Copyright (C) 2018 SuperTuxKart-Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 3
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
19+
#include "utils/public_player_value.hpp"
20+
21+
// Nothing for now

src/utils/public_player_value.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// SuperTuxKart - a fun racing game with go-kart
3+
// Copyright (C) 2018 SuperTuxKart-Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 3
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
19+
#ifndef PUBLIC_PLAYER_VALUE_HPP
20+
#define PUBLIC_PLAYER_VALUE_HPP
21+
22+
#include <functional>
23+
#include <string>
24+
#include <optional>
25+
26+
/**
27+
* A class that allows to show various (currently file-based) stats for players.
28+
*/
29+
class PublicPlayerValue
30+
{
31+
using Handler = std::function<std::optional<std::string>(const std::string&)>;
32+
using Updater = std::function<void()>;
33+
private:
34+
Handler m_method;
35+
Updater m_updater;
36+
37+
public:
38+
PublicPlayerValue(Handler method, Updater updater)
39+
: m_method(method), m_updater(updater)
40+
{}
41+
42+
std::optional<std::string> get(const std::string& player_name) const
43+
{ return m_method(player_name); }
44+
45+
void tryUpdate() const { m_updater(); }
46+
};
47+
48+
#endif // PUBLIC_PLAYER_VALUE_HPP
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// SuperTuxKart - a fun racing game with go-kart
3+
// Copyright (C) 2018 SuperTuxKart-Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 3
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
19+
#include "utils/public_player_value_storage.hpp"
20+
21+
std::vector<std::shared_ptr<PublicPlayerValue>> PublicPlayerValueStorage::values = {};
22+
23+
// Currently supported only for players with account,
24+
// however, currently we'll show it for local accounts too
25+
// if they have a corresponding name.
26+
std::string PublicPlayerValueStorage::get(const std::string& player_name)
27+
{
28+
// might add caching later
29+
30+
std::string res;
31+
for (const auto& ppv: values)
32+
{
33+
const auto item = ppv->get(player_name);
34+
if (!item.has_value())
35+
continue;
36+
37+
std::string value = item.value();
38+
if (!value.empty())
39+
{
40+
if (!res.empty())
41+
res += ", ";
42+
res += value;
43+
}
44+
}
45+
return res;
46+
} // get
47+
//-----------------------------------------------------------------------------
48+
49+
void PublicPlayerValueStorage::tryUpdate()
50+
{
51+
for (const auto& ppv: values)
52+
ppv->tryUpdate();
53+
} // tryUpdate
54+
//-----------------------------------------------------------------------------
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// SuperTuxKart - a fun racing game with go-kart
3+
// Copyright (C) 2018 SuperTuxKart-Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 3
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
19+
#ifndef PUBLIC_PLAYER_VALUE_STORAGE_HPP
20+
#define PUBLIC_PLAYER_VALUE_STORAGE_HPP
21+
22+
#include <vector>
23+
#include <string>
24+
#include <memory>
25+
#include "utils/public_player_value.hpp"
26+
27+
class PublicPlayerValueStorage
28+
{
29+
static std::vector<std::shared_ptr<PublicPlayerValue>> values;
30+
31+
public:
32+
static void add(const std::shared_ptr<PublicPlayerValue>& value)
33+
{
34+
values.push_back(value);
35+
}
36+
37+
static std::string get(const std::string& player_name);
38+
static void tryUpdate();
39+
};
40+
41+
#endif // PUBLIC_PLAYER_VALUE_STORAGE_HPP

0 commit comments

Comments
 (0)