• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 21632345767

03 Feb 2026 01:35PM UTC coverage: 50.746% (-0.008%) from 50.754%
21632345767

Pull #1885

github

web-flow
Merge fb01a4f33 into dc0ce04f6
Pull Request #1885: Allow changing color of unused player slots

1 of 8 new or added lines in 2 files covered. (12.5%)

5 existing lines in 2 files now uncovered.

22797 of 44924 relevant lines covered (50.75%)

42380.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.57
/libs/s25main/network/GameServer.cpp
1
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "GameServer.h"
6
#include "Debug.h"
7
#include "GameMessage.h"
8
#include "GameMessage_GameCommand.h"
9
#include "GameServerPlayer.h"
10
#include "GlobalGameSettings.h"
11
#include "JoinPlayerInfo.h"
12
#include "RTTR_Version.h"
13
#include "RttrConfig.h"
14
#include "Savegame.h"
15
#include "Settings.h"
16
#include "commonDefines.h"
17
#include "files.h"
18
#include "helpers/containerUtils.h"
19
#include "helpers/mathFuncs.h"
20
#include "helpers/random.h"
21
#include "network/CreateServerInfo.h"
22
#include "network/GameMessages.h"
23
#include "random/randomIO.h"
24
#include "gameTypes/LanGameInfo.h"
25
#include "gameTypes/TeamTypes.h"
26
#include "gameData/GameConsts.h"
27
#include "gameData/LanDiscoveryCfg.h"
28
#include "gameData/MaxPlayers.h"
29
#include "gameData/PortraitConsts.h"
30
#include "liblobby/LobbyClient.h"
31
#include "libsiedler2/ArchivItem_Map.h"
32
#include "libsiedler2/ArchivItem_Map_Header.h"
33
#include "libsiedler2/prototypen.h"
34
#include "s25util/SocketSet.h"
35
#include "s25util/colors.h"
36
#include "s25util/utf8.h"
37
#include <boost/container/static_vector.hpp>
38
#include <boost/filesystem.hpp>
39
#include <boost/nowide/convert.hpp>
40
#include <boost/nowide/fstream.hpp>
41
#include <helpers/chronoIO.h>
42
#include <iomanip>
43
#include <iterator>
44
#include <mygettext/mygettext.h>
45

46
struct GameServer::AsyncLog
47
{
48
    uint8_t playerId;
49
    bool done;
50
    AsyncChecksum checksum;
51
    std::string addData;
52
    std::vector<RandomEntry> randEntries;
53
    AsyncLog(uint8_t playerId, AsyncChecksum checksum) : playerId(playerId), done(false), checksum(checksum) {}
×
54
};
55

56
GameServer::ServerConfig::ServerConfig()
×
57
{
58
    Clear();
×
59
}
×
60

61
void GameServer::ServerConfig::Clear()
×
62
{
63
    servertype = ServerType::Local;
×
64
    gamename.clear();
×
65
    password.clear();
×
66
    port = 0;
×
67
    ipv6 = false;
×
68
}
×
69

70
GameServer::CountDown::CountDown() : isActive(false), remainingSecs(0) {}
×
71

72
void GameServer::CountDown::Start(unsigned timeInSec)
×
73
{
74
    isActive = true;
×
75
    remainingSecs = timeInSec;
×
76
    lasttime = SteadyClock::now();
×
77
}
×
78

79
void GameServer::CountDown::Stop()
×
80
{
81
    isActive = false;
×
82
}
×
83

84
bool GameServer::CountDown::Update()
×
85
{
86
    RTTR_Assert(isActive);
×
87
    SteadyClock::time_point curTime = SteadyClock::now();
×
88

89
    // Check if 1s has passed
90
    if(curTime - lasttime < std::chrono::seconds(1))
×
91
        return false;
×
92
    if(remainingSecs == 0)
×
93
    {
94
        Stop();
×
95
        return true;
×
96
    }
97
    // 1s has passed -> Reduce remaining time
98
    lasttime = curTime;
×
99
    remainingSecs--;
×
100
    return true;
×
101
}
102

103
GameServer::GameServer() : skiptogf(0), state(ServerState::Stopped), currentGF(0), lanAnnouncer(LAN_DISCOVERY_CFG) {}
×
104

105
GameServer::~GameServer()
×
106
{
107
    Stop();
×
108
}
×
109

110
///////////////////////////////////////////////////////////////////////////////
111
// Spiel hosten
112
bool GameServer::Start(const CreateServerInfo& csi, const MapDescription& map, const std::string& hostPw)
×
113
{
114
    Stop();
×
115

116
    // Name, Password und Kartenname kopieren
117
    config.gamename = csi.gameName;
×
118
    config.hostPassword = hostPw;
×
119
    config.password = csi.password;
×
120
    config.servertype = csi.type;
×
121
    config.port = csi.port;
×
122
    config.ipv6 = csi.ipv6;
×
123
    mapinfo.type = map.map_type;
×
124
    mapinfo.filepath = map.map_path;
×
125

126
    // Maps, Random-Maps, Savegames - Header laden und relevante Informationen rausschreiben (Map-Titel, Spieleranzahl)
127
    switch(mapinfo.type)
×
128
    {
129
        // Altes S2-Mapformat von BB
130
        case MapType::OldMap:
×
131
        {
132
            libsiedler2::Archiv map;
×
133

134
            // Karteninformationen laden
135
            if(libsiedler2::loader::LoadMAP(mapinfo.filepath, map, true) != 0)
×
136
            {
137
                LOG.write("GameServer::Start: ERROR: Map %1%, couldn't load header!\n") % mapinfo.filepath;
×
138
                return false;
×
139
            }
140
            const libsiedler2::ArchivItem_Map_Header& header =
141
              checkedCast<const libsiedler2::ArchivItem_Map*>(map.get(0))->getHeader();
×
142

143
            playerInfos.resize(header.getNumPlayers());
×
144
            mapinfo.title = s25util::ansiToUTF8(header.getName());
×
145
            ggs_.LoadSettings();
×
146
            currentGF = 0;
×
147
        }
148
        break;
×
149
        // Gespeichertes Spiel
150
        case MapType::Savegame:
×
151
        {
152
            Savegame save;
×
153

154
            if(!save.Load(mapinfo.filepath, SaveGameDataToLoad::HeaderAndSettings))
×
155
                return false;
×
156

157
            // Spieleranzahl
158
            playerInfos.resize(save.GetNumPlayers());
×
159
            mapinfo.title = save.GetMapName();
×
160

161
            for(unsigned i = 0; i < playerInfos.size(); ++i)
×
162
            {
163
                playerInfos[i] = JoinPlayerInfo(save.GetPlayer(i));
×
164
                // If it was a human we make it free, so someone can join
165
                if(playerInfos[i].ps == PlayerState::Occupied)
×
166
                    playerInfos[i].ps = PlayerState::Free;
×
167
            }
168

169
            ggs_ = save.ggs;
×
170
            currentGF = save.start_gf;
×
171
        }
172
        break;
×
173
    }
174

175
    if(playerInfos.empty())
×
176
    {
177
        LOG.write("Map %1% has no players!\n") % mapinfo.filepath;
×
178
        return false;
×
179
    }
NEW
180
    if(playerInfos.size() > MAX_PLAYERS)
×
181
    {
NEW
182
        LOG.write("Map %1% has too many players! (%1% > %2%)\n") % mapinfo.filepath % playerInfos.size() % MAX_PLAYERS;
×
NEW
183
        return false;
×
184
    }
185
    if(!mapinfo.mapData.CompressFromFile(mapinfo.filepath, &mapinfo.mapChecksum))
×
186
        return false;
×
187

188
    if(map.lua_path.has_value() && !bfs::is_regular_file(*map.lua_path))
×
189
        return false;
×
190

191
    bfs::path luaFilePath = map.lua_path.get_value_or(bfs::path(mapinfo.filepath).replace_extension("lua"));
×
192
    if(bfs::is_regular_file(luaFilePath))
×
193
    {
194
        if(!mapinfo.luaData.CompressFromFile(luaFilePath, &mapinfo.luaChecksum))
×
195
            return false;
×
196
        mapinfo.luaFilepath = luaFilePath;
×
197
    } else
198
        RTTR_Assert(mapinfo.luaFilepath.empty() && mapinfo.luaChecksum == 0);
×
199

200
    if(!mapinfo.verifySize())
×
201
    {
202
        LOG.write("Map %1% is to large!\n") % mapinfo.filepath;
×
203
        return false;
×
204
    }
205

206
    // ab in die Konfiguration
207
    state = ServerState::Config;
×
208

209
    // und das socket in listen-modus schicken
210
    if(!serversocket.Listen(config.port, config.ipv6, csi.use_upnp))
×
211
    {
212
        LOG.write("GameServer::Start: ERROR: Listening on port %d failed!\n") % config.port;
×
213
        LOG.writeLastError("Fehler");
×
214
        return false;
×
215
    }
216

217
    if(config.servertype == ServerType::LAN)
×
218
        lanAnnouncer.Start();
×
219
    else if(config.servertype == ServerType::Lobby)
×
220
    {
221
        LOBBYCLIENT.AddServer(config.gamename, mapinfo.title, (!config.password.empty()), config.port);
×
222
        LOBBYCLIENT.AddListener(this);
×
223
    }
224
    AnnounceStatusChange();
×
225

226
    return true;
×
227
}
228

