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 league_cmd.cpp Handling of league tables. */
11 #include "league_cmd.h"
12 #include "league_base.h"
13 #include "command_type.h"
14 #include "command_func.h"
16 #include "story_base.h"
18 #include "window_func.h"
19 #include "core/pool_func.hpp"
21 #include "safeguards.h"
23 LeagueTableElementPool
_league_table_element_pool("LeagueTableElement");
24 INSTANTIATE_POOL_METHODS(LeagueTableElement
)
26 LeagueTablePool
_league_table_pool("LeagueTable");
27 INSTANTIATE_POOL_METHODS(LeagueTable
)
30 * Checks whether a link is valid, i.e. has a valid target.
31 * @param link the link to check
32 * @return true iff the link is valid
34 bool IsValidLink(Link link
)
37 case LT_NONE
: return (link
.target
== 0);
38 case LT_TILE
: return IsValidTile(link
.target
);
39 case LT_INDUSTRY
: return Industry::IsValidID(link
.target
);
40 case LT_TOWN
: return Town::IsValidID(link
.target
);
41 case LT_COMPANY
: return Company::IsValidID(link
.target
);
42 case LT_STORY_PAGE
: return StoryPage::IsValidID(link
.target
);
43 default: return false;
49 * Create a new league table.
50 * @param flags type of operation
51 * @param title Title of the league table
52 * @param header Text to show above the table
53 * @param footer Text to show below the table
54 * @return the cost of this operation or an error
56 std::tuple
<CommandCost
, LeagueTableID
> CmdCreateLeagueTable(DoCommandFlag flags
, const std::string
&title
, const std::string
&header
, const std::string
&footer
)
58 if (_current_company
!= OWNER_DEITY
) return { CMD_ERROR
, INVALID_LEAGUE_TABLE
};
59 if (!LeagueTable::CanAllocateItem()) return { CMD_ERROR
, INVALID_LEAGUE_TABLE
};
60 if (title
.empty()) return { CMD_ERROR
, INVALID_LEAGUE_TABLE
};
62 if (flags
& DC_EXEC
) {
63 LeagueTable
*lt
= new LeagueTable();
67 return { CommandCost(), lt
->index
};
70 return { CommandCost(), INVALID_LEAGUE_TABLE
};
75 * Create a new element in a league table.
76 * @param flags type of operation
77 * @param table Id of the league table this element belongs to
78 * @param rating Value that elements are ordered by
79 * @param company Company to show the color blob for or INVALID_COMPANY
80 * @param text Text of the element
81 * @param score String representation of the score associated with the element
82 * @param link_type Type of the referenced object
83 * @param link_target Id of the referenced object
84 * @return the cost of this operation or an error
86 std::tuple
<CommandCost
, LeagueTableElementID
> CmdCreateLeagueTableElement(DoCommandFlag flags
, LeagueTableID table
, int64_t rating
, CompanyID company
, const std::string
&text
, const std::string
&score
, LinkType link_type
, LinkTargetID link_target
)
88 if (_current_company
!= OWNER_DEITY
) return { CMD_ERROR
, INVALID_LEAGUE_TABLE_ELEMENT
};
89 if (!LeagueTableElement::CanAllocateItem()) return { CMD_ERROR
, INVALID_LEAGUE_TABLE_ELEMENT
};
90 Link link
{link_type
, link_target
};
91 if (!IsValidLink(link
)) return { CMD_ERROR
, INVALID_LEAGUE_TABLE_ELEMENT
};
92 if (company
!= INVALID_COMPANY
&& !Company::IsValidID(company
)) return { CMD_ERROR
, INVALID_LEAGUE_TABLE_ELEMENT
};
94 if (flags
& DC_EXEC
) {
95 LeagueTableElement
*lte
= new LeagueTableElement();
98 lte
->company
= company
;
102 InvalidateWindowData(WC_COMPANY_LEAGUE
, table
);
103 return { CommandCost(), lte
->index
};
105 return { CommandCost(), INVALID_LEAGUE_TABLE_ELEMENT
};
109 * Update the attributes of a league table element.
110 * @param flags type of operation
111 * @param element Id of the element to update
112 * @param company Company to show the color blob for or INVALID_COMPANY
113 * @param text Text of the element
114 * @param link_type Type of the referenced object
115 * @param link_target Id of the referenced object
116 * @return the cost of this operation or an error
118 CommandCost
CmdUpdateLeagueTableElementData(DoCommandFlag flags
, LeagueTableElementID element
, CompanyID company
, const std::string
&text
, LinkType link_type
, LinkTargetID link_target
)
120 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
121 auto lte
= LeagueTableElement::GetIfValid(element
);
122 if (lte
== nullptr) return CMD_ERROR
;
123 if (company
!= INVALID_COMPANY
&& !Company::IsValidID(company
)) return CMD_ERROR
;
124 Link link
{link_type
, link_target
};
125 if (!IsValidLink(link
)) return CMD_ERROR
;
127 if (flags
& DC_EXEC
) {
128 lte
->company
= company
;
131 InvalidateWindowData(WC_COMPANY_LEAGUE
, lte
->table
);
133 return CommandCost();
137 * Update the score of a league table element.
138 * @param flags type of operation
139 * @param element Id of the element to update
140 * @param rating Value that elements are ordered by
141 * @param score String representation of the score associated with the element
142 * @return the cost of this operation or an error
144 CommandCost
CmdUpdateLeagueTableElementScore(DoCommandFlag flags
, LeagueTableElementID element
, int64_t rating
, const std::string
&score
)
146 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
147 auto lte
= LeagueTableElement::GetIfValid(element
);
148 if (lte
== nullptr) return CMD_ERROR
;
150 if (flags
& DC_EXEC
) {
151 lte
->rating
= rating
;
153 InvalidateWindowData(WC_COMPANY_LEAGUE
, lte
->table
);
155 return CommandCost();
159 * Remove a league table element.
160 * @param flags type of operation
161 * @param element Id of the element to update
162 * @return the cost of this operation or an error
164 CommandCost
CmdRemoveLeagueTableElement(DoCommandFlag flags
, LeagueTableElementID element
)
166 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
167 auto lte
= LeagueTableElement::GetIfValid(element
);
168 if (lte
== nullptr) return CMD_ERROR
;
170 if (flags
& DC_EXEC
) {
171 auto table
= lte
->table
;
173 InvalidateWindowData(WC_COMPANY_LEAGUE
, table
);
175 return CommandCost();