Skip to content

Commit

Permalink
Better error message for the dummy player case
Browse files Browse the repository at this point in the history
  • Loading branch information
stanmart committed Apr 23, 2024
1 parent 3b6518f commit dbbd5f1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
6 changes: 5 additions & 1 deletion _static/live_bargaining.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ for (let i = 0; i < numPlayers; i++) {
}

function sendOffer() {
if (totalSharedValue > 0 && !isMemberCheckboxes[0].checked) {
if (!lastPlayerIsDummy && totalSharedValue > 0 && !isMemberCheckboxes[0].checked) {
openPopup(`Invalid proposal: the budget is zero when Player ${player_names['P1']} is not included in the group`, 'error');
return;
}
if (lastPlayerIsDummy && totalSharedValue > 0 && !(isMemberCheckboxes[0].checked && isMemberCheckboxes[1].checked)) {
openPopup(`Invalid proposal: the budget is zero when Players ${player_names['P1']} and ${player_names['P2']} are not included in the group`, 'error');
return;
}
if (totalSharedValue > totalShareableValue) {
openPopup('Invalid proposal: total amount exceeds the budget available to this group', 'error');
return;
Expand Down
6 changes: 5 additions & 1 deletion _static/proposal_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ function sendOffer() {
'allocations': allocations,
};

if (totalSharedValue > 0 && !members[0]) {
if (!lastPlayerIsDummy && totalSharedValue > 0 && !isMemberCheckboxes[0].checked) {
openPopup(`Invalid proposal: the budget is zero when Player ${player_names['P1']} is not included in the group`, 'error');
return;
}
if (lastPlayerIsDummy && totalSharedValue > 0 && !(isMemberCheckboxes[0].checked && isMemberCheckboxes[1].checked)) {
openPopup(`Invalid proposal: the budget is zero when Players ${player_names['P1']} and ${player_names['P2']} are not included in the group`, 'error');
return;
}
if (totalSharedValue > totalShareableValue) {
openPopup('Invalid proposal: total amount exceeds the budget available to this group', 'error');
return;
Expand Down
27 changes: 24 additions & 3 deletions live_bargaining/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,37 @@ def check_proposal_validity(player: Player, members, allocations):
}
}

if not big_player_included and sum(allocations) > 0:
last_player_is_dummy = len(prod_fct) == C.PLAYERS_PER_GROUP - 1

if not last_player_is_dummy and not big_player_included and sum(allocations) > 0:
return {
player.id_in_group: {
"type": "error",
"content": (
"Invalid allocation: allocation has to be zero when "
f"Player {player.session.config['player_names']['P1']} is not included"
),
}
}

if (
last_player_is_dummy
and not (members[0] and members[1])
and sum(allocations) > 0
):
return {
player.id_in_group: {
"type": "error",
"content": f"Invalid allocation: allocation has to be zero when Player {player.session.config['player_names']['P1']} is not included", # noqa: E501
"content": (
"Invalid allocation: allocation has to be zero when Players "
f"{player.session.config['player_names']['P1']} and "
f"{player.session.config['player_names']['P2']} are not included"
),
}
}

num_small_players = coalition_size - big_player_included
if len(prod_fct) == C.PLAYERS_PER_GROUP - 1: # last player is a dummy player
if last_player_is_dummy: # last player is a dummy player
num_small_players -= members[-1] # the dummy player is always last

if sum(allocations) > prod_fct[num_small_players]:
Expand Down
39 changes: 25 additions & 14 deletions live_bargaining/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def create_offers(method, Y):
)


def test_invalid_input(method, Y, dummy_player, P1_name):
def test_invalid_input(method, Y, dummy_player, player_names):
non_existing_offer = method(1, {"type": "accept", "offer_id": 1})
expect(
non_existing_offer,
Expand Down Expand Up @@ -69,8 +69,8 @@ def test_invalid_input(method, Y, dummy_player, P1_name):
2,
{
"type": "propose",
"members": [True, False, True],
"allocations": [50, 0, 50],
"members": [True, True, True],
"allocations": [50, 50, 10],
},
)
expect(
Expand All @@ -91,15 +91,26 @@ def test_invalid_input(method, Y, dummy_player, P1_name):
"allocations": [0, 50, 50],
},
)
expect(
p1_not_included,
{
3: {
"type": "error",
"content": f"Invalid allocation: allocation has to be zero when Player {P1_name} is not included",
}
},
)
if not dummy_player:
expect(
p1_not_included,
{
3: {
"type": "error",
"content": f"Invalid allocation: allocation has to be zero when Player {player_names['P1']} is not included",
}
},
)
else:
expect(
p1_not_included,
{
3: {
"type": "error",
"content": f"Invalid allocation: allocation has to be zero when Players {player_names['P1']} and {player_names['P2']} are not included",
}
},
)

invalid_allocaion_negative = method(
1,
Expand Down Expand Up @@ -179,13 +190,13 @@ def call_live_method(method, **kwargs):
)

prod_fct = kwargs["group"].session.config["prod_fct"]
P1_name = kwargs["group"].session.config["player_names"]["P1"]
player_names = kwargs["group"].session.config["player_names"]
Y = list(prod_fct.values())[1]
dummy_player = len(prod_fct) == 2

if kwargs["round_number"] == 1:
print("Testing invalid input")
test_invalid_input(method, Y, dummy_player, P1_name)
test_invalid_input(method, Y, dummy_player, player_names)

if kwargs["round_number"] == 2:
print("Testing grand coalition")
Expand Down

0 comments on commit dbbd5f1

Please sign in to comment.