from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Task(Page): form_model = 'player' form_fields = ['count_v', 'test_skip'] timer_text = 'Verfügbare Zeit für Stufe 2:' def is_displayed(self): #import time return self.get_timeout_seconds() > 0 def vars_for_template(self): import lipsum import random import string def randomString(stringLength=10): """Generate a random string of fixed length """ letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(stringLength)) sentence = lipsum.generate_words(100) #print(sentence) from PIL import Image from PIL import ImageFont from PIL import ImageDraw #font = ImageFont.truetype('\\Users\\obst\\Dropbox\\Diss\\oTree\\fonts\\Roboto-Regular.ttf', size=18) #font = ImageFont.truetype('../fonts/Roboto-Regular.ttf', size=18) #font = ImageFont.truetype('https://github.com/grpc/grpc.github.io/raw/master/fonts/roboto/Roboto-Regular.ttf', size=18) #text = sentence #max_width = 500 image = Image.new('RGB', (500, 350), color = (240, 240, 240)) def text_wrap(text, font, max_width): lines = [] # If the width of the text is smaller than image width # we don't need to split it, just add it to the lines array # and return if font.getsize(text)[0] <= max_width: lines.append(text) else: # split the line by spaces to get words words = text.split(' ') i = 0 # append every word to a line while its width is shorter than image width while i < len(words): line = '' while i < len(words) and font.getsize(line + words[i])[0] <= max_width: line = line + words[i] + " " i += 1 if not line: line = words[i] i += 1 # when the line gets longer than the max width do not append the word, # add the line to the lines array lines.append(line) return lines def draw_text(text, imgname): # open the background file img = image # size() returns a tuple of (width, height) ##image_size = img.size # create the ImageFont instance #font_file_path = '\\Users\\obst\\Dropbox\\Diss\\oTree\\fonts\\Roboto-Regular.ttf' font_file_path = 'Roboto-Regular.ttf' #font_file_path = 'https://github.com/grpc/grpc.github.io/raw/master/fonts/roboto/Roboto-Regular.ttf' font = ImageFont.truetype(font_file_path, size=18, encoding="unic") # create an array with the right amount of lines lines = text_wrap(text, font, 480) # lines = text_wrap(text, font, image_size[0]) text_with_breaks = "" for i in lines: text_with_breaks += i+"\n" #print(lines) draw = ImageDraw.Draw(img) color = 'rgb(30, 30, 30)' draw.text((10, 10), text_with_breaks, fill=color, font=font) #img.save('_static\\' + imgname + '.png') img.save('_static/' + imgname + '.png') # define the image name imagename = randomString(10) # draw and save the picture draw_text(sentence, imagename) anzahl = sentence.count('v') print("Anzahl des Buchstaben v:", anzahl, " (Spieler ", self.player.id_in_group,")") if self.player.amount_v == None: self.player.amount_v = anzahl print("Die Variable amount_v war für Player ", self.player.id_in_group, "nicht gesetzt.") else: print("ACHTUNG: Die Variable amount_v ist bereits gesetzt für ", self.player.id_in_group) if self.round_number > 1: self.player.previous_count_v = self.player.in_round(self.round_number - 1).count_v self.player.previous_amount_v = self.player.in_round(self.round_number - 1).amount_v imgtmp = imagename + ".png?" + randomString(20) return dict( img = imgtmp, amount_v = anzahl, ) def before_next_page(self): if self.player.count_v == self.player.amount_v: self.player.payoff += self.participant.vars["payrate"] print("Player payoff:", self.player.payoff, "\nPayrate: ", self.participant.vars["payrate"]) def get_timeout_seconds(self): import time #Zu Testzwecken: Section kann übersprungen werden: if self.round_number > 1: for rd in self.player.in_previous_rounds(): if rd.test_skip: return 0 return self.participant.vars['expiry'] - time.time() else: #Diese Zeile muss erhalten bleiben return self.participant.vars['expiry'] - time.time() page_sequence = [Task]