Skip to content

Commit

Permalink
Unify code used to sell improvements from cities and economic view
Browse files Browse the repository at this point in the history
  • Loading branch information
blabber committed Jan 5, 2025
1 parent 3df41f9 commit 7f1d950
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
50 changes: 41 additions & 9 deletions client/repodlgs_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

// utility
#include "fcintl.h"
#include "log.h"
#include "support.h" // fc_snprintf()

// common
Expand Down Expand Up @@ -167,6 +166,34 @@ void get_economy_report_units_data(struct unit_entry *entries,
void sell_all_improvements(const struct impr_type *pimprove,
bool redundant_only, char *message,
size_t message_sz)
{
if (nullptr == client.conn.playing) {
return;
}

QList<struct city *> cities;
city_list_iterate(client.conn.playing->cities, pcity)
{
cities.append(pcity);
}
city_list_iterate_end;

sell_all_improvements_for_cities(cities, pimprove, redundant_only, message,
message_sz);
}

/**
Sell all improvements of the given type in cities. If
"redundant_only" is specified then only those improvements that are replaced
will be sold.
The "message" string will be filled with a GUI-friendly message about
what was sold.
*/
void sell_all_improvements_for_cities(QList<struct city *> cities,
const struct impr_type *pimprove,
bool redundant_only, char *message,
size_t message_sz)
{
int count = 0, gold = 0;

Expand All @@ -175,16 +202,21 @@ void sell_all_improvements(const struct impr_type *pimprove,
return;
}

city_list_iterate(client.conn.playing->cities, pcity)
{
if (!pcity->did_sell && city_has_building(pcity, pimprove)
&& (!redundant_only || is_improvement_redundant(pcity, pimprove))) {
count++;
gold += impr_sell_gold(pimprove);
city_sell_improvement(pcity, improvement_number(pimprove));
for (auto pcity : cities) {
int city_id = pcity->id;
if (nullptr == game_city_by_number(city_id)) {
continue;
}

if (pcity->did_sell || !city_has_building(pcity, pimprove)
|| (redundant_only && !is_improvement_redundant(pcity, pimprove))) {
continue;
}

count++;
gold += impr_sell_gold(pimprove);
city_sell_improvement(pcity, improvement_number(pimprove));
}
city_list_iterate_end;

if (count > 0) {
// FIXME: plurality of count is ignored!
Expand Down
4 changes: 4 additions & 0 deletions client/repodlgs_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void get_economy_report_units_data(struct unit_entry *entries,
void sell_all_improvements(const struct impr_type *pimprove,
bool redundant_only, char *message,
size_t message_sz);
void sell_all_improvements_for_cities(QList<struct city *> cities,
const struct impr_type *pimprove,
bool redundant_only, char *message,
size_t message_sz);
void disband_all_units(const struct unit_type *punittype,
bool in_cities_only, char *message,
size_t message_sz);
30 changes: 17 additions & 13 deletions client/views/view_cities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "fcintl.h"
// common
#include "citydlg_common.h"
#include "game.h"
#include "global_worklist.h"
// client
#include "cityrep_g.h"
Expand All @@ -32,6 +31,7 @@
#include "icons.h"
#include "page_game.h"
#include "qtg_cxxside.h"
#include "repodlgs_common.h"
#include "top_bar.h"
#include "views/view_cities.h"
#include "views/view_map.h"
Expand Down Expand Up @@ -720,24 +720,28 @@ void city_widget::sell(const struct impr_type *building)

hud_message_box *ask = new hud_message_box(king()->central_wdg);
QString imprname = improvement_name_translation(building);
QString buf =
QString(_("Are you sure you want to sell the %1?")).arg(imprname);
QString buf;
if (selected_cities.size() == 1) {
buf = QString(_("Are you sure you want to sell the %1?")).arg(imprname);
} else {
buf = QString(_("Do you really wish to sell every %1?")).arg(imprname);
}

ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
ask->setDefaultButton(QMessageBox::No);
ask->button(QMessageBox::Yes)->setText(_("Yes Sell"));
ask->set_text_title(buf, _("Sell?"));
ask->setAttribute(Qt::WA_DeleteOnClose);
connect(ask, &hud_message_box::accepted, this, [=]() {
for (auto *pcity : selected_cities) {
int city_id = pcity->id;
if (nullptr == game_city_by_number(city_id)) {
continue;
}

if (!pcity->did_sell && city_has_building(pcity, building)) {
city_sell_improvement(pcity, impr_id);
}
}
char buf[1024];
sell_all_improvements_for_cities(selected_cities, building, false, buf,
sizeof(buf));

hud_message_box *result = new hud_message_box(king()->central_wdg);
result->set_text_title(buf, _("Sell Results"));
result->setStandardButtons(QMessageBox::Ok);
result->setAttribute(Qt::WA_DeleteOnClose);
result->show();
});

ask->show();
Expand Down

0 comments on commit 7f1d950

Please sign in to comment.