class StockMarket(object): DEFAULT_PRICE = 5 INITIAL_MONEY = 500 def __init__(self, name = "unknown"): self.possession = Possession() self.prices = {} self.x = 0 self.y = 0 self.name = name #get some initial cash from loan sharks :D self.loan_money(StockMarket.INITIAL_MONEY) def loan_money(self, amount): #TODO: How the hell make this loan system... loanShark = Possession() loanShark._set_money(amount) loanShark.give_money(amount, self.possession) def get_price(self, resource): if type(resource) != type: resource = type(resource) if not resource in self.prices: self.prices[resource] = StockMarket.DEFAULT_PRICE return self.prices[resource] def set_price(self, resource, price): if type(resource) == type: self.prices[resource] = price else: self.prices[type(resource)] = price def find_resource(self, resourceType): return self.possession.get_resource(resourceType) def sell_resource(self, resource, seller): if self.possession.money < self.get_price(resource): return False seller.give_resource(resource, self.possession) self.possession.give_money(self.get_price(resource), seller) return True def buy_resource(self, resource, buyer): if buyer.money < self.get_price(resource): print "Buyer has not enough money" return False if type(resource) == type: resource = self.find_resource(resource) if resource == None: print "Stock has not required resource" return False self.possession.give_resource(resource, buyer) buyer.give_money(self.get_price(resource), self.possession) return True
def __init__(self, name = "unknown"): self.possession = Possession() self.prices = {} self.x = 0 self.y = 0 self.name = name #get some initial cash from loan sharks :D self.loan_money(StockMarket.INITIAL_MONEY)
def __init__(self, occupation, name="Unknown"): self.occupation = occupation self.occupation.npc = self self.schedule = Schedule() #TODO: Cannot think... just advance schedule so that it is already done when Advance is called the first time... self.schedule.advance(Schedule.MAX_TIME) self.possession = Possession() self.hungerLevel = 24 * 60 #enough for one day self.food_consumption = Npc.DEFAULT_FOOD_CONSUMPTION self.alive = True self.strategy = None self.city = None self.x = 100 self.y = 100 self.home_x = 0 self.home_y = 0 self.name = name
def read_possessions_data(dirname, game_code, quarter, players_data, events_data, shots_data): # Read the box document box_filename = 'NBA_FINALBOX_OPTICAL${}.XML'.format(game_code) document_box = ElementTree.parse(os.path.join(dirname, box_filename)) sports_statistics_box = document_box.getroot() sports_boxscores = next(c for c in sports_statistics_box if c.tag == 'sports-boxscores') nba_boxscores = next(c for c in sports_boxscores if c.tag == 'nba-boxscores') nba_boxscore = next(c for c in nba_boxscores if c.tag == 'nba-boxscore') possessions = next(c for c in nba_boxscore if c.tag == 'possessions') quarter_el = next( c for c in possessions if c.tag == 'quarter' and int(c.attrib['number']) == quarter) possessions_result = [] # For each possession, record start and end time # then make a processeable thread for it, and record all the shots for possession_el in [c for c in quarter_el if c.tag == 'possession']: new_possession = Possession( game_code, quarter, int(possession_el.attrib['team-global-id'])) start_time = float(possession_el.attrib['time-start'].split(':')[0])*60 + \ float(possession_el.attrib['time-start'].split(':')[1]) end_time = float(possession_el.attrib['time-end'].split(':')[0])*60 + \ float(possession_el.attrib['time-end'].split(':')[1]) new_possession.events_raw = events_data[np.logical_and( start_time > events_data['game clock'], events_data['game clock'] > end_time)] new_possession.make_thread(new_possession.events_raw, players_data) new_possession.shots = [] for shot in shots_data: if shot.event in new_possession.events_raw: new_possession.shots.append(shot) possessions_result.append(new_possession) return possessions_result
def loan_money(self, amount): #TODO: How the hell make this loan system... loanShark = Possession() loanShark._set_money(amount) loanShark.give_money(amount, self.possession)
class Npc(object): SLEEP_DURATION = 7 * 60 DEFAULT_FOOD_CONSUMPTION = 1 #amount of food needed per unit of time (now minute...) def __init__(self, occupation, name="Unknown"): self.occupation = occupation self.occupation.npc = self self.schedule = Schedule() #TODO: Cannot think... just advance schedule so that it is already done when Advance is called the first time... self.schedule.advance(Schedule.MAX_TIME) self.possession = Possession() self.hungerLevel = 24 * 60 #enough for one day self.food_consumption = Npc.DEFAULT_FOOD_CONSUMPTION self.alive = True self.strategy = None self.city = None self.x = 100 self.y = 100 self.home_x = 0 self.home_y = 0 self.name = name def print_status(self): if not self.alive: print "Npc is DEAD!" print "Npc (" + str(self.occupation) + ") has " + str(self.possession.money) + " money, owns:" for pos in self.possession.resources: print pos #TODO: How to handle time advancing. Now advancing goes fine if the time interval is smalles possible. #If the interval is increased, first schedule is advanced and then food. This leads to not wanted scenarios #where npc can do work without food. def advance(self, time): while (time > 0): if not self.alive: return #Day is completed, create new schedule if (self.schedule.is_done()): self.create_schedule() timeLeft = self.schedule.advance(time) self._consume_food(time - timeLeft) time = timeLeft def create_schedule(self): self.schedule = Schedule() if self.strategy == None: self._add_mandatory_actions() self.occupation.add_default_schedule(self, self.possession) else: self._add_mandatory_actions() self.strategy.create_schedule() def _consume_food(self, time): while (time > 0): if (self.hungerLevel <= 0): foods = self.possession.get_foods() if len(foods) == 0: self.alive = False print "NPC (" + str(self.occupation) + ") died from hunger!" return #just eat the first thing from the inventory... self.possession.destroy_resource(foods[0]) self.hungerLevel += foods[0].NUTRITIONAL_VALUE consumedAmount = min(time, self.hungerLevel) self.hungerLevel -= consumedAmount * self.food_consumption time -= consumedAmount def _add_mandatory_actions(self): self.schedule.add_action(Action(self, "Sleep", Npc.SLEEP_DURATION))