Skip to content

Commit 4617ff1

Browse files
committed
Put temporary teams in a separate unit utils/team_utils.*
Also remove unused code and fix inconsistent indexation inside temporary teams
1 parent cf4e5b7 commit 4617ff1

8 files changed

+230
-115
lines changed

src/network/network_player_profile.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#include "network/network_config.hpp"
2121
#include "network/stk_host.hpp"
2222

23-
float NetworkPlayerProfile::m_team_color[20] = {1.00, 0.05, 0.16, 0.33, 0.66, 0.78, 0.46, 0.94, 0.58, 0.00};
24-
2523
// ----------------------------------------------------------------------------
2624
/** Returns true if this player is local, i.e. running on this computer. This
2725
* is done by comparing the host id of this player with the host id of this

src/network/network_player_profile.hpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define HEADER_NETWORK_PLAYER_PROFILE
2424

2525
#include "network/kart_data.hpp"
26+
#include "utils/team_utils.hpp"
2627
#include "utils/types.hpp"
2728

2829
#include "irrString.h"
@@ -75,8 +76,6 @@ class NetworkPlayerProfile
7576

7677
int m_temporary_team;
7778

78-
static float m_team_color[20];
79-
8079
KartData m_kart_data;
8180
public:
8281
// ------------------------------------------------------------------------
@@ -97,7 +96,7 @@ class NetworkPlayerProfile
9796
m_handicap.store((HandicapLevel)0);
9897
m_local_player_id = 0;
9998
m_team.store(team);
100-
m_temporary_team = -1;
99+
m_temporary_team = 0;
101100
resetGrandPrixData();
102101
}
103102
// ------------------------------------------------------------------------
@@ -117,7 +116,7 @@ class NetworkPlayerProfile
117116
m_local_player_id = local_player_id;
118117
m_team.store(team);
119118
m_country_code = country_code;
120-
m_temporary_team = -1;
119+
m_temporary_team = 0;
121120
resetGrandPrixData();
122121
}
123122
// ------------------------------------------------------------------------
@@ -151,9 +150,9 @@ class NetworkPlayerProfile
151150
// ------------------------------------------------------------------------
152151
float getDefaultKartColor() const
153152
{
154-
if (m_temporary_team == -1)
153+
if (m_temporary_team == 0)
155154
return m_default_kart_color;
156-
return m_team_color[m_temporary_team];
155+
return TeamUtils::getTeamByIndex(m_temporary_team).getColor();
157156
}
158157
// ------------------------------------------------------------------------
159158
uint32_t getOnlineId() const { return m_online_id; }

src/network/protocols/command_manager.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -2739,15 +2739,15 @@ void CommandManager::process_swapteams(Context& context)
27392739
std::map<int, int> permutation_map_int;
27402740
for (char c: argv[1])
27412741
{
2742-
std::string type(1, c);
27432742
// todo remove that link to first char, it is awful
2744-
if (m_lobby->m_team_name_to_index.count(type) == 0)
2743+
// on the other hand, I'd better have roygbpcms than r,o,y,g,b,words
2744+
// so let it stay like that for now
2745+
std::string type(1, c);
2746+
int index = TeamUtils::getIndexByCode(type);
2747+
if (index == 0)
27452748
continue;
2746-
int index = m_lobby->m_team_name_to_index[type];
2747-
char c1 = m_lobby->m_team_default_names[index][0];
2748-
if (m_lobby->m_team_name_to_index.count(type)) {
2749-
permutation_map[c1] = 0;
2750-
}
2749+
char c1 = TeamUtils::getTeamByIndex(index).getPrimaryCode()[0];
2750+
permutation_map[c1] = 0;
27512751
}
27522752
std::string permutation;
27532753
for (auto& p: permutation_map)
@@ -2769,8 +2769,8 @@ void CommandManager::process_swapteams(Context& context)
27692769
}
27702770
for (auto& p: permutation_map)
27712771
{
2772-
int from = m_lobby->m_team_name_to_index[std::string(1, p.first)];
2773-
int to = m_lobby->m_team_name_to_index[std::string(1, p.second)];
2772+
int from = TeamUtils::getIndexByCode(std::string(1, p.first));
2773+
int to = TeamUtils::getIndexByCode(std::string(1, p.second));
27742774
permutation_map_int[from] = to;
27752775
}
27762776
m_lobby->shuffleTemporaryTeams(permutation_map_int);
@@ -2812,20 +2812,21 @@ void CommandManager::process_randomteams(Context& context)
28122812
continue;
28132813
players_number += p->getPlayerProfiles().size();
28142814
for (auto& profile : p->getPlayerProfiles())
2815-
profile->setTemporaryTeam(-1);
2815+
profile->setTemporaryTeam(0);
28162816
}
28172817
if (players_number == 0) {
28182818
std::string msg = "No one can play!";
28192819
m_lobby->sendStringToPeer(msg, peer);
28202820
return;
28212821
}
28222822
int teams_number = -1;
2823+
int max_number_of_teams = TeamUtils::getNumberOfTeams();
28232824
if (argv.size() < 2 || !StringUtils::parseString(argv[1], &teams_number)
2824-
|| teams_number < 1 || teams_number > 9)
2825+
|| teams_number < 1 || teams_number > max_number_of_teams)
28252826
{
28262827
teams_number = (int)round(sqrt(players_number));
2827-
if (teams_number > 9)
2828-
teams_number = 9;
2828+
if (teams_number > max_number_of_teams)
2829+
teams_number = max_number_of_teams;
28292830
if (players_number > 1 && teams_number <= 1)
28302831
teams_number = 2;
28312832
}
@@ -2834,7 +2835,7 @@ void CommandManager::process_randomteams(Context& context)
28342835
"Created %d teams for %d players", teams_number, players_number);
28352836
std::vector<int> available_colors;
28362837
std::vector<int> profile_colors;
2837-
for (int i = 1; i <= 9; ++i)
2838+
for (int i = 1; i <= max_number_of_teams; ++i)
28382839
available_colors.push_back(i);
28392840

28402841
std::random_device rd;
@@ -2856,7 +2857,7 @@ void CommandManager::process_randomteams(Context& context)
28562857
continue;
28572858
for (auto& profile : p->getPlayerProfiles()) {
28582859
std::string name = StringUtils::wideToUtf8(profile->getName());
2859-
std::string color = m_lobby->m_team_default_names[profile_colors.back()];
2860+
std::string color = TeamUtils::getTeamByIndex(profile_colors.back()).getPrimaryCode();
28602861
m_lobby->setTemporaryTeam(name, color);
28612862
if (profile_colors.size() > 1) // prevent crash just in case
28622863
profile_colors.pop_back();

src/network/protocols/command_manager.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "network/protocols/command_permissions.hpp"
4343
#include "utils/enum_extended_reader.hpp"
4444
#include "utils/set_typo_fixer.hpp"
45+
#include "utils/team_utils.hpp"
4546

4647
class ServerLobby;
4748
class Event;

src/network/protocols/server_lobby.cpp

+9-83
Original file line numberDiff line numberDiff line change
@@ -216,51 +216,8 @@ ServerLobby::ServerLobby() : LobbyProtocol()
216216
((std::string) ServerConfig::m_help);
217217

218218
m_command_manager = CommandManager(nullptr);
219-
220-
m_team_name_to_index = {
221-
{"red", 1}, {"r", 1},
222-
{"orange", 2}, {"o", 2},
223-
{"yellow", 3}, {"y", 3},
224-
{"green", 4}, {"g", 4},
225-
{"blue", 5}, {"b", 5},
226-
{"purple", 6}, {"p", 6},
227-
{"violet", 6}, {"v", 6},
228-
{"cyan", 7}, {"c", 7},
229-
{"magenta", 8}, {"m", 8},
230-
{"pink", 8},
231-
{"sky", 9}, {"s", 9}
232-
};
233-
234-
m_team_default_names = {"none", "red", "orange", "yellow", "green", "blue", "purple", "cyan", "magenta", "sky"};
235-
236-
m_team_index_to_icon = {
237-
{1, StringUtils::utf32ToUtf8({0x1f7e5})},
238-
{2, StringUtils::utf32ToUtf8({0x1f7e7})},
239-
{3, StringUtils::utf32ToUtf8({0x1f7e8})},
240-
{4, StringUtils::utf32ToUtf8({0x1f7e9})},
241-
{5, StringUtils::utf32ToUtf8({0x1f7e6})},
242-
{6, StringUtils::utf32ToUtf8({0x1f7ea})},
243-
{7, StringUtils::utf32ToUtf8({0x1f5fd})},
244-
{8, StringUtils::utf32ToUtf8({0x1f338})},
245-
{9, StringUtils::utf32ToUtf8({0x2604})}
246-
};
247-
248-
m_team_name = {"No Team",
249-
StringUtils::utf32ToUtf8({0x1f7e5}) + "Red",
250-
StringUtils::utf32ToUtf8({0x1f7e7}) + "Orange",
251-
StringUtils::utf32ToUtf8({0x1f7e8}) + "Yellow",
252-
StringUtils::utf32ToUtf8({0x1f7e9}) + "Green",
253-
StringUtils::utf32ToUtf8({0x1f7e6}) + "Blue",
254-
StringUtils::utf32ToUtf8({0x1f7ea}) + "Purple",
255-
StringUtils::utf32ToUtf8({0x1f5fd}) + "Cyan",
256-
StringUtils::utf32ToUtf8({0x1f338}) + "Magenta",
257-
StringUtils::utf32ToUtf8({ 0x2604}) + "Sky"
258-
};
259-
260219
m_shuffle_gp = ServerConfig::m_shuffle_gp;
261-
262220
m_current_max_players_in_game.store(ServerConfig::m_max_players_in_game);
263-
264221
m_consent_on_replays = false;
265222

266223
m_fixed_lap = ServerConfig::m_fixed_lap_count;
@@ -4644,7 +4601,7 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
46444601
auto it2 = m_team_for_player.find(username);
46454602
if (it2 != m_team_for_player.end())
46464603
{
4647-
player->setTemporaryTeam(it2->second - 1);
4604+
player->setTemporaryTeam(it2->second);
46484605
}
46494606
peer->addPlayer(player);
46504607
}
@@ -4964,8 +4921,8 @@ void ServerLobby::updatePlayerList(bool update_when_reset_server)
49644921
prefix = "[" + prefix + "] ";
49654922
}
49664923
int team = profile->getTemporaryTeam();
4967-
if (team != -1) {
4968-
prefix = m_team_index_to_icon[team + 1] + " " + prefix;
4924+
if (team != 0) {
4925+
prefix = TeamUtils::getTeamByIndex(team).getEmoji() + " " + prefix;
49694926
}
49704927

49714928
profile_name = StringUtils::utf8ToWide(prefix) + profile_name;
@@ -7146,36 +7103,6 @@ void ServerLobby::initCategories()
71467103
}
71477104
}
71487105
}
7149-
/*
7150-
Temporary team indexing:
7151-
network_player_profile.hpp: -1 for none, [0..5] for teams
7152-
server config: (-1 for none,) [0..5] for teams
7153-
server lobby command: 0 for none, [1..6] for teams
7154-
m_team_for_player: 0 for none, [1..6] for teams
7155-
Sorry for the confusion.
7156-
*/
7157-
// change categories according to the teams
7158-
for (const auto& who: m_team_for_player)
7159-
{
7160-
int idx = who.second;
7161-
std::string player = who.first;
7162-
for (const auto& pair: m_team_name_to_index)
7163-
{
7164-
if (pair.second < 0)
7165-
{
7166-
if (pair.second == -idx)
7167-
{
7168-
m_player_categories[pair.first].insert(player);
7169-
m_categories_for_player[player].insert(pair.first);
7170-
}
7171-
else
7172-
{
7173-
m_player_categories[pair.first].erase(player);
7174-
m_categories_for_player[player].erase(pair.first);
7175-
}
7176-
}
7177-
}
7178-
}
71797106
} // initCategories
71807107
//-----------------------------------------------------------------------------
71817108
void ServerLobby::initTournamentPlayers()
@@ -7527,7 +7454,7 @@ std::string ServerLobby::getGrandPrixStandings(bool showIndividual, bool showTea
75277454
for (unsigned i = 0; i < results2.size(); i++)
75287455
{
75297456
response << (i + 1) << ". ";
7530-
response << " " << m_team_name[results2[i].second];
7457+
response << " " << TeamUtils::getTeamByIndex(results2[i].second).getNameWithEmoji();
75317458
response << " " << results2[i].first.score;
75327459
response << " " << "(" << StringUtils::timeToString(results2[i].first.time) << ")";
75337460
response << "\n";
@@ -8165,8 +8092,7 @@ bool ServerLobby::isSoccerGoalTarget() const
81658092
// This should be moved later to another unit.
81668093
void ServerLobby::setTemporaryTeam(const std::string& username, std::string& arg)
81678094
{
8168-
auto it = m_team_name_to_index.find(arg);
8169-
int index = (it == m_team_name_to_index.end() ? 0 : it->second);
8095+
int index = TeamUtils::getIndexByCode(arg);
81708096
m_team_for_player[username] = index;
81718097
irr::core::stringw wide_player_name = StringUtils::utf8ToWide(username);
81728098
std::shared_ptr<STKPeer> player_peer = STKHost::get()->findPeerByName(
@@ -8177,7 +8103,7 @@ void ServerLobby::setTemporaryTeam(const std::string& username, std::string& arg
81778103
{
81788104
if (profile->getName() == wide_player_name)
81798105
{
8180-
profile->setTemporaryTeam(index - 1);
8106+
profile->setTemporaryTeam(index);
81818107
break;
81828108
}
81838109
}
@@ -8192,7 +8118,7 @@ void ServerLobby::clearTemporaryTeams()
81928118

81938119
for (auto& peer : STKHost::get()->getPeers())
81948120
for (auto& profile : peer->getPlayerProfiles())
8195-
profile->setTemporaryTeam(-1);
8121+
profile->setTemporaryTeam(0);
81968122
} // clearTemporaryTeams
81978123
//-----------------------------------------------------------------------------
81988124

@@ -8209,9 +8135,9 @@ void ServerLobby::shuffleTemporaryTeams(const std::map<int, int>& permutation)
82098135
{
82108136
for (auto &profile: peer->getPlayerProfiles())
82118137
{
8212-
auto it = permutation.find(profile->getTemporaryTeam() + 1);
8138+
auto it = permutation.find(profile->getTemporaryTeam());
82138139
if (it != permutation.end())
8214-
profile->setTemporaryTeam(it->second - 1);
8140+
profile->setTemporaryTeam(it->second);
82158141
}
82168142
}
82178143
auto old_scores = m_gp_team_scores;

src/network/protocols/server_lobby.hpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
#include "network/protocols/lobby_protocol.hpp"
2323
#include "utils/cpp2011.hpp"
24+
#include "utils/kart_elimination.hpp"
25+
#include "utils/team_utils.hpp"
2426
#include "utils/time.hpp"
2527
#include "utils/track_filter.hpp"
26-
#include "utils/kart_elimination.hpp"
2728
#include "karts/controller/player_controller.hpp"
2829
#include "network/protocols/command_manager.hpp"
2930

@@ -369,8 +370,6 @@ class ServerLobby : public LobbyProtocol
369370

370371
std::map<int, GPScore> m_gp_team_scores;
371372

372-
std::vector<std::string> m_team_name;
373-
374373
int m_tournament_game;
375374

376375
int m_fixed_lap;
@@ -392,12 +391,6 @@ class ServerLobby : public LobbyProtocol
392391
bool m_allowed_to_start;
393392

394393
bool m_consent_on_replays;
395-
396-
std::map<std::string, int> m_team_name_to_index;
397-
398-
std::vector<std::string> m_team_default_names;
399-
400-
std::map<int, std::string> m_team_index_to_icon;
401394

402395
bool m_shuffle_gp;
403396

0 commit comments

Comments
 (0)