from otree.api import Currency as c, currency_range from . import pages from ._builtin import Bot from .models import Constants import test_cases as tc import random as r # FOR ALPHA/BETA TESTING class PlayerBot(Bot): """"This class represents a virtual player that will input the values that each page requires and that allows to test if there are errors in a specific page or field. -Yield statements will change the value of the fields -Assert statements check if the field values have a specific value or not There are two main modes for the bots *alpha: They check if the game runs with values that belong to the boundaries of each variable. It has 4 submodes **inside: checks for errors using inside the boundaries **boundaries: checks for errors using the boundary values **outside: checks for errors using values outside the boundaries **types: checks if the values that has been inputted are the same type of the respective variables *beta: They check how the game would run in a real environment with a big amount of players. To activate it, run the game with N bots in the 'inside' mode """ def play_round(self): test_mode = 'inside' tests_for_lender = tc.lender_cases[test_mode] tests_for_borrower = tc.borrower_cases[test_mode] if test_mode in tc.available_test_modes: if self.player.role() == 'lender': yield (pages.Loan_Amount, {'loan': tests_for_lender['loan']}) assert self.group.loan == tests_for_lender['loan'] if Constants.contract_enforcement is False: if self.group.accept_loan == 'Yes': yield (pages.Collateral_Decision, 6) assert self.group.satisfied == tests_for_lender['satisfied'] if self.group.satisfied == 'No': seized_collateral_aux = r.randrange(0, self.group.value_of_collateral) if Constants.contract_enforcement is False: yield (pages.Seize_Collateral, {'seized_collateral': seized_collateral_aux}) assert self.group.seized_collateral == seized_collateral_aux elif self.player.role() == 'borrower': yield (pages.Collateral_Offer, {'collateral': tests_for_borrower['collateral']}) assert self.group.collateral == c(tests_for_borrower['collateral']) yield (pages.Loan_Package, {'accept_loan': tests_for_borrower['accept_loan']}) assert self.group.accept_loan == tests_for_borrower['accept_loan'] if self.group.accept_loan == 'Yes': lender_return_aux = r.randrange(0, Constants.productivity * self.group.loan) yield (pages.Realize_Return, {'lender_return': lender_return_aux}) assert self.group.lender_return == c(lender_return_aux) else: print('There are only two roles: Borrower and Lender. You ' 'are using another one that is not allowed.') yield (pages.Results) else: print('Testing mode error: An invalid mode has been inputted.')