From: Daniel Karbach Date: Tue, 20 Oct 2015 15:40:30 +0000 (+0200) Subject: (shabby) client side handling of messages X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1d225566f79566e2dbbad8cb2876696f4c074ff5;p=blank.git (shabby) client side handling of messages --- diff --git a/src/client/Client.hpp b/src/client/Client.hpp index a5f85e5..e124ae5 100644 --- a/src/client/Client.hpp +++ b/src/client/Client.hpp @@ -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 &); diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index 49c5474..dc84199 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -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 update_status; + ChatState chat; + }; } diff --git a/src/client/MasterState.hpp b/src/client/MasterState.hpp index a29ff83..78ab370 100644 --- a/src/client/MasterState.hpp +++ b/src/client/MasterState.hpp @@ -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; diff --git a/src/client/client.cpp b/src/client/client.cpp index 455550a..44163f6 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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; + } +} + } } diff --git a/src/client/net.cpp b/src/client/net.cpp index c3bbb67..36d598b 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -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(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) diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 91ee482..0744a7c 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -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);