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_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 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 adding_map(): log.debug("Generating a map") if Map.query.count() < 1: all_baseg = BaseGood.query.all() season = Season.query.first() map_o = MapGen(season, basegoods=all_baseg) map_o.generate()
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 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 calculate_new_prod_price(affected_producables=[]): for pr in affected_producables: unique_bg_list = pr.blueprint() # reduce the list to unique items first new_price = 0 for bg in unique_bg_list: new_price += bg.price try: new_price_after_multi = (new_price * (pr.time - (pr.time/5))) log.debug('adapted Price for {} from previous {} to {}'.format(pr.name, pr.price, new_price_after_multi)) # Plus some extra money that acts # as salary for the 'work' # this can depend on the pr.time pr.price = new_price_after_multi db_session.commit() except: log.debug('Something went wrong during the price calculation of Producables')
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 get_users(): log.debug("Querying all Users") users = User.query.all() results = users_schema.dump(users) return jsonify({'users': results.data})
from api.database.models import User, BaseGood, Producable,\ Technology, Event, Effect,\ UserSchema, BaseGoodSchema,\ ProducableSchema, Inventory,\ InventorySchema, BuildQueue,\ BuildQueueSchema, BlueprintSchema,\ CapabilitiesSchema, EventSchema,\ EffectSchema, TechnologySchema from flask_cors import CORS, cross_origin from multiprocess import Process import os import calculations import time import multiprocessing log.debug("Initializing Database") init_db() app = Flask(__name__) api = Api(app) cors = CORS(app, resources={r"/*": {"origins": "*"}}) users_schema = UserSchema(many=True) user_schema = UserSchema() basegoods_schema = BaseGoodSchema(many=True) basegood_schema = BaseGoodSchema() producables_schema = ProducableSchema(many=True) blueprint_schema = BlueprintSchema() capabilities_schema = CapabilitiesSchema() producable_schema = ProducableSchema()