229
unsigned GameServer::GetNumFilledSlots() const
×
230
{
231
    unsigned numFilled = 0;
×
232
    for(const JoinPlayerInfo& player : playerInfos)
×
233
    {
234
        if(player.ps != PlayerState::Free)
×
235
            ++numFilled;
×
236
    }
237
    return numFilled;
×
238
}
239

240
void GameServer::AnnounceStatusChange()
×
241
{
242
    if(config.servertype == ServerType::LAN)
×
243
    {
244
        LanGameInfo info;
×
245
        info.name = config.gamename;
×
246
        info.hasPwd = !config.password.empty();
×
247
        info.map = mapinfo.title;
×
248
        info.curNumPlayers = GetNumFilledSlots();
×
249
        info.maxNumPlayers = playerInfos.size();
×
250
        info.port = config.port;
×
251
        info.isIPv6 = config.ipv6;
×
252
        info.version = rttr::version::GetReadableVersion();
×
253
        info.revision = rttr::version::GetRevision();
×
254
        Serializer ser;
×
255
        info.Serialize(ser);
×
256
        lanAnnouncer.SetPayload(ser.GetData(), ser.GetLength());
×
257
    } else if(config.servertype == ServerType::Lobby)
×
258
    {
259
        if(LOBBYCLIENT.IsIngame())
×
260
            LOBBYCLIENT.UpdateServerNumPlayers(GetNumFilledSlots(), playerInfos.size());
×
261
    }
262
}
×
263

264
void GameServer::LC_Status_Error(const std::string& /*error*/)
×
265
{
266
    // Error during adding of server to lobby -> Stop
267
    Stop();
×
268
}
×
269

270
void GameServer::LC_Created()
×
271
{
272
    // All good -> Don't listen anymore
273
    LOBBYCLIENT.RemoveListener(this);
×
274
    AnnounceStatusChange();
×
275
}
×
276

277
void GameServer::Run()
×
278
{
279
    if(state == ServerState::Stopped)
×
280
        return;
×
281

282
    // auf tote Clients prüfen
283
    ClientWatchDog();
×
284

285
    // auf neue Clients warten
286
    if(state == ServerState::Config)
×
287
        RunStateConfig();
×
288
    else if(state == ServerState::Loading)
×
289
        RunStateLoading();
×
290
    else if(state == ServerState::Game)
×
291
        RunStateGame();
×
292

293
    // post zustellen
294
    FillPlayerQueues();
×
295

296
    // Execute messages
297
    for(GameServerPlayer& player : networkPlayers)
×
298
    {
299
        // Ignore kicked players
300
        if(!player.socket.isValid())
×
301
            continue;
×
302
        player.executeMsgs(*this);
×
303
    }
304
    // Send afterwards as most messages are relayed which should be done as fast as possible
305
    for(GameServerPlayer& player : networkPlayers)
×
306
    {
307
        // Ignore kicked players
308
        if(!player.socket.isValid())
×
309
            continue;
×
310
        player.sendMsgs(10);
×
311
    }
312
    helpers::erase_if(networkPlayers, [](const auto& player) { return !player.socket.isValid(); });
×
313

314
    lanAnnouncer.Run();
×
315
}
316

317
void GameServer::RunStateConfig()
×
318
{
319
    WaitForClients();
×
320
    if(countdown.IsActive() && countdown.Update())
×
321
    {
322
        // nun echt starten
323
        if(!countdown.IsActive())
×
324
        {
325
            if(!StartGame())
×
326
            {
327
                Stop();
×
328
                return;
×
329
            }
330
        } else
331
            SendToAll(GameMessage_Countdown(countdown.GetRemainingSecs()));
×
332
    }
333
}
334

335
void GameServer::RunStateLoading()
×
336
{
337
    if(!nwfInfo.isReady())
×
338
    {
339
        if(SteadyClock::now() - loadStartTime > std::chrono::seconds(LOAD_TIMEOUT))
×
340
        {
341
            for(const NWFPlayerInfo& player : nwfInfo.getPlayerInfos())
×
342
            {
343
                if(player.isLagging)
×
344
                    KickPlayer(player.id, KickReason::PingTimeout, __LINE__);
×
345
            }
346
        }
347
        return;
×
348
    }
349
    LOG.write("SERVER: Game loaded by all players after %1%\n")
×
350
      % helpers::withUnit(std::chrono::duration_cast<std::chrono::seconds>(SteadyClock::now() - loadStartTime));
×
351
    // The first NWF is ready. Server has to set up "missing" commands so every future command is for the correct NWF as
352
    // specified with cmdDelay. We have commands for NWF 0. When clients execute this they will send the commands for
353
    // NWF cmdDelay. So commands for NWF 1..cmdDelay-1 are missing. Do this here and before the NWFDone is sent,
354
    // otherwise we might get them in a wrong order when messages are sent asynchronously
355
    for(unsigned i = 1; i < nwfInfo.getCmdDelay(); i++)
×
356
    {
357
        for(const NWFPlayerInfo& player : nwfInfo.getPlayerInfos())
×
358
        {
359
            GameMessage_GameCommand msg(player.id, nwfInfo.getPlayerCmds(player.id).checksum,
×
360
                                        std::vector<gc::GameCommandPtr>());
×
361
            SendToAll(msg);
×
362
            nwfInfo.addPlayerCmds(player.id, msg.cmds);
×
363
        }
364
    }
365

366
    NWFServerInfo serverInfo = nwfInfo.getServerInfo();
×
367
    // Send cmdDelay NWFDone messages
368
    // First send the OK for NWF 0 which is also the game ready command
369
    // Note: Do not store. It already is in NWFInfo
370
    SendToAll(GameMessage_Server_NWFDone(serverInfo.gf, serverInfo.newGFLen, serverInfo.nextNWF));
×
371
    RTTR_Assert(framesinfo.nwf_length > 0);
×
372
    // Then the remaining OKs for the commands sent above
373
    for(unsigned i = 1; i < nwfInfo.getCmdDelay(); i++)
×
374
    {
375
        serverInfo.gf = serverInfo.nextNWF;
×
376
        serverInfo.nextNWF += framesinfo.nwf_length;
×
377
        SendNWFDone(serverInfo);
×
378
    }
379

380
    // And go!
381
    framesinfo.lastTime = FramesInfo::UsedClock::now();
×
382
    state = ServerState::Game;
×
383
}
384

385
void GameServer::RunStateGame()
×
386
{
387
    if(!framesinfo.isPaused)
×
388
        ExecuteGameFrame();
×
389
}
×
390

391
void GameServer::Stop()
×
392
{
393
    if(state == ServerState::Stopped)
×
394
        return;
×
395

396
    // player verabschieden
397
    playerInfos.clear();
×
398
    networkPlayers.clear();
×
399

400
    // aufräumen
401
    framesinfo.Clear();
×
402
    config.Clear();
×
403
    mapinfo.Clear();
×
404
    countdown.Stop();
×
405

406
    // laden dicht machen
407
    serversocket.Close();
×
408
    // clear jump target
409
    skiptogf = 0;
×
410

411
    // clear async logs
412
    asyncLogs.clear();
×
413

414
    lanAnnouncer.Stop();
×
415

416
    if(LOBBYCLIENT.IsLoggedIn()) // steht die Lobbyverbindung noch?
×
417
        LOBBYCLIENT.DeleteServer();
×
418
    LOBBYCLIENT.RemoveListener(this);
×
419

420
    // status
421
    state = ServerState::Stopped;
×
422
    LOG.write("server state changed to stop\n");
×
423
}
424

