oTree Forum >

Problem when assigning points to players

#1 by dmartinezfelip

Dear community,

I am trying to implement a point assignment page where players can decide how many points to give to each player (For example, 1 point to Player 2, 10 points to player 4 and so on). However, the points are not being correctly sent/stored. Does anyone have any ideas on how I can solve this? Thanks in advance.

This is the relevant part of the code: 

Class Player(BasePlayer):
       points_to_assign = models.IntegerField(min=0, initial=0)
    points_received = models.IntegerField(initial=0)
    did_assign = models.BooleanField(initial=False)

class PointsAssignment(Page):
    form_model = 'player'
    form_fields = []

    def vars_for_template(player: Player):
        choices = [(p.id_in_group, f"Player {p.random_id} (Contribution: {p.contribution_percentage()}%)") for p in player.get_others_in_group()]
        return dict(player_choices=choices)

    def before_next_page(player: Player, timeout_happened):
        print(f"before_next_page called for player {player.id_in_group}")
        for p in player.get_others_in_group():
            points_field = f'points_to_assign_{p.id_in_group}'
            points_to_assign = int(player.participant.vars.get(points_field, 0))
            print(f"Assigning points: player {player.id_in_group} -> player {p.id_in_group}, points_to_assign = {points_to_assign}")  # Debugging print
            if points_to_assign > 0:
                p.points_received += points_to_assign
                print(f"Updated player {p.id_in_group}: points_received = {p.points_received}")
        player.did_assign = True
        print(f"Player {player.id_in_group} did_assign = {player.did_assign}")
{% block title %}
    {% trans "Assign Points" %}
{% endblock %}

{% block content %}
                <table class="table">
                        <tr class="header1">
                            <th>{% trans "Group Member" %}</th>
                            <th>{% trans "How many points to assign" %}</th>
                        {% for id, label in player_choices %}
                        <tr class="header2">
                                <label for="{{ id }}">{{ label }}</label>
                                <input type="number" id="{{ id }}" name="points_to_assign" min="0" value="0"> <!-- Input for number of points -->
                        {% endfor %}
                <button type="submit" class="btn btn-primary">{% trans "Submit Points" %}</button>

after testing and assigning points, the debug displays: 
before_next_page called for player 4
Assigning points: player 4 -> player 1, points_to_assign = 0
Assigning points: player 4 -> player 2, points_to_assign = 0
Assigning points: player 4 -> player 3, points_to_assign = 0
Player 4 did_assign = True

Write a reply

Set forum username