summaryrefslogtreecommitdiff
path: root/src/player.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2014-05-30 16:04:07 -0400
committerShadowNinja <shadowninja@minetest.net>2014-06-23 15:45:59 -0400
commit7e6db1b80344a519e53a9967a159c8d3585a9b9d (patch)
treedcdd53f2f558b8c57ca4b58a754e7ed0be015c6c /src/player.cpp
parent50127510e72732b069be040db2d2bbdad4d0be1c (diff)
downloadminetest-7e6db1b80344a519e53a9967a159c8d3585a9b9d.tar.gz
minetest-7e6db1b80344a519e53a9967a159c8d3585a9b9d.tar.bz2
minetest-7e6db1b80344a519e53a9967a159c8d3585a9b9d.zip
Only keep players loaded while they're connected
Diffstat (limited to 'src/player.cpp')
-rw-r--r--src/player.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/player.cpp b/src/player.cpp
index 4dadf26d0..78ba17e89 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -283,6 +283,72 @@ void Player::clearHud()
}
}
+
+void RemotePlayer::save(const std::string &savedir)
+{
+ bool newplayer = true;
+
+ /* We have to iterate through all files in the players directory
+ * and check their player names because some file systems are not
+ * case-sensitive and player names are case-sensitive.
+ */
+
+ // A player to deserialize files into to check their names
+ RemotePlayer testplayer(m_gamedef);
+
+ std::vector<fs::DirListNode> player_files = fs::GetDirListing(savedir);
+ for(u32 i = 0; i < player_files.size(); i++) {
+ if (player_files[i].dir || player_files[i].name[0] == '.') {
+ continue;
+ }
+
+ // Full path to this file
+ std::string path = savedir + "/" + player_files[i].name;
+
+ // Open file and deserialize
+ std::ifstream is(path.c_str(), std::ios_base::binary);
+ if (!is.good()) {
+ infostream << "Failed to read " << path << std::endl;
+ continue;
+ }
+ testplayer.deSerialize(is, player_files[i].name);
+
+ if (strcmp(testplayer.getName(), m_name) == 0) {
+ // Open file and serialize
+ std::ostringstream ss(std::ios_base::binary);
+ serialize(ss);
+ if (!fs::safeWriteToFile(path, ss.str())) {
+ infostream << "Failed to write " << path << std::endl;
+ }
+ newplayer = false;
+ break;
+ }
+ }
+
+ if (newplayer) {
+ bool found = false;
+ std::string path = savedir + "/" + m_name;
+ for (u32 i = 0; i < 1000; i++) {
+ if (!fs::PathExists(path)) {
+ found = true;
+ break;
+ }
+ path = savedir + "/" + m_name + itos(i);
+ }
+ if (!found) {
+ infostream << "Didn't find free file for player " << m_name << std::endl;
+ return;
+ }
+
+ // Open file and serialize
+ std::ostringstream ss(std::ios_base::binary);
+ serialize(ss);
+ if (!fs::safeWriteToFile(path, ss.str())) {
+ infostream << "Failed to write " << path << std::endl;
+ }
+ }
+}
+
/*
RemotePlayer
*/
@@ -292,3 +358,4 @@ void RemotePlayer::setPosition(const v3f &position)
if(m_sao)
m_sao->setBasePosition(position);
}
+