Add kart id checking so that only peer with correct kart can control

This commit is contained in:
Benau 2018-09-14 21:42:35 +08:00
parent a53307cc14
commit 83a456e835
3 changed files with 21 additions and 2 deletions

View File

@ -184,7 +184,13 @@ void GameProtocol::handleControllerAction(Event *event)
//rewind_delta = not_rewound - cur_ticks;
}
uint8_t kart_id = data.getUInt8();
assert(kart_id < World::getWorld()->getNumKarts());
if (NetworkConfig::get()->isServer() &&
!event->getPeer()->availableKartID(kart_id))
{
Log::warn("GameProtocol", "Wrong kart id %d from %s.",
kart_id, event->getPeer()->getAddress().toString().c_str());
return;
}
PlayerAction action = (PlayerAction)(data.getUInt8());
int value = data.getUInt32();

View File

@ -496,13 +496,17 @@ void ServerLobby::asynchronousUpdate()
m_game_setup->sortPlayersForGrandPrix();
m_game_setup->sortPlayersForTeamGame();
auto players = m_game_setup->getConnectedPlayers();
for (auto& player : players)
player->getPeer()->clearAvailableKartIDs();
NetworkString* load_world = getNetworkString();
load_world->setSynchronous(true);
load_world->addUInt8(LE_LOAD_WORLD).encodeString(std::get<0>(result))
.addUInt8(std::get<1>(result)).addUInt8(std::get<2>(result))
.addUInt8((uint8_t)players.size());
for (auto player : players)
for (unsigned i = 0; i < players.size(); i++)
{
std::shared_ptr<NetworkPlayerProfile>& player = players[i];
player->getPeer()->addAvailableKartID(i);
load_world->encodeString(player->getName())
.addUInt32(player->getHostId())
.addFloat(player->getDefaultKartColor())

View File

@ -89,6 +89,8 @@ protected:
std::atomic<uint32_t> m_average_ping;
std::set<unsigned> m_available_kart_ids;
public:
STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id);
// ------------------------------------------------------------------------
@ -181,6 +183,13 @@ public:
bool isWaitingForGame() const { return m_waiting_for_game.load(); }
// ------------------------------------------------------------------------
bool isDisconnected() const { return m_disconnected.load(); }
// ------------------------------------------------------------------------
void clearAvailableKartIDs() { m_available_kart_ids.clear(); }
// ------------------------------------------------------------------------
void addAvailableKartID(unsigned id) { m_available_kart_ids.insert(id); }
// ------------------------------------------------------------------------
bool availableKartID(unsigned id)
{ return m_available_kart_ids.find(id) != m_available_kart_ids.end(); }
}; // STKPeer
#endif // STK_PEER_HPP