From 623318a35ddee277a0eb3360ffe8abbdd2421a43 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 28 May 2016 18:45:39 +0100 Subject: [PATCH] refactor NetworkPlayer --- openrct2.vcxproj | 2 ++ src/network/NetworkPlayer.cpp | 51 +++++++++++++++++++++++++++++++++++ src/network/NetworkPlayer.h | 51 +++++++++++++++++++++++++++++++++++ src/network/network.cpp | 27 ------------------- src/network/network.h | 22 +-------------- 5 files changed, 105 insertions(+), 48 deletions(-) create mode 100644 src/network/NetworkPlayer.cpp create mode 100644 src/network/NetworkPlayer.h diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 915ae7a8fd..a784642cfc 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -89,6 +89,7 @@ + @@ -368,6 +369,7 @@ + diff --git a/src/network/NetworkPlayer.cpp b/src/network/NetworkPlayer.cpp new file mode 100644 index 0000000000..9674e8c0d5 --- /dev/null +++ b/src/network/NetworkPlayer.cpp @@ -0,0 +1,51 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#include "NetworkPacket.h" +#include "NetworkPlayer.h" + +extern "C" +{ + #include "../interface/window.h" + #include "../localisation/localisation.h" +} + +void NetworkPlayer::SetName(const std::string &name) +{ + // 36 == 31 + strlen(" #255"); + NetworkPlayer::name = name.substr(0, 36); + utf8_remove_format_codes((utf8 *)NetworkPlayer::name.data(), false); +} + +void NetworkPlayer::Read(NetworkPacket &packet) +{ + const utf8 * name = packet.ReadString(); + SetName(name); + packet >> id >> flags >> group; +} + +void NetworkPlayer::Write(NetworkPacket &packet) +{ + packet.WriteString((const char*)name.c_str()); + packet << id << flags << group; +} + +void NetworkPlayer::AddMoneySpent(money32 cost) +{ + money_spent += cost; + commands_ran++; + window_invalidate_by_number(WC_PLAYER, id); +} diff --git a/src/network/NetworkPlayer.h b/src/network/NetworkPlayer.h new file mode 100644 index 0000000000..35afa2423d --- /dev/null +++ b/src/network/NetworkPlayer.h @@ -0,0 +1,51 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#pragma once + +#include +#include "../common.h" + +extern "C" +{ + #include "../world/map.h" +} + +class NetworkPacket; + +class NetworkPlayer +{ +public: + uint8 id = 0; + std::string name; + uint16 ping = 0; + uint8 flags = 0; + uint8 group = 0; + money32 money_spent = MONEY(0, 0); + uint32 commands_ran = 0; + int last_action = -999; + uint32 last_action_time = 0; + rct_xyz16 last_action_coord = { 0 }; + std::string keyhash; + + NetworkPlayer() = default; + + void SetName(const std::string &name); + + void Read(NetworkPacket &packet); + void Write(NetworkPacket &packet); + void AddMoneySpent(money32 cost); +}; diff --git a/src/network/network.cpp b/src/network/network.cpp index 2e4f89f9ba..77fe4b05e3 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -90,33 +90,6 @@ static void network_get_private_key_path(utf8 *buffer, size_t bufferSize, const static void network_get_public_key_path(utf8 *buffer, size_t bufferSize, const utf8 * playerName, const utf8 * hash); static void network_get_keymap_path(utf8 *buffer, size_t bufferSize); -void NetworkPlayer::Read(NetworkPacket& packet) -{ - const char* name = packet.ReadString(); - SetName(name); - packet >> id >> flags >> group; -} - -void NetworkPlayer::Write(NetworkPacket& packet) -{ - packet.WriteString((const char*)name.c_str()); - packet << id << flags << group; -} - -void NetworkPlayer::SetName(const std::string &name) -{ - // 36 == 31 + strlen(" #255"); - NetworkPlayer::name = name.substr(0, 36); - utf8_remove_format_codes((utf8*)NetworkPlayer::name.data(), false); -} - -void NetworkPlayer::AddMoneySpent(money32 cost) -{ - money_spent += cost; - commands_ran++; - window_invalidate_by_number(WC_PLAYER, id); -} - Network::Network() { wsa_initialized = false; diff --git a/src/network/network.h b/src/network/network.h index 0135b7ff9a..0b01b9f69f 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -81,29 +81,9 @@ extern "C" { #include "NetworkGroup.h" #include "NetworkKey.h" #include "NetworkPacket.h" +#include "NetworkPlayer.h" #include "NetworkUser.h" -class NetworkPlayer -{ -public: - NetworkPlayer() = default; - void Read(NetworkPacket& packet); - void Write(NetworkPacket& packet); - void SetName(const std::string &name); - void AddMoneySpent(money32 cost); - uint8 id = 0; - std::string name; - uint16 ping = 0; - uint8 flags = 0; - uint8 group = 0; - money32 money_spent = MONEY(0, 0); - unsigned int commands_ran = 0; - int last_action = -999; - uint32 last_action_time = 0; - rct_xyz16 last_action_coord = { 0 }; - std::string keyhash; -}; - class Network { public: