in api/choice_algorithm.py [0:0]
def select_participant(wheel, participant):
"""
Register the selection of a participant by updating the weights of all participants for a given wheel
:param wheel: Wheel dictionary:
{
"id": string ID of the wheel (DDB Hash Key),
"name": string name of the wheel,
"participant_count": number of participants in the wheel,
}
:param participant: Participant dictionary:
{
"id": string ID of the participant (DDB Hash Key),
"name": string name of the participant,
"url": Participant's URL,
"wheel_id": string ID of the wheel the participant belongs to,
"weight": participant's weight in the selection algorithm
}
:return: None
"""
num_participants = 0
total_weight = Decimal(0)
for p in WheelParticipant.iter_query(KeyConditionExpression=Key('wheel_id').eq(wheel['id'])):
num_participants = num_participants+1
total_weight += p['weight']
# Factor is the number by which all weights must be multiplied
# so total weight will be equal to the number of participants.
factor = Decimal(num_participants) / total_weight
if num_participants > 1:
weight_share = participant['weight'] / Decimal(num_participants - 1)
with WheelParticipant.batch_writer() as batch:
# Redistribute and normalize the weights.
for p in WheelParticipant.iter_query(KeyConditionExpression=Key('wheel_id').eq(wheel['id'])):
if p['id'] == participant['id']:
p['weight'] = 0
else:
p['weight'] += Decimal(weight_share)
p['weight'] *= factor
batch.put_item(Item=p)
Wheel.update_item(
Key={'id': wheel['id']},
**to_update_kwargs({'participant_count': num_participants})
)