]> git.localhorst.tv Git - blank.git/commitdiff
(shabby) client side handling of messages
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 20 Oct 2015 15:40:30 +0000 (17:40 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 21 Oct 2015 07:18:22 +0000 (09:18 +0200)
src/client/Client.hpp
src/client/InteractiveState.hpp
src/client/MasterState.hpp
src/client/client.cpp
src/client/net.cpp
src/standalone/MasterState.cpp

index a5f85e5f58c70ca4a69c02753a6c68278010abdf..e124ae51200dd8d169a6a000bf98d6489d39a846 100644 (file)
@@ -37,6 +37,10 @@ public:
                float yaw,
                std::uint8_t actions,
                std::uint8_t slot);
+       std::uint16_t SendMessage(
+               std::uint8_t type,
+               std::uint32_t ref,
+               const std::string &msg);
 
 private:
        void HandlePacket(const UDPpacket &);
index 49c54745f8f88c57b94af1b52d6a7f72fb12fd68..dc84199c6be49025b35fe42a1feb1e6248d5bd8d 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "ChunkReceiver.hpp"
 #include "NetworkedInput.hpp"
+#include "../app/ChatState.hpp"
 #include "../app/IntervalTimer.hpp"
 #include "../audio/SoundBank.hpp"
 #include "../graphics/SkyBox.hpp"
@@ -31,7 +32,8 @@ class MasterState;
 
 class InteractiveState
 : public State
-, public ClientController {
+, public ClientController
+, public ChatState::Responder {
 
 public:
        explicit InteractiveState(MasterState &, std::uint32_t player_id);
@@ -52,6 +54,7 @@ public:
        void Handle(const Packet::EntityUpdate &);
        void Handle(const Packet::PlayerCorrection &);
        void Handle(const Packet::BlockUpdate &);
+       void Handle(const Packet::Message &);
 
        void SetAudio(bool) override;
        void SetVideo(bool) override;
@@ -59,6 +62,8 @@ public:
        void SetDebug(bool) override;
        void Exit() override;
 
+       void OnLineSubmit(const std::string &) override;
+
 private:
        /// flag entity as updated by given packet
        /// returns false if the update should be ignored
@@ -89,6 +94,8 @@ private:
        };
        std::map<std::uint32_t, UpdateStatus> update_status;
 
+       ChatState chat;
+
 };
 
 }
index a29ff83e68e668102fdf4b4bb1d7ab3fd732744f..78ab3703b756539bad02d8f26ce4511efd4886ff 100644 (file)
@@ -61,6 +61,7 @@ public:
        void On(const Packet::ChunkBegin &) override;
        void On(const Packet::ChunkData &) override;
        void On(const Packet::BlockUpdate &) override;
+       void On(const Packet::Message &) override;
 
 private:
        Environment &env;
index 455550afeccc27500120409a1b8162be50a5fdc6..44163f6cc17b74b9cb548b3bd93242e6028b6a90 100644 (file)
@@ -60,7 +60,8 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , chunk_renderer(player.GetChunks())
 , loop_timer(16)
 , sky(master.GetEnv().loader.LoadCubeMap("skybox"))
-, update_status() {
+, update_status()
+, chat(master.GetEnv(), *this, *this) {
        if (!save.Exists()) {
                save.Write(master.GetWorldConf());
        }
@@ -86,7 +87,13 @@ void InteractiveState::OnEnter() {
 void InteractiveState::Handle(const SDL_Event &event) {
        switch (event.type) {
                case SDL_KEYDOWN:
-                       interface.HandlePress(event.key);
+                       // TODO: move to interface
+                       if (event.key.keysym.sym == SDLK_RETURN) {
+                               master.GetEnv().state.Push(&chat);
+                               hud.KeepMessages(true);
+                       } else {
+                               interface.HandlePress(event.key);
+                       }
                        break;
                case SDL_KEYUP:
                        interface.HandleRelease(event.key);
@@ -265,6 +272,12 @@ void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
        }
 }
 
+void InteractiveState::Handle(const Packet::Message &pack) {
+       string msg;
+       pack.ReadMessage(msg);
+       hud.PostMessage(msg);
+}
+
 void InteractiveState::SetAudio(bool b) {
        master.GetConfig().audio.enabled = b;
        if (b) {
@@ -306,6 +319,10 @@ void InteractiveState::Exit() {
        master.Quit();
 }
 
+void InteractiveState::OnLineSubmit(const string &line) {
+       master.GetClient().SendMessage(1, 0, line);
+}
+
 
 MasterState::MasterState(
        Environment &env,
@@ -458,5 +475,15 @@ void MasterState::On(const Packet::BlockUpdate &pack) {
        state->Handle(pack);
 }
 
+void MasterState::On(const Packet::Message &pack) {
+       if (state) {
+               state->Handle(pack);
+       } else {
+               string msg;
+               pack.ReadMessage(msg);
+               cout << "got message before interface was created: " << msg << endl;
+       }
+}
+
 }
 }
index c3bbb67b40be4d8c7b81ab3d67a62f3ed0cff81b..36d598bd9b2ba76681c509483a052f22717d6a6b 100644 (file)
@@ -306,6 +306,18 @@ uint16_t Client::SendPart() {
        return conn.Send(client_pack, client_sock);
 }
 
+uint16_t Client::SendMessage(
+       uint8_t type,
+       uint32_t ref,
+       const string &msg
+) {
+       auto pack = Packet::Make<Packet::Message>(client_pack);
+       pack.WriteType(type);
+       pack.WriteReferral(ref);
+       pack.WriteMessage(msg);
+       client_pack.len = sizeof(Packet::Header) + Packet::Message::GetSize(msg);
+       return conn.Send(client_pack, client_sock);
+}
 
 NetworkedInput::NetworkedInput(World &world, Player &player, Client &client)
 : PlayerController(world, player)
index 91ee4826682fbdaca890b76d3c1c8bb0c3ab4929..0744a7c52260e36f221e033421957ef1b7a15d45 100644 (file)
@@ -86,7 +86,7 @@ void MasterState::OnPause() {
 void MasterState::Handle(const SDL_Event &event) {
        switch (event.type) {
                case SDL_KEYDOWN:
-                       // TODO: move to interface?
+                       // TODO: move to interface
                        if (event.key.keysym.sym == SDLK_RETURN) {
                                env.state.Push(&chat);
                                hud.KeepMessages(true);