def eat_rations(conn, group, heroes, ranker): """ 势力粮草消耗 :param conn: :param group: :param heroes: :param ranker: :return: group """ g_rations = group.get('g_rations') g_ranker = ranker.get(group.get('g_corps')) new_group = dict() ranker_eat = round( group.get('g_ranker') * g_ranker.get('r_force') * 0.3 + 1) hero_eat = 0 for h_id in Conn.get_group_hero(conn, group.get('g_id')): hero_eat += round(heroes.get(h_id).get('h_force') / 20 + 1) g_eat = hero_eat + ranker_eat g_rations -= g_eat new_group['g_rations'] = g_rations group['g_rations'] = g_rations condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本月{0}消耗粮草{1}石'.format(group.get('g_name'), g_eat)) return group
def change_corps(conn, group, ranker, choice): """ 变更势力兵种,会依据变更的兵种消耗不同数量的黄金 :param choice: :param conn: :param group: :param ranker: :return: group """ g_gold = group.get('g_gold') g_corps = group.get('g_corps') o_gold = round( group.get('g_ranker') * (ranker.get(g_corps).get('r_force') + ranker.get(g_corps).get('r_defense')) / 40) u_gold = round( group.get('g_ranker') * (ranker.get(choice).get('r_force') + ranker.get(choice).get('r_defense')) / 20) new_gold = g_gold - (u_gold - o_gold) new_group = dict() if g_gold >= u_gold: new_group['g_corps'] = choice new_group['g_gold'] = new_gold group['g_gold'] = new_gold condition = 'g_id = {}'.format(group.get('g_id')) if group.get('g_status') == 1: print('{0}兵种变更为{1},共花费黄金{2}两!'.format( group.get('g_name'), ranker.get(choice).get('r_name'), u_gold)) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def get_populace(conn, group): """ 势力人口操作 :param conn: :param group: :return: group """ g_populace = group.get('g_populace') g_morale = group.get('g_morale') new_group = dict() # 人口增加量受势力人口和势力民心影响 new_populace = random.randint( round(g_populace * (1 + g_morale / 1000) / 300), round(g_populace * (1 + g_morale / 1000) / 100)) # 人口增加会降低势力民心,降低量受新增人口和势力民心影响 new_morale = round(new_populace / (1000 * (1 + g_morale / 1000))) new_morale = new_morale if new_morale >= 1 else 1 g_populace += new_populace g_morale -= new_morale new_group['g_populace'] = g_populace new_group['g_morale'] = g_morale group['g_morale'] = g_morale group['g_populace'] = g_populace condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本月{0}新增人口{1}人,民心减少{2}点'.format(group.get('g_name'), new_populace, new_morale)) return group
def patrol_group(conn, group, heroes, hero_id): """ 巡逻领地 能提高势力民心 有几率发现贼寇探子,成功击杀贼寇探子会获得更多民心 有几率发现贼寇据点,需在征讨界面对据点进行摧毁,摧毁一定获得数量不等的粮草和黄金,有几率获得宝物 :param conn: :param group: :param heroes: :param hero_id: :return: p_group [group, r_type] """ k = random.random() h_force = 0 name = '' new_group = dict() g_morale = group.get('g_morale') for h_id in hero_id: h_force += heroes.get(h_id).get('h_force') name += '{},'.format(heroes.get(h_id).get('h_name')) if k < 0.4: # 无隐患 r_type = 1 morale = round(h_force / 20) g_morale += morale print('巡逻完成,未发现隐患,{0}民心上升{1}点!'.format(group.get('g_name'), morale)) group['g_morale'] = g_morale new_group['g_morale'] = g_morale con = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, con) elif k < 0.8: # 讨贼 r_type = 2 else: r_type = 3 # 据点 p_group = [group, r_type] return p_group
def pay_provisions(conn, group, heroes, ranker): """ 势力军饷发放 :param conn: :param group: :param heroes: :param ranker: :return: group """ g_gold = group.get('g_gold') g_ranker = ranker.get(group.get('g_corps')) new_group = dict() pay_ranker = round( group.get('g_ranker') * (g_ranker.get('r_force') + g_ranker.get('r_defense')) / 10) pay_hero = 0 for h_id in Conn.get_group_hero(conn, group.get('g_id')): pay_hero += round((heroes.get(h_id).get('h_lead') * 1.1 + heroes.get(h_id).get('h_force') * 1.2 + heroes.get(h_id).get('h_brain') * 1.1 + heroes.get(h_id).get('h_politics') * 1.1 + heroes.get(h_id).get('h_charm') * 0.8) / 10) g_pay = pay_hero + pay_ranker g_gold -= g_pay new_group['g_gold'] = g_gold group['g_gold'] = g_gold condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本季度{0}发放军饷黄金{1}两'.format(group.get('g_name'), g_pay)) return group
def auto_create_group(conn, g_num, avg_num, r_num): """ 自动生成指定数量的势力,并随机分配一定范围内的英雄数到势力中,并修改相应的英雄状态和属性 :param conn: 数据库操作对象 :param g_num: 指定生成势力数 :param avg_num: 指定分配到势力的英雄数 :param r_num: 指定分配到势力的英雄数波动范围 :return: None """ # 获取英雄信息 heroes = Conn.get_heroes(conn) # 主公备选英雄ID列表 wait_list = list(heroes.keys()) # 主公信息 lord_dict = dict() # 新建势力信息 build_group = dict() # 势力所属英雄信息 g_hero = dict() # 循环创建g_num个势力 for i in range(g_num): # 分配到各个势力的英雄数范围 h_num = avg_num + random.randint(0, r_num) # 从主公候选英雄列表中随机获取一个英雄ID h_id = random.choice(wait_list) # 从候选名单中移除已经选中的英雄ID wait_list.remove(h_id) # 新建势力信息 build_group['g_lord'] = h_id build_group['g_name'] = '{}军'.format(heroes.get(h_id).get('h_name')) # 调用方法创建势力(向数据库写入势力信息) Conn.insert_data(conn, 'hero_group', build_group) # 主公英雄属性变化,五维全部+5,修改h_group为势力ID,修改状态为主公 lord_dict['h_group'] = i + 1 lord_dict['h_lead'] = heroes.get(h_id).get('h_lead') + 5 lord_dict['h_force'] = heroes.get(h_id).get('h_force') + 5 lord_dict['h_brain'] = heroes.get(h_id).get('h_brain') + 5 lord_dict['h_politics'] = heroes.get(h_id).get('h_politics') + 5 lord_dict['h_charm'] = heroes.get(h_id).get('h_charm') + 5 lord_dict['h_status'] = 3 # 数据库更新条件语句 condition = 'h_id={}'.format(h_id) # 调用方法将英雄变更为主公(修改数据库内指定英雄的属性) Conn.update_data(conn, 'heroes', lord_dict, condition) # 循环分配特定个数的势力所属英雄 for j in range(h_num): # 英雄属性变化 g_hero['h_group'] = i + 1 g_hero['h_status'] = 2 # 从英雄列表中随机选择一个英雄加入势力 h_id = random.choice(wait_list) # 从英雄列表中移除已加入势力的英雄ID wait_list.remove(h_id) # 数据库更新条件语句 condition = 'h_id={}'.format(h_id) # 调用方法将英雄变更为势力所属(修改数据库内指定英雄的属性) Conn.update_data(conn, 'heroes', g_hero, condition) print('当前游戏难度将新增敌对势力共{}个!'.format(g_num))
def fight_enemy(conn, group, heroes, hero_id, spy): """ 讨贼 :param conn: :param group: :param heroes: :param hero_id: :param spy: :return: group """ new_group = dict() que = queue.Queue() que.put(True) print('*' * 20) print('{0:<4}\t{1:<10}\t{2:<4}'.format('ID', '姓名', '武力')) for h_id in hero_id: print('{0:<4}\t{1:<10}\t{2:<4}'.format( h_id, heroes.get(h_id).get('h_name'), heroes.get(h_id).get('h_force'))) choice = input('请选择出战的英雄ID,可多选:\n').strip().split() h_num = len(choice) enemies = list(spy.keys()) result = kumite(que, heroes, choice, spy, enemies) g_gold = group.get('g_gold') g_morale = group.get('g_morale') if result == 1: # 讨贼胜利,获得民心、金钱、宝物 morale = round( sum([spy.get(i).get('s_force') for i in list(spy.keys())]) / 10 / h_num) gold = sum([spy.get(i).get('s_gold') for i in list(spy.keys())]) print('讨贼胜利,获得民心{0}点,黄金{1}两!'.format(morale, gold)) g_morale += morale g_gold += gold group['g_morale'] = g_morale new_group['g_morale'] = g_morale group['g_gold'] = g_gold new_group['g_gold'] = g_gold else: # 讨贼失败,损失民心、金钱、宝物 morale = round( sum([spy.get(i).get('s_force') for i in list(spy.keys())]) / 10 / h_num) gold = random.randint( sum([spy.get(i).get('s_force') for i in list(spy.keys())]) * 5, sum([spy.get(i).get('s_force') for i in list(spy.keys())]) * 10) print('讨贼失败,损失民心{0}点,黄金{1}两!'.format( morale, gold if g_gold > gold else g_gold)) g_morale -= morale g_gold -= gold if g_gold > gold else g_gold group['g_morale'] = g_morale new_group['g_morale'] = g_morale group['g_gold'] = g_gold new_group['g_gold'] = g_gold condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def get_economy(conn, group, heroes, hero_id, check): """ 势力经济开发操作 :param conn: 数据库连接对象 :param group: 势力信息 :param heroes: 英雄信息 :param hero_id: 势力英雄ID列表 :param check: 确认执行 :return: group """ if check == 1: # 定义一个字典接收势力变更信息 new_group = dict() # 获取势力黄金 g_gold = group.get('g_gold') # 计算本次执行任务的经费 used_gold = 50 * len(hero_id) # 判断势力黄金数是否足够执行任务经费如果不足,则直接返还原group if g_gold < used_gold: if group.get('g_status') == 1: print('黄金不足!') return group # 获取势力经济 g_economy = group.get('g_economy') # 获取势力科技 g_science = group.get('g_science') # 执行英雄名称 name = '' # 接收执行英雄的政治 h_politics = 0 # 传入多个执行英雄ID,循环累加英雄政治,拼接英雄名 for h_id in hero_id: h_politics += heroes.get(h_id).get('h_politics') name += '{},'.format(heroes.get(h_id).get('h_name')) # 势力黄金减少执行英雄数*50两 g_gold -= used_gold # 经济变化量受英雄政治和势力科技影响 new_economy = random.randint( round(h_politics * 0.05 * (1 + g_science / 1000)), round(h_politics * 0.08 * (1 + g_science / 1000))) g_economy += new_economy if g_economy >= 1000: g_economy = 1000 if group.get('g_status') == 1: print('势力经济已达最大值!') # 分别更新势力信息 new_group['g_gold'] = g_gold group['g_gold'] = g_gold new_group['g_economy'] = g_economy group['g_economy'] = g_economy # 玩家势力输出信息,否则不输出 if group.get('g_status') == 1: print('本月{0}执行任务共提升经济{1}点'.format(name[:-1], new_economy)) condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def new_group(conn, hero, c_num): """ 玩家新建势力 :param conn: 数据库连接对象 :param hero: 玩家新建英雄信息 :param c_num: 当前游戏难度下新建势力可招揽的最大英雄数 :return: None """ # 新建势力信息 build_group = dict() # 新主公信息 lord_hero = dict() # 势力所属英雄信息 g_hero = dict() # 势力信息 build_group['g_id'] = 10 build_group['g_lord'] = hero.get('h_id') build_group['g_name'] = '{}军'.format(hero.get('h_name')) # 调用方法创建新势力(向数据库中写入势力数据) Conn.insert_data(conn, 'hero_group', build_group) # 主公信息 lord_hero['h_group'] = 10 lord_hero['h_lead'] = hero.get('h_lead') + 5 lord_hero['h_force'] = hero.get('h_force') + 5 lord_hero['h_brain'] = hero.get('h_brain') + 5 lord_hero['h_politics'] = hero.get('h_politics') + 5 lord_hero['h_charm'] = hero.get('h_charm') + 5 lord_hero['h_status'] = 3 # 数据库更新条件语句 condition = 'h_id={}'.format(hero.get('h_id')) # 调用方法更新主公属性(修改数据库中指定英雄的属性) Conn.update_data(conn, 'heroes', lord_hero, condition) # 获取英雄信息 heroes = Conn.get_heroes(conn) # 候选英雄ID列表 h_candidate = list() # 循环匹配在野英雄,并将英雄ID存放到候选英雄ID列表 for k, v in heroes.items(): if v.get('h_status') == 1: h_candidate.append(k) # 接收玩家选择的英雄ID,且只接收最大限定人数,并分割为列表存储 PM.print_choice_hero(h_candidate, c_num, heroes) choices = input().strip().split(' ')[:c_num] # 循环更新所选英雄属性 for choice in choices: g_hero['h_group'] = 10 g_hero['h_status'] = 2 condition = 'h_id={}'.format(choice) Conn.update_data(conn, 'heroes', g_hero, condition) print('新建势力{}成功!'.format(build_group.get('g_name')))
def new_game(self, h_num, g_num, avg_num, r_num, c_num, game_hard): """ 新游戏执行操作 :param game_hard: 游戏难度 :param h_num: 新游戏生成英雄总数 :param g_num: 新游戏生成势力总数 :param avg_num: 新游戏平均分配到各势力的英雄数 :param r_num: 新游戏势力英雄数波动范围 :param c_num: 新游戏玩家创建新势力最大可选择的最大英雄数 :return: None """ # 初始化数据库表 Conn.create_hero_table(self.conn) Conn.create_group_table(self.conn, game_hard) Conn.create_den_table(self.conn) # 自动生成英雄和势力 HB.auto_create_hero(self.conn, h_num) GB.auto_create_group(self.conn, g_num, avg_num, r_num) choice = int(input('1 新建英雄 2 选择势力\n')) if choice == 1: # 新建英雄 new_hero = HB.new_hero(self.conn) choice_lord = int(input('是否使用新建英雄新建势力,选择是则新建英雄作为势力主公新建势力,否则新建英雄成为在野英雄:\n1 是 2 否\n')) if choice_lord == 1: # 新建势力 GB.new_group(self.conn, new_hero, c_num) # 获取英雄和势力信息 heroes = Conn.get_heroes(self.conn) groups = Conn.get_groups(self.conn) PM.print_choice_group(self.conn, groups, heroes) choice = input().strip() # 玩家扮演势力标记修改 g_play = dict() g_play['g_status'] = 1 condition = 'g_id = {}'.format(choice) Conn.update_data(self.conn, 'hero_group', g_play, condition) # 宝物数据初始化 gems = dict() gems['t_group'] = 0 gems['t_hero'] = 0 Conn.clear_data(self.conn, 'gems', gems) # 纪年初始化 years = dict() years['year'] = 184 years['month'] = 1 Conn.clear_data(self.conn, 'years', years) # 运行游戏主体 Main.run(self.conn)
def get_military(conn, group, heroes, hero_id, check): """ 势力军事操作 :param conn: :param group: :param heroes: :param hero_id: :param check: :return: group """ if check == 1: new_group = dict() g_gold = group.get('g_gold') # 计算本次执行任务的经费 used_gold = 50 * len(hero_id) # 判断势力黄金数是否足够执行任务经费如果不足,则直接返还原group if g_gold < used_gold: if group.get('g_status') == 1: print('黄金不足!') return group g_military = group.get('g_military') g_science = group.get('g_science') name = '' h_force = 0 for h_id in hero_id: h_force += heroes.get(h_id).get('h_force') name += '{},'.format(heroes.get(h_id).get('h_name')) g_gold -= used_gold new_group['g_gold'] = g_gold group['g_gold'] = g_gold # 军事变化量受英雄武力和势力科技影响 new_military = random.randint( round(h_force * 0.05 * (1 + g_science / 1000)), round(h_force * 0.08 * (1 + g_science / 1000))) g_military += new_military if g_military >= 1000: g_military = 1000 if group.get('g_status') == 1: print('势力军事已达最大值!') new_group['g_military'] = g_military group['g_military'] = g_military if group.get('g_status') == 1: print('本月{0}执行任务共提升军事{1}点'.format(name[:-1], new_military)) condition = 'g_id = %s' % group.get('g_id') Conn.update_data(conn, 'hero_group', new_group, condition) return group
def get_farming(conn, group, heroes, hero_id, check): """ 势力农业开发操作 :param conn: :param group: :param heroes: :param hero_id: :param check: :return: group """ if check == 1: new_group = dict() g_gold = group.get('g_gold') # 计算本次执行任务的经费 used_gold = 50 * len(hero_id) # 判断势力黄金数是否足够执行任务经费如果不足,则直接返还原group if g_gold < used_gold: if group.get('g_status') == 1: print('黄金不足!') return group g_farming = group.get('g_farming') g_science = group.get('g_science') name = '' h_politics = 0 for h_id in hero_id: h_politics += heroes.get(h_id).get('h_politics') name += '{},'.format(heroes.get(h_id).get('h_name')) g_gold -= used_gold new_group['g_gold'] = g_gold group['g_gold'] = g_gold # 农业变化量受英雄政治和势力科技影响 new_farming = random.randint( round(h_politics * 0.05 * (1 + g_science / 1000)), round(h_politics * 0.08 * (1 + g_science / 1000))) g_farming += new_farming if g_farming >= 1000: g_farming = 1000 if group.get('g_status') == 1: print('势力农业已达最大值!') new_group['g_farming'] = g_farming group['g_farming'] = g_farming if group.get('g_status') == 1: print('本月{0}执行任务共提升农业{1}点'.format(name[:-1], new_farming)) condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def get_science(conn, group, heroes, hero_id, check): """ 势力科技操作 :param conn: :param group: :param heroes: :param hero_id: :param check: :return: group """ if check == 1: new_group = dict() g_gold = group.get('g_gold') # 计算本次执行任务的经费 used_gold = 50 * len(hero_id) # 判断势力黄金数是否足够执行任务经费如果不足,则直接返还原group if g_gold < used_gold: if group.get('g_status') == 1: print('黄金不足!') return group g_science = group.get('g_science') name = '' h_brain = 0 for h_id in hero_id: h_brain += heroes.get(h_id).get('h_brain') name += '%s,' % heroes.get(h_id).get('h_name') g_gold -= used_gold new_group['g_gold'] = g_gold group['g_gold'] = g_gold # 科技变化量受英雄智力和势力科技影响 new_science = random.randint( round(h_brain * 0.05 * (1 + g_science / 1000)), round(h_brain * 0.08 * (1 + g_science / 1000))) g_science += new_science if g_science >= 1000: g_science = 1000 if group.get('g_status') == 1: print('势力科技已达最大值!') new_group['g_science'] = g_science group['g_science'] = g_science if group.get('g_status') == 1: print('本月{0}执行任务共提升科技{1}点'.format(name[:-1], new_science)) condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def get_morale(conn, group, heroes, hero_id, check): """ 势力民心操作 :param conn: :param group: :param heroes: :param hero_id: :param check: :return: group """ if check == 1: new_group = dict() g_gold = group.get('g_gold') # 计算本次执行任务的经费 used_gold = 50 * len(hero_id) # 判断势力黄金数是否足够执行任务经费如果不足,则直接返还原group if g_gold < used_gold: if group.get('g_status') == 1: print('黄金不足!') return group g_morale = group.get('g_morale') name = '' h_charm = 0 for h_id in hero_id: h_charm += heroes.get(h_id).get('h_charm') name += '{},'.format(heroes.get(h_id).get('h_name')) g_gold -= used_gold new_group['g_gold'] = g_gold group['g_gold'] = g_gold # 民心变化量受英雄魅力和势力民心影响 new_morale = random.randint( round(h_charm * 0.08 * (1 + g_morale / 1000)), round(h_charm * 0.1 * (1 + g_morale / 1000))) g_morale += new_morale if g_morale >= 1000: g_morale = 1000 if group.get('g_status') == 1: print('势力民心已达最大值!') new_group['g_morale'] = g_morale group['g_morale'] = g_morale if group.get('g_status') == 1: print('本月{0}执行任务共提升民心{1}点'.format(name[:-1], new_morale)) condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group
def train_corps(conn, group, heroes, hero_id, t_type): """ 训练势力兵种,获得兵种经验。 :param conn: :param group: :param heroes: :param hero_id: :param t_type: :return: """ new_group = dict() g_experience = group.get('g_experience') g_ranker = group.get('g_ranker') h_lead = 0 h_force = 0 name = '' for h_id in hero_id: h_lead += heroes.get(h_id).get('h_lead') h_force += heroes.get(h_id).get('h_force') name += '{},'.format(heroes.get(h_id).get('h_name')) if t_type == 1: exp = round(h_lead * 10 / g_ranker) if round(h_lead * 10 / g_ranker) > 0 else 1 else: exp = round((h_lead + h_force) * 10 / g_ranker) if round(h_lead * 10 / g_ranker) > 0 else 1 g_experience += exp group['g_experience'] = g_experience new_group['g_experience'] = g_experience condition = 'g_id={}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本月{0}完成训练,势力兵种经验提升{1}点'.format(name[:-1], exp)) if g_experience >= 50 and group.get('g_corps') < 2: print('当前兵种可升级!') elif g_experience >= 200 and group.get('g_corps') < 3: print('当前兵种可升级!') elif g_experience >= 450 and group.get('g_corps') < 4: print('当前兵种可升级!') elif g_experience >= 950 and group.get('g_corps') < 5: print('当前兵种可升级!') return group
def rewards_hero(conn, group, gems, t_id, heroes, h_id, check): """ 对势力英雄进行宝物赏赐与罚没 :param h_id: :param group: :param check: :param conn: :param gems: :param t_id: :param heroes: :return: heroes """ new_gem = dict() new_hero = dict() gem = gems.get(t_id) hero = heroes.get(h_id) # 赏赐 if check == 1: new_gem['t_hero'] = h_id new_hero['h_lead'] = hero.get('h_lead') + gem.get('t_lead') new_hero['h_force'] = hero.get('h_force') + gem.get('t_force') new_hero['h_brain'] = hero.get('h_brain') + gem.get('t_brain') new_hero['h_politics'] = hero.get('h_politics') + gem.get('t_politics') new_hero['h_charm'] = hero.get('h_charm') + gem.get('t_charm') condition_t = 't_id = {}'.format(t_id) condition_h = 'h_id = {}'.format(h_id) Conn.update_data(conn, 'gems', new_gem, condition_t) Conn.update_data(conn, 'heroes', new_hero, condition_h) if group.get('g_status') == 1: print('{0}获得赏赐宝物{1},属性提升!'.format(hero.get('h_name'), gem.get('t_name'))) print('{}:臣叩谢圣恩,必鞠躬尽瘁,死而后已!'.format(hero.get('h_name'))) # 收回 else: new_gem['t_hero'] = 0 new_hero['h_lead'] = hero.get('h_lead') - gem.get('t_lead') new_hero['h_force'] = hero.get('h_force') - gem.get('t_force') new_hero['h_brain'] = hero.get('h_brain') - gem.get('t_brain') new_hero['h_politics'] = hero.get('h_politics') - gem.get('t_politics') new_hero['h_charm'] = hero.get('h_charm') - gem.get('t_charm') condition_t = 't_id = {}'.format(t_id) condition_h = 'h_id = {}'.format(h_id) Conn.update_data(conn, 'gems', new_gem, condition_t) Conn.update_data(conn, 'heroes', new_hero, condition_h) if group.get('g_status') == 1: print('{0}失去宝物{1},属性下降!'.format(hero.get('h_name'), gem.get('t_name'))) print('{}:臣有负圣望,甘受惩罚,绝无怨言!'.format(hero.get('h_name'))) heroes = Conn.get_heroes(conn) return heroes
def get_rations(conn, group): """ 势力粮草收入 :param conn: :param group: :return: group """ g_rations = group.get('g_rations') g_farming = group.get('g_farming') g_populace = group.get('g_populace') g_science = group.get('g_science') new_group = dict() # 粮草收入量受势力农业和势力人口以及势力科技影响 new_rations = random.randint( round(g_farming * g_populace * (1 + g_science / 1000) / 500), round(g_farming * g_populace * (1 + g_science / 1000) / 300)) g_rations += new_rations new_group['g_rations'] = g_rations group['g_rations'] = g_rations condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本季度{0}收获粮草{1}石'.format(group.get('g_name'), new_rations)) return group
def get_gold(conn, group): """ 势力黄金收入 :param conn: :param group: :return: group """ g_gold = group.get('g_gold') g_economy = group.get('g_economy') g_populace = group.get('g_populace') g_science = group.get('g_science') new_group = dict() # 黄金收入量受势力经济和势力人口以及势力科技影响 new_gold = random.randint( round(g_economy * g_populace * (1 + g_science / 1000) / 1000), round(g_economy * g_populace * (1 + g_science / 1000) / 800)) g_gold += new_gold new_group['g_gold'] = g_gold group['g_gold'] = g_gold condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) if group.get('g_status') == 1: print('本季度{0}获得黄金{1}两'.format(group.get('g_name'), new_gold)) return group
def run(conn): """ 游戏运行主体 :param conn: 数据库连接对象 :return: None """ # 获取游戏纪年 years = Conn.get_years(conn) # 年标记 year = years[0] # 回合标记 time = years[1] # 玩家势力信息字典 group = dict() print('\n{0}恭迎主公!{1}\n'.format('-' * 70, '-' * 70)) while True: print('*' * 150) # 月标记 month = 12 if time % 12 == 0 else time % 12 # 获取所有势力信息 groups = Conn.get_groups(conn) # 获取所有英雄信息 heroes = Conn.get_heroes(conn) # 获取所有宝物信息 gems = Conn.get_gems(conn) # 获取所有兵种信息 ranker = Conn.get_ranker(conn) # 取出标记为玩家的势力信息 for k, v in groups.items(): if v.get('g_status') == 1: group = groups.get(k) PM.print_group_info(group, ranker, year, month) g_id = group.get('g_id') # 获取玩家势力中英雄ID列表 group_hero = Conn.get_group_hero(conn, g_id) # 游戏纪年更新 now_years = dict() now_years['year'] = year now_years['month'] = month Conn.clear_data(conn, 'years', now_years) start = int(input('新的一月,请主公决策:\n1 势力朝会 2 退隐山林\n')) if start != 1: print('\n{0}恭送主公!{1}\n'.format('-' * 70, '-' * 70)) exit() # 循环,直到势力所有英雄都有了任务或选择了退朝,执行任务 while True: # 主命令 # 内政:经济 农业 军事 科技 民心 # 军政:募兵 训练 巡逻 征讨 # 英雄:寻访 # 官职:擢升 罢黜 # 赏罚:赏赐 罚没 # 市场:粮草交易:买进 卖出 # 宝物交易:收购 贩卖 # 情报:势力 英雄 宝物 兵种 # 退朝 main_order = int( input('请下达命令:1 内政 2 军政 3 英雄 4 市场 5 情报 0 退朝\n')) if main_order not in [1, 2, 3, 4, 5]: break # 内政命令 if main_order == 1: # 次命令 # 经济 农业 军事 科技 民心 manage_order = int( input('请选择内政策略:1 经济 2 农业 3 军事 4 科技 5 民心 0 取消\n')) if manage_order not in [1, 2, 3, 4, 5]: continue # 执行听候差遣的英雄列表 c_hero = choice_executor(group_hero, heroes) hero_id = c_hero[0] check = int( input('任务需要经费黄金{}两,是否执行:\n1 是 2 否\n'.format( 50 * len(hero_id)))) # 经济 if manage_order == 1: if check == 1: # 执行经济开发操作 group = MG.get_economy(conn, group, heroes, hero_id, check) # 农业 elif manage_order == 2: if check == 1: # 执行农业开发操作 group = MG.get_farming(conn, group, heroes, hero_id, check) # 军事 elif manage_order == 3: if check == 1: # 执行军事提升操作 group = MG.get_military(conn, group, heroes, hero_id, check) # 科技 elif manage_order == 4: if check == 1: # 执行科技提升操作 group = MG.get_science(conn, group, heroes, hero_id, check) # 民心 else: if check == 1: # 执行民心提升操作 group = MG.get_morale(conn, group, heroes, hero_id, check) group_hero = c_hero[1] # 势力英雄都执行任务后自动结束本回合 if len(group_hero) == 0: print('谨遵圣谕,臣等告退!') break # 军政命令 elif main_order == 2: # 次命令 # 募兵 训练 巡逻 征讨 manage_order = int( input('请选择军政策略:1 募兵 2 训练 3 巡逻 4 征讨 0 取消\n')) if manage_order not in [1, 2, 3, 4]: continue # 执行听候差遣的英雄列表 c_hero = choice_executor(group_hero, heroes) hero_id = c_hero[0] # 募兵 if manage_order == 1: # 计算最大招募兵力数 max_ranker = round(group.get('g_populace') / 50) new_ranker = int( input('当前最大可招募兵力为{},请输入本次招募兵力数:\n'.format(max_ranker))) check = int( input('是否确认招募新兵{}人:\n1 是 2 否\n'.format(new_ranker))) if check == 1: # 执行士兵招募操作 group = MG.get_ranker(conn, group, heroes, hero_id, max_ranker, new_ranker, check) else: continue # 训练 elif manage_order == 2: check = int(input('请指定训练内容:\n1 变更兵种 2 常规训练 3 强化训练\n')) if check not in [1, 2, 3]: check = 2 if check == 1: exp = group.get('g_experience') level = (1 if exp < 50 else 2 if exp < 200 else 3 if exp < 450 else 4 if exp < 950 else 5) level_ranker = Conn.get_level_ranker(conn, level) PM.print_corps_info(level_ranker, ranker) choice = int(input('请指定变更兵种ID:')) if choice not in level_ranker: continue MG.change_corps(conn, group, ranker, choice) elif check == 2: t_type = 1 # 常规训练 group = MG.train_corps(conn, group, heroes, hero_id, t_type) else: t_type = 2 # 强化训练 group = MG.train_corps(conn, group, heroes, hero_id, t_type) # 巡逻 elif manage_order == 3: p_group = MG.patrol_group(conn, group, heroes, hero_id) group = p_group[0] r_type = p_group[1] if r_type == 2: # 讨贼巡逻,发现贼寇探子,完成讨贼 spy = MG.find_spy(group) group = MG.fight_enemy(conn, group, heroes, hero_id, spy) elif r_type == 3: # 据点巡逻,发现贼寇据点 MG.find_den(conn, group, ranker) # 征讨 else: print('征讨胜利') group_hero = c_hero[1] # 势力英雄都执行任务后自动结束本回合 if len(group_hero) == 0: print('谨遵圣谕,臣等告退!') break # 英雄命令 elif main_order == 3: # 次命令 # 寻访 # 官职:擢升 罢黜 # 赏罚:赏赐 罚没 manage_order = int(input('请选择执行内容:1 寻访 2 官职 3 赏罚 0 取消\n')) if manage_order not in [1, 2, 3]: continue # 寻访 if manage_order == 1: # 执行听候差遣的英雄列表 c_hero = choice_executor(group_hero, heroes) hero_id = c_hero[0] group_hero = c_hero[1] group = MG.find_recluse_treasure(conn, group, heroes, hero_id, gems) # 官职 elif manage_order == 2: group_heroes = Conn.get_group_hero(conn, group.get('g_id')) PM.print_group_hero(group_heroes, heroes) h_id = int(input().strip()) key_order = int(input('请选择任免:1 擢升 2 罢黜 0 取消\n')) if key_order == 1: print('擢升了官职') elif key_order == 2: print('罢黜了官职') else: continue # 赏罚 else: key_order = int(input('请选择赏罚:1 赏赐 2 罚没 0 取消\n')) if key_order == 1: # 获取玩家势力中宝物ID列表 group_gem = Conn.get_group_gem(conn, group.get('g_id')) if len(group_gem) > 0: group_heroes = Conn.get_group_hero( conn, group.get('g_id')) PM.print_group_hero(group_heroes, heroes) h_id = int(input().strip()) group_gem = [ t_id for t_id in group_gem if gems.get(t_id).get('t_hero') == 0 ] PM.print_choice_gem(group_gem, gems, heroes) t_id = int(input('请输入赏赐宝物ID:\n')) heroes = MG.rewards_hero(conn, group, gems, t_id, heroes, h_id, 1) elif key_order == 2: # 获取玩家势力中宝物ID列表 group_gem = Conn.get_group_gem(conn, group.get('g_id')) if len(group_gem) > 0: group_gem = [ t_id for t_id in group_gem if gems.get(t_id).get('t_hero') != 0 ] if len(group_gem) == 0: print('无持有宝物的英雄!') continue PM.print_choice_gem(group_gem, gems, heroes) t_id = int(input('请输入收回宝物ID:\n')) heroes = MG.rewards_hero( conn, group, gems, t_id, heroes, gems.get(t_id).get('t_hero'), 2) else: continue # 势力英雄都执行任务后自动结束本回合 if len(group_hero) == 0: print('谨遵圣谕,臣等告退!') break # 市场命令 elif main_order == 4: # 次命令 # 粮草交易:买进 卖出 # 宝物交易:收购 贩卖 manage_order = int(input('请选择交易内容:1 粮草交易 2 宝物交易 0 取消\n')) if manage_order not in [1, 2]: continue # 执行听候差遣的英雄列表 c_hero = choice_executor(group_hero, heroes) hero_id = c_hero[0] # 粮草交易 if manage_order == 1: key_order = int(input('请选择交易类型:1 买进 2 卖出 0 取消\n')) if key_order == 1: print('买进了粮草') elif key_order == 2: print('卖出了粮草') else: continue # 宝物交易 else: key_order = int(input('请选择交易类型:1 收购 2 贩卖 0 取消\n')) if key_order == 1: print('收购了宝物') elif key_order == 2: print('贩卖了宝物') else: continue group_hero = c_hero[1] # 势力英雄都执行任务后自动结束本回合 if len(group_hero) == 0: print('谨遵圣谕,臣等告退!') break # 情报命令 else: # 次命令 # 势力 英雄 宝物 兵种 query_order = int( input('请选择情报内容:1 势力一览 2 英雄一览 3 宝物一览 4 兵种一览\n')) if query_order not in [1, 2, 3, 4]: continue # 势力情报 if query_order == 1: PM.print_groups_info(groups, conn, heroes) # 英雄情报 elif query_order == 2: PM.print_heroes_info(groups, heroes) # 宝物情报 elif query_order == 3: PM.print_gems_info(groups, gems, heroes) # 兵种情报 else: level_ranker = ranker.keys() PM.print_corps_info(level_ranker, ranker) # 势力英雄都执行任务后自动结束本回合 if len(group_hero) == 0: print('谨遵圣谕,臣等告退!') break # 每月势力人口增加 group = MG.get_populace(conn, group) # 每月势力粮草消耗 group = MG.eat_rations(conn, group, heroes, ranker) # 对势力现状评定,有几率触发特殊事件 MG.check_group(conn, group, heroes, gems) # 每三个回合即每个季度执行 if time % 3 == 0: # 发放军饷 group = MG.pay_provisions(conn, group, heroes, ranker) # 势力黄金收入 group = MG.get_gold(conn, group) # 势力粮草收入 group = MG.get_rations(conn, group) # 每12个回合及每年执行 if time % 12 == 0: # 游戏纪年增加 year += 1 hero = dict() # 所有英雄年龄增加 for h_id in heroes.keys(): hero['h_age'] = heroes.get(h_id).get('h_age') + 1 # 如果英雄年龄增加后超过70岁,则英雄状态变更为死亡 if hero.get('h_age') > 70: hero['h_status'] = 0 hero['h_group'] = 0 print('{0}{1}年岁已高,不幸病逝!'.format( groups.get( heroes.get(h_id).get('h_group')).get('g_name'), heroes.get(h_id).get('h_name'))) condition = 'h_id = {}'.format(h_id) Conn.update_data(conn, 'heroes', hero, condition) # 电脑势力执行任务 MainAI.run(conn, time) time += 1
def find_recluse_treasure(conn, group, heroes, hero_id, gems): """ 寻访,概率寻访到在野英雄、在野宝物、黄金、粮草 :param hero_id: :param conn: :param group: :param heroes: :param gems: :return: group """ # 在野英雄ID列表 recluse = Conn.find_recluse(conn) # 在野宝物ID列表 treasure = Conn.find_treasure(conn) # 势力变更信息及开关 new_group = dict() check_g = False # 英雄变更信息 new_hero = dict() # 宝物变更信息 new_gem = dict() # 获取势力钱粮信息 g_rations = group.get('g_rations') g_gold = group.get('g_gold') for h_id in hero_id: # 概率参数 # [0, 0.3) 30%概率寻访没有收获 # [0.3, 0.5) 20%概率寻访发现粮草 # [0.5, 0.7) 20%概率寻访发现黄金 # [0.7, 0.9) 20%概率寻访发现英雄 # [0.9, 1] 10%概率寻访发现宝物 k = random.random() * (1 - random.random()) if len(treasure) < 1 and len(recluse) > 0: k -= 0.11 elif len(recluse) < 1 and len(treasure) > 0: k = 0.99 elif len(recluse) + len(treasure) < 1: k -= 0.31 hero = heroes.get(h_id) if 0.2 <= k < 0.45: rations = random.randint( hero.get('h_brain') + hero.get('h_charm'), (hero.get('h_brain') + hero.get('h_charm')) * 2) g_rations += rations group['g_rations'] = g_rations new_group['g_rations'] = g_rations if group.get('g_status') == 1: print('{0}发现粮草,{1}粮草增加{2}'.format(hero.get('h_name'), group.get('g_name'), rations)) check_g = True elif 0.45 <= k < 0.7: gold = random.randint( hero.get('h_brain') + hero.get('h_charm'), (hero.get('h_brain') + hero.get('h_charm')) * 3) g_gold += gold group['g_gold'] = g_gold new_group['g_gold'] = g_gold if group.get('g_status') == 1: print('{0}发现黄金,{1}黄金增加{2}'.format(hero.get('h_name'), group.get('g_name'), gold)) check_g = True elif 0.7 <= k < 0.9: # 发现英雄ID try: re_id = random.choice(recluse) except Exception as e: print(e) print('{}本次寻访未发现任何事物'.format(hero.get('h_name'))) continue # 英雄投奔概率 # 受寻访英雄的智力和魅力值影响 hero_1 = (hero.get('h_brain') + hero.get('h_charm')) / 2 hero_2 = (heroes.get(re_id).get('h_brain') + heroes.get(re_id).get('h_charm')) / 2 if group.get('g_status') == 1: print('{0}发现在野英雄{1}!是否选择招揽!'.format( hero.get('h_name'), heroes.get(re_id).get('h_name'))) print( '姓名:{0}\n性别:{1}\n年龄:{2}\n身份:{3}\n统率:{4}\n武力:{5}\n智力:{6}\n政治:{7}\n魅力:{8}' .format( heroes.get(re_id).get('h_name'), heroes.get(re_id).get('h_gender'), heroes.get(re_id).get('h_age'), heroes.get(re_id).get('h_identity'), heroes.get(re_id).get('h_lead'), heroes.get(re_id).get('h_force'), heroes.get(re_id).get('h_brain'), heroes.get(re_id).get('h_politics'), heroes.get(re_id).get('h_charm'))) is_get = int(input('1 招揽 2 放弃\n')) if is_get == 1: choice = hero_1 / hero_2 - random.random() print( '{}:阁下慢走,方才见阁下举止不凡,一表人才,想来阁下绝非常人,何不投效我军,谋一个出路?'.format( hero.get('h_name'))) if choice > 0.6: print('{0}:久闻{1}大名,承蒙不弃,{0}愿效死力!'.format( heroes.get(re_id).get('h_name'), group.get('g_name'))) print('{0}:哈哈,我军得{1}相助,何愁天下不平?'.format( hero.get('h_name'), heroes.get(re_id).get('h_name'))) else: print('{0}:多谢大人好意,然{0}并无意出仕,还望大人恕罪。'.format( heroes.get(re_id).get('h_name'))) print('{0}:唉,既如此,{0}也不好再为难阁下,告辞。'.format( hero.get('h_name'))) if choice > 0.6: new_hero['h_group'] = group.get('g_id') new_hero['h_status'] = 2 recluse.remove(re_id) print('{0}成功招揽到英雄{1}!'.format( group.get('g_name'), heroes.get(re_id).get('h_name'))) condition_h = 'h_id = {}'.format(re_id) Conn.update_data(conn, 'heroes', new_hero, condition_h) elif 0.9 <= k <= 1: # 发现宝物ID # 受势力民心影响 treasure = [ t_id for t_id in treasure if gems.get(t_id).get('t_level') < ( 2 if group.get('g_morale') < 100 else 3 if group.get('g_morale') < 500 else 4) ] tr_id = random.choice(treasure) new_gem['t_group'] = group.get('g_id') condition_t = 't_id = {}'.format(tr_id) if group.get('g_status') == 1: print('{0}成功寻获到宝物{1}!'.format(hero.get('h_name'), gems.get(tr_id).get('t_name'))) Conn.update_data(conn, 'gems', new_gem, condition_t) else: if group.get('g_status') == 1: print('{}本次寻访未发现任何事物'.format(hero.get('h_name'))) if check_g: condition_g = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition_g) return group
def check_group(conn, group, heroes, gems): """ 势力现状评定,有几率触发特殊事件 :param gems: :param conn: :param group: :param heroes: :return: group """ g_morale = group.get('g_morale') g_gold = group.get('g_gold') g_rations = group.get('g_rations') g_ranker = group.get('g_ranker') g_populace = group.get('g_populace') group_hero = Conn.get_group_hero(conn, group.get('g_id')) # 下野英雄信息字典 retire = dict() # 势力变更信息字典 new_group = dict() # 势力变更开关 check_g = False # 英雄变更开关 check_h = False # 黄金小于0 if g_gold < 0: k = random.random() if 0.2 < k < 0.7: # 兵力哗变叛逃 deserter = random.randint(round(g_ranker * 0.05), round(g_ranker * 0.1)) g_ranker -= deserter group['g_ranker'] = g_ranker new_group['g_ranker'] = g_ranker print('因{0}军饷不足,{1}名士兵哗变叛逃了!'.format(group.get('g_name'), deserter)) check_g = True if 0.5 < k < 0.55: # 英雄辞官下野 h_id = random.choice(group_hero) group_hero.remove(h_id) retire[h_id] = {'h_group': 0, 'h_status': 1} print('因{0}军饷不足,{1}辞官下野了!'.format(group.get('g_name'), heroes.get(h_id).get('h_name'))) check_h = True # 粮草小于0 if g_rations < 0: k = random.random() if 0.2 < k < 0.8: # 兵力哗变叛逃 deserter = random.randint(round(g_ranker * 0.08), round(g_ranker * 0.15)) g_ranker -= deserter group['g_ranker'] = g_ranker new_group['g_ranker'] = g_ranker print('因{0}粮草不足,{1}名士兵哗变叛逃了!'.format(group.get('g_name'), deserter)) if 0.53 < k < 0.55: # 英雄辞官下野 h_id = random.choice(group_hero) group_hero.remove(h_id) retire[h_id] = {'h_group': 0, 'h_status': 1} print('因{0}粮草不足,{1}辞官下野了!'.format(group.get('g_name'), heroes.get(h_id).get('h_name'))) check_h = True g_ranker = round(g_ranker * 0.6) g_populace += round(g_ranker * 0.4) g_gold += round(g_ranker * 0.4 / 100) new_group['g_ranker'] = g_ranker new_group['g_gold'] = g_gold new_group['g_populace'] = g_populace group['g_ranker'] = g_ranker group['g_gold'] = g_gold group['g_populace'] = g_populace check_g = True # 民心大于600 if g_morale > 600: k = random.random() if 0.3 < k < 0.7: # 人口迁入 follower = random.randint(round(g_populace * 0.05), round(g_populace * 0.08)) g_populace += follower group['g_populace'] = g_populace new_group['g_populace'] = g_populace print('因{0}民心所向,人口迁入了{1}人!'.format(group.get('g_name'), follower)) check_g = True if 0.4 < k < 0.6: # 钱粮捐献 gold = random.randint(round(g_populace / 500), round(g_populace / 200)) rations = random.randint(round(g_populace / 80), round(g_populace / 30)) g_gold += gold g_rations += rations group['g_gold'] = g_gold group['g_rations'] = g_rations new_group['g_gold'] = g_gold new_group['g_rations'] = g_rations print('因{0}民心所向,民众发起粮草捐赠,粮草增加{1}石,黄金增加{2}两!'.format( group.get('g_name'), rations, gold)) check_g = True if 0.5 < k < 0.55: # 志愿参军 volunteer = random.randint(round(g_populace * 0.003), round(g_populace * 0.08)) g_ranker += volunteer g_populace -= volunteer group['g_ranker'] = g_ranker group['g_populace'] = g_populace new_group['g_ranker'] = g_ranker new_group['g_populace'] = g_populace print('因{0}民心所向,民众发起志愿参军,兵力增加{1}名!'.format(group.get('g_name'), volunteer)) check_g = True if 0.52 < k < 0.53: # 进贡宝物 new_gems = dict() treasure = Conn.find_treasure(conn) t_id = random.choice(treasure) new_gems['t_group'] = group.get('g_id') condition_t = 't_id = {}'.format(t_id) print('因{0}民心所向,民众进贡传家宝物,{0}获得宝物{1}!'.format( group.get('g_name'), gems.get(t_id).get('t_name'))) Conn.update_data(conn, 'gems', new_gems, condition_t) # 民心大于300 elif g_morale > 300: k = random.random() if 0.5 < k < 0.6: # 人口迁入 follower = random.randint(round(g_populace * 0.03), round(g_populace * 0.06)) g_populace += follower group['g_populace'] = g_populace new_group['g_populace'] = g_populace print('因{0}民心所向,人口迁入了{1}人!'.format(group.get('g_name'), follower)) check_g = True if 0.55 < k < 0.6: # 钱粮捐献 gold = random.randint(round(g_populace / 500), round(g_populace / 200)) rations = random.randint(round(g_populace / 80), round(g_populace / 30)) g_gold += gold g_rations += rations group['g_gold'] = g_gold group['g_rations'] = g_rations new_group['g_gold'] = g_gold new_group['g_rations'] = g_rations print('因{0}民心所向,民众发起粮草捐赠,粮草增加{1}石,黄金增加{2}两!'.format( group.get('g_name'), rations, gold)) check_g = True # 民心大于100 elif g_morale > 100: k = random.random() if 0.5 < k < 0.6: # 人口迁入 follower = random.randint(round(g_populace * 0.01), round(g_populace * 0.03)) g_populace += follower group['g_populace'] = g_populace new_group['g_populace'] = g_populace print('因{0}民心所向,人口迁入了{1}人!'.format(group.get('g_name'), follower)) check_g = True # 民心小于30 if 10 < g_morale < 30: k = random.random() if 0.5 < k < 0.7: # 人口流失 refugee = random.randint(round(g_populace * 0.03), round(g_populace * 0.05)) g_populace -= refugee group['g_populace'] = g_populace new_group['g_populace'] = g_populace print('因{0}民心不足,人口流失了{1}人!'.format(group.get('g_name'), refugee)) check_g = True # 民心小于10 elif g_morale < 10: # 人口流失 refugee = random.randint(round(g_populace * 0.05), round(g_populace * 0.08)) g_populace -= refugee # 士兵哗变叛逃 deserter = random.randint(round(g_ranker * 0.05), round(g_ranker * 0.08)) g_ranker -= deserter group['g_ranker'] = g_ranker new_group['g_ranker'] = g_ranker group['g_populace'] = g_populace new_group['g_populace'] = g_populace print('因{0}民心不足,人口流失了{1}人,{2}名士兵哗变叛逃了!'.format(group.get('g_name'), refugee, deserter)) check_g = True if check_g: condition_g = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition_g) if check_h: for hid in retire.keys(): condition_h = 'h_id = {}'.format(hid) Conn.update_data(conn, 'heroes', retire.get(hid), condition_h) return group
def get_ranker(conn, group, heroes, hero_id, max_ranker, new_ranker, check): """ 势力募兵操作 :param conn: :param group: :param heroes: :param hero_id: :param max_ranker: :param new_ranker: :param check: :return: group """ if check == 1: g_populace = group.get('g_populace') g_ranker = group.get('g_ranker') g_rations = group.get('g_rations') g_gold = group.get('g_gold') g_morale = group.get('g_morale') new_group = dict() name = '' h_lead = 0 if g_rations < 0: return group for h_id in hero_id: h_lead += heroes.get(h_id).get('h_lead') name += '{},'.format(heroes.get(h_id).get('h_name')) if new_ranker > max_ranker: new_ranker = max_ranker g_ranker += new_ranker if g_ranker > g_populace / 10: new_ranker = round(g_populace / 10) - (g_ranker - new_ranker) if group.get('g_status') == 1: print('因势力兵力不得超过总人口的十分之一,所以本次招募最大兵力为{}'.format(new_ranker)) g_ranker = round(g_populace / 10) h_buff = round(h_lead * (1 + g_morale / 1000) / 20) # 募兵消耗粮草和黄金,并影响势力民心,消耗量受执行英雄统率影响 new_rations = round(new_ranker / 50 - h_buff) if new_ranker / 50 - h_buff > 0 else 0 new_gold = round(new_ranker / 200 - h_buff) if new_ranker / 200 - h_buff > 0 else 0 new_morale = round(new_ranker / 1000) if new_ranker / 1000 > 0 else 0 new_morale = 20 if new_morale > g_morale * 0.3 else new_morale g_populace -= new_ranker g_rations -= new_rations g_gold -= new_gold g_morale -= new_morale new_group['g_populace'] = g_populace new_group['g_rations'] = g_rations new_group['g_gold'] = g_gold new_group['g_morale'] = g_morale new_group['g_ranker'] = g_ranker group['g_populace'] = g_populace group['g_rations'] = g_rations group['g_gold'] = g_gold group['g_morale'] = g_morale group['g_ranker'] = g_ranker if group.get('g_status') == 1: print( '本次{0}成功招募士兵共{1}人,消耗粮草{2}石,消耗黄金{3}两,降低民心{4}点,减少人口{5}人'.format( name[:-1], new_ranker, new_rations, new_gold, new_morale, new_ranker)) condition = 'g_id = {}'.format(group.get('g_id')) Conn.update_data(conn, 'hero_group', new_group, condition) return group