425
// Check if there are players that have not been assigned a team but only a random team.
426
// Those players are assigned a team now optionally trying to balance the number of players per team.
427
// Returns true iff players have been assigned.
428
bool GameServer::assignPlayersOfRandomTeams(std::vector<JoinPlayerInfo>& playerInfos)
43✔
429
{
430
    static_assert(NUM_TEAMS == 4, "Expected exactly 4 playable teams!");
431
    RTTR_Assert(playerInfos.size() <= MAX_PLAYERS);
43✔
432

433
    using boost::container::static_vector;
434
    using PlayerIndex = unsigned;
435
    using TeamIndex = unsigned;
436
    const auto teamIdxToTeam = [](const TeamIndex teamNum) {
141✔
437
        RTTR_Assert(teamNum < NUM_TEAMS);
141✔
438
        return Team(static_cast<TeamIndex>(Team::Team1) + teamNum);
141✔
439
    };
440

441
    std::array<unsigned, NUM_TEAMS> numPlayersInTeam{};
43✔
442
    struct AssignPlayer
443
    {
444
        PlayerIndex player;
445
        static_vector<TeamIndex, NUM_TEAMS> possibleTeams;
446
        TeamIndex chosenTeam = 0;
447
    };
448

449
    static_vector<AssignPlayer, MAX_PLAYERS> playersToAssign;
×
450
    auto rng = helpers::getRandomGenerator();
43✔
451

452
    bool playerWasAssigned = false;
43✔
453

454
    // Assign (fully) random teams, count players in team and sort into playersToAssign
455
    for(PlayerIndex player = 0; player < playerInfos.size(); ++player)
277✔
456
    {
457
        auto& playerInfo = playerInfos[player];
234✔
458
        if(playerInfo.team == Team::Random)
234✔
459
        {
460
            const TeamIndex randTeam = std::uniform_int_distribution<TeamIndex>{0, NUM_TEAMS - 1u}(rng);
2✔
461
            playerInfo.team = teamIdxToTeam(randTeam);
2✔
462
            playerWasAssigned = true;
2✔
463
        }
464
        switch(playerInfo.team)
234✔
465
        {
466
            case Team::Team1: ++numPlayersInTeam[0]; break;
44✔
467
            case Team::Team2: ++numPlayersInTeam[1]; break;
7✔
468
            case Team::Team3: ++numPlayersInTeam[2]; break;
39✔
469
            case Team::Team4: ++numPlayersInTeam[3]; break;
3✔
470
            case Team::Random1To2: playersToAssign.emplace_back(AssignPlayer{player, {0, 1}}); break;
114✔
471
            case Team::Random1To3: playersToAssign.emplace_back(AssignPlayer{player, {0, 1, 2}}); break;
261✔
472
            case Team::Random1To4: playersToAssign.emplace_back(AssignPlayer{player, {0, 1, 2, 3}}); break;
42✔
473
            case Team::Random: RTTR_Assert(false); break;
×
474
            case Team::None: break;
2✔
475
        }
476
    }
477

478
    // To make the teams as even as possible we start to assign the most constrained players first
479
    std::sort(playersToAssign.begin(), playersToAssign.end(), [](const AssignPlayer& lhs, const AssignPlayer& rhs) {
43✔
480
        return lhs.possibleTeams.size() < rhs.possibleTeams.size();
612✔
481
    });
482

483
    // Put each player into a random team with the currently least amount of players using the possible teams only
484
    for(AssignPlayer& player : playersToAssign)
407✔
485
    {
486
        // Determine the teams with the minima size for the currently possible teams and choose one randomly
487
        unsigned minNextTeamSize = std::numeric_limits<unsigned>::max();
139✔
488
        static_vector<TeamIndex, NUM_TEAMS> teamsForNextPlayer;
×
489
        for(const TeamIndex team : player.possibleTeams)
1,203✔
490
        {
491
            if(minNextTeamSize > numPlayersInTeam[team])
393✔
492
            {
493
                teamsForNextPlayer.clear();
494
                teamsForNextPlayer.push_back(team);
495
                minNextTeamSize = numPlayersInTeam[team];
208✔
496
            } else if(minNextTeamSize == numPlayersInTeam[team])
185✔
497
                teamsForNextPlayer.push_back(team);
498
        }
499
        player.chosenTeam = helpers::getRandomElement(rng, teamsForNextPlayer);
139✔
500

501
        ++numPlayersInTeam[player.chosenTeam];
139✔
502
        playerWasAssigned = true;
139✔
503
    }
504
    // Now the teams are as even as possible and the uneven team(s) is a random one within the constraints
505
    // To have some more randomness we swap players within their constraints
506
    std::shuffle(playersToAssign.begin(), playersToAssign.end(), rng);
43✔
507
    for(auto it = playersToAssign.begin(); it != playersToAssign.end(); ++it)
321✔
508
    {
509
        // Search for a random player with which we can swap, including ourselfes
510
        // Go only forward to avoid back-swapping
511
        static_vector<decltype(it), MAX_PLAYERS> possibleSwapTargets;
×
512
        for(auto it2 = it; it2 != playersToAssign.end(); ++it2)
807✔
513
        {
514
            if(helpers::contains(it->possibleTeams, it2->chosenTeam)
668✔
515
               && helpers::contains(it2->possibleTeams, it->chosenTeam))
952✔
516
                possibleSwapTargets.push_back(it2);
517
        }
518
        const auto itSwapTarget = helpers::getRandomElement(rng, possibleSwapTargets);
139✔
519
        std::swap(it->chosenTeam, itSwapTarget->chosenTeam);
278✔
520
        playerInfos[it->player].team = teamIdxToTeam(it->chosenTeam);
278✔
521
    }
522

523
    return playerWasAssigned;
86✔
524
}
525

526
bool GameServer::StartGame()
×
527
{
528
    lanAnnouncer.Stop();
×
529

530
    // Finalize the team selection for unassigned players.
531
    if(assignPlayersOfRandomTeams(playerInfos))
×
532
        SendToAll(GameMessage_Player_List(playerInfos));
×
533

534
    // Bei Savegames wird der Startwert von den Clients aus der Datei gelesen!
535
    unsigned random_init;
536
    if(mapinfo.type == MapType::Savegame)
×
537
        random_init = 0;
×
538
    else
539
        random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
×
540

541
    nwfInfo.init(currentGF, 3);
×
542

543
    // Send start first, then load the rest
544
    SendToAll(GameMessage_Server_Start(random_init, nwfInfo.getNextNWF(), nwfInfo.getCmdDelay()));
×
545
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_SERVER_START(%d)\n") % random_init;
×
546

547
    // Höchsten Ping ermitteln
548
    unsigned highest_ping = 0;
×
549
    for(const JoinPlayerInfo& player : playerInfos)
×
550
    {
551
        if(player.ps == PlayerState::Occupied)
×
552
        {
553
            if(player.ping > highest_ping)
×
554
                highest_ping = player.ping;
×
555
        }
556
    }
557

558
    framesinfo.gfLengthReq = framesinfo.gf_length = SPEED_GF_LENGTHS[ggs_.speed];
×
559

560
    // NetworkFrame-Länge bestimmen, je schlechter (also höher) die Pings, desto länger auch die Framelänge
561
    framesinfo.nwf_length = CalcNWFLenght(FramesInfo::milliseconds32_t(highest_ping));
×
562

563
    LOG.write("SERVER: Using gameframe length of %1%\n") % helpers::withUnit(framesinfo.gf_length);
×
564
    LOG.write("SERVER: Using networkframe length of %1% GFs (%2%)\n") % framesinfo.nwf_length
×
565
      % helpers::withUnit(framesinfo.nwf_length * framesinfo.gf_length);
×
566

567
    for(unsigned id = 0; id < playerInfos.size(); id++)
×
568
    {
569
        if(playerInfos[id].isUsed())
×
570
            nwfInfo.addPlayer(id);
×
571
    }
572

573
    // Add server info so nwfInfo can be ready but do NOT send it yet, as we wait for the player commands before sending
574
    // the done msg
575
    nwfInfo.addServerInfo(NWFServerInfo(currentGF, framesinfo.gf_length / FramesInfo::milliseconds32_t(1),
×
576
                                        currentGF + framesinfo.nwf_length));
×
577

578
    state = ServerState::Loading;
×
579
    loadStartTime = SteadyClock::now();
×
580

581
    return true;
×
582
}
583

584
unsigned GameServer::CalcNWFLenght(FramesInfo::milliseconds32_t minDuration) const
×
585
{
586
    constexpr unsigned maxNumGF = 20;
×
587
    for(unsigned i = 1; i < maxNumGF; ++i)
×
588
    {
589
        if(i * framesinfo.gf_length >= minDuration)
×
590
            return i;
×
591
    }
592
    return maxNumGF;
×
593
}
594

595
void GameServer::SendNWFDone(const NWFServerInfo& info)
×
596
{
597
    nwfInfo.addServerInfo(info);
×
598
    SendToAll(GameMessage_Server_NWFDone(info.gf, info.newGFLen, info.nextNWF));
×
599
}
×
600

601
void GameServer::SendToAll(const GameMessage& msg)
×
602
{
603
    for(GameServerPlayer& player : networkPlayers)
×
604
    {
605
        // ist der Slot Belegt, dann Nachricht senden
606
        if(player.isActive())
×
607
            player.sendMsgAsync(msg.clone());
×
608
    }
609
}
×
610

611
void GameServer::KickPlayer(uint8_t playerId, KickReason cause, uint32_t param)
×
612
{
613
    if(playerId >= playerInfos.size())
×
614
        return;
×
615
    JoinPlayerInfo& playerInfo = playerInfos[playerId];
×
616
    GameServerPlayer* player = GetNetworkPlayer(playerId);
×
617
    if(player)
×
618
        player->closeConnection();
×
619
    // Non-existing or connecting player
620
    if(!playerInfo.isUsed())
×
621
        return;
×
622
    playerInfo.ps = PlayerState::Free;
×
623

624
    SendToAll(GameMessage_Player_Kicked(playerId, cause, param));
×
625

626
    // If we are ingame, replace by KI
627
    if(state == ServerState::Game || state == ServerState::Loading)
×
628
    {
629
        playerInfo.ps = PlayerState::AI;
×
630
        playerInfo.aiInfo = AI::Info(AI::Type::Dummy);
×
631
    } else
632
        CancelCountdown();
×
633

634
    AnnounceStatusChange();
×
635
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYERKICKED(%d,%d,%d)\n") % unsigned(playerId) % unsigned(cause)
×
636
      % unsigned(param);
×
637
}
638

