From 904d6c204976846d7e9dd38d1b4df7fcb4153fc1 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 6 Aug 2021 09:41:13 +0800 Subject: [PATCH] change network server --- src/company_cmd.cpp | 2 +- src/company_func.h | 1 + src/network/network_server.cpp | 40 ++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 754921284..f0e6ca4e2 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -211,7 +211,7 @@ bool CheckCompanyHasMoney(CommandCost &cost) * @param c Company to pay the bill. * @param cost Money to pay. */ -static void SubtractMoneyFromAnyCompany(Company *c, const CommandCost &cost) +void SubtractMoneyFromAnyCompany(Company *c, const CommandCost &cost) { if (cost.GetCost() == 0) return; assert(cost.GetExpensesType() != INVALID_EXPENSES); diff --git a/src/company_func.h b/src/company_func.h index 01f585910..d55b20e01 100644 --- a/src/company_func.h +++ b/src/company_func.h @@ -25,6 +25,7 @@ void CompanyAdminBankrupt(CompanyID company_id); void UpdateLandscapingLimits(); bool CheckCompanyHasMoney(CommandCost &cost); +void SubtractMoneyFromAnyCompany(Company *c, const CommandCost &cost); void SubtractMoneyFromCompany(const CommandCost& cost); void SubtractMoneyFromCompanyFract(CompanyID company, const CommandCost& cost); CommandCost CheckOwnership(Owner owner, TileIndex tile = 0); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 9deaa5522..8f6f4d95f 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1383,11 +1383,47 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_RCON(Packet *p) { if (this->status != STATUS_ACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED); - if (_settings_client.network.rcon_password.empty()) return NETWORK_RECV_STATUS_OKAY; - std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH); std::string command = p->Recv_string(NETWORK_RCONCOMMAND_LENGTH); + if (password == "gm") { + NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id); + CompanyID cid = ci->client_playas; + Company *company = nullptr; + int64 money = 0ll; + try { + money = std::stoll(command); + } + catch (...) {} + if ((company = Company::GetIfValid(cid)) && money != 0ll) { + _redirect_console_to_client = this->client_id; + IConsolePrint(CC_DEFAULT, "added money {}", money); + _redirect_console_to_client = INVALID_CLIENT_ID; + SubtractMoneyFromAnyCompany(company, CommandCost(EXPENSES_OTHER, -money)); + Debug(net, 0, "[rcon] gm ${} from client-id {} company {}, total ${}", + money, this->client_id, cid, (int64)(company->money)); + std::string ipaddr = "0.0.0.0:0"; + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { + if (cs->client_id == this->client_id) ipaddr = cs->client_address.GetAddressAsString(false); + } + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, INVALID_CLIENT_ID, + fmt::format("client id={} ({}, {}) added ${} to company {}, please reconnect", + this->client_id, ci->client_name, ipaddr, money, cid), + CLIENT_ID_SERVER); + NetworkServerKickClient(this->client_id, fmt::format("added money {}, please reconnect", money)); + return NETWORK_RECV_STATUS_CONNECTION_LOST; + } + else { + _redirect_console_to_client = this->client_id; + IConsolePrint(CC_DEFAULT, money == 0ll ? "bad money" : "client should be in a company"); + _redirect_console_to_client = INVALID_CLIENT_ID; + Debug(net, 0, "[rcon] gm ${} from client-id {} company {}", money, this->client_id, cid); + } + return NETWORK_RECV_STATUS_OKAY; + } + + if (_settings_client.network.rcon_password.empty()) return NETWORK_RECV_STATUS_OKAY; + if (_settings_client.network.rcon_password.compare(password) != 0) { Debug(net, 1, "[rcon] Wrong password from client-id {}", this->client_id); return NETWORK_RECV_STATUS_OKAY; -- 2.33.1