def ask_firm_active_choice_recording(self, game_id, t, position, price): """called by active firm""" firm_id = self.data.firms_id[game_id] self.log("Firm active {} asks to save its price and position.".format( firm_id)) self.log("Client's time is {}, server's time is {}.".format( t, self.time_manager.t)) if t == self.time_manager.t: out = self.reply(function_name(), self.time_manager.t) if not self.data.current_state["active_replied"]: self.firm_active_first_step(firm_id, price, position, function_name()) self.set_state(role="firm", role_id=firm_id, state=function_name()) self.time_manager.check_state() return out elif t > self.time_manager.t: return "error/time_is_superior" else: return self.reply(function_name(), t)
def ask_customer_choice_recording(self, game_id, t, extra_view, firm): customer_id = self.data.customers_id[game_id] self.log("Customer {} asks for recording his choice as t {}: " "{} for extra view, {} for firm.".format(game_id, t, extra_view, firm)) self.log("Client's time is {}, server's time is {}.".format(t, self.time_manager.t)) self.set_time_since_last_request(game_id, "customer") if t == self.time_manager.t: out = self.reply(game_id, function_name(), self.time_manager.t, self.check_end(t)) if not self.data.current_state["customer_replies"][customer_id]: self.customer_end_of_turn(customer_id=customer_id, extra_view=extra_view, firm=firm) self.time_manager.check_state() else: self.log("Customer {} asks for recording his choice as t {} but already replied" .format(game_id, t, extra_view, firm)) state = "end_game" if self.check_end(t) else function_name() self.set_state(role="customer", role_id=customer_id, state=state) return out elif t > self.time_manager.t: return self.reply_error("time_is_superior") else: return self.reply(game_id, function_name(), t, self.check_end(t))
def ask_init(self, android_id): if not self.is_ended(): server_id, game_id = \ self.controller.id_manager.get_ids_from_android_id(android_id, max_n=len(self.data.roles)) if game_id != -1: role = self.get_role(server_id) if not role: return "Unknown server id: {}".format(server_id) self.data.roles[game_id] = role if role == "firm": return self.init_firms(function_name(), game_id, role) else: return self.init_customers(function_name(), game_id, role) else: return "Error with ID manager. Maybe not authorized to participate." else: return "Game ended. Connection refused"
def ask_customer_firm_choices(self, game_id, t): customer_id = self.data.customers_id[game_id] self.log("Customer {} asks for firm choices as t {}.".format( customer_id, t)) self.log("Client's time is {}, server's time is {}.".format( t, self.time_manager.t)) if t == self.time_manager.t: if self.time_manager.state == "active_has_played": x, prices = self.get_prices_and_positions() self.set_state(role="customer", role_id=customer_id, state=function_name()) return self.reply(function_name(), self.time_manager.t, x[0], x[1], prices[0], prices[1]) else: return "error/wait" elif t > self.time_manager.t: return "error/time_is_superior" else: x = self.data.history["firm_positions"][t] prices = self.data.history["firm_prices"][t] return self.reply(function_name(), t, x[0], x[1], prices[0], prices[1])
def reply_firm_passive_opponent_choice(self, t, position, price): if self.t == t and self.state == "firm_opponent_choice": self.opp_position = position self.opp_price = price self.queue.put(("ask_firm_passive_customer_choices", )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))
def reply_customer_choice_recording(self, t, end): if self.t == t and self.state == "customer_choice_recording": self.queue.put(( "customer_end_of_turn", end, )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))
def ask_admin_firm_choice(self, t): """called by admin""" game_id = -1 self.log("admin asks for active firm strategies.") self.log("Client's time is {}, server's time is {}.".format(t, self.time_manager.t)) if t == self.time_manager.t: if self.time_manager.state == "active_has_played" or \ self.time_manager.state == "active_has_played_and_all_customers_replied": firm_active_id = self.data.current_state["firm_status"].index("active") out = self.reply( game_id, function_name(), self.time_manager.t, # firm_active_id, self.data.current_state["firm_positions"][firm_active_id], self.data.current_state["firm_prices"][firm_active_id], ) return out else: return self.reply_error("wait") elif t > self.time_manager.t: return self.reply_error("time_is_superior") else: firm_active_id = self.data.history["firm_status"][t].index("active") return self.reply( game_id, function_name(), t, # firm_active_id, self.data.history["firm_positions"][t][firm_active_id], self.data.history["firm_prices"][t][firm_active_id], )
def ask_firm_passive_opponent_choice(self, game_id, t): """called by a passive firm""" firm_id = self.data.firms_id[game_id] opponent_id = (firm_id + 1) % 2 self.log("Firm passive {} asks for opponent strategy.".format(firm_id)) self.log("Client's time is {}, server's time is {}.".format(t, self.time_manager.t)) self.set_time_since_last_request(game_id, "firm") if t == self.time_manager.t: if self.time_manager.state == "active_has_played" or \ self.time_manager.state == "active_has_played_and_all_customers_replied": out = self.reply( game_id, function_name(), self.time_manager.t, self.data.current_state["firm_positions"][opponent_id], self.data.current_state["firm_prices"][opponent_id], ) self.time_manager.check_state() self.set_state(role="firm", role_id=firm_id, state=function_name()) return out else: return self.reply_error("wait") elif t > self.time_manager.t: return self.reply_error("time_is_superior") else: return self.reply( game_id, function_name(), t, self.data.history["firm_positions"][t][opponent_id], self.data.history["firm_prices"][t][opponent_id], )
def ask_firm_passive_customer_choices(self, game_id, t): firm_id = self.data.firms_id[game_id] self.log( "Firm passive {} asks for its number of clients.".format(firm_id)) self.log("Client's time is {}, server's time is {}.".format( t, self.time_manager.t)) if t == self.time_manager.t: if self.time_manager.state == "active_has_played_and_all_customers_replied": if not self.data.current_state["passive_gets_results"]: choices = self.get_client_choices(firm_id, t) out = self.reply(function_name(), self.time_manager.t, choices, self.check_end(t)) self.firm_end_of_turn(firm_id=firm_id, t=t, status="passive") state = "end_game" if self.check_end( t) else function_name() self.set_state(role="firm", role_id=firm_id, state=state) self.time_manager.check_state() return out else: return "error/wait" elif t > self.time_manager.t: return "error/time_is_superior" else: choices = self.get_client_choices(firm_id, t) return self.reply(function_name(), t, choices, self.check_end(t))
def ask_admin_customer_choices(self, t): game_id = -1 self.log("admin asks for client choices.") self.log("Client's time is {}, server's time is {}.".format(t, self.time_manager.t)) if t == self.time_manager.t: if self.time_manager.state == "active_has_played_and_all_customers_replied": firm_active_id = self.data.current_state["firm_status"].index("active") out = self.reply( game_id, function_name(), self.time_manager.t, self.get_client_choices(1, t), self.check_end(t) ) return out else: return self.reply_error("wait") elif t > self.time_manager.t: return self.reply_error("time_is_superior") else: firm_active_id = self.data.history["firm_status"][t].index("active") return self.reply( game_id, function_name(), t, self.get_client_choices(firm_active_id, t), self.check_end(t) )
def reply_firm_passive_customer_choices(self, *args): t = args[0] choices = args[1:-1] end = args[-1] if self.t == t and self.state == "firm_customer_choices": self.queue.put(( "firm_passive_end_of_turn", choices, end, )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))
def reply_customer_firm_choices(self, t, position_0, position_1, price_0, price_1): if self.t == t and self.state == "customer_firm_choices": self.queue.put(( "customer_choice", position_0, position_1, price_0, price_1, )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))
def ask_firm_active_customer_choices(self, game_id, t): """called by active firm""" firm_id = self.data.firms_id[game_id] self.log("Firm active {} asks the number of its clients.".format(firm_id)) self.log("Client's time is {}, server's time is {}.".format(t, self.time_manager.t)) self.set_time_since_last_request(game_id, "firm") if t == self.time_manager.t: if self.time_manager.state == "active_has_played_and_all_customers_replied": choices = self.get_client_choices(firm_id, t) out = self.reply(game_id, function_name(), self.time_manager.t, choices, self.check_end(t)) self.firm_end_of_turn(firm_id=firm_id, t=t, status="active") state = "end_game" if self.check_end(t) else function_name() self.set_state(role="firm", role_id=firm_id, state=state) self.time_manager.check_state() return out else: return self.reply_error("wait") elif t > self.time_manager.t: return self.reply_error("time_is_superior") else: choices = self.get_client_choices(firm_id, t) return self.reply(game_id, function_name(), t, choices, self.check_end(t))
def init_firms(self, func_name, game_id, role): if game_id not in self.data.firms_id.keys(): firm_id = len(self.data.firms_id) self.data.firms_id[game_id] = firm_id # if device already asked for init, get id else: firm_id = self.data.firms_id[game_id] state, position, price, opp_position, opp_price, profits = self.get_firms_data( firm_id) self.check_remaining_agents() self.set_state(role="firm", role_id=firm_id, state=function_name()) return self.reply(func_name, game_id, self.time_manager.t, role, position, state, price, opp_position, opp_price, profits)
def init_customers(self, func_name, game_id, role): if game_id not in self.data.customers_id.keys(): customer_id = len(self.data.customers_id) self.data.customers_id[game_id] = customer_id else: customer_id = self.data.customers_id[game_id] position, exploration_cost, utility_consumption, utility = self.get_customers_data( customer_id) self.check_remaining_agents() self.set_state(role="customer", role_id=customer_id, state=function_name()) return self.reply(func_name, game_id, self.time_manager.t, role, position, exploration_cost, utility_consumption, utility)
def reply_init(self, *args): if self.state == "init": self.game_id, self.t, self.role, self.position = args[:4] self.score = args[-1] self.n_positions = self.game_parameters["n_positions"] if self.role == "firm": self.firm_attributes["state"] = args[4] self.firm_attributes["price"] = args[5] initial_opponent_position = args[6] initial_opponent_price = args[7] self.firm_attributes["n_prices"] = self.game_parameters[ "n_prices"] if self.firm_attributes["state"] == "active": self.queue.put(( "firm_active_beginning_of_turn", initial_opponent_position, initial_opponent_price, )) else: self.queue.put(("firm_passive_beginning_of_turn", )) else: self.customer_attributes[ "extra_view_possibilities"] = np.arange( 0, self.n_positions - 1) self.queue.put(("ask_customer_firm_choices", )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))
def reply_firm_active_choice_recording(self, t): if self.t == t and self.state == "firm_choice_recording": self.queue.put(("ask_firm_active_customer_choices", )) else: raise Exception("Time problem or state problem with: {}".format( function_name()))