from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) from statistics import mean import random from random import randrange, shuffle from itertools import cycle from main.scenarios import Scenario, GraphType, VehicleType author = 'Marlene Bargou' doc = """ Continuous Trolley """ class Constants(BaseConstants): name_in_url = 'main' players_per_group = None num_rounds = 1 base_scenarios = [ Scenario(people_left=5, people_right=1, people_middle=0), Scenario(people_left=1, people_right=5, people_middle=0, swapped_people=True), Scenario(people_left=5, people_right=1, people_middle=1), Scenario(people_left=1, people_right=5, people_middle=1, swapped_people=True), Scenario(people_left=4, people_right=0, people_middle=1), Scenario(people_left=0, people_right=4, people_middle=1, swapped_people=True), # Scenario(people_left=5, people_right=1, people_middle=0, vehicle_right=VehicleType.BIKE, swapped_people=True), Scenario(people_left=5, people_right=1, people_middle=0, vehicle_right=VehicleType.BIKE) ] # add Scenarios with no graphs, one accident graph, one fatality graph, both accident/fatality graph # graph_scenarios = base_scenarios + [s.copy().set_graphs(GraphType.ACCIDENT) for s in base_scenarios] + [s.copy( # ).set_graphs(GraphType.FATALITY) for s in base_scenarios] + [s.copy().set_graphs(GraphType.ACCIDENT, # GraphType.FATALITY) for s in base_scenarios] # set graphs to accident/fatality graph (8) graph_scenarios = [s.set_graphs(GraphType.ACCIDENT, GraphType.FATALITY) for s in base_scenarios] # add Scenarios where graphs are swapped (8 * 2 = 16) swapped_graph_scenarios = graph_scenarios + [s.copy().swap_graphs() for s in graph_scenarios] # all Scenarios with graphs swapped (16) scenarios = swapped_graph_scenarios class Subsession(BaseSubsession): def creating_session(self): # shuffle scenarios shuffled_scenarios = random.sample(Constants.scenarios, len(Constants.scenarios)) # endlessly cycle over the scenarios scenario_iter = cycle(shuffled_scenarios) # draw one scenario for each participant for p in self.get_players(): drawn_scenario = next(scenario_iter) p.participant.vars[self.round_number] = drawn_scenario # save data p.scenario_name = drawn_scenario.get_treatment_name() p.scenario_id = drawn_scenario.scenario_id p.people_left = drawn_scenario.people_left p.people_right = drawn_scenario.people_right p.people_middle = drawn_scenario.people_middle p.vehicle_left = drawn_scenario.vehicle_left.value p.vehicle_right = drawn_scenario.vehicle_right.value p.swapped_people = drawn_scenario.swapped_people p.graph1 = drawn_scenario.graph1.value p.graph2 = drawn_scenario.graph2.value p.swapped_graphs = drawn_scenario.swapped_graphs p.participant.vars['graph1'] = drawn_scenario.graph1.value p.participant.vars['graph2'] = drawn_scenario.graph2.value p.participant.vars['people_middle'] = drawn_scenario.people_middle class Group(BaseGroup): pass class Player(BasePlayer): scenario_id = models.IntegerField() scenario_name = models.StringField() # vehicle info people_left = models.IntegerField() vehicle_left = models.StringField() people_middle = models.IntegerField() people_right = models.IntegerField() vehicle_right = models.StringField() swapped_people = models.BooleanField() # graph info graph1 = models.StringField() graph2 = models.StringField() swapped_graphs = models.BooleanField() # user input info x_left_initial = models.FloatField() y_left_initial = models.FloatField() x_right_initial = models.FloatField() y_right_initial = models.FloatField() x_left = models.FloatField() y_left = models.FloatField() x_right = models.FloatField() y_right = models.FloatField() duration_decision = models.FloatField()