639
// testet, ob in der Verbindungswarteschlange Clients auf Verbindung warten
640
void GameServer::ClientWatchDog()
×
641
{
642
    SocketSet set;
×
643
    set.Clear();
×
644

645
    // sockets zum set hinzufügen
646
    for(GameServerPlayer& player : networkPlayers)
×
647
        set.Add(player.socket);
×
648

649
    // auf fehler prüfen
650
    if(set.Select(0, 2) > 0)
×
651
    {
652
        for(const GameServerPlayer& player : networkPlayers)
×
653
        {
654
            if(set.InSet(player.socket))
×
655
            {
656
                LOG.write(_("SERVER: Error on socket of player %1%, bye bye!\n")) % player.playerId;
×
657
                KickPlayer(player.playerId, KickReason::ConnectionLost, __LINE__);
×
658
            }
659
        }
660
    }
661

662
    for(GameServerPlayer& player : networkPlayers)
×
663
    {
664
        if(player.hasTimedOut())
×
665
        {
666
            LOG.write(_("SERVER: Reserved slot %1% freed due to timeout\n")) % player.playerId;
×
667
            KickPlayer(player.playerId, KickReason::PingTimeout, __LINE__);
×
668
        } else
669
            player.doPing();
×
670
    }
671
}
×
672

673
void GameServer::ExecuteGameFrame()
×
674
{
675
    RTTR_Assert(state == ServerState::Game);
×
676

677
    FramesInfo::UsedClock::time_point currentTime = FramesInfo::UsedClock::now();
×
678
    FramesInfo::milliseconds32_t passedTime =
679
      std::chrono::duration_cast<FramesInfo::milliseconds32_t>(currentTime - framesinfo.lastTime);
×
680

681
    // prüfen ob GF vergangen
682
    if(passedTime >= framesinfo.gf_length || skiptogf > currentGF)
×
683
    {
684
        // NWF vergangen?
685
        if(currentGF == nwfInfo.getNextNWF())
×
686
        {
687
            if(CheckForLaggingPlayers())
×
688
            {
689
                // Check for kicking every second
690
                static FramesInfo::UsedClock::time_point lastLagKickTime;
691
                if(currentTime - lastLagKickTime >= std::chrono::seconds(1))
×
692
                {
693
                    lastLagKickTime = currentTime;
×
694
                    CheckAndKickLaggingPlayers();
×
695
                }
696
                // Skip the rest
697
                return;
×
698
            } else
699
                ExecuteNWF();
×
700
        }
701
        // Advance GF
702
        ++currentGF;
×
703
        // Normally we set lastTime = curTime (== lastTime + passedTime) where passedTime is ideally 1 GF
704
        // But we might got called late, so we advance the time by 1 GF anyway so in that case we execute the next GF a
705
        // bit earlier. Exception: We lag many GFs behind, then we advance by the full passedTime - 1 GF which means we
706
        // are now only 1 GF behind and execute that on the next call
707
        if(passedTime <= 4 * framesinfo.gf_length)
×
708
            passedTime = framesinfo.gf_length;
×
709
        else
710
            passedTime -= framesinfo.gf_length;
×
711
        framesinfo.lastTime += passedTime;
×
712
    }
713
}
714

715
void GameServer::ExecuteNWF()
×
716
{
717
    // Check for asyncs
718
    if(CheckForAsync())
×
719
    {
720
        // Pause game
721
        RTTR_Assert(!framesinfo.isPaused);
×
722
        SetPaused(true);
×
723

724
        // Notify players
725
        std::vector<unsigned> checksumHashes;
×
726
        checksumHashes.reserve(networkPlayers.size());
×
727
        for(const GameServerPlayer& player : networkPlayers)
×
728
            checksumHashes.push_back(nwfInfo.getPlayerCmds(player.playerId).checksum.getHash());
×
729
        SendToAll(GameMessage_Server_Async(checksumHashes));
×
730

731
        // Request async logs
732
        for(GameServerPlayer& player : networkPlayers)
×
733
        {
734
            asyncLogs.push_back(AsyncLog(player.playerId, nwfInfo.getPlayerCmds(player.playerId).checksum));
×
735
            player.sendMsgAsync(new GameMessage_GetAsyncLog());
×
736
        }
737
    }
738
    const NWFServerInfo serverInfo = nwfInfo.getServerInfo();
×
739
    RTTR_Assert(serverInfo.gf == currentGF);
×
740
    RTTR_Assert(serverInfo.nextNWF > currentGF);
×
741
    // First save old values
742
    unsigned lastNWF = nwfInfo.getLastNWF();
×
743
    FramesInfo::milliseconds32_t oldGFLen = framesinfo.gf_length;
×
744
    nwfInfo.execute(framesinfo);
×
745
    if(oldGFLen != framesinfo.gf_length)
×
746
    {
747
        LOG.write(_("SERVER: At GF %1%: Speed changed from %2% to %3%. NWF %4%\n")) % currentGF
×
748
          % helpers::withUnit(oldGFLen) % helpers::withUnit(framesinfo.gf_length) % framesinfo.nwf_length;
×
749
    }
750
    NWFServerInfo newInfo(lastNWF, framesinfo.gfLengthReq / FramesInfo::milliseconds32_t(1),
×
751
                          lastNWF + framesinfo.nwf_length);
×
752
    if(framesinfo.gfLengthReq != framesinfo.gf_length)
×
753
    {
754
        // Speed will change, adjust nwf length so the time will stay constant
755
        using namespace std::chrono;
756
        using MsDouble = duration<double, std::milli>;
757
        double newNWFLen =
758
          framesinfo.nwf_length * framesinfo.gf_length / duration_cast<MsDouble>(framesinfo.gfLengthReq);
×
759
        newInfo.nextNWF = lastNWF + std::max(1u, helpers::iround<unsigned>(newNWFLen));
×
760
    }
761
    SendNWFDone(newInfo);
×
762
}
×
763

764
bool GameServer::CheckForAsync()
×
765
{
766
    if(networkPlayers.empty())
×
767
        return false;
×
768
    bool isAsync = false;
×
769
    const AsyncChecksum& refChecksum = nwfInfo.getPlayerCmds(networkPlayers.front().playerId).checksum;
×
770
    for(const GameServerPlayer& player : networkPlayers)
×
771
    {
772
        const AsyncChecksum& curChecksum = nwfInfo.getPlayerCmds(player.playerId).checksum;
×
773

774
        // Checksummen nicht gleich?
775
        if(curChecksum != refChecksum)
×
776
        {
777
            LOG.write(_("Async at GF %1% of player %2% vs %3%. Checksums:\n%4%\n%5%\n\n")) % currentGF % player.playerId
×
778
              % networkPlayers.front().playerId % curChecksum % refChecksum;
×
779
            isAsync = true;
×
780
        }
781
    }
782
    return isAsync;
×
783
}
784

785
void GameServer::CheckAndKickLaggingPlayers()
×
786
{
787
    for(const GameServerPlayer& player : networkPlayers)
×
788
    {
789
        const unsigned timeOut = player.getLagTimeOut();
×
790
        if(timeOut == 0)
×
791
            KickPlayer(player.playerId, KickReason::PingTimeout, __LINE__);
×
792
        else if(timeOut <= 30
×
793
                && (timeOut % 5 == 0
×
794
                    || timeOut < 5)) // Notify every 5s if max 30s are remaining, if less than 5s notify every second
×
795
            LOG.write(_("SERVER: Kicking player %1% in %2% seconds\n")) % player.playerId % timeOut;
×
796
    }
797
}
×
798

799
bool GameServer::CheckForLaggingPlayers()
×
800
{
801
    if(nwfInfo.isReady())
×
802
        return false;
×
803
    for(GameServerPlayer& player : networkPlayers)
×
804
    {
805
        if(nwfInfo.getPlayerInfo(player.playerId).isLagging)
×
806
            player.setLagging();
×
807
    }
808
    return true;
×
809
}
810

811
// testet, ob in der Verbindungswarteschlange Clients auf Verbindung warten
812
void GameServer::WaitForClients()
×
813
{
814
    SocketSet set;
×
815

816
    set.Add(serversocket);
×
817
    if(set.Select(0, 0) > 0)
×
818
    {
819
        RTTR_Assert(set.InSet(serversocket));
×
820
        Socket socket = serversocket.Accept();
×
821

822
        // Verbindung annehmen
823
        if(!socket.isValid())
×
824
            return;
×
825

826
        unsigned newPlayerId = GameMessageWithPlayer::NO_PLAYER_ID;
×
827
        // Geeigneten Platz suchen
828
        for(unsigned playerId = 0; playerId < playerInfos.size(); ++playerId)
×
829
        {
830
            if(playerInfos[playerId].ps == PlayerState::Free && !GetNetworkPlayer(playerId))
×
831
            {
832
                networkPlayers.push_back(GameServerPlayer(playerId, socket));
×
833
                newPlayerId = playerId;
×
834
                break;
×
835
            }
836
        }
837

838
        GameMessage_Player_Id msg(newPlayerId);
×
839
        MessageHandler::send(socket, msg);
×
840

841
        // war kein platz mehr frei, wenn ja dann verbindung trennen?
842
        if(newPlayerId == 0xFFFFFFFF)
×
843
            socket.Close();
×
844
    }
845
}
846

