def seed_producables(): log.debug("Seeding Producables") for pb in producables: if db_session.query(Producable).filter(Producable.name == pb).count() == 0: prd = Producable(name=pb, price=randint(4,59), time=randint(5,10)) db_session.add(prd) db_session.commit()
def seed_technology(): if len(db_session.query(Technology).all()) <= 0: log.debug("Adding Technology") for tex in technologies: tech = Technology(name=tex) db_session.add(tech) db_session.commit()
def seed_events(): if len(db_session.query(Event).all()) <= 0: log.debug("Adding Events") for evn in events: event = Event(name=evn) db_session.add(event) db_session.commit()
def process_buildqueue(): while (1): prs = Producable.query.all() for pr in prs: log.debug("Checking buildqueue for {}".format(pr.name)) queue = BuildQueue.query.filter(BuildQueue.active == True).filter( BuildQueue.producable_id == pr.id) itm = queue.first() if not itm: # add a 'done' callback that compares time_done and datetime.now log.info( "Getting BuildQueue items with criteria: Not active, Not Processed" ) all_possible_new = BuildQueue.query.filter( BuildQueue.active == False).filter( BuildQueue.processed == False).filter( BuildQueue.producable_id == pr.id) if len( all_possible_new.filter(BuildQueue.time_done > datetime .datetime.now()).all()) > 0: log.info( "Found items that have not been processed although they are done." ) log.info("Do something") apn = all_possible_new.all() if len(apn) >= 1: apn[0].active = True db_session.commit() log.info("Setting item {} to active".format(apn[0].id)) else: log.info("No items in buildqueue for Producable {}".format( pr.name)) if itm: log.info("#{} has an active flag".format(itm.id)) now = datetime.datetime.now() if itm.time_done <= now: inv = Inventory(basegood_id=None, producable_id=itm.producable_id, user_id=itm.user_id) prod_name = Producable.query.get(itm.producable_id).name user_name = User.query.get(itm.user_id).name log.info("{} finished building.".format(prod_name)) log.info("Adding {} to {}'s inventory".format( prod_name, user_name)) itm.active = False itm.processed = True log.info("Marking #{} as processed and Non-active".format( itm.id)) db_session.add(inv) db_session.commit() else: d1_ts = time.mktime(now.timetuple()) d2_ts = time.mktime(itm.time_done.timetuple()) minutes_left = int(d2_ts - d1_ts) / 60 log.info( "#{} is not ready yet. Takes {} minutes more.".format( itm.id, minutes_left)) time.sleep(3)
def seed_basegoods(): log.debug("Seeding Basegoods") for bg in basegoods: if db_session.query(BaseGood).filter(BaseGood.name == bg).count() == 0: basegood = BaseGood(name=bg, initprice=randint(1, 25), price=randint(4, 59)) db_session.add(basegood) db_session.commit()
def fill_inventory(): log.debug("Filling user inventories") for bg in basegoods: if db_session.query(Inventory).filter(BaseGood.name == bg).count() <= 0: basegood = BaseGood.query.filter(BaseGood.name == bg).first() all_users = User.query.all() for user in all_users: new_inv = Inventory(basegood=basegood, user_id=user.id, producable_id=None) db_session.add(new_inv) db_session.commit()
def link_effect_technology(): log.debug("Linking Effects with Technologies") if len(db_session.query(EffectTechnology).all()) <= 0: for tech in technologies: tech_o = db_session.query(Technology).filter(Technology.name == tech).first() for effect in blueprints[tech]: effect_o = Effect.query.filter(Effect.name == effect).first() effect_link = EffectTechnology(technology_id=tech_o.id, effect_id=effect_o.id) db_session.add(effect_link) db_session.commit()
def link_effect_event(): log.debug("Linking Effects with Events") if len(db_session.query(EffectEvent).all()) <= 0: for event in events: event_o = db_session.query(Event).filter(Event.name == event).first() for effect in blueprints[event]: effect_o = Effect.query.filter(Effect.name == effect).first() effect_link = EffectEvent(event_id=event_o.id, effect_id=effect_o.id) db_session.add(effect_link) db_session.commit()
def generate(self): for good in self.goods: quota = self.prc / len(self.goods) abs_num = (self.abs_res / 100) * quota # TODO: distribute the quota randomly and map it to absolute number map_o = Map(basegood_id=good.id, season_id=self.season_id, initial_ammount=abs_num, ammount=abs_num) db_session.add(map_o) db_session.commit()
def seed_effects(): if len(db_session.query(Effect).all()) <= 0: log.debug("Adding Effects") #if db_session.query(Effect).filter(Effect.name == effects[0]).count() == 0: for efc in effects: # the return here is: # {'FastProduction': [{'description': 'that'}, {'name': 'that'}]} # dict of array of dicts # currently disabled for simplification effect = Effect(name=efc) db_session.add(effect) db_session.commit()
def adding_seasons(): log.debug("Adding users to Season") if Season.query.count() < 1: season_one = Season(season_start=datetime.datetime.utcnow(), season_end=datetime.datetime.utcnow()) db_session.add(season_one) db_session.commit() all_users = User.query.all() season = Season.query.first() for user in all_users: user.season_id = season.id db_session.commit()
def seed_users(): log.debug("Seeding the Database") fake = Factory.create() log.debug("Seeding Users") for times in range(max_users): if db_session.query(User).count() <= max_users: usr = User(name=fake.name(), email=fake.email(), password=fake.state(), balance=randint(1000, 9000)) db_session.add(usr) db_session.commit()
def create_links(): log.debug("Creating Blueprints") if len(db_session.query(Blueprint).all()) <= 0: for prod in producables: # find producable by name -> get id # lookup in table to get the ammount and type # iterate over the basegoods # get quantity from table prod_o = db_session.query(Producable).filter(Producable.name == prod).first() for bg in blueprints[prod]: #bg_name, quant = bg.items()[0] # rather stupid python3 bg_name = list(bg.keys())[0] quant = bg[bg_name] basegood_o = BaseGood.query.filter(BaseGood.name == bg_name).first() for i in range(quant): blueprint = Blueprint(basegood_id=basegood_o.id, producable_id=prod_o.id) db_session.add(blueprint) db_session.commit()
def trigger_build(pr): current_user = User.query.get(1) producable = Producable.query.get(pr) if producable is None: return not_found() log.info("Triggering build for {}".format(producable.name)) inv = current_user.inv_expanded() for basegood in producable.blueprint(): if basegood in inv: # dont know if thats the best option ? # just count the respective len and ask the DB inv.remove(basegood) else: return insufficient_funds() for basegood in producable.blueprint(): # .delete() is designed to bulk delete # .limit() does not work # .first() does not work # how to savely delete only one item? this is bad.. inv = Inventory.query.\ filter(Inventory.user_id == current_user.id).\ filter(Inventory.basegood_id == basegood.id).\ first() Inventory.query.filter_by(id=inv.id).delete() try: build_queue = BuildQueue(user_id=current_user.id, producable_id=producable.id, time_start=datetime.datetime.utcnow(), time_done=datetime.datetime.utcnow() + datetime.timedelta(minutes=producable.time), active=False, processed=False) db_session.add(build_queue) db_session.commit() result = producable_schema.dump(producable) return jsonify({'message': 'production initialized', 'producable': result.data}) except: return service_unavailable()
def add_basegood_to_inv(user_id, bg_id): user = db_session.query(User).filter(User.id == user_id).first() bg = BaseGood.query.get(1) new_inv = Inventory(basegood=bg, user_id=user.id, producable_id=None) db_session.add(new_inv) db_session.commit()