]> git.localhorst.tv Git - blank.git/commitdiff
move serverside joining from state to server
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 7 Oct 2015 15:38:13 +0000 (17:38 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 7 Oct 2015 15:38:13 +0000 (17:38 +0200)
src/server/Server.hpp
src/server/ServerState.cpp
src/server/ServerState.hpp
src/server/net.cpp

index 372a947ef65fe0e14c1aa209d091036fcd47ed6b..c12b86aa75ba0b4d9e17e726aab2c54c44474491 100644 (file)
@@ -2,6 +2,7 @@
 #define BLANK_SERVER_SERVER_HPP
 
 #include "../app/Config.hpp"
+#include "../world/World.hpp"
 #include "../world/WorldManipulator.hpp"
 
 #include <list>
@@ -10,8 +11,9 @@
 
 namespace blank {
 
+class ChunkIndex;
 class CompositeModel;
-class World;
+class Player;
 class WorldSave;
 
 namespace server {
@@ -22,7 +24,7 @@ class Server
 : public WorldManipulator {
 
 public:
-       Server(const Config::Network &, World &, const WorldSave &);
+       Server(const Config::Network &, World &, const World::Config &, const WorldSave &);
        ~Server();
 
        void Handle();
@@ -39,6 +41,8 @@ public:
        bool HasPlayerModel() const noexcept;
        const CompositeModel &GetPlayerModel() const noexcept;
 
+       Player *JoinPlayer(const std::string &name);
+
        void SetBlock(Chunk &, int, const Block &) override;
 
 private:
@@ -52,6 +56,7 @@ private:
        std::list<ClientConnection> clients;
 
        World &world;
+       ChunkIndex &spawn_index;
        const WorldSave &save;
        const CompositeModel *player_model;
 
index e16872ffe74c094030ace36c87a92a7c566cfcfa..fc0da820e4513aeef509a032454c03497e571faa 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "../app/Environment.hpp"
 #include "../app/TextureIndex.hpp"
+#include "../io/WorldSave.hpp"
 #include "../net/io.hpp"
 
 #include <iostream>
@@ -20,12 +21,11 @@ ServerState::ServerState(
 : env(env)
 , block_types()
 , world(block_types, wc)
-, spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , generator(gc)
 , chunk_loader(world.Chunks(), generator, ws)
 , skeletons()
 , spawner(world, skeletons, env.rng)
-, server(config.net, world, ws)
+, server(config.net, world, wc, ws)
 , loop_timer(16) {
        TextureIndex tex_index;
        env.loader.LoadBlockTypes("default", block_types, tex_index);
@@ -40,12 +40,18 @@ ServerState::ServerState(
 }
 
 ServerState::~ServerState() {
-       world.Chunks().UnregisterIndex(spawn_index);
+
 }
 
 
 void ServerState::Handle(const SDL_Event &event) {
        if (event.type == SDL_QUIT) {
+               std::cout << "saving remaining chunks" << std::endl;
+               for (Chunk &chunk : world.Chunks()) {
+                       if (chunk.ShouldUpdateSave()) {
+                               chunk_loader.SaveFile().Write(chunk);
+                       }
+               }
                env.state.PopAll();
        }
 }
index e22f9b1e3c3f58a3825fa48a973f21e6f764fc58..3a4851470edb8a1cecf555e52048a4a971fcd45d 100644 (file)
@@ -7,7 +7,6 @@
 #include "../app/State.hpp"
 #include "../model/Skeletons.hpp"
 #include "../world/BlockTypeRegistry.hpp"
-#include "../world/ChunkIndex.hpp"
 #include "../world/ChunkLoader.hpp"
 #include "../world/Generator.hpp"
 #include "../world/World.hpp"
@@ -42,7 +41,6 @@ private:
        HeadlessEnvironment &env;
        BlockTypeRegistry block_types;
        World world;
-       ChunkIndex &spawn_index;
        Generator generator;
        ChunkLoader chunk_loader;
        Skeletons skeletons;
index 16aa1cb004733865337976e72c51c78da6382e87..1e60ff6ead06d3dfd7905946866d4d4920ead0fd 100644 (file)
@@ -395,11 +395,6 @@ void ClientConnection::AttachPlayer(Player &player) {
        DetachPlayer();
        input.reset(new DirectInput(server.GetWorld(), player, server));
        PlayerEntity().Ref();
-       if (server.GetWorldSave().Exists(player)) {
-               server.GetWorldSave().Read(player);
-       } else {
-               // TODO: spawn
-       }
 
        old_base = PlayerChunks().Base();
        Chunk::Pos begin = PlayerChunks().CoordsBegin();
@@ -411,6 +406,7 @@ void ClientConnection::AttachPlayer(Player &player) {
                        }
                }
        }
+       // TODO: should the server do this?
        if (HasPlayerModel()) {
                GetPlayerModel().Instantiate(PlayerEntity().GetModel());
        }
@@ -489,7 +485,7 @@ void ClientConnection::On(const Packet::Login &pack) {
        string name;
        pack.ReadPlayerName(name);
 
-       Player *new_player = server.GetWorld().AddPlayer(name);
+       Player *new_player = server.JoinPlayer(name);
 
        if (new_player) {
                // success!
@@ -567,11 +563,16 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept {
 }
 
 
-Server::Server(const Config::Network &conf, World &world, const WorldSave &save)
+Server::Server(
+       const Config::Network &conf,
+       World &world,
+       const World::Config &wc,
+       const WorldSave &save)
 : serv_sock(nullptr)
 , serv_pack{ -1, nullptr, 0 }
 , clients()
 , world(world)
+, spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , save(save)
 , player_model(nullptr) {
        serv_sock = SDLNet_UDP_Open(conf.port);
@@ -584,6 +585,7 @@ Server::Server(const Config::Network &conf, World &world, const WorldSave &save)
 }
 
 Server::~Server() {
+       world.Chunks().UnregisterIndex(spawn_index);
        delete[] serv_pack.data;
        SDLNet_UDP_Close(serv_sock);
 }
@@ -655,6 +657,22 @@ const CompositeModel &Server::GetPlayerModel() const noexcept {
        return *player_model;
 }
 
+Player *Server::JoinPlayer(const string &name) {
+       if (spawn_index.MissingChunks() > 0) {
+               return nullptr;
+       }
+       Player *player = world.AddPlayer(name);
+       if (!player) {
+               return nullptr;
+       }
+       if (save.Exists(*player)) {
+               save.Read(*player);
+       } else {
+               // TODO: spawn
+       }
+       return player;
+}
+
 void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
        chunk.SetBlock(index, block);
        // TODO: batch chunk changes