847
// füllt die warteschlangen mit "paketen"
848
void GameServer::FillPlayerQueues()
×
849
{
850
    SocketSet set;
×
851
    bool msgReceived = false;
×
852

853
    // erstmal auf Daten überprüfen
854
    do
×
855
    {
856
        // sockets zum set hinzufügen
857
        for(const GameServerPlayer& player : networkPlayers)
×
858
            set.Add(player.socket);
×
859

860
        msgReceived = false;
×
861

862
        // ist eines der Sockets im Set lesbar?
863
        if(set.Select(0, 0) > 0)
×
864
        {
865
            for(GameServerPlayer& player : networkPlayers)
×
866
            {
867
                if(set.InSet(player.socket))
×
868
                {
869
                    // nachricht empfangen
870
                    if(!player.receiveMsgs())
×
871
                    {
872
                        LOG.write(_("SERVER: Receiving Message for player %1% failed, kicking...\n")) % player.playerId;
×
873
                        KickPlayer(player.playerId, KickReason::ConnectionLost, __LINE__);
×
874
                    } else
875
                        msgReceived = true;
×
876
                }
877
            }
878
        }
879
    } while(msgReceived);
880
}
×
881

882
bool GameServer::OnGameMessage(const GameMessage_Pong& msg)
×
883
{
884
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
885
    if(player)
×
886
    {
887
        unsigned ping = player->calcPingTime();
×
888
        if(ping == 0u)
×
889
            return true;
×
890
        playerInfos[msg.senderPlayerID].ping = ping;
×
891
        SendToAll(GameMessage_Player_Ping(msg.senderPlayerID, ping));
×
892
    }
893
    return true;
×
894
}
895

896
bool GameServer::OnGameMessage(const GameMessage_Server_Type& msg)
×
897
{
898
    if(state != ServerState::Config)
×
899
    {
900
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
901
        return true;
×
902
    }
903

904
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
905
    if(!player)
×
906
        return true;
×
907

908
    auto typeok = GameMessage_Server_TypeOK::StatusCode::Ok;
×
909
    if(msg.type != config.servertype)
×
910
        typeok = GameMessage_Server_TypeOK::StatusCode::InvalidServerType;
×
911
    else if(msg.revision != rttr::version::GetRevision())
×
912
        typeok = GameMessage_Server_TypeOK::StatusCode::WrongVersion;
×
913

914
    player->sendMsg(GameMessage_Server_TypeOK(typeok, rttr::version::GetRevision()));
×
915

916
    if(typeok != GameMessage_Server_TypeOK::StatusCode::Ok)
×
917
        KickPlayer(msg.senderPlayerID, KickReason::ConnectionLost, __LINE__);
×
918
    return true;
×
919
}
920

921
bool GameServer::OnGameMessage(const GameMessage_Server_Password& msg)
×
922
{
923
    if(state != ServerState::Config)
×
924
    {
925
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
926
        return true;
×
927
    }
928

929
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
930
    if(!player)
×
931
        return true;
×
932

933
    std::string passwordok = (config.password == msg.password ? "true" : "false");
×
934
    if(msg.password == config.hostPassword)
×
935
    {
936
        passwordok = "true";
×
937
        playerInfos[msg.senderPlayerID].isHost = true;
×
938
    } else
939
        playerInfos[msg.senderPlayerID].isHost = false;
×
940

941
    player->sendMsgAsync(new GameMessage_Server_Password(passwordok));
×
942

943
    if(passwordok == "false")
×
944
        KickPlayer(msg.senderPlayerID, KickReason::WrongPassword, __LINE__);
×
945
    return true;
×
946
}
947

948
bool GameServer::OnGameMessage(const GameMessage_Chat& msg)
×
949
{
950
    int playerID = GetTargetPlayer(msg);
×
951
    if(playerID >= 0)
×
952
        SendToAll(GameMessage_Chat(playerID, msg.destination, msg.text));
×
953
    return true;
×
954
}
955

956
bool GameServer::OnGameMessage(const GameMessage_Player_State& msg)
×
957
{
958
    if(state != ServerState::Config)
×
959
    {
960
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
961
        return true;
×
962
    }
963
    // Can't do this. Have to have a joined player
964
    if(msg.ps == PlayerState::Occupied)
×
965
        return true;
×
966

967
    int playerID = GetTargetPlayer(msg);
×
968
    if(playerID < 0)
×
969
        return true;
×
970
    JoinPlayerInfo& player = playerInfos[playerID];
×
971
    const PlayerState oldPs = player.ps;
×
972
    // Can't change self
973
    if(playerID != msg.senderPlayerID)
×
974
    {
975
        // oh ein spieler, weg mit ihm!
976
        if(GetNetworkPlayer(playerID))
×
977
            KickPlayer(playerID, KickReason::NoCause, __LINE__);
×
978

979
        if(mapinfo.type == MapType::Savegame)
×
980
        {
981
            // For savegames we cannot set anyone on a locked slot as the player does not exist on the map
982
            if(player.ps != PlayerState::Locked)
×
983
            {
984
                // And we don't lock!
985
                player.ps = msg.ps == PlayerState::Locked ? PlayerState::Free : msg.ps;
×
986
                player.aiInfo = msg.aiInfo;
×
987
            }
988
        } else
989
        {
990
            player.ps = msg.ps;
×
991
            player.aiInfo = msg.aiInfo;
×
992
        }
993
        if(player.ps == PlayerState::Free && config.servertype == ServerType::Local)
×
994
        {
995
            player.ps = PlayerState::AI;
×
996
            player.aiInfo = AI::Info(AI::Type::Default);
×
997
        }
998
    }
999
    // Even when nothing changed we send the data because the other players might have expected a change
1000

1001
    if(player.ps == PlayerState::AI)
×
1002
    {
1003
        player.SetAIName(playerID);
×
1004
        SendToAll(GameMessage_Player_Name(playerID, player.name));
×
1005
    }
1006
    // If slot is filled, check current color
1007
    if(player.isUsed())
×
1008
        CheckAndSetColor(playerID, player.color);
×
1009
    SendToAll(GameMessage_Player_State(playerID, player.ps, player.aiInfo));
×
1010

1011
    if(oldPs != player.ps)
×
1012
        player.isReady = (player.ps == PlayerState::AI);
×
1013
    SendToAll(GameMessage_Player_Ready(playerID, player.isReady));
×
1014
    PlayerDataChanged(playerID);
×
1015
    AnnounceStatusChange();
×
1016
    return true;
×
1017
}
1018

1019
bool GameServer::OnGameMessage(const GameMessage_Player_Name& msg)
×
1020
{
1021
    if(state != ServerState::Config)
×
1022
    {
1023
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1024
        return true;
×
1025
    }
1026
    int playerID = GetTargetPlayer(msg);
×
1027
    if(playerID < 0)
×
1028
        return true;
×
1029

1030
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_PLAYER_NAME(%s)\n") % playerID % msg.playername;
×
1031

1032
    playerInfos[playerID].name = msg.playername;
×
1033
    SendToAll(GameMessage_Player_Name(playerID, msg.playername));
×
1034
    PlayerDataChanged(playerID);
×
1035

1036
    return true;
×
1037
}
1038

1039
bool GameServer::OnGameMessage(const GameMessage_Player_Portrait& msg)
×
1040
{
1041
    if(state != ServerState::Config)
×
1042
    {
1043
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1044
        return true;
×
1045
    }
1046
    int playerID = GetTargetPlayer(msg);
×
1047
    if(playerID < 0)
×
1048
        return true;
×
1049
    if(msg.playerPortraitIndex >= Portraits.size())
×
1050
        return true;
×
1051

1052
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_PLAYER_PORTRAIT(%u)\n") % playerID % msg.playerPortraitIndex;
×
1053
    playerInfos[playerID].portraitIndex = msg.playerPortraitIndex;
×
1054
    SendToAll(GameMessage_Player_Portrait(playerID, msg.playerPortraitIndex));
×
1055
    PlayerDataChanged(playerID);
×
1056

1057
    return true;
×
1058
}
1059

1060
bool GameServer::OnGameMessage(const GameMessage_Player_Nation& msg)
×
1061
{
1062
    if(state != ServerState::Config)
×
1063
    {
1064
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1065
        return true;
×
1066
    }
1067
    int playerID = GetTargetPlayer(msg);
×
1068
    if(playerID < 0)
×
1069
        return true;
×
1070

1071
    playerInfos[playerID].nation = msg.nation;
×
1072

1073
    SendToAll(GameMessage_Player_Nation(playerID, msg.nation));
×
1074
    PlayerDataChanged(playerID);
×
1075
    return true;
×
1076
}
1077

1078
bool GameServer::OnGameMessage(const GameMessage_Player_Team& msg)
×
1079
{
1080
    if(state != ServerState::Config)
×
1081
    {
1082
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1083
        return true;
×
1084
    }
1085
    int playerID = GetTargetPlayer(msg);
×
1086
    if(playerID < 0)
×
1087
        return true;
×
1088

1089
    playerInfos[playerID].team = msg.team;
×
1090

1091
    SendToAll(GameMessage_Player_Team(playerID, msg.team));
×
1092
    PlayerDataChanged(playerID);
×
1093
    return true;
×
1094
}
1095

1096
bool GameServer::OnGameMessage(const GameMessage_Player_Color& msg)
×
1097
{
1098
    if(state != ServerState::Config)
×
1099
    {
1100
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1101
        return true;
×
1102
    }
1103
    int playerID = GetTargetPlayer(msg);
×
1104
    if(playerID < 0)
×
1105
        return true;
×
1106

1107
    CheckAndSetColor(playerID, msg.color);
×
1108
    PlayerDataChanged(playerID);
×
1109
    return true;
×
1110
}
1111

