def post_invest_plan(self, cmd: CommunitcationParserResult): # Verify, that company exists company = self.logic_handler.companies_handler.company_by_uuid( cmd.company_uuid) if company is None: msg = CommunicationProtocol.invest_plan_create_and_post( InvestmentPlanCreateResult.NO_SUCH_COMPANY.value) self.ws.write_message(msg) return # verify that player is owner player_uuid = company.owner_info[1] if player_uuid != self.client_data.uuid: msg = CommunicationProtocol.invest_plan_create_and_post( InvestmentPlanCreateResult.NOT_OWNER.value) self.ws.write_message(msg) return # Create plan object in investment Market plan = self.investment_market.create_and_post_investment_plan( server_time=self.logic_handler.server_time, c_uuid=cmd.company_uuid, c_name=company.name, invest_value=cmd.invest_value, i_type=cmd.invest_type, payback_value=cmd.payback_value, cycle_period=cmd.invest_cycles) # Append plan to pending list of investment plans company.invest_plan_append(plan) # send result msg = CommunicationProtocol.invest_plan_create_and_post( InvestmentPlanCreateResult.SUCCESS.value) self.ws.write_message(msg)
def on_logged(self, result: CommunitcationParserResult): client_data = self.users_dao.get_user_by_login(result.login, result.password) if client_data.uuid != "": self.clients_handler.store_connected_client(self) # Client data should be obtain from data base self.client_data = copy.deepcopy( client_data) # Copy and create new object of Client data # Give to operation module client data self.client_operation.client_data = self.client_data self.logged_in = True # Admin request sent if result.admin is True: # If client data credentials has admin if client_data.admin is True: # Set connection as admin self.is_admin = True print("client connected. login : "******"\tuuid: ", self.client_data.uuid, "\t as admin: ", self.is_admin) # send message to client msg = CommunicationProtocol.create_login_result_msg( self.logged_in, client_data.uuid, msg=client_data.error_message, admin=self.is_admin) self.write_message(msg)
def request_working_plan_create(self, cmd: CommunitcationParserResult): result = self.logic_handler.request_working_plan_create( c_uuid=cmd.company_uuid, begin_cycle=cmd.begin_cycle, end_cycle=cmd.end_cycle, target=cmd.target_earn) plan_request = CommunicationProtocol.working_plan_request_result( result) self.ws.write_message(plan_request)
def parse_clinet_message(msg): # message should be json msg = CommunicationProtocol.verify_msg(msg) if msg is None: # end function is message is none return CommunitcationParserResult(False) switcher = { MessageType.LOGIN.value: CommunitcationParser.login_request, MessageType.REGISTRATION.value: CommunitcationParser.registration_request, MessageType.KEEP_ALIVE.value: CommunitcationParser.keep_alive_respond, MessageType.COMPANIES_OPEN_LIST.value: CommunitcationParser.companies_all_list_request, MessageType.CLIENT_DATA.value: CommunitcationParser.client_data_request, MessageType.BUY_STOCK.value: CommunitcationParser.stock_buy_request, MessageType.NEWS_BY_TIME.value: CommunitcationParser.news_bytime_request, MessageType.NEWS_BY_AMOUNT.value: CommunitcationParser.news_byamount_request, MessageType.SELL_SILVER_STOCK.value: CommunitcationParser.sell_silver_stock, MessageType.MESSAGING.value: CommunitcationParser.messaging, MessageType.COMPANY_SILVER_STOCK_HISTORY.value: CommunitcationParser.history_silver_stock, MessageType.CREATE_PLAYER_COMPANY.value: CommunitcationParser.create_player_company, MessageType.WORKING_PLAN_REQUEST.value: CommunitcationParser.request_working_plan, MessageType.WORKING_PLAN_APPLY.value: CommunitcationParser.apply_working_plan, MessageType.SHORT_INFO.value: CommunitcationParser.short_info_request, MessageType.INVEST_PLAN_CREATE.value: CommunitcationParser.create_invest_plan, MessageType.INVEST_MARKET_LIST.value: CommunitcationParser.list_invest_market, MessageType.INVEST_MAKE.value: CommunitcationParser.investment_make, MessageType.COMPANIES_NAME_LIST.value: CommunitcationParser.companies_name_list_request, MessageType.MARKET_LIST.value: CommunitcationParser.market_list, MessageType.SELL_ON_STOCK_MARKET.value: CommunitcationParser.sell_stock_on_market } # Verification is this admin message func = switcher.get(int(msg["type"])) result = func(msg) return result
def short_client_data(self, cmd): utils.unused(cmd) player_data = self.client_data.player_data.get_player_value_info( ) # Money and stock's money news_list = self.logic_handler.news_handler.get_news_list_byamount(3) msg = CommunicationProtocol.prepare_short_info( self.client_data.login, player_data, self.logic_handler.server_time, self.logic_handler.game_cycle, news_list) self.ws.write_message(msg)
def request_client_data(self, cmd): utils.unused(cmd) s_list = self.client_data.player_data.get_all_stocks_to_list() c_list = self.client_data.player_data.get_all_own_companies() client_data_msg = CommunicationProtocol.create_client_data_msg( login=self.client_data.login, money=self.client_data.player_data.money, stock_list=s_list, companies_list=c_list, server_time=self.logic_handler.server_time) self.ws.write_message(client_data_msg)
def create_player_company_request(self, cmd: CommunitcationParserResult): result: CompanyCreateResult result = self.logic_handler.request_to_create_closed_company( company_name=cmd.new_company_name, b_type=cmd.b_type_int, money=cmd.money_invest, stocks=cmd.stocks_list, client_data=self.client_data) # Send result message create_company_msg = CommunicationProtocol.create_company_result( result.value) self.ws.write_message(create_company_msg)
def receive_investment(self, debt: float, contract_uuid: str): # Verify, that investment contract is still in player's active contract = self.client_data.player_data.get_investment_contract_active( contract_uuid) if contract is None: return # Close contract and give money to player self.client_data.player_data.investment_close_contract(debt, contract) # Notify player about this msg = CommunicationProtocol.investment_receive(debt, contract.company_uuid, contract.company_name) self.ws.write_message(msg)
def investment_apply(self, cmd: CommunitcationParserResult): # First let's check if investment plan exists plan = self.investment_market.make_investment(cmd.investment_uuid) if plan is None: msg = CommunicationProtocol.investment_make_result( InvestmentMakeResult.NO_SUCH_INVESTMENT_PLAN.value) self.ws.write_message(msg) return # Verify, that company still exists company = self.logic_handler.companies_handler.company_by_uuid( plan.company_uuid) if company is None: msg = CommunicationProtocol.investment_make_result( InvestmentMakeResult.NO_SUCH_COMPANY.value) self.ws.write_message(msg) return # Verify that company has this investment plan as pending if company.invesment_plan_pending_existance_verify(plan) is False: msg = CommunicationProtocol.investment_make_result( InvestmentMakeResult.NO_PLAN_IN_COMPANY.value) self.ws.write_message(msg) return # After this applying functions go. # Verify, that player has enough money and remove them from player if self.client_data.player_data.make_investment( plan, self.logic_handler.game_cycle) is False: msg = CommunicationProtocol.investment_make_result( InvestmentMakeResult.NOT_ENOUGH_MONEY.value) self.ws.write_message(msg) return # Move investment to production state in company company.investment_apply(plan) # Set end cycle of investment plan # Send positive result to client msg = CommunicationProtocol.investment_make_result( InvestmentMakeResult.SUCCESS.value) self.ws.write_message(msg)
def messaging_execution(self, result_msg: CommunitcationParserResult): res: MessagingParserResult res = MessagingModule.parse_message(result_msg) if res.msg_type == MessagingTypes.NONE: # Got error in parsing message. Do nothing return elif res.msg_type == MessagingTypes.GLOBAL: sender_name = self.client_data.login clients_list = self.clients_handler.list_connected_clients() msg_json = CommunicationProtocol.create_global_message( sender_name, self.logic_handler.server_time, res.msg_text) for client in clients_list: client: ClientHandler client.client_operation.ws.write_message(msg_json)
def request_open_companies_list(self, cmd): utils.unused(cmd) c_list = self.logic_handler.companies_open_list_client() c_list_msg = CommunicationProtocol.create_companies_open_list(c_list) self.ws.write_message(c_list_msg)
def market_elements_list(self, cmd: CommunitcationParserResult): utils.unused(cmd) body = self.stock_market.get_market_list() msg = CommunicationProtocol.create_market_list(body) self.ws.write_message(msg)
def companies_name_list(self, cmd: CommunitcationParserResult): utils.unused(cmd) c_list = self.logic_handler.companies_handler.get_companies_id_to_list( ) msg = CommunicationProtocol.create_companies_name_list(c_list) self.ws.write_message(msg)
def send_keep_alive(self): msg = CommunicationProtocol.create_keep_alive_msg() self.write_message(msg)
def request_to_sell_stock(self, cmd: CommunitcationParserResult): result: StockSellResult result = self.logic_handler.request_to_sell_stock( cmd.company_uuid, cmd.stock_amount, self.client_data) result_msg = CommunicationProtocol.create_sell_result(result.value) self.ws.write_message(result_msg)
def apply_working_plan(self, cmd: CommunitcationParserResult): result: CompanyWorkingRequestResult result = self.logic_handler.request_working_plan_apply( cmd.company_uuid, cmd.w_plan_uuid) msg = CommunicationProtocol.working_plan_apply(result.value) self.ws.write(msg)
def request_silver_stock_history(self, cmd: CommunitcationParserResult): history_list = self.logic_handler.companies_handler.get_silver_stocks_history( cmd.company_uuid) history_list_msg = CommunicationProtocol.create_history_silver_stock( history_list) self.ws.write_message(history_list_msg)
def request_for_news_list_bytime(self, cmd: CommunitcationParserResult): news_list = self.logic_handler.request_news_list_bytime(cmd.news_time) news_list_msg = CommunicationProtocol.create_news_list(news_list) self.ws.write_message(news_list_msg)
def list_invest_market(self, cmd: CommunitcationParserResult): utils.unused(cmd) m_list = self.investment_market.list_of_investment_offers() msg = CommunicationProtocol.invest_market_list(m_list) self.ws.write(msg)