Update: Translations from eints
[openttd-github.git] / src / script / api / script_client.cpp
blob9b65f638db35edfea7928eeaee0adb17ea93863d
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file script_client.cpp Implementation of ScriptClient. */
10 #include "../../stdafx.h"
11 #include "script_client.hpp"
12 #include "../../network/network.h"
13 #include "../../network/network_base.h"
15 #include "../../safeguards.h"
17 /**
18 * Finds NetworkClientInfo given client-identifier,
19 * is used by other methods to resolve client-identifier.
20 * @param client The client to get info structure for
21 * @return A pointer to corresponding CI struct or nullptr when not found.
23 static NetworkClientInfo *FindClientInfo(ScriptClient::ClientID client)
25 if (client == ScriptClient::CLIENT_INVALID) return nullptr;
26 if (!_networking) return nullptr;
27 return NetworkClientInfo::GetByClientID((::ClientID)client);
30 /* static */ ScriptClient::ClientID ScriptClient::ResolveClientID(ScriptClient::ClientID client)
32 return (FindClientInfo(client) == nullptr ? ScriptClient::CLIENT_INVALID : client);
35 /* static */ std::optional<std::string> ScriptClient::GetName(ScriptClient::ClientID client)
37 NetworkClientInfo *ci = FindClientInfo(client);
38 if (ci == nullptr) return std::nullopt;
39 return ci->client_name;
42 /* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client)
44 NetworkClientInfo *ci = FindClientInfo(client);
45 if (ci == nullptr) return ScriptCompany::COMPANY_INVALID;
46 return (ScriptCompany::CompanyID)ci->client_playas;
49 /* static */ ScriptDate::Date ScriptClient::GetJoinDate(ScriptClient::ClientID client)
51 NetworkClientInfo *ci = FindClientInfo(client);
52 if (ci == nullptr) return ScriptDate::DATE_INVALID;
53 return (ScriptDate::Date)ci->join_date.base();