def process_client(self, data_lst, sender): if data_lst[0] == NetMsgs.car_confirm: if self.car: _btn = self._buttons(self.car)[0] _btn['state'] = NORMAL _btn.setAlphaScale(1) self.car = car = data_lst[1] eng.log('car confirmed: ' + car) btn = self._buttons(car)[0] btn['state'] = DISABLED btn.setAlphaScale(.25) if data_lst[0] == NetMsgs.car_deny: eng.log('car denied') if data_lst[0] == NetMsgs.car_selection: car = data_lst[1] eng.log('car selection: ' + car) btn = self._buttons(car)[0] btn['state'] = DISABLED btn.setAlphaScale(.25) if data_lst[0] == NetMsgs.car_deselection: car = data_lst[1] eng.log('car deselection: ' + car) btn = self._buttons(car)[0] btn['state'] = NORMAL btn.setAlphaScale(1) if data_lst[0] == NetMsgs.start_race: eng.log('start_race: ' + str(data_lst)) # manage as event game.logic.season = SingleRaceSeason() game.fsm.demand('Race', self.track_path, self.car, data_lst[2:])
def on_car_selected(self, car): dev = self.mediator.options['development'] self.season = SingleRaceSeason(self.__season_props( self.mediator.gameprops, car, [car], self.mediator.options['settings']['cars_number'], True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'])) self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start()
def on_start(self): GameLogic.on_start(self) dev = self.mdt.options['development'] car = dev['car'] if 'car' in dev else '' track = dev['track'] if 'track' in dev else '' if car and track: self.season = SingleRaceSeason(Utils().season_props(car)) self.season.attach_obs(self.mdt.event.on_season_end) self.season.attach_obs(self.mdt.event.on_season_cont) self.mdt.fsm.demand('Race', track, car, self.season.drivers) else: self.mdt.fsm.demand('Menu')
def on_driver_selected_server(self, player_name, track, car, cars): dev = self.mediator.options['development'] #self.season = SingleRaceSeason(self.__season_props( # self.mediator.gameprops, car, cars, # self.mediator.options['settings']['cars_number'], True, 0, 0, 0, # dev['race_start_time'], dev['countdown_seconds'])) sprops = self.__season_props( self.mediator.gameprops, car, cars, self.mediator.options['settings']['cars_number'], True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'], self.mediator.options['settings']['camera']) sprops.car_names = cars sprops.player_car_names = cars self.season = SingleRaceSeason(sprops) for i, drv in enumerate(self.season.logic.drivers): dinfo = self.mediator.gameprops.drivers_info[i] drv.logic.dprops.info = dinfo self.season.logic.props.car_names = cars self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start() #self.mediator.options['settings']['player_name'] = player_name self.mediator.gameprops.player_name = player_name #self.mediator.options.store() packet = [NetMsgs.start_race, len(self.eng.car_mapping)] def process(k): '''Processes a car.''' for addr, carname in self.eng.car_mapping.items(): if carname == k: return addr sprops = self.season.props drivers = sprops.drivers for k in self.eng.car_mapping.values(): for _drv in drivers: if _drv.dprops.car_name == k: drv = _drv packet += [ process(k), drv.dprops.info.img_idx, drv.dprops.car_name, drv.dprops.info.name, drv.dprops.info.speed, drv.dprops.info.adherence, drv.dprops.info.stability ] self.eng.server.send(packet) self.eng.log_mgr.log('start race (on driver): ' + str(packet)) self.eng.log('drivers: ' + str(drivers)) self.eng.do_later( 2.0, self.mediator.fsm.demand, ['Race', track, car, cars, self.season.logic.drivers])
def evaluate_starting(self): connections = eng.connections + [self] if all(conn in self.current_cars for conn in connections): packet = [NetMsgs.start_race, len(self.current_cars)] def process(k): '''Processes a car.''' return 'server' if k == self else k.getAddress().getIpString() for k, val in self.current_cars.items(): packet += [process(k), val] Server().send(packet) eng.log('start race: ' + str(packet)) curr_car = self.current_cars[self] # manage as event game.logic.season = SingleRaceSeason() game.fsm.demand('Race', self.track_path, curr_car, packet[2:])
def on_car_start_client(self, track, car, cars, packet, room): #drv_info = self.mediator.gameprops.drivers_info #for i, drv_name in enumerate(packet[4::3]): # drv_info[i] = drv_info[i]._replace(name=drv_name) #self.mediator.gameprops = self.mediator.gameprops._replace(drivers_info=drv_info) dev = self.mediator.options['development'] sprops = self.__season_props( self.mediator.gameprops, car, cars, len(cars), True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'], self.mediator.options['settings']['camera'], 'onlinemp', room) self.season = SingleRaceSeason(sprops) drivers = sprops.drivers packet_drivers = [] for i in range(packet[0]): offset = i * 6 pdrv = packet[1 + offset: 1 + offset + 6] packet_drivers += [pdrv] for pdrv in packet_drivers: for drv in drivers: if drv.dprops.info.img_idx == pdrv[0]: drv.logic.dprops.car_name = pdrv[1] drv.logic.dprops.info.name = pdrv[2] drv.logic.dprops.info.speed = pdrv[3] drv.logic.dprops.info.adherence = pdrv[4] drv.logic.dprops.info.stability = pdrv[5] for i, pdrv in enumerate(packet_drivers): prev_drv = drivers[i] for j, drv in enumerate(drivers): if drv.dprops.info.img_idx == pdrv[0]: drivers[i] = drv drivers[j] = prev_drv sprops.drivers = drivers sprops.car_names = cars #self.season = SingleRaceSeason(self.__season_props( # self.mediator.gameprops, car, cars, # self.mediator.options['settings']['cars_number'], True, 0, 0, 0, # dev['race_start_time'], dev['countdown_seconds'])) self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start() self.mediator.fsm.demand('Race', track, car, cars, self.season.logic.drivers)
def on_driver_selected_mp(self, player_names, track, cars, drivers): self.mediator.gameprops.player_names = player_names dev = self.mediator.options['development'] sprops = self.__season_props( self.mediator.gameprops, cars, cars, self.mediator.options['settings']['cars_number'], True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'], self.mediator.options['settings']['camera'], 'localmp') sprops.car_names = cars sprops.player_car_names = cars self.season = SingleRaceSeason(sprops) for i, drv in enumerate(self.season.logic.drivers): dinfo = self.mediator.gameprops.drivers_info[i] drv.logic.dprops.info = dinfo for idx, (car, drv_idx) in enumerate(zip(cars, drivers)): for drv in self.season.logic.drivers: if drv.logic.dprops.info.img_idx == drv_idx: drv.logic.dprops.player_idx = idx for drv in self.season.logic.drivers: if drv.logic.dprops.info.img_idx == drv_idx: old_car = drv.logic.dprops.car_name for drv in self.season.logic.drivers: if drv.logic.dprops.car_name == car: if drv.logic.dprops.player_idx != idx: drv.logic.dprops.car_name = old_car for drv in self.season.logic.drivers: if drv.logic.dprops.info.img_idx == drv_idx: drv.logic.dprops.car_name = car self.season.logic.props.car_names = cars self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start() drivers = sprops.drivers self.eng.log('drivers: ' + str(drivers)) self.eng.do_later( 2.0, self.mediator.fsm.demand, ['Race', track, cars, cars, self.season.logic.drivers])
def on_start(self): GameLogic.on_start(self) self.__process_default() dev = self.mediator.options['development'] car = dev['car'] if 'car' in dev else '' track = dev['track'] if 'track' in dev else '' server = dev['server'] if 'server' in dev else '' if car and track and not server: # for development's quickstart self.season = SingleRaceSeason( self.__season_props( self.mediator.gameprops, car, [], self.mediator.options['settings']['cars_number'], True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'])) self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start() self.mediator.fsm.demand('Race', track, car, [car], self.season.logic.drivers, self.season.ranking) elif car and server: # for development's quickstart self.season = SingleRaceSeason( self.__season_props( self.mediator.gameprops, car, [], self.mediator.options['settings']['cars_number'], True, 0, 0, 0, dev['race_start_time'], dev['countdown_seconds'])) self.season.attach_obs(self.mediator.event.on_season_end) self.season.attach_obs(self.mediator.event.on_season_cont) self.season.start() if server == 'server': # i am the server def process_msg(data_lst, sender): if data_lst[0] == NetMsgs.car_request: client_car = data_lst[1] self.eng.car_mapping[data_lst[-1]] = client_car self.eng.log_mgr.log('car requested: %s %s' % (data_lst[-1], client_car)) if data_lst[0] == NetMsgs.driver_selection: s_ip = sender.get_address().get_ip_string() if s_ip not in self.current_drivers: self.current_drivers += [s_ip] _car = data_lst[1] driver_name = data_lst[2] driver_id = data_lst[3] driver_speed = data_lst[4] driver_adherence = data_lst[5] driver_stability = data_lst[6] self.eng.log_mgr.log('driver selected: %s (%s, %s) ' % (driver_name, driver_id, _car)) #gprops = self.mediator.gameprops #cars = gprops.cars_names[:] #car_idx = cars.index(_car) #gprops.drivers_info[car_idx] = gprops.drivers_info[car_idx]._replace( # img_idx=driver_id) #gprops.drivers_info[car_idx] = gprops.drivers_info[car_idx]._replace( # name=driver_name) #gprops.drivers_info[car_idx] = gprops.drivers_info[car_idx]._replace( # speed=driver_speed) #gprops.drivers_info[car_idx] = gprops.drivers_info[car_idx]._replace( # adherence=driver_adherence) #gprops.drivers_info[car_idx] = gprops.drivers_info[car_idx]._replace( # stability=driver_stability) sprops = self.season.props drivers = sprops.drivers for drv in drivers: if drv.dprops.info.img_idx == driver_id: drv.logic.dprops.car_name = _car drv.logic.dprops.info.name = driver_name drv.logic.dprops.info.speed = driver_speed drv.logic.dprops.info.adherence = driver_adherence drv.logic.dprops.info.stability = driver_stability sprops.drivers = drivers self.start_network_race_server(car, track) def process_connection(client_address): self.eng.log_mgr.log('connection from ' + client_address) self.current_drivers = [self, client_address] self.eng.server.start(process_msg, process_connection) self.eng.car_mapping = {} self.eng.car_mapping['self'] = car #gprops = self.mediator.gameprops #cars = gprops.cars_names[:] #car_idx = cars.index(car) #cars.remove(car) #shuffle(cars) #drv_idx = range(8) #drv_idx.remove(0) #shuffle(drv_idx) #gprops.drivers_info[car_idx] = gprops.drivers_info[0]._replace( # img_idx=0) #nname = self.mediator.options['settings']['player_name'] #gprops.drivers_info[car_idx] = gprops.drivers_info[0]._replace( # name=nname) #gprops.drivers_info[0] = gprops.drivers_info[0]._replace( # img_idx=car_idx) sprops = self.season.props drivers = sprops.drivers for drv in drivers: if drv.dprops.info.img_idx == 0: drv.logic.dprops.car_name = car drv.logic.dprops.info.name = self.mediator.options[ 'settings']['player_name'] else: def process_msg(data_lst, sender): if data_lst[0] == NetMsgs.track_selected: self.eng.log_mgr.log('track selected: ' + data_lst[1]) self.sel_track = data_lst[1] if data_lst[0] == NetMsgs.start_race: self.eng.log_mgr.log('start_race: ' + str(data_lst)) cars = data_lst[4::7] self.on_car_start_client(self.sel_track, car, cars, data_lst) self.eng.client.start(process_msg, server) self.eng.client.send( [NetMsgs.car_request, car, self.eng.client.my_addr]) gprops = self.mediator.gameprops sprops = self.season.props drivers = sprops.drivers for drv in drivers: if drv.dprops.info.img_idx == 1: curr_drv = drv #self.eng.client.send([ # NetMsgs.driver_selection, car, self.mediator.options['settings']['player_name'], 1, # gprops.drivers_info[1].speed, gprops.drivers_info[1].adherence, # gprops.drivers_info[1].stability, self.eng.client.my_addr]) self.eng.client.send([ NetMsgs.driver_selection, car, self.mediator.options['settings']['player_name'], 1, curr_drv.dprops.info.speed, curr_drv.dprops.info.adherence, curr_drv.dprops.info.stability, self.eng.client.my_addr ]) else: self.mediator.fsm.demand('Menu')
def on_car_selected(self, car): self.season = SingleRaceSeason(Utils().season_props(car)) self.season.attach_obs(self.mdt.event.on_season_end) self.season.attach_obs(self.mdt.event.on_season_cont)