Skip to content

Commit

Permalink
reuse logic from MatchingPairsGame rules
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed Feb 19, 2024
1 parent 280b284 commit c74d3a9
Showing 1 changed file with 5 additions and 84 deletions.
89 changes: 5 additions & 84 deletions backend/experiment/rules/matching_pairs_lite.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import random
import json

from django.utils.translation import gettext_lazy as _

from .base import Base
from experiment.actions import Final, Playlist, Trial, Info
from experiment.actions.playback import MatchingPairs
from result.utils import prepare_result

from section.models import Section
from .matching_pairs import MatchingPairsGame
from experiment.actions import Final, Playlist, Info


class MatchingPairsLite(Base):
class MatchingPairsLite(MatchingPairsGame):
ID = 'MATCHING_PAIRS_LITE'
num_pairs = 8
show_animation = False
Expand All @@ -34,86 +27,14 @@ def next_round(self, session):
return [trial]
else:
# final score saves the result from the cleared board into account
social_info = self.social_media_info(session.experiment, session.final_score)
social_info['apps'].append('clipboard')
score = Final(
session,
title='Score',
final_text='Can you score higher than your friends and family? Share and let them try!',
final_text='End of the game',
button={
'text': 'Play again',
},
rank=self.rank(session, exclude_unfinished=False),
social=social_info,
feedback_info=self.feedback_info()
)
cont = self.get_matching_pairs_trial(session)
return [score, cont]

def get_matching_pairs_trial(self, session):
json_data = session.load_json_data()
pairs = json_data.get('pairs', [])
if len(pairs) < self.num_pairs:
pairs = list(session.playlist.section_set.order_by().distinct('group').values_list('group', flat=True))
random.shuffle(pairs)
selected_pairs = pairs[:self.num_pairs]
session.save_json_data({'pairs': pairs[self.num_pairs:]})
originals = session.playlist.section_set.filter(group__in=selected_pairs, tag='Original')
degradations = json_data.get('degradations')
if not degradations:
degradations = ['Original', '1stDegradation', '2ndDegradation']
random.shuffle(degradations)
degradation_type = degradations.pop()
session.save_json_data({'degradations': degradations})
if degradation_type == 'Original':
player_sections = player_sections = list(originals) * 2
else:
degradations = session.playlist.section_set.filter(group__in=selected_pairs, tag=degradation_type)
player_sections = list(originals) + list(degradations)
random.shuffle(player_sections)
playback = MatchingPairs(
sections=player_sections,
stop_audio_after=5,
show_animation=self.show_animation,
score_feedback_display=self.score_feedback_display,
)
trial = Trial(
title='Tune twins',
playback=playback,
feedback_form=None,
config={'show_continue_button': False}
)
return trial

def calculate_score(self, result, data):
''' not used in this experiment '''
pass

def calculate_intermediate_score(self, session, result):
''' will be called every time two cards have been turned '''
result_data = json.loads(result)
first_card = result_data['lastCard']
first_section = Section.objects.get(pk=first_card['id'])
first_card['filename'] = str(first_section.filename)
second_card = result_data['currentCard']
second_section = Section.objects.get(pk=second_card['id'])
second_card['filename'] = str(second_section.filename)
if first_section.group == second_section.group:
if 'seen' in second_card:
score = 20
given_response = 'match'
else:
score = 10
given_response = 'lucky match'
else:
if 'seen' in second_card:
score = -10
given_response = 'misremembered'
else:
score = 0
given_response = 'no match'
prepare_result('move', session, json_data=result_data,
score=score, given_response=given_response)
return score


return score

0 comments on commit c74d3a9

Please sign in to comment.