Esempio n. 1
0
def ws_message(message, group_name, question):
    print("*********RECEIVE************")
    print("group_name: ", group_name)
    # Decrypt the url
    group_id = group_name[5:]
    question_nb = int(question[8:])
    # Decrypt the received message
    jsonmessage = json.loads(message.content['text'])
    player_id = jsonmessage['id']
    myplayer = OtreePlayer.objects.get(id=player_id)
    if 'decision' in jsonmessage:
        # Treat the decision
        argument = jsonmessage['argument']
        # Save the current decision
        currentdecision = myplayer.transitivedecision_set.create()
        currentdecision.related_question = Choices.questions[question_nb]
        currentdecision.player_decision = jsonmessage['decision']
        for a in argument:
            currentdecision.argument.add(Argument.objects.get(pk=a))
        currentdecision.save()  # important: save to DB!
    if 'question2expert' in jsonmessage:
        # Treat the question
        question = myplayer.questiontoexpert_set.create()
        question.question_text = jsonmessage['question2expert']
        question.related_question = Choices.questions[question_nb]
        question.save()
        # Update the experts with this new question
        textforgroup = json.dumps({
            "whatsnew": "newquestion",
        })
        channelsGroup("EXPERTS").send({
            "text": textforgroup,
        })
Esempio n. 2
0
def sendUpdatedData(**kwargs):
    def create_odict_from_object(obj, fieldnames):
        data = OrderedDict()
        for f in fieldnames:
            data[f] = getattr(obj, f)
        return data

    groups_qs = Group.objects.filter(session_id=kwargs["session_id"],
                                     subsession_id=kwargs["subsession_id"],
                                     round_number=kwargs["round_number"])
    # Update all groups
    qs_players = Player.objects.select_related('subsession', 'group') \
        .prefetch_related('transitivedecision_set__argument') \
        .all()

    starting_datetime = timezone.make_aware(kwargs["starting_time"],
                                            timezone.get_default_timezone())
    current_datetime = timezone.make_aware(datetime.now(),
                                           timezone.get_default_timezone())

    data_dec1 = []
    data_dec2 = []
    data_dec_notyet = []
    i = 0
    for single_date in date_range(starting_datetime, current_datetime):
        data_dec1.append(0)
        data_dec2.append(0)
        data_dec_notyet.append(0)
        for p in qs_players:
            transitivedecisions = list(
                p.transitivedecision_set.order_by('timestamp'))
            while True:
                if len(transitivedecisions) == 0:
                    data_dec_notyet[i] += 1
                    break
                last_trans_dec = transitivedecisions.pop()
                if last_trans_dec.timestamp < single_date:
                    if last_trans_dec.player_decision == Choices.choices_q123[
                            1]:
                        data_dec1[i] += 1
                    elif last_trans_dec.player_decision == Choices.choices_q123[
                            2]:
                        data_dec2[i] += 1
                    break
        i += 1
    for group in groups_qs:
        # Build the data
        # Send the data
        group_name = "group" + str(group.id)
        textforgroup = json.dumps({
            "datanotyet": data_dec_notyet,
            "data1": data_dec1,
            "data2": data_dec2,
        })
        channelsGroup(group_name).send({
            "text": textforgroup,
        })
Esempio n. 3
0
def sendUpdatedData(**kwargs):
    def create_odict_from_object(obj, fieldnames):
        data = OrderedDict()
        for f in fieldnames:
            data[f] = getattr(obj, f)
        return data

    groups_qs = Group.objects.filter(session_id=kwargs["session_id"],
                                     subsession_id=kwargs["subsession_id"],
                                     round_number=kwargs["round_number"])

    starting_datetime = timezone.make_aware(kwargs["starting_time"],
                                            timezone.get_default_timezone())
    current_datetime = timezone.make_aware(datetime.now(),
                                           timezone.get_default_timezone())
    for single_date in date_range(starting_datetime, current_datetime):
        print("single_date: ", single_date)

    for group in groups_qs:
        # Build the data
        dict_for_group = {}
        for p in group.get_players():
            new_key = "data_player_" + str(p.id_in_group)
            dict_for_group[new_key] = []
            transitivedecisions_for_thisplayer = list(
                p.transitivedecision_set.order_by('-timestamp'))
            if len(transitivedecisions_for_thisplayer) == 0:
                for _ in date_range(starting_datetime, current_datetime):
                    dict_for_group[new_key].append(Constants.c_initial_nbETP)
            elif len(transitivedecisions_for_thisplayer) == 1:
                only_trans_dec = transitivedecisions_for_thisplayer.pop()
                for single_date in date_range(starting_datetime,
                                              current_datetime):
                    if single_date < only_trans_dec.timestamp:
                        dict_for_group[new_key].append(
                            Constants.c_initial_nbETP)
                    else:
                        dict_for_group[new_key].append(only_trans_dec.nb_ETP)
            else:
                previous_trans_dec = transitivedecisions_for_thisplayer.pop()
                next_trans_dec = transitivedecisions_for_thisplayer.pop()
                for single_date in date_range(starting_datetime,
                                              current_datetime):
                    if single_date < previous_trans_dec.timestamp:
                        dict_for_group[new_key].append(
                            Constants.c_initial_nbETP)
                    elif single_date < next_trans_dec.timestamp:
                        dict_for_group[new_key].append(
                            previous_trans_dec.nb_ETP)
                    else:
                        while (next_trans_dec.timestamp < single_date) \
                                & (len(transitivedecisions_for_thisplayer) != 0):
                            previous_trans_dec = next_trans_dec
                            next_trans_dec = transitivedecisions_for_thisplayer.pop(
                            )
                        dict_for_group[new_key].append(
                            previous_trans_dec.nb_ETP)
        # Send the data
        group_name = "group" + str(group.id)
        #??? print("dict_for_group: ", dict_for_group)
        textforgroup = json.dumps(dict_for_group)
        channelsGroup(group_name).send({
            "text": textforgroup,
        })
Esempio n. 4
0
def ws_experts_disconnect(message):
    print("*********DISCONNECT************")
    channelsGroup("EXPERTS").discard(message.reply_channel)
Esempio n. 5
0
def ws_experts_connect(message):
    print("*********CONNECT************")
    channelsGroup("EXPERTS").add(message.reply_channel)
Esempio n. 6
0
def ws_disconnect(message, group_name, question):
    print("*********DISCONNECT************")
    channelsGroup(group_name).discard(message.reply_channel)
Esempio n. 7
0
def ws_connect(message, group_name, question):
    print("*********CONNECT************")
    channelsGroup(group_name).add(message.reply_channel)