1112
bool GameServer::OnGameMessage(const GameMessage_Player_Ready& msg)
×
1113
{
1114
    if(state != ServerState::Config)
×
1115
    {
1116
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1117
        return true;
×
1118
    }
1119
    int playerID = GetTargetPlayer(msg);
×
1120
    if(playerID < 0)
×
1121
        return true;
×
1122

1123
    JoinPlayerInfo& player = playerInfos[playerID];
×
1124

1125
    player.isReady = msg.ready;
×
1126

1127
    // countdown ggf abbrechen
1128
    if(!player.isReady && countdown.IsActive())
×
1129
        CancelCountdown();
×
1130

1131
    SendToAll(GameMessage_Player_Ready(playerID, msg.ready));
×
1132
    return true;
×
1133
}
1134

1135
bool GameServer::OnGameMessage(const GameMessage_MapRequest& msg)
×
1136
{
1137
    if(state != ServerState::Config)
×
1138
    {
1139
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1140
        return true;
×
1141
    }
1142
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1143
    if(!player)
×
1144
        return true;
×
1145

1146
    if(msg.requestInfo)
×
1147
    {
1148
        player->sendMsgAsync(new GameMessage_Map_Info(mapinfo.filepath.filename().string(), mapinfo.type,
×
1149
                                                      mapinfo.mapData.uncompressedLength, mapinfo.mapData.data.size(),
×
1150
                                                      mapinfo.luaData.uncompressedLength, mapinfo.luaData.data.size()));
×
1151
    } else if(player->isMapSending())
×
1152
    {
1153
        // Don't send again
1154
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1155
    } else
1156
    {
1157
        // Send map data
1158
        unsigned curPos = 0;
×
1159
        unsigned remainingSize = mapinfo.mapData.data.size();
×
1160
        while(remainingSize)
×
1161
        {
1162
            unsigned chunkSize = std::min(MAP_PART_SIZE, remainingSize);
×
1163

1164
            player->sendMsgAsync(new GameMessage_Map_Data(true, curPos, &mapinfo.mapData.data[curPos], chunkSize));
×
1165
            curPos += chunkSize;
×
1166
            remainingSize -= chunkSize;
×
1167
        }
1168

1169
        // And lua data (if there is any)
1170
        RTTR_Assert(mapinfo.luaFilepath.empty() == mapinfo.luaData.data.empty());
×
1171
        RTTR_Assert(mapinfo.luaData.data.empty() == (mapinfo.luaData.uncompressedLength == 0));
×
1172
        curPos = 0;
×
1173
        remainingSize = mapinfo.luaData.data.size();
×
1174
        while(remainingSize)
×
1175
        {
1176
            unsigned chunkSize = std::min(MAP_PART_SIZE, remainingSize);
×
1177

1178
            player->sendMsgAsync(new GameMessage_Map_Data(false, curPos, &mapinfo.luaData.data[curPos], chunkSize));
×
1179
            curPos += chunkSize;
×
1180
            remainingSize -= chunkSize;
×
1181
        }
1182
        // estimate time. max 60 chunks/s (currently limited by framerate), assume 50 (~25kb/s)
1183
        auto numChunks = (mapinfo.mapData.data.size() + mapinfo.luaData.data.size()) / MAP_PART_SIZE;
×
1184
        player->setMapSending(std::chrono::seconds(numChunks / 50 + 1));
×
1185
    }
1186
    return true;
×
1187
}
1188

1189
bool GameServer::OnGameMessage(const GameMessage_Map_Checksum& msg)
×
1190
{
1191
    if(state != ServerState::Config)
×
1192
    {
1193
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1194
        return true;
×
1195
    }
1196
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1197
    if(!player)
×
1198
        return true;
×
1199

1200
    bool checksumok = (msg.mapChecksum == mapinfo.mapChecksum && msg.luaChecksum == mapinfo.luaChecksum);
×
1201

1202
    LOG.writeToFile("CLIENT%d >>> SERVER: NMS_MAP_CHECKSUM(%u) expected: %u, ok: %s\n") % unsigned(msg.senderPlayerID)
×
1203
      % msg.mapChecksum % mapinfo.mapChecksum % (checksumok ? "yes" : "no");
×
1204

1205
    // Send response. If map data was not sent yet, the client may retry
1206
    player->sendMsgAsync(new GameMessage_Map_ChecksumOK(checksumok, !player->isMapSending()));
×
1207

1208
    LOG.writeToFile("SERVER >>> CLIENT%d: NMS_MAP_CHECKSUM(%d)\n") % unsigned(msg.senderPlayerID) % checksumok;
×
1209

1210
    if(!checksumok)
×
1211
    {
1212
        if(player->isMapSending())
×
1213
            KickPlayer(msg.senderPlayerID, KickReason::WrongChecksum, __LINE__);
×
1214
    } else
1215
    {
1216
        JoinPlayerInfo& playerInfo = playerInfos[msg.senderPlayerID];
×
1217
        // Used? Then we got this twice or some error happened. Remove him
1218
        if(playerInfo.isUsed())
×
1219
            KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1220
        else
1221
        {
1222
            // Inform others about a new player
1223
            SendToAll(GameMessage_Player_New(msg.senderPlayerID, playerInfo.name));
×
1224

1225
            LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYER_NEW(%d, %s)\n") % unsigned(msg.senderPlayerID)
×
1226
              % playerInfo.name;
×
1227

1228
            // Mark as used and assign a unique color
1229
            // Do this before sending the player list to avoid sending useless updates
1230
            playerInfo.ps = PlayerState::Occupied;
×
1231
            CheckAndSetColor(msg.senderPlayerID, playerInfo.color);
×
1232

1233
            // Send remaining data and mark as active
1234
            player->sendMsgAsync(new GameMessage_Server_Name(config.gamename));
×
1235
            player->sendMsgAsync(new GameMessage_Player_List(playerInfos));
×
1236
            player->sendMsgAsync(new GameMessage_GGSChange(ggs_));
×
1237
            player->setActive();
×
1238
        }
1239
        AnnounceStatusChange();
×
1240
    }
1241
    return true;
×
1242
}
1243

1244
// speed change message
1245
bool GameServer::OnGameMessage(const GameMessage_Speed& msg)
×
1246
{
1247
    if(state != ServerState::Game)
×
1248
    {
1249
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1250
        return true;
×
1251
    }
1252
    framesinfo.gfLengthReq = FramesInfo::milliseconds32_t(msg.gf_length);
×
1253
    return true;
×
1254
}
1255

1256
bool GameServer::OnGameMessage(const GameMessage_GameCommand& msg)
×
1257
{
1258
    int targetPlayerId = GetTargetPlayer(msg);
×
1259
    if((state != ServerState::Game && state != ServerState::Loading) || targetPlayerId < 0
×
1260
       || (state == ServerState::Loading && !msg.cmds.gcs.empty()))
×
1261
    {
1262
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1263
        return true;
×
1264
    }
1265

1266
    if(!nwfInfo.addPlayerCmds(targetPlayerId, msg.cmds))
×
1267
        return true; // Ignore
×
1268
    GameServerPlayer* player = GetNetworkPlayer(targetPlayerId);
×
1269
    if(player)
×
1270
        player->setNotLagging();
×
1271
    SendToAll(GameMessage_GameCommand(targetPlayerId, msg.cmds.checksum, msg.cmds.gcs));
×
1272

1273
    return true;
×
1274
}
1275

1276
bool GameServer::OnGameMessage(const GameMessage_AsyncLog& msg)
×
1277
{
1278
    if(state != ServerState::Game)
×
1279
    {
1280
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1281
        return true;
×
1282
    }
1283
    bool foundPlayer = false;
×
1284
    for(AsyncLog& log : asyncLogs)
×
1285
    {
1286
        if(log.playerId != msg.senderPlayerID)
×
1287
            continue;
×
1288
        RTTR_Assert(!log.done);
×
1289
        if(log.done)
×
1290
            return true;
×
1291
        foundPlayer = true;
×
1292
        log.addData += msg.addData;
×
1293
        log.randEntries.insert(log.randEntries.end(), msg.entries.begin(), msg.entries.end());
×
1294
        if(msg.last)
×
1295
        {
1296
            LOG.write(_("Received async logs from %1% (%2% entries).\n")) % unsigned(log.playerId)
×
1297
              % log.randEntries.size();
×
1298
            log.done = true;
×
1299
        }
1300
    }
1301
    if(!foundPlayer)
×
1302
    {
1303
        LOG.write(_("Received async log from %1%, but did not expect it!\n")) % unsigned(msg.senderPlayerID);
×
1304
        return true;
×
1305
    }
1306

1307
    // Check if we have all logs
1308
    for(const AsyncLog& log : asyncLogs)
×
1309
    {
1310
        if(!log.done)
×
1311
            return true;
×
1312
    }
1313

1314
    LOG.write(_("Async logs received completely.\n"));
×
1315

1316
    const bfs::path asyncFilePath = SaveAsyncLog();
×
1317
    if(!asyncFilePath.empty())
×
1318
        SendAsyncLog(asyncFilePath);
×
1319

1320
    // Kick all players that have a different checksum from the host
1321
    AsyncChecksum hostChecksum;
×
1322
    for(const AsyncLog& log : asyncLogs)
×
1323
    {
1324
        if(playerInfos.at(log.playerId).isHost)
×
1325
        {
1326
            hostChecksum = log.checksum;
×
1327
            break;
×
1328
        }
1329
    }
1330
    for(const AsyncLog& log : asyncLogs)
×
1331
    {
1332
        if(log.checksum != hostChecksum)
×
1333
            KickPlayer(log.playerId, KickReason::Async, __LINE__);
×
1334
    }
1335
    return true;
×
1336
}
1337

