from . import models from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants class Introduction(Page): form_model = 'player' form_fields = ['bids_quant'] def vars_for_template(self): p = self.player return { 'your_p': p.participant.vars['rnd_demand_p'], 'your_q': p.participant.vars['rnd_demand_q'], } def before_next_page(self): self.player.is_active = True if self.timeout_happened: self.player.is_active = False print('Игрок ') print(self.player.id_in_group) print(' из группы ') print(self.player.group.id_in_subsession) print(' теперь считается неактивным') class Bid(Page): form_model = 'player' form_fields = ['first_bid_price', 'first_bid_vol', 'second_bid_price', 'second_bid_vol', 'third_bid_price', 'third_bid_vol'] def get_form_fields(self): player = self.player if player.bids_quant == 3: return ['first_bid_price', 'first_bid_vol', 'second_bid_price', 'second_bid_vol', 'third_bid_price', 'third_bid_vol'] else: if player.bids_quant == 2: return ['first_bid_price', 'first_bid_vol', 'second_bid_price', 'second_bid_vol'] else: return ['first_bid_price', 'first_bid_vol'] def error_message(self, values): print('В чем дело') player = self.player if player.bids_quant == 3: if (values['first_bid_vol'] + values['second_bid_vol'] + values[ 'third_bid_vol'] > 100 + 100*self.group.active_players): print('Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')') return 'Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')' else: if player.bids_quant == 2: if (values['first_bid_vol'] + values['second_bid_vol'] > 100 + 100*self.group.active_players): print('Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')') return 'Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')' else: if (values['first_bid_vol'] > 100 + 100*self.group.active_players): print('Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')') return 'Выбрано больше путевок, чем есть на рынке (всего ' + str(100 + 100*self.group.active_players)+')' def vars_for_template(self): p = self.player return { 'your_p': p.participant.vars['rnd_demand_p'], 'your_q': p.participant.vars['rnd_demand_q'], 'your_bids': p.bids_quant } def before_next_page(self): if self.timeout_happened: self.player.first_bid_vol = 0 self.player.first_bid_price = 0 self.player.second_bid_vol = 0 self.player.second_bid_price = 0 self.player.third_bid_vol = 0 self.player.third_bid_price = 0 else: #упорядочим если много заявок if self.player.bids_quant == 3: if self.player.first_bid_price > self.player.second_bid_price: self.player.first_bid_price, self.player.second_bid_price = self.player.second_bid_price, self.player.first_bid_price self.player.first_bid_vol, self.player.second_bid_vol = self.player.second_bid_vol, self.player.first_bid_vol if self.player.second_bid_price > self.player.third_bid_price: self.player.second_bid_price, self.player.third_bid_price = self.player.third_bid_price, self.player.second_bid_price self.player.second_bid_vol, self.player.third_bid_vol = self.player.third_bid_vol, self.player.second_bid_vol if self.player.first_bid_price > self.player.second_bid_price: self.player.first_bid_price, self.player.second_bid_price = self.player.second_bid_price, self.player.first_bid_price self.player.first_bid_vol, self.player.second_bid_vol = self.player.second_bid_vol, self.player.first_bid_vol else: if self.player.bids_quant == 2: if self.player.first_bid_price > self.player.second_bid_price: self.player.first_bid_price, self.player.second_bid_price = self.player.second_bid_price, self.player.first_bid_price self.player.first_bid_vol, self.player.second_bid_vol = self.player.second_bid_vol, self.player.first_bid_vol print(self.player.first_bid_vol) print(self.player.second_bid_vol) print(self.player.third_bid_vol) class ResultsWaitPage(WaitPage): body_text = "Подождем других..." template_name = 'parks_auction/ResultsWaitPage.html' def after_all_players_arrive(self): self.group.set_payoffs() def vars_for_template(self): p_s = [] q_s = [] bids = [] p_s.append(0) p_s.append(self.player.first_bid_price) bids.append({'price': self.player.first_bid_price,'vol': self.player.first_bid_vol}) if self.player.bids_quant == 3: p_s.append(self.player.second_bid_price) p_s.append(self.player.third_bid_price) q_s.append(self.player.first_bid_vol+self.player.second_bid_vol+self.player.third_bid_vol) q_s.append(self.player.second_bid_vol+self.player.third_bid_vol) q_s.append(self.player.third_bid_vol) bids.append({'price':self.player.second_bid_price, 'vol':self.player.second_bid_vol}) bids.append({'price':self.player.third_bid_price, 'vol':self.player.third_bid_vol}) else: if self.player.bids_quant == 2: p_s.append(self.player.second_bid_price) q_s.append(self.player.first_bid_vol + self.player.second_bid_vol) q_s.append(self.player.second_bid_vol) bids.append({'price':self.player.second_bid_price, 'vol':self.player.second_bid_vol}) else: q_s.append(self.player.first_bid_vol) q_s.append(0) return {'p_s': p_s, 'q_s': q_s, 'bids_quant': self.player.bids_quant, 'my_bids': bids } class Results1(Page): def is_displayed(self): sum_units = 0 for p in self.group.get_players(): p.count_quota() sum_units = sum_units + p.units added_supply = self.group.volume_cap - sum_units for p in self.group.get_players(): p.share = p.units/sum_units p.units = p.units + round(added_supply*p.share) return True def vars_for_template(self): graph_points = [] my_graph = [] c_jumps = self.session.vars['jumps'] c_r_plus = self.session.vars['r_plus'] c_r_minus = self.session.vars['r_minus'] max_price = c_jumps[len(c_jumps)-1]+100 volume_cap = 100 + 100*self.group.active_players graph_points.append([0, c_r_plus[0]]) i = 0 for i in range(len(c_jumps)): graph_points.append([c_jumps[i], c_r_plus[i]]) graph_points.append([c_jumps[i], c_r_minus[i]]) print(i, [c_jumps[i], c_r_minus[i]]) graph_points.append([c_jumps[len(c_jumps)-1]+100, 0]) if self.player.bids_quant == 3: my_graph.append([0, self.player.first_bid_vol+self.player.second_bid_vol+self.player.third_bid_vol]) my_graph.append([self.player.first_bid_price, self.player.first_bid_vol + self.player.second_bid_vol + self.player.third_bid_vol]) my_graph.append([self.player.first_bid_price, self.player.second_bid_vol + self.player.third_bid_vol]) my_graph.append([self.player.second_bid_price, self.player.second_bid_vol + self.player.third_bid_vol]) my_graph.append([self.player.second_bid_price, self.player.third_bid_vol]) my_graph.append([self.player.third_bid_price, self.player.third_bid_vol]) my_graph.append([self.player.third_bid_price, 0]) my_graph.append([max_price, 0]) else: if self.player.bids_quant == 2: my_graph.append([0, self.player.first_bid_vol + self.player.second_bid_vol]) my_graph.append([self.player.first_bid_price, self.player.first_bid_vol + self.player.second_bid_vol]) my_graph.append([self.player.first_bid_price, self.player.second_bid_vol]) my_graph.append([self.player.second_bid_price, self.player.second_bid_vol]) my_graph.append([self.player.second_bid_price, 0]) my_graph.append([max_price, 0]) else: my_graph.append([0, self.player.first_bid_vol ]) my_graph.append([self.player.first_bid_price, self.player.first_bid_vol]) my_graph.append([self.player.first_bid_price, 0]) my_graph.append([max_price, 0]) return { 'jumps': self.session.vars['jumps'], 'r_plus': self.session.vars['r_plus'], 'r_minus': self.session.vars['r_minus'], 'capacities': self.session.vars['capacities'], 'price_to_stop': max_price, 'vol_cap': volume_cap, 'total_production': self.group.total_units, 'g_p_p': graph_points, 'price': self.group.price, 'max_total_vol': c_r_plus[0], 'my_g_p_p': my_graph, 'my_quant': self.player.units } class WaitBeforeBid(WaitPage): def after_all_players_arrive(self): print('Все тут') self.group.active_players = 0 for player in self.group.get_players(): if player.is_active: self.group.active_players = self.group.active_players + 1 print('Активных игроков:') print(self.group.active_players) class ChoosePrice(Page): form_model = 'player' form_fields = ['user_price'] def is_displayed(self): return self.player.units > 0 def vars_for_template(self): intersection_q = self.player.units intersection_p = self.player.indiv_demand_intercept - self.player.indiv_demand_coeff*intersection_q actual_demand = [] actual_demand.append([0, intersection_q]) actual_demand.append([intersection_p, intersection_q]) actual_demand.append([self.player.indiv_demand_intercept, 0]) return { 'my_quant': self.player.units, 'cutoff_price': self.group.price, 'marginal_cost': Constants.marginal_cost, 'min_price': self.group.price+Constants.marginal_cost, 'trunc_demand': actual_demand, 'your_p': self.player.participant.vars['rnd_demand_p'], 'your_q': self.player.participant.vars['rnd_demand_q'] } class FinalResult(Page): pass page_sequence = [Introduction, WaitBeforeBid, Bid, ResultsWaitPage, Results1, ChoosePrice]