Commit 866c144b by Anselme

bug fixes in the server

parent a404fe16
......@@ -80,7 +80,7 @@ sf::Packet& operator >>(sf::Packet& packet, MapKoban& map)
return packet;
}
MapKoban* MapKoban::getTestMap()
const MapKoban& MapKoban::getTestMap()
{
static MapKoban* map = nullptr;
if(map == nullptr)
......@@ -94,5 +94,5 @@ MapKoban* MapKoban::getTestMap()
map->addCrate(3,5);
map->addCrate(7,5);
}
return map;
return *map;
}
......@@ -38,7 +38,7 @@ public:
friend sf::Packet& operator <<(sf::Packet& packet, const MapKoban& map);
friend sf::Packet& operator >>(sf::Packet& packet, MapKoban& map);
static MapKoban* getTestMap();
static const MapKoban &getTestMap();
};
......
......@@ -42,6 +42,16 @@ void MapInstance::update()
}
}
void MapInstance::setMapData(const MapKoban &new_map)
{
m_map = new_map;
sf::Packet packet;
packet << MAP_INIT << m_map;
SocketHandler* socket_handler = m_server->getSocketHandler();
if(socket_handler != nullptr)
socket_handler->broadcastMessage(packet);
}
bool MapInstance::isAvailable(sf::String id)
{
return (m_characters.find(id) == m_characters.end());
......@@ -74,7 +84,7 @@ bool MapInstance::teleportPlayer(const sf::String &id, sf::Vector2i pos)
player_add.id = id;
player_add.color = player->color;
player_add.pos = pos;
player_add.turn_id = m_map->getTurn();
player_add.turn_id = m_map.getTurn();
sf::Packet packet;
packet << PLAYER_REMOVE;
packet << player_remove;
......@@ -135,7 +145,7 @@ void MapInstance::filterMapDiff()
charac->position = target;
if(p_diff.action == PUSHING)
filteredDiff->moves.push_back({(unsigned int)m_map->findCrate(target + dir),dir});
filteredDiff->moves.push_back({(unsigned int)m_map.findCrate(target + dir),dir});
break;
}
case BLOCKED:
......@@ -151,24 +161,24 @@ void MapInstance::filterMapDiff()
PlayerAction MapInstance::resolveMove(sf::Vector2i pos, Direction dir)
{
sf::Vector2i target = pos + dir;
switch (m_map->getTile(target)){
switch (m_map.getTile(target)){
case FLOOR:
{
//check if there is a crate on this tile
int crateId = m_map->findCrate(target);
int crateId = m_map.findCrate(target);
sf::String playerId = findPlayer(target);
if(crateId != NO_CRATE)
{
sf::Vector2i dest = target+dir;
switch(m_map->getTile(dest))
switch(m_map.getTile(dest))
{
case FLOOR:
{
int targetCrateId = m_map->findCrate(dest);
int targetCrateId = m_map.findCrate(dest);
sf::String targetPlayerId = findPlayer(dest);
if(targetCrateId == NO_CRATE && targetPlayerId.isEmpty())
{
m_map->getCrate(crateId).position = dest;
m_map.getCrate(crateId).position = dest;
return PUSHING;
}else
return BLOCKED;
......
......@@ -26,7 +26,7 @@ namespace std {
class MapInstance
{
Server* m_server;
MapKoban* m_map;
MapKoban m_map;
MapDiff m_diff;
std::unordered_map<sf::String, CharacterData*> m_characters;
unsigned int m_current_turn_id;
......@@ -38,17 +38,17 @@ public:
~MapInstance();
void update();
void setMapData(MapKoban* new_map){m_map = new_map;}
MapKoban* getMapData(){return m_map;}
int getWidth(){return m_map->getWidth();}
int getHeight(){return m_map->getHeight();}
void setMapData(const MapKoban &new_map);
MapKoban& getMapData(){return m_map;}
int getWidth(){return m_map.getWidth();}
int getHeight(){return m_map.getHeight();}
void addSpawn(sf::Vector2i spawnPos) { m_spawns.push_back(spawnPos); }
void resetSpawns() { m_spawns.clear(); }
void respawnPlayers();
sf::Vector2i getRandomSpawn() { return m_spawns.size() ? m_spawns[rand()%m_spawns.size()] : sf::Vector2i(1, 1); }
TileType getTile(sf::Vector2i pos){return m_map->getTile(pos);}
TileType getTile(sf::Vector2i pos){return m_map.getTile(pos);}
void addCharacter(CharacterData* charac);
void removeCharacter(const sf::String &id);
......
......@@ -9,9 +9,11 @@
#include <mapinstance.h>
#include <sockethandler.h>
Server::Server(unsigned short port)
Server::Server(unsigned short port) :
m_map(new MapInstance(this)),
m_socket_handler(nullptr),
m_socketUpdaterRunning(nullptr)
{
m_map = new MapInstance(this);
resetMap();
m_socket_handler = new SocketHandler(this,port);
......
......@@ -112,7 +112,7 @@ void SocketHandler::handleSocketMessage(sf::TcpSocket& socket)
auth_answer.isOk = false;
packet_out << AUTH_ANSWER << auth_answer;
if(auth_answer.isOk){
packet_out << MAP_INIT << *map->getMapData();
packet_out << MAP_INIT << map->getMapData();
packet_out = map->packCharacterData(packet_out);
}
safeSend(socket,packet_out);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment