Skip to content

Commit

Permalink
Improve chat history and disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed Feb 12, 2023
1 parent 8d656cd commit abee154
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 34 deletions.
5 changes: 1 addition & 4 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,10 @@ void Client::onPeerDisconnected(peer_t peer_id)

// send event for client destruction
{
GameEvent e(GameEvent::C2G_DIALOG);
e.text = new std::string("Server disconnected");
sendNewEvent(e);

GameEvent e2(GameEvent::C2G_DISCONNECT);
sendNewEvent(e2);
}
printf("Client: Disconnected from the server\n");
}

void Client::processPacket(peer_t peer_id, Packet &pkt)
Expand Down
9 changes: 6 additions & 3 deletions src/core/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string.h> // strerror
#include <sstream>

#if 1
#if 0
#define DEBUGLOG(...) printf(__VA_ARGS__)
#else
#define DEBUGLOG(...) /* SILENCE */
Expand Down Expand Up @@ -72,8 +72,9 @@ Connection::Connection(Connection::ConnectionType type, const char *name)

Connection::~Connection()
{
DEBUGLOG("--- ENet %s: Cleaning up ...\n", m_name);
m_running = false;
if (!m_host)
return;

for (size_t i = 0; i < m_host->peerCount; ++i)
enet_peer_disconnect_later(&m_host->peers[i], 0);
Expand All @@ -84,6 +85,8 @@ Connection::~Connection()
ERRORLOG("--- ENet %s: Failed to join thread status=%d\n", m_name, errno);
}
}
while (m_thread)
sleep(1);

// Apply force if needed
for (size_t i = 0; i < m_host->peerCount; ++i)
Expand Down Expand Up @@ -161,7 +164,7 @@ void Connection::disconnect(peer_t peer_id)
if (!peer)
return;

enet_peer_disconnect(peer, 0);
enet_peer_disconnect_later(peer, 0);
// Actual handling done in async event handler
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef std::unique_lock<std::mutex> SimpleLock;

typedef uint32_t peer_t; // same as in ENetPeer

void sleep_ms(long delay);

// Auto-unlock wrapper for larger operations
template<typename T>
class PtrLock {
Expand Down
20 changes: 13 additions & 7 deletions src/gui/gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ void SceneGameplay::draw()
core::dimension2di(wsize.Width - SIZEW, wsize.Height - 50 - 160)
);
if (m_chathistory_text.empty())
m_chathistory_text = L"Chat history:\n";
m_chathistory_text = L"--- Start of chat history ---\n";

auto e = m_gui->gui->addEditBox(m_chathistory_text.c_str(), rect_ch, true);
e->setAutoScroll(true);
e->setMultiLine(true);
e->setWordWrap(true);
e->setEnabled(false);
e->setDrawBackground(false);
e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_LOWERRIGHT);
e->setOverrideColor(0xFFCCCCCC);
m_chathistory = e;
}
Expand Down Expand Up @@ -209,6 +209,16 @@ bool SceneGameplay::OnEvent(const SEvent &e)
}
break;
case EMIE_MOUSE_WHEEL:
{
core::vector2di pos(e.MouseInput.X, e.MouseInput.Y);
auto root = m_gui->gui->getRootGUIElement();
auto element = root->getElementFromPoint(pos);
if (element && element != root) {
// Forward inputs to the corresponding element
return false;
}
}

{
float dir = e.MouseInput.Wheel > 0 ? 1 : -1;

Expand Down Expand Up @@ -326,11 +336,8 @@ bool SceneGameplay::OnEvent(GameEvent &e)
}
return true;
case E::C2G_DIALOG:
printf(" * SYSTEM: %s\n", e.text->c_str());
// TODO: show an actual dialog?
break;
case E::C2G_DISCONNECT:
m_gui->disconnect();
return true;
default: break;
}
return false;
Expand Down Expand Up @@ -389,7 +396,6 @@ video::ITexture *SceneGameplay::generateTexture(const wchar_t *text, u32 color)
return texture;
}


void SceneGameplay::updateWorld()
{
auto world = m_gui->getClient()->getWorld();
Expand Down
43 changes: 26 additions & 17 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "lobby.h"
#include "gameplay.h"

void sleep_ms(long delay);
extern const char *VERSION_STRING;

Gui::Gui()
{
Expand All @@ -20,7 +20,12 @@ Gui::Gui()

ASSERT_FORCED(device, "Failed to initialize driver");

device->setWindowCaption(L"OpenEdits v1.0.2-dev");
{
// Title bar
core::stringw version;
core::multibyteToWString(version, VERSION_STRING);
device->setWindowCaption(version.c_str());
}

scenemgr = device->getSceneManager();
gui = device->getGUIEnvironment();
Expand Down Expand Up @@ -95,6 +100,18 @@ void Gui::run()
t_last = t_now;
}

if (m_pending_disconnect) {
if (m_client) {
delete m_client;
m_client = nullptr;
}
if (m_server) {
delete m_server;
m_server = nullptr;
}
m_pending_disconnect = false;
}

if (m_client) {
m_client->step(dtime);
}
Expand Down Expand Up @@ -171,6 +188,10 @@ bool Gui::OnEvent(GameEvent &e)
if (!m_initialized)
return false;

if (e.type_c2g == GameEvent::C2G_DISCONNECT) {
disconnect();
return true;
}
return getHandler(m_scenetype)->OnEvent(e);
}

Expand Down Expand Up @@ -222,28 +243,16 @@ void Gui::connect(SceneConnect *sc)
m_client->setEventHandler(this);
setNextScene(SceneHandlerType::Lobby);
} else {
puts("Wait timed out.");
delete m_client;
m_client = nullptr;

delete m_server;
m_server = nullptr;
puts("Connection timed out: Server is not reachable.");
m_pending_disconnect = true;
}
}

void Gui::disconnect()
{
setNextScene(SceneHandlerType::Connect);

if (m_client) {
delete m_client;
m_client = nullptr;
}

if (m_server) {
delete m_server;
m_server = nullptr;
}
m_pending_disconnect = true;
}

void Gui::joinWorld(SceneLobby *sc)
Expand Down
1 change: 1 addition & 0 deletions src/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Gui : public IEventReceiver, public GameEventHandler {
inline void setNextScene(SceneHandlerType type) { m_scenetype_next = type; }

bool m_initialized = false;
bool m_pending_disconnect = false;

SceneHandlerType m_scenetype_next;
SceneHandlerType m_scenetype;
Expand Down
17 changes: 14 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ void sleep_ms(long delay)
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}

const char *VERSION_STRING = "OpenEdits v1.0.3-dev";

void unittest();

static void exit_cleanup()
Expand Down Expand Up @@ -86,9 +88,18 @@ int main(int argc, char *argv[])
g_blockmanager = new BlockManager();
register_packs();

if (argc >= 2 && strcmp(argv[1], "--unittest") == 0) {
unittest();
return EXIT_SUCCESS;
if (argc >= 2) {
if (strcmp(argv[1], "--version") == 0) {
puts(VERSION_STRING);
return EXIT_SUCCESS;
}
if (strcmp(argv[1], "--unittest") == 0) {
// Depends on BlockManager and ENet
unittest();
return EXIT_SUCCESS;
}
puts("-!- Unknown command line option.");
return EXIT_FAILURE;
}

Gui gui;
Expand Down

0 comments on commit abee154

Please sign in to comment.