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/>.
8 /** @file script_league.cpp Implementation of ScriptLeagueTable. */
10 #include "../../stdafx.h"
12 #include "script_league.hpp"
14 #include "../script_instance.hpp"
15 #include "script_error.hpp"
16 #include "../../league_base.h"
17 #include "../../league_cmd.h"
19 #include "../../safeguards.h"
22 /* static */ bool ScriptLeagueTable::IsValidLeagueTable(LeagueTableID table_id
)
24 return ::LeagueTable::IsValidID(table_id
);
27 /* static */ ScriptLeagueTable::LeagueTableID
ScriptLeagueTable::New(Text
*title
, Text
*header
, Text
*footer
)
29 ScriptObjectRef
title_counter(title
);
30 ScriptObjectRef
header_counter(header
);
31 ScriptObjectRef
footer_counter(footer
);
33 EnforceDeityMode(LEAGUE_TABLE_INVALID
);
34 EnforcePrecondition(LEAGUE_TABLE_INVALID
, title
!= nullptr);
35 std::string encoded_title
= title
->GetEncodedText();
36 EnforcePreconditionEncodedText(LEAGUE_TABLE_INVALID
, encoded_title
);
38 std::string encoded_header
= (header
!= nullptr ? header
->GetEncodedText() : std::string
{});
39 std::string encoded_footer
= (footer
!= nullptr ? footer
->GetEncodedText() : std::string
{});
41 if (!ScriptObject::Command
<CMD_CREATE_LEAGUE_TABLE
>::Do(&ScriptInstance::DoCommandReturnLeagueTableID
, encoded_title
, encoded_header
, encoded_footer
)) return LEAGUE_TABLE_INVALID
;
43 /* In case of test-mode, we return LeagueTableID 0 */
44 return (ScriptLeagueTable::LeagueTableID
)0;
47 /* static */ bool ScriptLeagueTable::IsValidLeagueTableElement(LeagueTableElementID element_id
)
49 return ::LeagueTableElement::IsValidID(element_id
);
52 /* static */ ScriptLeagueTable::LeagueTableElementID
ScriptLeagueTable::NewElement(ScriptLeagueTable::LeagueTableID table
, SQInteger rating
, ScriptCompany::CompanyID company
, Text
*text
, Text
*score
, LinkType link_type
, SQInteger link_target
)
54 ScriptObjectRef
text_counter(text
);
55 ScriptObjectRef
score_counter(score
);
57 EnforceDeityMode(LEAGUE_TABLE_ELEMENT_INVALID
);
59 EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID
, IsValidLeagueTable(table
));
61 EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID
, company
== ScriptCompany::COMPANY_INVALID
|| ScriptCompany::ResolveCompanyID(company
) != ScriptCompany::COMPANY_INVALID
);
62 CompanyID c
= (::CompanyID
)company
;
63 if (company
== ScriptCompany::COMPANY_INVALID
) c
= INVALID_COMPANY
;
65 EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID
, text
!= nullptr);
66 std::string encoded_text
= text
->GetEncodedText();
67 EnforcePreconditionEncodedText(LEAGUE_TABLE_ELEMENT_INVALID
, encoded_text
);
69 EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID
, score
!= nullptr);
70 std::string encoded_score
= score
->GetEncodedText();
71 EnforcePreconditionEncodedText(LEAGUE_TABLE_ELEMENT_INVALID
, encoded_score
);
73 EnforcePrecondition(LEAGUE_TABLE_ELEMENT_INVALID
, IsValidLink(Link((::LinkType
)link_type
, link_target
)));
75 if (!ScriptObject::Command
<CMD_CREATE_LEAGUE_TABLE_ELEMENT
>::Do(&ScriptInstance::DoCommandReturnLeagueTableElementID
, table
, rating
, c
, encoded_text
, encoded_score
, (::LinkType
)link_type
, (::LinkTargetID
)link_target
)) return LEAGUE_TABLE_ELEMENT_INVALID
;
77 /* In case of test-mode, we return LeagueTableElementID 0 */
78 return (ScriptLeagueTable::LeagueTableElementID
)0;
81 /* static */ bool ScriptLeagueTable::UpdateElementData(LeagueTableElementID element
, ScriptCompany::CompanyID company
, Text
*text
, LinkType link_type
, SQInteger link_target
)
83 ScriptObjectRef
text_counter(text
);
85 EnforceDeityMode(false);
86 EnforcePrecondition(false, IsValidLeagueTableElement(element
));
88 EnforcePrecondition(false, company
== ScriptCompany::COMPANY_INVALID
|| ScriptCompany::ResolveCompanyID(company
) != ScriptCompany::COMPANY_INVALID
);
89 CompanyID c
= (::CompanyID
)company
;
90 if (company
== ScriptCompany::COMPANY_INVALID
) c
= INVALID_COMPANY
;
92 EnforcePrecondition(false, text
!= nullptr);
93 std::string encoded_text
= text
->GetEncodedText();
94 EnforcePreconditionEncodedText(false, encoded_text
);
96 EnforcePrecondition(false, IsValidLink(Link((::LinkType
)link_type
, link_target
)));
98 return ScriptObject::Command
<CMD_UPDATE_LEAGUE_TABLE_ELEMENT_DATA
>::Do(element
, c
, encoded_text
, (::LinkType
)link_type
, (::LinkTargetID
)link_target
);
101 /* static */ bool ScriptLeagueTable::UpdateElementScore(LeagueTableElementID element
, SQInteger rating
, Text
*score
)
103 ScriptObjectRef
score_counter(score
);
105 EnforceDeityMode(false);
106 EnforcePrecondition(false, IsValidLeagueTableElement(element
));
108 EnforcePrecondition(false, score
!= nullptr);
109 std::string encoded_score
= score
->GetEncodedText();
110 EnforcePreconditionEncodedText(false, encoded_score
);
112 return ScriptObject::Command
<CMD_UPDATE_LEAGUE_TABLE_ELEMENT_SCORE
>::Do(element
, rating
, encoded_score
);
115 /* static */ bool ScriptLeagueTable::RemoveElement(LeagueTableElementID element
)
117 EnforceDeityMode(false);
118 EnforcePrecondition(false, IsValidLeagueTableElement(element
));
120 return ScriptObject::Command
<CMD_REMOVE_LEAGUE_TABLE_ELEMENT
>::Do(element
);