1338
bool GameServer::OnGameMessage(const GameMessage_RemoveLua& msg)
×
1339
{
1340
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1341
    {
1342
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1343
        return true;
×
1344
    }
1345
    mapinfo.luaFilepath.clear();
×
1346
    mapinfo.luaData.Clear();
×
1347
    mapinfo.luaChecksum = 0;
×
1348
    SendToAll(msg);
×
1349
    CancelCountdown();
×
1350
    return true;
×
1351
}
1352

1353
bool GameServer::OnGameMessage(const GameMessage_Countdown& msg)
×
1354
{
1355
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1356
    {
1357
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1358
        return true;
×
1359
    }
1360

1361
    NetworkPlayer* nwPlayer = GetNetworkPlayer(msg.senderPlayerID);
×
1362
    if(!nwPlayer || countdown.IsActive())
×
1363
        return true;
×
1364

1365
    if(!ArePlayersReady())
×
1366
        nwPlayer->sendMsgAsync(new GameMessage_CancelCountdown(true));
×
1367
    else
1368
    {
1369
        // Just to make sure update all player infos
1370
        SendToAll(GameMessage_Player_List(playerInfos));
×
1371
        // Start countdown (except its single player)
1372
        if(networkPlayers.size() > 1)
×
1373
        {
1374
            countdown.Start(msg.countdown);
×
1375
            SendToAll(GameMessage_Countdown(countdown.GetRemainingSecs()));
×
1376
            LOG.writeToFile("SERVER >>> Countdown started(%d)\n") % countdown.GetRemainingSecs();
×
1377
        } else if(!StartGame())
×
1378
            Stop();
×
1379
    }
1380

1381
    return true;
×
1382
}
1383

1384
bool GameServer::OnGameMessage(const GameMessage_CancelCountdown& msg)
×
1385
{
1386
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1387
    {
1388
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1389
        return true;
×
1390
    }
1391

1392
    CancelCountdown();
×
1393
    return true;
×
1394
}
1395

1396
bool GameServer::OnGameMessage(const GameMessage_Pause& msg)
×
1397
{
1398
    if(IsHost(msg.senderPlayerID))
×
1399
        SetPaused(msg.paused);
×
1400
    return true;
×
1401
}
1402

1403
bool GameServer::OnGameMessage(const GameMessage_SkipToGF& msg)
×
1404
{
1405
    if(IsHost(msg.senderPlayerID))
×
1406
    {
1407
        skiptogf = msg.targetGF;
×
1408
        SendToAll(msg);
×
1409
    }
1410
    return true;
×
1411
}
1412

1413
bool GameServer::OnGameMessage(const GameMessage_GGSChange& msg)
×
1414
{
1415
    if(state != ServerState::Config || !IsHost(msg.senderPlayerID))
×
1416
    {
1417
        KickPlayer(msg.senderPlayerID, KickReason::InvalidMsg, __LINE__);
×
1418
        return true;
×
1419
    }
1420
    ggs_ = msg.ggs;
×
1421
    SendToAll(msg);
×
1422
    CancelCountdown();
×
1423
    return true;
×
1424
}
1425

1426
void GameServer::CancelCountdown()
×
1427
{
1428
    if(!countdown.IsActive())
×
1429
        return;
×
1430
    // Countdown-Stop allen mitteilen
1431
    countdown.Stop();
×
1432
    SendToAll(GameMessage_CancelCountdown());
×
1433
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_CANCELCOUNTDOWN\n");
×
1434
}
1435

1436
bool GameServer::ArePlayersReady() const
×
1437
{
1438
    // Alle Spieler da?
1439
    for(const JoinPlayerInfo& player : playerInfos)
×
1440
    {
1441
        // noch nicht alle spieler da -> feierabend!
1442
        if(player.ps == PlayerState::Free || (player.isHuman() && !player.isReady))
×
1443
            return false;
×
1444
    }
1445

1446
    std::set<unsigned> takenColors;
×
1447

1448
    // Check all players have different colors
1449
    for(const JoinPlayerInfo& player : playerInfos)
×
1450
    {
1451
        if(player.isUsed())
×
1452
        {
1453
            if(helpers::contains(takenColors, player.color))
×
1454
                return false;
×
1455
            takenColors.insert(player.color);
×
1456
        }
1457
    }
1458
    return true;
×
1459
}
1460

1461
void GameServer::PlayerDataChanged(unsigned playerIdx)
×
1462
{
1463
    CancelCountdown();
×
1464
    JoinPlayerInfo& player = GetJoinPlayer(playerIdx);
×
1465
    if(player.ps != PlayerState::AI && player.isReady)
×
1466
    {
1467
        player.isReady = false;
×
1468
        SendToAll(GameMessage_Player_Ready(playerIdx, false));
×
1469
    }
1470
}
×
1471

1472
bfs::path GameServer::SaveAsyncLog()
×
1473
{
1474
    // Get the highest common counter number and start from there (remove all others)
1475
    unsigned maxCtr = 0;
×
1476
    for(const AsyncLog& log : asyncLogs)
×
1477
    {
1478
        if(!log.randEntries.empty() && log.randEntries[0].counter > maxCtr)
×
1479
            maxCtr = log.randEntries[0].counter;
×
1480
    }
1481
    // Number of entries = max(asyncLogs[0..n].randEntries.size())
1482
    unsigned numEntries = 0;
×
1483
    for(AsyncLog& log : asyncLogs)
×
1484
    {
1485
        auto it = helpers::find_if(log.randEntries, [maxCtr](const auto& e) { return e.counter == maxCtr; });
×
1486
        log.randEntries.erase(log.randEntries.begin(), it);
×
1487
        if(numEntries < log.randEntries.size())
×
1488
            numEntries = log.randEntries.size();
×
1489
    }
1490
    // No entries :(
1491
    if(numEntries == 0 || asyncLogs.size() < 2u)
×
1492
        return "";
×
1493

1494
    // count identical lines
1495
    unsigned numIdentical = 0;
×
1496
    for(unsigned i = 0; i < numEntries; i++)
×
1497
    {
1498
        bool isIdentical = true;
×
1499
        if(i >= asyncLogs[0].randEntries.size())
×
1500
            break;
×
1501
        const RandomEntry& refEntry = asyncLogs[0].randEntries[i];
×
1502
        for(const AsyncLog& log : asyncLogs)
×
1503
        {
1504
            if(i >= log.randEntries.size())
×
1505
            {
1506
                isIdentical = false;
×
1507
                break;
×
1508
            }
1509
            const RandomEntry& curEntry = log.randEntries[i];
×
1510
            if(curEntry.maxExcl != refEntry.maxExcl || curEntry.rngState != refEntry.rngState
×
1511
               || curEntry.objId != refEntry.objId)
×
1512
            {
1513
                isIdentical = false;
×
1514
                break;
×
1515
            }
1516
        }
1517
        if(isIdentical)
×
1518
            ++numIdentical;
×
1519
        else
1520
            break;
×
1521
    }
1522

1523
    LOG.write(_("There are %1% identical async log entries.\n")) % numIdentical;
×
1524

1525
    bfs::path filePath =
1526
      RTTRCONFIG.ExpandPath(s25::folders::logs) / (s25util::Time::FormatTime("async_%Y-%m-%d_%H-%i-%s") + "Server.log");
×
1527

1528
    // open async log
1529
    bnw::ofstream file(filePath);
×
1530

1531
    if(file)
×
1532
    {
1533
        file << "Map: " << mapinfo.title << std::endl;
×
1534
        file << std::setfill(' ');
×
1535
        for(const AsyncLog& log : asyncLogs)
×
1536
        {
1537
            const JoinPlayerInfo& plInfo = playerInfos.at(log.playerId);
×
1538
            file << "Player " << std::setw(2) << unsigned(log.playerId) << (plInfo.isHost ? '#' : ' ') << "\t\""
×
1539
                 << plInfo.name << '"' << std::endl;
×
1540
            file << "System info: " << log.addData << std::endl;
×
1541
            file << "\tChecksum: " << std::setw(0) << log.checksum << std::endl;
×
1542
        }
1543
        for(const AsyncLog& log : asyncLogs)
×
1544
            file << "Checksum " << std::setw(2) << unsigned(log.playerId) << std::setw(0) << ": " << log.checksum
×
1545
                 << std::endl;
×
1546

1547
        // print identical lines, they help in tracing the bug
1548
        for(unsigned i = 0; i < numIdentical; i++)
×
1549
            file << "[ I ]: " << asyncLogs[0].randEntries[i] << "\n";
×
1550
        for(unsigned i = numIdentical; i < numEntries; i++)
×
1551
        {
1552
            for(const AsyncLog& log : asyncLogs)
×
1553
            {
1554
                if(i < log.randEntries.size())
×
1555
                    file << "[C" << std::setw(2) << unsigned(log.playerId) << std::setw(0)
×
1556
                         << "]: " << log.randEntries[i] << '\n';
×
1557
            }
1558
        }
1559

1560
        LOG.write(_("Async log saved at %1%\n")) % filePath;
×
1561
        return filePath;
×
1562
    } else
1563
    {
1564
        LOG.write(_("Failed to save async log at %1%\n")) % filePath;
×
1565
        return "";
×
1566
    }
1567
}
1568

