def produce_resources(the_world, the_team): """Produces resources for the coming turn""" # Get production upkeep_cost = get_upkeep(the_team, the_world) database.query(the_world.cursor, log_f.new_log("team_f.get_upkeep", "Upkeep: %s" % upkeep_cost, "", team = the_team.id) ) produced_resources, new_resources = rules.team_rules.produce_resources( the_world.cursor, the_team, the_world=the_world ) # Log production database.query(the_world.cursor, log_f.new_log("team_f.produce_resources", "Produced: %s\nNew: %s" % (str(produced_resources), str(new_resources)), "", team = the_team.id) ) # Apply upkeep new_resources -= "Materials:%s" % upkeep_cost queries = new_resources.make_set_queries(the_team.id) from functions import queries_f for q in queries: try: queries_f.log_query(the_world.cursor, q, subject="Pre orders resources") the_world.cursor.execute(q) except Exception as e: print("Query: %s\n" % q) raise e return queries
def record_resources(the_world, the_team): """Records the end of turn resources for the team""" the_team.get_resources(the_world.cursor, force_requery=True) strings = [] for res_id, r in enumerate(resource_list.data_list): if res_id not in the_team.resources.value: continue if the_team.resources.value[res_id] == 0: continue if r.reset: continue res_value = the_team.resources.value[res_id] # Typecast if we can if res_value == int(res_value): res_value = int(res_value) strings.append("%s:%s" % (r.name, res_value)) # Log previous resources database.query(the_world.cursor, log_f.new_log("team_f.record_resources", ",".join(strings), "", team = the_team.id) ) query = """UPDATE teams SET previous_resources = '%s' WHERE id = %d;""" % (database.escape(",".join(strings)), the_team.id) try: the_world.cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
def test_new_log(self): vals = ( (("a, b, c", "abc", "", -1, -1, 40), "INSERT INTO logs (tags, content, cost, player, team, turn)\n\t\tvalues\n\t\t('a, b, c', 'abc', '', -1, -1, 40)"), ) for args, expected in vals: r = log_f.new_log(*args) self.assertEqual(expected, r)
def record_favour(the_world, the_team): from rules import deity_rules deity_dict = the_world.deities() the_team.get_deities(the_world.cursor) for d in the_team.deities.keys(): new_favour, favour_desc = deity_rules.calculate_favour(the_world, the_team, d) query = """UPDATE team_deities SET favour = %d WHERE team = %d AND deity = %d;""" % (int(new_favour), int(the_team.id), int(d)) try: the_world.cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # Log favour database.query(the_world.cursor, log_f.new_log("team_f.record_favour", "Deity: %d, Favour: %d, Desc: %s" % (int(d), int(new_favour), favour_desc), "", team = the_team.id) ) # Update team item too the_team.deities[d] = new_favour
def grow_cities(the_world, the_team): # return grow_cities_old(the_world, the_team) from pages import common from functions import path_f from functions import queries_f queries = [] """Grows all the cities for the team""" city_dict = the_world.cities_from_team(the_team.id) team_rules.Food(the_world.cursor, the_team, the_world=the_world) modifier = team_rules._food_needed_modifier(the_world, the_team) # First work out how much food they have for city_id, the_city in city_dict.items(): if the_city.dead > 0: continue the_city.surplus_food = ((the_city.surplus_food/1000) - (the_city.size * modifier))*1000 # Now we go and try to redistribute the food a bit logs = [] for city_id, the_city in city_dict.items(): if the_city.dead > 0: continue if the_city.surplus_food < 0: for city_id2, city2 in city_dict.items(): if the_city.surplus_food >= 0: continue if city2.surplus_food <= 0: continue # if path_f.pythagoras(the_city, city2) > 1000: continue# More than 1k map units if city2.surplus_food > abs(the_city.surplus_food): # 2nd city has more than enough food city2.surplus_food += the_city.surplus_food the_city.surplus_food = 0 else: # 2nd city can get us some of the way the_city.surplus_food += city2.surplus_food city2.surplus_food = 0 growth_rate, growth_constant = rules.city_rules.city_growth_rate(the_world.cursor, the_team, the_city, the_world) new_population = int((the_city.population * growth_rate) + growth_constant) if new_population < 1: logs.append("Kill %s (id: %d) as pop = %d" % (the_city.name, city_id, new_population)) queries.append("""UPDATE cities SET dead = %d WHERE id = %d;""" % (common.current_turn(), city_id)) else: logs.append("Grow %s (id: %d) to %s, old pop: %d, old slave: %d, rate: %d, constant: %d" % ( the_city.name, city_id, new_population, the_city.population, the_city.slaves, growth_rate, growth_constant) ) queries.extend([ "-- Growing city %d, old pop: %d, old slave: %d, new pop, %d, rate: %d, constant: %d" % ( city_id, the_city.population, the_city.slaves, new_population, growth_rate, growth_constant, ), """UPDATE cities SET population = %d WHERE id = %d;""" % (new_population, city_id), ]) database.query(the_world.cursor, log_f.new_log("team_f.grow_cities", "\n".join(logs), "", team = the_team.id) ) # for q in queries: # try: # queries_f.log_query(the_world.cursor, q, subject="Growing cities") # the_world.cursor.execute(q) # except Exception as e: # print("Query: %s\n" % q) # raise e try: database.query( the_world.cursor, queries ) except Exception as e: print("Queries: %s\n" % "\n".join(queries)) raise e return queries