From 7e6db1b80344a519e53a9967a159c8d3585a9b9d Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 30 May 2014 16:04:07 -0400 Subject: Only keep players loaded while they're connected --- src/environment.cpp | 231 +++++++++++++++------------------------------------- 1 file changed, 67 insertions(+), 164 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/environment.cpp b/src/environment.cpp index 6bbc715d0..91f5ea2b6 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -100,6 +100,18 @@ void Environment::removePlayer(u16 peer_id) } } +void Environment::removePlayer(const char *name) +{ + for (std::list::iterator it = m_players.begin(); + it != m_players.end(); ++it) { + if (strcmp((*it)->getName(), name) == 0) { + delete *it; + m_players.erase(it); + return; + } + } +} + Player * Environment::getPlayer(u16 peer_id) { for(std::list::iterator i = m_players.begin(); @@ -332,10 +344,12 @@ void ActiveBlockList::update(std::list &active_positions, */ ServerEnvironment::ServerEnvironment(ServerMap *map, - GameScripting *scriptIface, IGameDef *gamedef): + GameScripting *scriptIface, IGameDef *gamedef, + const std::string &path_world) : m_map(map), m_script(scriptIface), m_gamedef(gamedef), + m_path_world(path_world), m_send_recommended_timer(0), m_active_block_interval_overload_skip(0), m_game_time(0), @@ -401,196 +415,85 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 return true; } -void ServerEnvironment::serializePlayers(const std::string &savedir) +void ServerEnvironment::saveLoadedPlayers() { - std::string players_path = savedir + "/players"; + std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - std::set saved_players; - - std::vector player_files = fs::GetDirListing(players_path); - for(u32 i=0; icheckModified()) - { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - player->serialize(ss); - if(!fs::safeWriteToFile(path, ss.str())) - { - infostream<<"Failed to write "<::iterator it = m_players.begin(); + it != m_players.end(); + ++it) { + RemotePlayer *player = static_cast(*it); + if (player->checkModified()) { + player->save(players_path); } } +} - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { - Player *player = *i; - if(saved_players.find(player) != saved_players.end()) - { - /*infostream<<"Player "<getName() - <<" was already saved."<getName(); - // Don't save unnamed player - if(playername == "") - { - //infostream<<"Not saving unnamed player."<getName()<<" to " - <serialize(ss); - if(!fs::safeWriteToFile(path, ss.str())) - { - infostream<<"Failed to write "<(getPlayer(playername.c_str())); + if (player) { + player->save(players_path); } - - //infostream<<"Saved "<(getPlayer(playername.c_str())); + bool newplayer = false; + bool foundplayer = false; + if (!player) { + player = new RemotePlayer(m_gamedef); + newplayer = true; + } std::vector player_files = fs::GetDirListing(players_path); - for(u32 i=0; ideSerialize(is, player_files[i].name); - /*infostream<<"Loaded test player with name "<getName(), PLAYERNAME_ALLOWED_CHARS)) { + infostream << "Not loading player with invalid name: " + << player->getName() << std::endl; + continue; } - // Load player - { - verbosestream<<"Reading player "<deSerialize(is, player_files[i].name); + if (player->getName() == playername) { + // We found our player + foundplayer = true; + break; } - if(newplayer) - { - addPlayer(player); - } } + if (!foundplayer) { + return NULL; + } + if (newplayer) { + addPlayer(player); + } + return player; } -void ServerEnvironment::saveMeta(const std::string &savedir) +void ServerEnvironment::saveMeta() { - std::string path = savedir + "/env_meta.txt"; + std::string path = m_path_world + DIR_DELIM "env_meta.txt"; // Open file and serialize std::ostringstream ss(std::ios_base::binary); @@ -609,9 +512,9 @@ void ServerEnvironment::saveMeta(const std::string &savedir) } } -void ServerEnvironment::loadMeta(const std::string &savedir) +void ServerEnvironment::loadMeta() { - std::string path = savedir + "/env_meta.txt"; + std::string path = m_path_world + DIR_DELIM "env_meta.txt"; // Open file and deserialize std::ifstream is(path.c_str(), std::ios_base::binary); -- cgit v1.2.3