DataCenter.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import Common
  2. from LocalModel import BaZi, SiZhu
  3. class DataCenter():
  4. bazi: BaZi
  5. tianganTexts: [str]
  6. dizhiTexts: [str]
  7. cangGanPowerPercentage: {} # key:5-8, value: [v1,v2,v3]
  8. cangganWuXing: {} # key: 5-8, value:[火,None,None]
  9. cangganShiShen: {} # key:5-8,value:[食,None,None]
  10. wuxingCounter: {} # key:五行, value: int
  11. wuxingCounterIncludeCangGan: {} # key:五行, value: int
  12. wuxingWangShuai: {} # key五行,value:旺相休囚死
  13. shishenCounter: {} # key:十神, value: int
  14. shishenCounterIncludeCangGan: {} # key:十神, value: int
  15. gaonengTexts: [str]
  16. gaonengInWeiZhi: {} # key 位置, value:text
  17. mukuState: {} # key 5-8 ,value:None | True | False
  18. shishenPower: {} # key 十神, value:int
  19. powerStep: {} # key:五行,value:power step,对应旺相休囚死
  20. genqi: {} # key 位置, value: [真,假]
  21. lushenAmount: int
  22. banluAmount: int
  23. shishenMap: {} # key 位置,value:十神
  24. shiShenTexts: []
  25. shishenZengQiang: {} # key 十神,value:int
  26. xiongjiTexts: []
  27. shishenOfTianGan: {} # key 十神 value:天干
  28. shishenOfDiZhi: {} # key 十神 value:地支
  29. def __init__(self, bazi: BaZi):
  30. # 这个时候只知道text,muku(不带状态)
  31. self.bazi = bazi
  32. self.tianganTexts = []
  33. self.dizhiTexts = []
  34. self.mukuState = {5: None, 6: None, 7: None, 8: None}
  35. self.cangganWuXing = {5: None, 6: None, 7: None, 8: None}
  36. self.cangganShiShen = {5: None, 6: None, 7: None, 8: None}
  37. self.cangGanPowerPercentage = {5: [0, 0, 0], 6: [0, 0, 0], 7: [0, 0, 0], 8: [0, 0, 0]}
  38. self.wuxingCounter = {}
  39. self.wuxingCounterIncludeCangGan = {}
  40. self.shishenCounter = {}
  41. self.shishenCounterIncludeCangGan = {}
  42. self.shishenPower = {}
  43. self.gaonengInWeiZhi = {}
  44. self.powerStep = {}
  45. self.genqi = {1: [0, 0], 2: [0, 0], 3: [0, 0], 4: [0, 0]}
  46. self.lushenAmount = 0
  47. self.banluAmount = 0
  48. self.shishenMap = {}
  49. self.shiShenTexts = []
  50. self.xiongjiTexts = []
  51. self.shishenOfTianGan = {}
  52. self.shishenOfDiZhi = {}
  53. self.wuxingWangShuai = {}
  54. self.shishenZengQiang = {}
  55. for dizhi in bazi.sizhu_dizhis():
  56. if dizhi.isLuShen:
  57. self.lushenAmount += 1
  58. if dizhi.isBanLu:
  59. self.banluAmount += 1
  60. for i in range(1, 9):
  61. self.gaonengInWeiZhi[i] = []
  62. self.shishenMap[i] = ""
  63. for wx in Common.wuxing_ary:
  64. self.powerStep[wx] = 0
  65. self.wuxingWangShuai[wx] = ""
  66. for ss in Common.shishen_ary:
  67. self.shishenPower[ss] = 0
  68. self.shishenOfTianGan[ss] = ""
  69. self.shishenOfDiZhi[ss] = []
  70. self.shishenZengQiang[ss] = 0
  71. for sz in bazi.sizhu_all():
  72. if sz.isTianGan:
  73. self.tianganTexts.append(sz.text)
  74. else:
  75. self.dizhiTexts.append(sz.text)
  76. def list_gaoneng(self):
  77. self.gaonengTexts = []
  78. tgs = self.bazi.sizhu_tiangans()
  79. for tg1 in tgs:
  80. for tg2 in tgs:
  81. if tg1.weiZhi == tg2.weiZhi:
  82. continue
  83. gns = Common.find_gaoneng_by_texts(tg1.text, tg2.text)
  84. for gn in gns:
  85. if not self.gaonengTexts.__contains__(gn["name"]):
  86. self.gaonengTexts.append(gn["name"])
  87. dzs = self.bazi.sizhu_dizhis()
  88. for dz1 in dzs:
  89. for dz2 in dzs:
  90. for dz3 in dzs:
  91. if dz1.weiZhi == dz2.weiZhi or dz1.weiZhi == dz3.weiZhi or dz2.weiZhi == dz3.weiZhi:
  92. continue
  93. gns = Common.find_gaoneng_by_texts(dz1.text, dz2.text, dz3.text)
  94. for gn in gns:
  95. if not self.gaonengTexts.__contains__(gn["name"]):
  96. self.gaonengTexts.append(gn["name"])
  97. if dz1.weiZhi == dz2.weiZhi:
  98. continue
  99. gns2 = Common.find_gaoneng_by_texts(dz1.text, dz2.text)
  100. for gn in gns2:
  101. if not self.gaonengTexts.__contains__(gn["name"]):
  102. self.gaonengTexts.append(gn["name"])
  103. def has_gaoneng(self, text: str):
  104. if text is None:
  105. return False
  106. return self.gaonengTexts.__contains__(text)
  107. def update_muku_state(self):
  108. for sz in self.bazi.sizhu_dizhis():
  109. if sz.muku is not None:
  110. self.mukuState[sz.weiZhi] = False
  111. info = Common.get_muku_info(sz.text)
  112. if info is not None:
  113. # 是否锁库
  114. if not self.has_gaoneng(info["lock"]):
  115. # 没锁库,有冲则开库
  116. if self.has_gaoneng(info["chong"]):
  117. self.mukuState[sz.weiZhi] = True
  118. else:
  119. # 锁库,自刑则开库、三会开库、拱合开库、暗合+冲开库
  120. if self.has_gaoneng(info["zixing"]):
  121. self.mukuState[sz.weiZhi] = True
  122. elif self.has_gaoneng(info["sanhui"]):
  123. self.mukuState[sz.weiZhi] = True
  124. elif self.has_gaoneng(info["gonghe"]):
  125. self.mukuState[sz.weiZhi] = True
  126. else:
  127. if self.has_gaoneng(info["anhe"]) and self.has_gaoneng(info["chong"]):
  128. self.mukuState[sz.weiZhi] = True
  129. def update_counter(self):
  130. # 此时四柱填充完成,知道wuxing shishen canggan
  131. for wx in Common.wuxing_ary:
  132. self.wuxingCounter[wx] = 0
  133. self.wuxingCounterIncludeCangGan[wx] = 0
  134. for ss in Common.shishen_ary:
  135. self.shishenCounter[ss] = 0
  136. self.shishenCounterIncludeCangGan[ss] = 0
  137. for sz in self.bazi.sizhu_all():
  138. self.wuxingCounter[sz.wuXing] += 1
  139. self.shishenCounter[sz.shiShen] += 1
  140. # 这里顺便计算下地支藏干的power分布
  141. if not sz.isTianGan:
  142. self.cangganWuXing[sz.weiZhi] = [sz.gan1WuXing, sz.gan2WuXing, sz.gan3WuXing]
  143. self.cangganShiShen[sz.weiZhi] = [sz.gan1ShiShen, sz.gan2ShiShen, sz.gan3ShiShen]
  144. per = [0, 0, 0]
  145. ct = 0
  146. if sz.cangGan1 is not None:
  147. ct += 1
  148. if sz.cangGan2 is not None:
  149. ct += 1
  150. if sz.cangGan3 is not None:
  151. ct += 1
  152. if ct == 1:
  153. per = [1, 0, 0]
  154. elif ct == 2:
  155. per = [0.7, 0.3, 0]
  156. elif ct == 3:
  157. if sz.muku is None:
  158. per = [0.6, 0.3, 0.1]
  159. else:
  160. # 注意,此时墓库的状态还未更新到sz.muku,会导致获得sz.muku.isOpen等属性获取失败
  161. # 因此这里要使用预存的mukuState进行判定
  162. state = self.mukuState[sz.weiZhi]
  163. if state is not None:
  164. if state is True:
  165. per = [0.1, 0.6, 0.3]
  166. else:
  167. per = [0.6, 0.3, 0.1]
  168. self.cangGanPowerPercentage[sz.weiZhi] = per
  169. else:
  170. self.wuxingCounterIncludeCangGan[sz.wuXing] += 1
  171. self.shishenCounterIncludeCangGan[sz.shiShen] += 1
  172. # 根据上面计算的藏干power分布,重新计算地支藏干五行
  173. for sz in self.bazi.sizhu_dizhis():
  174. per = self.cangGanPowerPercentage[sz.weiZhi]
  175. ganWuXing = self.cangganWuXing[sz.weiZhi]
  176. ganShiShen = self.cangganShiShen[sz.weiZhi]
  177. for i in range(0, 3):
  178. if ganWuXing[i] is not None:
  179. self.wuxingCounterIncludeCangGan[ganWuXing[i]] += round(per[i], 1)
  180. if ganShiShen[i] is not None:
  181. self.shishenCounterIncludeCangGan[ganShiShen[i]] += round(per[i], 1)
  182. def increase_shishen_power(self, shishen: str, power: int):
  183. self.shishenPower[shishen] += power
  184. def count_wuxing(self, wuxing: str, includeCangGan: bool):
  185. try:
  186. if includeCangGan is True:
  187. return self.wuxingCounterIncludeCangGan[wuxing]
  188. else:
  189. return self.wuxingCounter[wuxing]
  190. except KeyError:
  191. return 0
  192. def count_shishen(self, shishen: str, includeCangGan: bool):
  193. try:
  194. if includeCangGan is True:
  195. return self.shishenCounterIncludeCangGan[shishen]
  196. else:
  197. return self.shishenCounter[shishen]
  198. except KeyError:
  199. return 0
  200. def get_shishen_power(self, shishen: str):
  201. try:
  202. return self.shishenPower[shishen]
  203. except KeyError:
  204. return 0
  205. def add_gaoneng_in_weizhi(self, weizhi: int, text: str):
  206. self.gaonengInWeiZhi[weizhi].append(text)
  207. def has_gaoneng_in_weizhi(self, weizhi: int, text: str):
  208. if text is None:
  209. return False
  210. try:
  211. lst = self.gaonengInWeiZhi[weizhi]
  212. return lst.__contains__(text)
  213. except KeyError:
  214. return False
  215. def get_canggan_power_percentage(self, weizhi: int):
  216. try:
  217. return self.cangGanPowerPercentage[weizhi]
  218. except KeyError:
  219. return [0, 0, 0]
  220. def get_cangan_power_percentage_by_zhu(self, sz: SiZhu):
  221. result = [0, 0, 0]
  222. if sz.isTianGan:
  223. return result
  224. ct = 0
  225. if sz.cangGan1 is not None:
  226. ct += 1
  227. if sz.cangGan2 is not None:
  228. ct += 1
  229. if sz.cangGan3 is not None:
  230. ct += 1
  231. if ct == 1:
  232. result = [1, 0, 0]
  233. elif ct == 2:
  234. result = [0.7, 0.3, 0]
  235. elif ct == 3:
  236. if sz.muku is None:
  237. result = [0.6, 0.3, 0.1]
  238. else:
  239. if sz.muku.isOpen is True:
  240. result = [0.1, 0.6, 0.3]
  241. else:
  242. result = [0.6, 0.3, 0.1]
  243. return result
  244. def get_power_step(self, wuxing: str):
  245. try:
  246. return self.powerStep[wuxing]
  247. except KeyError:
  248. return 0
  249. def has_all_shishen(self, shishens: [str]):
  250. result = True
  251. for ss in shishens:
  252. if not self.shiShenTexts.__contains__(ss):
  253. result = False
  254. break
  255. return result
  256. def has_shishen(self, shishen: str):
  257. return self.shiShenTexts.__contains__(shishen)
  258. # 存在一个就返回True
  259. def has_shishen_in_dizhi(self, shishens: [str]):
  260. result = False
  261. for i in range(5, 9):
  262. if self.shishenMap[i] in shishens:
  263. result = True
  264. break
  265. return result
  266. def has_xiongji(self, key: str):
  267. return self.xiongjiTexts.__contains__(key)
  268. def get_tiangan_by_shishen(self, shishen: str):
  269. return self.shishenOfTianGan[shishen]
  270. def get_dizhi_by_shishen(self, shishen: str):
  271. return self.shishenOfDiZhi[shishen]
  272. def get_wangshuai_by_wuxing(self, wuxing: str):
  273. return self.wuxingWangShuai[wuxing]