import Common import GaoNengLogic import SiZhuLogic from DataCenter import DataCenter from LocalModel import BaZi, GaoNeng, SiZhu, SiZhuWeiZhi, PingJia, Score def calc_fangan(bazi: BaZi, dc: DataCenter): for tg in Common.tiangan_ary: gns1 = GaoNengLogic.fix_gaoneng_by_text(tg, bazi.sizhu_tiangans(), bazi, -1, dc) sz = __build_fangan_info(tg, gns1, bazi, dc) for item in gns1: __calc_gaoneng_extra_power(item, sz) GaoNengLogic.__calc_fangxiang(item, -1) __update_fangan_gaoneng_powers(sz, bazi) __calc_pingjia(sz, bazi) for dz in Common.dizhi_ary: gns2 = GaoNengLogic.fix_gaoneng_by_text(dz, bazi.sizhu_dizhis(), bazi, -1, dc) sz = __build_fangan_info(dz, gns2, bazi, dc) for item in gns2: __calc_gaoneng_extra_power(item, sz) GaoNengLogic.__calc_fangxiang(item, -1) __update_fangan_gaoneng_powers(sz, bazi) __calc_pingjia(sz, bazi) def __build_fangan_info(key: str, gns: [GaoNeng], bazi: BaZi, dc: DataCenter): isTianGan = Common.is_tiangan(key) sz = SiZhu(bazi.qiankun, isTianGan, key, SiZhuWeiZhi.extra) if isTianGan: SiZhuLogic.__fill_sizhu_of_tiangan(sz) SiZhuLogic.__fill_shishen(bazi.riGan, sz) else: SiZhuLogic.__fill_sizhu_of_dizhi(sz) tg = Common.get_tiangan_by_dizhi(sz.text) SiZhuLogic.__fill_sizhu_of_zhangsheng(sz, tg, bazi.riGan.text) SiZhuLogic.__fill_cang_gan(sz, bazi.riGan) SiZhuLogic.__fill_shishen(bazi.riGan, sz) sz.gaonengs = gns if not sz.isTianGan: SiZhuLogic.__check_muku(sz, bazi, dc, sz.gaonengs) sz.power = dc.get_shishen_power(sz.shiShen) if sz.power == 0: sz.power = dc.get_power_step(sz.wuXing) per = dc.get_cangan_power_percentage_by_zhu(sz) sz.update_canggan_powers(per) sz.selfPower = sz.get_canggan_power_by_wuxing(sz.wuXing) sz.check_lushen(bazi.riGan.text) bazi.fangans.append(sz) return sz def __update_fangan_gaoneng_powers(sz: SiZhu, bazi: BaZi): # 记录新增的高能关系对其他柱的影响 sz.extraGaoNengMap = {} for i in range(1, 9): sz.extraGaoNengMap[i] = [] for gn in sz.gaonengs: if gn.zhu1 != -1: gn1 = gn.copySelf() GaoNengLogic.__calc_fangxiang(gn1, gn1.zhu1) sz.extraGaoNengMap[gn.zhu1].append(gn1) if gn.zhu2 != -1: gn2 = gn.copySelf() GaoNengLogic.__calc_fangxiang(gn2, gn2.zhu2) sz.extraGaoNengMap[gn.zhu2].append(gn2) if gn.zhu3 is not None and gn.zhu3 != -1: gn3 = gn.copySelf() GaoNengLogic.__calc_fangxiang(gn3, gn3.zhu3) sz.extraGaoNengMap[gn.zhu3].append(gn3) def __calc_gaoneng_extra_power(gn: GaoNeng, sz: SiZhu): # 由于是新增的四柱导致的高能关系变化,所以有可能新增的在原局并不存在 # 这会导致原先计算的gn.power 等于0 # 而既然新增了,那么对应的力量也应该是增加的 # 这会导致冲穿的结果可能不同 wuxing = gn.wuxing1 index = 1 if gn.zhu2 == -1: wuxing = gn.wuxing2 index = 2 elif gn.zhu3 == -1: wuxing = gn.wuxing3 index = 3 power = sz.get_canggan_power_by_wuxing(wuxing) if index == 1: gn.extraPower1 = power elif index == 2: gn.extraPower2 = power elif index == 3: gn.extraPower3 = power def __calc_pingjia(sz: SiZhu, bazi: BaZi): sz.pingjia = PingJia() sz.pingjia.mubiao.append(__calc_guansha_score("官", sz)) sz.pingjia.mubiao.append(__calc_guansha_score("杀", sz)) sz.pingjia.mubiao.append(__calc_caixing_score("财", sz)) sz.pingjia.mubiao.append(__calc_caixing_score("才", sz)) sz.pingjia.genji.append(__calc_yinxiao_score("印", sz)) sz.pingjia.genji.append(__calc_yinxiao_score("枭", sz)) sz.pingjia.zhuli.append(__calc_bijie_score("比", sz)) sz.pingjia.zhuli.append(__calc_bijie_score("劫", sz)) __calc_shouhu_score(sz, bazi) __calc_lushen_score(sz, bazi) sz.pingjia.calc_scores() def __calc_guansha_score(key: str, sz: SiZhu): sc = Score(key) gnsHe = __find_gaoneng_in_zhu(key, "合", sz) # 没有合得1分,有合看合什么依次评分 if len(gnsHe) == 0: sc.he = 1 for gn in gnsHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 he = 0 if other == "印": he = 10 elif other == "食": he = 9 elif other == "财": he = 8 elif other == "枭": he = 7 elif other == "才": he = 6 elif other == "比": he = 5 elif other == "伤": he = 4 elif other == "劫": he = 3 # sz.pingjia.mubiaoDetail.append(key + "合" + other + "+" + str(he)) sc.he += he # 暗合与合一样的记分方式 gnsAnHe = __find_gaoneng_in_zhu(key, "暗合", sz) if len(gnsAnHe) == 0: sc.anHe = 1 for gn in gnsAnHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 anHe = 0 if other == "印": anHe = 10 elif other == "食": anHe = 9 elif other == "财": anHe = 8 elif other == "枭": anHe = 7 elif other == "才": anHe = 6 elif other == "比": anHe = 5 elif other == "伤": anHe = 4 elif other == "劫": anHe = 3 sc.anHe += anHe # sz.pingjia.mubiaoDetail.append(key + "暗合" + other + "+" + str(anHe)) gnsChong = __find_gaoneng_in_zhu(key, "冲", sz) # 没有冲给5分,有冲冲赢了给分,没赢不得分 if len(gnsChong) == 0: sc.chong = 5 for gn in gnsChong: other = gn.shishen1 powerOther = gn.power1 + gn.extraPower1 powerSelf = gn.power2 + gn.extraPower2 if gn.shishen1 == key: other = gn.shishen2 powerSelf = gn.power1 + gn.extraPower1 powerOther = gn.power2 + gn.extraPower2 chong = 0 if powerSelf < powerOther: if other in ["食", "伤"]: chong = 10 if other in ["比", "劫"]: chong = 9 sc.chong += chong # sz.pingjia.mubiaoDetail.append(key + "冲" + other + "+" + str(chong)) gnsChuan = __find_gaoneng_in_zhu(key, "穿", sz) # 没有穿满分,一个穿扣一分 sc.chuan = 10 - len(gnsChuan) # sz.pingjia.mubiaoDetail.append(key + "穿" + str(len(gnsChuan)) + "次+" + str(sc.chuan)) gnsPo = __find_gaoneng_in_zhu(key, "破", sz) # 没有破满分,一个破扣一分 sc.po = 10 - len(gnsPo) # sz.pingjia.mubiaoDetail.append(key + "破" + str(len(gnsPo)) + "次+" + str(sc.po)) sc.calcTotal() return sc def __calc_caixing_score(key: str, sz: SiZhu): sc = Score(key) gnsHe = __find_gaoneng_in_zhu(key, "合", sz) # 没有合得1分,有合看合什么依次评分 if len(gnsHe) == 0: sc.he = 1 for gn in gnsHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 he = 0 if other == "印": he = 10 elif other == "官": he = 9 elif other == "食": he = 8 elif other == "枭": he = 7 elif other == "比": he = 6 elif other == "伤": he = 5 elif other == "劫": he = 4 elif other == "杀": he = 3 # sz.pingjia.mubiaoDetail.append(key + "合" + other + "+" + str(he)) sc.he += he # 暗合与合一样的记分方式 gnsAnHe = __find_gaoneng_in_zhu(key, "暗合", sz) if len(gnsAnHe) == 0: sc.anHe = 1 for gn in gnsAnHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 anHe = 0 if other == "印": anHe = 10 elif other == "官": anHe = 9 elif other == "食": anHe = 8 elif other == "枭": anHe = 7 elif other == "比": anHe = 6 elif other == "伤": anHe = 5 elif other == "劫": anHe = 4 elif other == "杀": anHe = 3 # sz.pingjia.mubiaoDetail.append(key + "暗合" + other + "+" + str(anHe)) sc.anHe += anHe gnsChong = __find_gaoneng_in_zhu(key, "冲", sz) # 没有冲给5分,有冲冲赢了给分,没赢不得分 if len(gnsChong) == 0: sc.chong = 5 for gn in gnsChong: other = gn.shishen1 powerOther = gn.power1 + gn.extraPower1 powerSelf = gn.power2 + gn.extraPower2 if gn.shishen1 == key: other = gn.shishen2 powerSelf = gn.power1 + gn.extraPower1 powerOther = gn.power2 + gn.extraPower2 chong = 0 if powerSelf < powerOther: if other in ["印", "枭"]: chong = 10 if other in ["比", "劫"]: chong = 9 # sz.pingjia.mubiaoDetail.append(key + "冲" + other + "+" + str(chong)) sc.chong += chong gnsChuan = __find_gaoneng_in_zhu(key, "穿", sz) # 没有穿满分,一个穿扣一分 sc.chuan = 10 - len(gnsChuan) # sz.pingjia.mubiaoDetail.append(key + "穿" + str(len(gnsChuan)) + "次+" + str(sc.chuan)) gnsPo = __find_gaoneng_in_zhu(key, "破", sz) # 没有破满分,一个破扣一分 sc.po = 10 - len(gnsPo) # sz.pingjia.mubiaoDetail.append(key + "破" + str(len(gnsPo)) + "次+" + str(sc.po)) sc.calcTotal() return sc def __calc_yinxiao_score(key: str, sz: SiZhu): sc = Score(key) gnsHe = __find_gaoneng_in_zhu(key, "合", sz) # 没有合得1分,有合看合什么依次评分 if len(gnsHe) == 0: sc.he = 1 for gn in gnsHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 he = 0 if other == "食": he = 10 elif other == "伤": he = 9 elif other == "比": he = 8 elif other == "劫": he = 7 sc.he += he # 暗合与合一样的记分方式 gnsAnHe = __find_gaoneng_in_zhu(key, "暗合", sz) if len(gnsAnHe) == 0: sc.anHe = 1 for gn in gnsAnHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 anHe = 0 if other == "食": anHe = 10 elif other == "伤": anHe = 9 elif other == "比": anHe = 8 elif other == "劫": anHe = 7 sc.anHe += anHe gnsChong = __find_gaoneng_in_zhu(key, "冲", sz) # 没有冲给5分,有冲冲赢了给分,没赢不得分 if len(gnsChong) == 0: sc.chong = 5 for gn in gnsChong: other = gn.shishen1 powerOther = gn.power1 + gn.extraPower1 powerSelf = gn.power2 + gn.extraPower2 if gn.shishen1 == key: other = gn.shishen2 powerSelf = gn.power1 + gn.extraPower1 powerOther = gn.power2 + gn.extraPower2 chong = 0 if powerSelf < powerOther: if other in ["食"]: chong = 10 if other in ["伤"]: chong = 9 sc.chong += chong gnsChuan = __find_gaoneng_in_zhu(key, "穿", sz) # 没有穿满分,一个穿扣一分 sc.chuan = 10 - len(gnsChuan) gnsPo = __find_gaoneng_in_zhu(key, "破", sz) # 没有破满分,一个破扣一分 sc.po = 10 - len(gnsPo) sc.calcTotal() return sc def __calc_bijie_score(key: str, sz: SiZhu): sc = Score(key) gnsHe = __find_gaoneng_in_zhu(key, "合", sz) # 没有合得1分,有合看合什么依次评分 if len(gnsHe) == 0: sc.he = 1 for gn in gnsHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 he = 0 if other == "食": he = 10 elif other == "伤": he = 9 sc.he += he # 暗合与合一样的记分方式 gnsAnHe = __find_gaoneng_in_zhu(key, "暗合", sz) if len(gnsAnHe) == 0: sc.anHe = 1 for gn in gnsAnHe: other = gn.shishen1 if gn.shishen1 == key: other = gn.shishen2 anHe = 0 if other == "食": anHe = 10 elif other == "伤": anHe = 9 sc.anHe += anHe # 比肩不看冲 sc.chong = 1 gnsChuan = __find_gaoneng_in_zhu(key, "穿", sz) # 没有穿满分,一个穿扣一分 sc.chuan = 10 - len(gnsChuan) gnsPo = __find_gaoneng_in_zhu(key, "破", sz) # 没有破满分,一个破扣一分 sc.po = 10 - len(gnsPo) sc.calcTotal() return sc def __calc_shouhu_score(sz: SiZhu, bazi: BaZi): # 计算地支六合化穿破刑的部分,冲不用保护,冲可能是好事 for gn in sz.gaonengs: if gn.leibie == "地支合": # 对自己这柱的保护 selfShouHuChuan = __count_gaoneng_in_list(sz.gaonengs, "穿") selfShouHuPo = __count_gaoneng_in_list(sz.gaonengs, "破") selfShouHuXing = __count_gaoneng_in_list(sz.gaonengs, "刑") selfShouHuLuShen = 0 if sz.isLuShen: selfShouHuLuShen += 1 if sz.isBanLu: selfShouHuLuShen += 0.5 selfShouHu = selfShouHuLuShen * 1.3 + selfShouHuChuan * 1.2 + selfShouHuPo * 1.1 + selfShouHuXing * 1 # 对其他柱的保护 otherShouHu = 0 other = gn.zhu1 if sz.text == gn.arg1: other = gn.zhu2 zhu = bazi.getZhuByWeiZhi(other) if zhu is not None: otherShouHuChuan = __count_gaoneng_in_list(zhu.gaonengs, "穿") otherShouHuPo = __count_gaoneng_in_list(zhu.gaonengs, "破") otherShouHuXing = __count_gaoneng_in_list(zhu.gaonengs, "刑") otherShouHuLuShen = 0 if zhu.isLuShen: otherShouHuLuShen += 1 if zhu.isBanLu: otherShouHuLuShen += 0.5 otherShouHu = otherShouHuLuShen * 1.3 + otherShouHuChuan * 1.2 + otherShouHuPo * 1.1 + otherShouHuXing * 1 sz.pingjia.shouhuScore += (selfShouHu + otherShouHu) sz.pingjia.shouhuScore *= 1000 def __calc_lushen_score(sz: SiZhu, bazi: BaZi): if sz.isLuShen: sz.pingjia.lushenScore += 2 if sz.isBanLu: sz.pingjia.lushenScore += 1 he = __count_gaoneng_in_list_by_leibie(sz.gaonengs, "地支合") if he == 0: # 没有地支合的保护,才会被破才穿 po = __count_gaoneng_in_list(sz.gaonengs, "破") if po > 0: sz.pingjia.lushenScore *= 0.5 chuan = __count_gaoneng_in_list(sz.gaonengs, "穿") if chuan > 0: sz.pingjia.lushenScore = 0 sz.pingjia.lushenScore *= 1000 def __find_gaoneng_in_zhu(key: str, guanxi: str, sz: SiZhu) -> [GaoNeng]: result: [GaoNeng] = [] for gn in sz.gaonengs: if gn.guanxi == guanxi: if gn.shishen1 == key or gn.shishen2 == key: result.append(gn) return result def __count_gaoneng_in_list(gns: [GaoNeng], guanxi: str): ct = 0 for gn in gns: if gn.guanxi == guanxi: ct += 1 return ct def __count_gaoneng_in_list_by_leibie(gns: [GaoNeng], leibie: str): ct = 0 for gn in gns: if gn.leibie == leibie: ct += 1 return ct