Skip to content

Commit fb825e9

Browse files
committed
cleanup contexts::selected_cubes
1 parent 723c391 commit fb825e9

8 files changed

Lines changed: 36 additions & 35 deletions

File tree

src/game/play_verify.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ namespace banggame {
4343
}
4444

4545
std::unordered_map<card_ptr, int> cubes;
46-
for (const auto &entry : ctx.get_all<contexts::selected_cubes>()) {
47-
for (card_ptr c : entry.cubes) {
48-
if (++cubes[c] > c->num_cubes()) {
49-
return {"ERROR_NOT_ENOUGH_CUBES_ON", c};
50-
}
46+
for (card_ptr c : contexts::selected_cubes::get_all_cubes(ctx)) {
47+
if (++cubes[c] > c->num_cubes()) {
48+
return {"ERROR_NOT_ENOUGH_CUBES_ON", c};
5149
}
5250
}
5351

src/target_types/armedanddangerous/player_per_cube.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "target_types/base/player.h"
55

6-
#include "select_cubes.h"
6+
#include "select_cubes_repeat.h"
77

88
namespace banggame {
99

@@ -14,7 +14,7 @@ namespace banggame {
1414
player_list players;
1515
};
1616

17-
targeting_select_cubes target_cubes;
17+
targeting_select_cubes_repeat target_cubes;
1818
targeting_player target_player;
1919
int extra_players;
2020

src/target_types/armedanddangerous/select_cubes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace banggame {
3333
}
3434

3535
void targeting_select_cubes::add_context(card_ptr origin_card, player_ptr origin, const effect_holder &effect, effect_context &ctx, const card_list &target_cards) {
36-
ctx.add(contexts::selected_cubes{ origin_card, target_cards, ncubes });
36+
ctx.add(contexts::selected_cubes{ origin_card, target_cards });
3737
}
3838

3939
void targeting_select_cubes::on_play(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx, const card_list &target_cards) {

src/target_types/armedanddangerous/select_cubes.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,31 @@ namespace banggame {
3939

4040
namespace contexts {
4141
struct selected_cubes {
42-
const_card_ptr origin_card;
42+
card_ptr origin_card;
4343
card_list cubes;
44-
int ncubes;
44+
45+
selected_cubes(card_ptr origin_card, card_list cubes)
46+
: origin_card{origin_card}
47+
, cubes{std::move(cubes)} {}
48+
49+
selected_cubes(card_ptr origin_card, int ncubes)
50+
: origin_card{origin_card}
51+
, cubes{static_cast<size_t>(ncubes), origin_card} {}
52+
53+
static auto get_all_cubes(const effect_context &ctx) {
54+
return ctx.get_all<selected_cubes>() | rv::for_each(&selected_cubes::cubes);
55+
}
4556

4657
static bool contains(const effect_context &ctx, const_card_ptr origin_card) {
47-
for (const selected_cubes &entry : ctx.get_all<selected_cubes>()) {
48-
if (entry.origin_card == origin_card && !entry.cubes.empty()) {
49-
return true;
50-
}
51-
}
52-
return false;
58+
return rn::contains(ctx.get_all<selected_cubes>(), origin_card, &selected_cubes::origin_card);
5359
}
5460

5561
static int count_repeats(const effect_context &ctx, const_card_ptr origin_card) {
56-
int result = 0;
57-
for (const selected_cubes &entry : ctx.get_all<selected_cubes>()) {
58-
if (entry.origin_card == origin_card && entry.ncubes != 0) {
59-
result += entry.cubes.size() / entry.ncubes;
60-
}
61-
}
62-
return result;
62+
return static_cast<int>(rn::count(ctx.get_all<selected_cubes>(), origin_card, &selected_cubes::origin_card));
6363
}
6464

6565
static int count_selected_on(const effect_context &ctx, const_card_ptr target_card) {
66-
int result = 0;
67-
for (const selected_cubes &entry : ctx.get_all<selected_cubes>()) {
68-
for (card_ptr c : entry.cubes) {
69-
if (c == target_card) ++result;
70-
}
71-
}
72-
return result;
66+
return static_cast<int>(rn::count(get_all_cubes(ctx), target_card));
7367
}
7468
};
7569
}

src/target_types/armedanddangerous/select_cubes_optional.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ namespace banggame {
2121
return {};
2222
}
2323

24+
void add_context(card_ptr origin_card, player_ptr origin, const effect_holder &effect, effect_context &ctx, const card_list &target) {
25+
if (!target.empty()) {
26+
targeting_select_cubes::add_context(origin_card, origin, effect, ctx, target);
27+
}
28+
}
29+
2430
game_string get_error(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx, const card_list &target) {
2531
if (!target.empty()) {
2632
return targeting_select_cubes::get_error(origin_card, origin, effect, ctx, target);

src/target_types/armedanddangerous/select_cubes_repeat.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace banggame {
1010

1111
return sample_elements(get_all_cubes(origin), ncubes * num_repeats, origin->m_game->bot_rng);
1212
}
13+
14+
void targeting_select_cubes_repeat::add_context(card_ptr origin_card, player_ptr origin, const effect_holder &effect, effect_context &ctx, const card_list &target) {
15+
for (auto chunk : target | rv::chunk(ncubes)) {
16+
ctx.add(contexts::selected_cubes{ origin_card, rn::to<card_list>(chunk) });
17+
}
18+
}
1319

1420
game_string targeting_select_cubes_repeat::get_error(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx, const card_list &target_cards) {
1521
if (target_cards.size() % ncubes != 0) {

src/target_types/armedanddangerous/select_cubes_repeat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace banggame {
1515
}
1616

1717
card_list random_target(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx);
18+
void add_context(card_ptr origin_card, player_ptr origin, const effect_holder &effect, effect_context &ctx, const card_list &target);
1819
game_string get_error(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx, const card_list &target);
1920
};
2021

src/target_types/armedanddangerous/self_cubes.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ namespace banggame {
1414
}
1515

1616
void targeting_self_cubes::add_context(card_ptr origin_card, player_ptr origin, const effect_holder &effect, effect_context &ctx, value_type) {
17-
ctx.add(contexts::selected_cubes{
18-
origin_card,
19-
card_list{static_cast<size_t>(ncubes), origin_card},
20-
ncubes
21-
});
17+
ctx.add(contexts::selected_cubes{ origin_card, ncubes });
2218
}
2319

2420
void targeting_self_cubes::on_play(card_ptr origin_card, player_ptr origin, const effect_holder &effect, const effect_context &ctx, value_type) {

0 commit comments

Comments
 (0)