1569
void GameServer::SendAsyncLog(const bfs::path& asyncLogFilePath)
×
1570
{
1571
    if(SETTINGS.global.submit_debug_data == 1
×
1572
#ifdef _WIN32
1573
       || (MessageBoxW(nullptr,
1574
                       boost::nowide::widen(_("The game clients are out of sync. Would you like to send debug "
1575
                                              "information to RttR to help us avoiding this in "
1576
                                              "the future? Thank you very much!"))
1577
                         .c_str(),
1578
                       boost::nowide::widen(_("Error")).c_str(),
1579
                       MB_YESNO | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND)
1580
           == IDYES)
1581
#endif
1582
    )
1583
    {
1584
        DebugInfo di;
×
1585
        LOG.write(_("Sending async logs %1%.\n")) % (di.SendAsyncLog(asyncLogFilePath) ? "succeeded" : "failed");
×
1586

1587
        di.SendReplay();
×
1588
    }
1589
}
×
1590

1591
void GameServer::CheckAndSetColor(unsigned playerIdx, unsigned newColor)
×
1592
{
1593
    RTTR_Assert(playerIdx < playerInfos.size());
×
1594
    // If either of those fails we may not find a valid color!
1595
    static_assert(PLAYER_COLORS.size() >= MAX_PLAYERS, "Not enough player colors defined!");
NEW
1596
    RTTR_Assert(playerInfos.size() <= PLAYER_COLORS.size());
×
1597

1598
    // Get colors used by other players
1599
    std::set<unsigned> takenColors;
×
1600
    for(unsigned p = 0; p < playerInfos.size(); ++p)
×
1601
    {
1602
        // Skip self
1603
        if(p == playerIdx)
×
1604
            continue;
×
1605

1606
        JoinPlayerInfo& otherPlayer = playerInfos[p];
×
1607
        if(otherPlayer.isUsed())
×
1608
            takenColors.insert(otherPlayer.color);
×
1609
    }
1610

1611
    // Look for a unique color
1612
    int newColorIdx = JoinPlayerInfo::GetColorIdx(newColor);
×
1613
    while(helpers::contains(takenColors, newColor))
×
1614
        newColor = PLAYER_COLORS[(++newColorIdx) % PLAYER_COLORS.size()];
×
1615

NEW
1616
    JoinPlayerInfo& player = playerInfos[playerIdx];
×
1617
    if(player.color == newColor)
×
1618
        return;
×
UNCOV
1619
    player.color = newColor;
×
1620

1621
    SendToAll(GameMessage_Player_Color(playerIdx, player.color));
×
1622
    LOG.writeToFile("SERVER >>> BROADCAST: NMS_PLAYER_TOGGLECOLOR(%d, %d)\n") % playerIdx % player.color;
×
1623
}
1624

1625
bool GameServer::OnGameMessage(const GameMessage_Player_Swap& msg)
×
1626
{
1627
    if(state != ServerState::Game && state != ServerState::Config)
×
1628
        return true;
×
1629
    int targetPlayer = GetTargetPlayer(msg);
×
1630
    if(targetPlayer < 0)
×
1631
        return true;
×
1632
    auto player1 = static_cast<uint8_t>(targetPlayer);
×
1633
    if(player1 == msg.player2 || msg.player2 >= playerInfos.size())
×
1634
        return true;
×
1635

1636
    SwapPlayer(player1, msg.player2);
×
1637
    return true;
×
1638
}
1639

1640
bool GameServer::OnGameMessage(const GameMessage_Player_SwapConfirm& msg)
×
1641
{
1642
    GameServerPlayer* player = GetNetworkPlayer(msg.senderPlayerID);
×
1643
    if(!player)
×
1644
        return true;
×
1645
    for(auto it = player->getPendingSwaps().begin(); it != player->getPendingSwaps().end(); ++it)
×
1646
    {
1647
        if(it->first == msg.player && it->second == msg.player2)
×
1648
        {
1649
            player->getPendingSwaps().erase(it);
×
1650
            break;
×
1651
        }
1652
    }
1653
    return true;
×
1654
}
1655

1656
void GameServer::SwapPlayer(const uint8_t player1, const uint8_t player2)
×
1657
{
1658
    // TODO: Swapping the player messes up the ids because our IDs are indizes. Usually this works because we use the
1659
    // sender player ID for messages received by the server and set the right ID for messages sent by the server.
1660
    // However (currently only) the host may send messages for another player. Those will not get the adjusted ID till
1661
    // he gets the swap message. So there is a short time, where the messages may be executed for the wrong player.
1662
    // Idea: Use actual IDs for players in messages (unique)
1663
    if(state == ServerState::Config)
×
1664
    {
1665
        // Swap player during match-making
1666
        // Swap everything
1667
        using std::swap;
1668
        swap(playerInfos[player1], playerInfos[player2]);
×
1669
        // In savegames some things cannot be changed
1670
        if(mapinfo.type == MapType::Savegame)
×
1671
            playerInfos[player1].FixSwappedSaveSlot(playerInfos[player2]);
×
1672
    } else if(state == ServerState::Game)
×
1673
    {
1674
        // Ingame we can only switch to a KI
1675
        if(playerInfos[player1].ps != PlayerState::Occupied || playerInfos[player2].ps != PlayerState::AI)
×
1676
            return;
×
1677

1678
        LOG.write("GameServer::ChangePlayer %i - %i \n") % unsigned(player1) % unsigned(player2);
×
1679
        using std::swap;
1680
        swap(playerInfos[player2].ps, playerInfos[player1].ps);
×
1681
        swap(playerInfos[player2].aiInfo, playerInfos[player1].aiInfo);
×
1682
        swap(playerInfos[player2].isHost, playerInfos[player1].isHost);
×
1683
    }
1684
    // Change ids of network players (if any). Get both first!
1685
    GameServerPlayer* newPlayer = GetNetworkPlayer(player2);
×
1686
    GameServerPlayer* oldPlayer = GetNetworkPlayer(player1);
×
1687
    if(newPlayer)
×
1688
        newPlayer->playerId = player1;
×
1689
    if(oldPlayer)
×
1690
        oldPlayer->playerId = player2;
×
1691
    SendToAll(GameMessage_Player_Swap(player1, player2));
×
1692
    const auto pSwap = std::make_pair(player1, player2);
×
1693
    for(GameServerPlayer& player : networkPlayers)
×
1694
    {
1695
        if(!player.isActive())
×
1696
            continue;
×
1697
        player.getPendingSwaps().push_back(pSwap);
×
1698
    }
1699
}
1700

1701
GameServerPlayer* GameServer::GetNetworkPlayer(unsigned playerId)
×
1702
{
1703
    for(GameServerPlayer& player : networkPlayers)
×
1704
    {
1705
        if(player.playerId == playerId)
×
1706
            return &player;
×
1707
    }
1708
    return nullptr;
×
1709
}
1710

1711
void GameServer::SetPaused(bool paused)
×
1712
{
1713
    if(framesinfo.isPaused == paused)
×
1714
        return;
×
1715
    framesinfo.isPaused = paused;
×
1716
    SendToAll(GameMessage_Pause(framesinfo.isPaused));
×
1717
    for(GameServerPlayer& player : networkPlayers)
×
1718
        player.setNotLagging();
×
1719
}
1720

1721
JoinPlayerInfo& GameServer::GetJoinPlayer(unsigned playerIdx)
×
1722
{
1723
    return playerInfos.at(playerIdx);
×
1724
}
1725

1726
bool GameServer::IsHost(unsigned playerIdx) const
×
1727
{
1728
    return playerIdx < playerInfos.size() && playerInfos[playerIdx].isHost;
×
1729
}
1730

1731
int GameServer::GetTargetPlayer(const GameMessageWithPlayer& msg)
×
1732
{
1733
    if(msg.player != 0xFF)
×
1734
    {
1735
        if(msg.player < playerInfos.size() && (msg.player == msg.senderPlayerID || IsHost(msg.senderPlayerID)))
×
1736
        {
1737
            GameServerPlayer* networkPlayer = GetNetworkPlayer(msg.senderPlayerID);
×
1738
            if(networkPlayer->isActive())
×
1739
                return msg.player;
×
1740
            unsigned result = msg.player;
×
1741
            // Apply pending swaps
1742
            for(auto& pSwap : networkPlayer->getPendingSwaps()) //-V522
×
1743
            {
1744
                if(pSwap.first == result)
×
1745
                    result = pSwap.second;
×
1746
                else if(pSwap.second == result)
×
1747
                    result = pSwap.first;
×
1748
            }
1749
            return result;
×
1750
        }
1751
    } else if(msg.senderPlayerID < playerInfos.size())
×
1752
        return msg.senderPlayerID;
×
1753
    return -1;
×
1754
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc