Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def data_check(self, participant):
"""Check a participants data."""
nodes = Node.query.filter_by(participant_id=participant.id).all()
if len(nodes) != self.experiment_repeats + self.practice_repeats:
print(
"Error: Participant has {} nodes. Data check failed".format(len(nodes))
)
return False
nets = [n.network_id for n in nodes]
if len(nets) != len(set(nets)):
print(
"Error: Participant participated in the same network \
multiple times. Data check failed"
)
return False
if None in [n.fitness for n in nodes]:
def fail_bystander_vectors(self):
# mafiosi = self.nodes(type=Mafioso)
mafiosi = Node.query.filter_by(network_id=self.id,
property2='True', type='mafioso').all()
for v in self.vectors():
if not isinstance(v.origin, Source) and not (
v.origin in mafiosi and v.destination in mafiosi):
v.fail()
def fail_participant(self, participant):
"""Fail all the nodes of a participant."""
participant_nodes = Node.query\
.filter_by(participant_id=participant.id, failed=False)\
.all()
for node in participant_nodes:
node.fail()
time = int(time)
victim_name = None
victim_type = None
winner = None
# If it's night but should be day, then call setup_daytime()
if not daytime and (
int(elapsed_time.total_seconds() -
switches / 2 * daybreak_duration) == night_round_duration):
victim_name, winner = net.setup_daytime()
# If it's day but should be night, then call setup_nighttime()
elif daytime and (
int(elapsed_time.total_seconds() -
(switches + 1) / 2 * nightbreak_duration) == day_round_duration):
victim_name, winner = net.setup_nighttime()
victim_type = Node.query.filter_by(property1=victim_name).one().type
elif was_daytime != net.daytime:
nodes = Node.query.filter_by(network_id=net.id,
property2='True').all()
mafiosi = Node.query.filter_by(network_id=net.id,
property2='True',
type='mafioso').all()
victim_name = Node.query.filter_by(
network_id=net.id,
property2='False'
).order_by('property3').all()[-1].property1
if daytime:
if len(mafiosi) > len(nodes) - len(mafiosi) - 1:
winner = 'mafia'
else:
victim_type = Node.query.filter_by(
property1=victim_name
)
working = (
models.Participant.query.filter_by(status="working")
.with_entities(func.count(models.Participant.id))
.scalar()
)
state["unfilled_networks"] = len(unfilled_nets)
nodes_remaining = 0
required_nodes = 0
if state["unfilled_networks"] == 0:
if working == 0 and state["completed"] is None:
state["completed"] = True
else:
for net in unfilled_nets:
node_count = (
models.Node.query.filter_by(network_id=net.id, failed=False)
.with_entities(func.count(models.Node.id))
.scalar()
)
net_size = net.max_size
required_nodes += net_size
nodes_remaining += net_size - node_count
state["nodes_remaining"] = nodes_remaining
state["required_nodes"] = required_nodes
if state["completed"] is None:
state["completed"] = False
# Regenerate a waiting room message when checking status
# to counter missed messages at the end of the waiting room
nonfailed_count = models.Participant.query.filter(
(models.Participant.status == "working")
def fail_participant(self, participant):
"""Fail all the nodes of a participant."""
participant_nodes = Node.query\
.filter_by(participant_id=participant.id, failed=False)\
.all()
for node in participant_nodes:
node.fail()
def phase(node_id, switches, was_daytime):
try:
exp = MafiaExperiment(db.session)
this_node = Node.query.filter_by(id=node_id).one()
net = Network.query.filter_by(id=this_node.network_id).one()
nodes = Node.query.filter_by(network_id=net.id).order_by(
'creation_time').all()
node = nodes[-1]
elapsed_time = timenow() - node.creation_time
daytime = (net.daytime == 'True')
day_round_duration = 150
night_round_duration = 30
break_duration = 3
daybreak_duration = day_round_duration + break_duration
nightbreak_duration = night_round_duration + break_duration
time = elapsed_time.total_seconds()
if switches % 2 == 0:
time = night_round_duration - (
elapsed_time.total_seconds() -
switches / 2 * daybreak_duration
) % night_round_duration
else:
def live_participants(node_id, get_all):
try:
exp = MafiaExperiment(db.session)
this_node = Node.query.filter_by(id=node_id).one()
if get_all == 1:
nodes = Node.query.filter_by(network_id=this_node.network_id,
property2='True').all()
else:
nodes = Node.query.filter_by(network_id=this_node.network_id,
property2='True',
type='mafioso').all()
participants = []
for node in nodes:
if node.property1 == this_node.property1:
participants.append(node.property1 + ' (you!)')
else:
participants.append(node.property1)
random.shuffle(participants)
exp.save()
return Response(
response=json.dumps({'participants': participants}),
status=200,
mimetype='application/json')