"""오늘 일진과 내 사주가 만나는 사실들, 연애 기운, 사주 점수.""" from dataclasses import dataclass from datetime import date from typing import Literal, Optional from services.saju.ganji import main_stem from services.saju.pillars import Chart, Pillar, day_pillar from services.saju.rules import ( OFFICER, PALACE, WEALTH, Relation, branch_relations, branch_ten_god, dohwa_of, hongyeom_of, stem_relations, ten_god, triple_relations, ) # 사주 점수 가중치 (초안, 주인이 조정). 50 에서 시작해 사실마다 더하고 0~100 으로 자른다. 용신은 보지 않는다 SCORE_WEIGHTS = { "base": 50, # 오늘 천간의 십성 "ten_god": {"비견": 2, "겁재": -2, "식신": 4, "상관": -2, "편재": 3, "정재": 4, "편관": -4, "정관": 3, "편인": -1, "정인": 4}, "branch_ten_god_share": 0.5, # 오늘 지지(정기) 십성은 천간 점수의 이 비율 "relation": { "천간합": 4, "천간충": -5, "육합": 5, "삼합": 6, "반합": 3, "충": -6, "형": -4, "삼형": -6, "자형": -2, "파": -2, "해": -2, "원진": -3, }, "position_share": {"year": 0.8, "month": 1.0, "day": 1.5, "hour": 0.8}, # 관계 점수에 곱함. 삼합·삼형은 1 "도화": 3, "홍염": 2, } Gender = Literal["male", "female"] LOVE_DAY_RELATIONS = ("육합", "반합", "충", "형", "자형", "원진") @dataclass(frozen=True) class Interaction: position: str # year·month·day·hour (삼합·삼형은 묶인 자리 중 첫 자리) palace: str part: str # stem·branch relation: Relation positions: tuple[str, ...] = () # 삼합·삼형에 묶인 원국 자리 전부 @dataclass(frozen=True) class LoveFacts: relevant: bool gender: Optional[Gender] reasons: tuple[dict, ...] # {"kind": 재성|관성|도화|홍염|일지, ...} @dataclass(frozen=True) class Contribution: fact: str # 십성 이름, 관계 이름, 도화, 홍염 chars: str position: Optional[str] points: float @dataclass(frozen=True) class DailyFacts: day: date pillar: Pillar stem_ten_god: str branch_main_stem: str branch_ten_god: str interactions: tuple[Interaction, ...] dohwa: tuple[dict, ...] # 오늘 지지가 도화일 때 {"basis": "year"|"day", "branch": 도화 지지} hongyeom: bool love: LoveFacts score: int contributions: tuple[Contribution, ...] def interactions(chart: Chart, today: Pillar) -> list[Interaction]: """오늘 천간·지지와 원국 각 기둥의 합·충·형·파·해·원진. 오늘 글자가 있어야 완성되는 삼합·삼형도 넣는다.""" pillars = chart.pillars() out = [] for pos, p in pillars.items(): out += [Interaction(pos, PALACE[pos], "stem", r) for r in stem_relations(today.stem, p.stem)] out += [Interaction(pos, PALACE[pos], "branch", r) for r in branch_relations(today.branch, p.branch)] natal = [p.branch for p in pillars.values()] already = {r.group for r in triple_relations(natal)} for r in triple_relations(natal + [today.branch]): if today.branch in r.group and r.group not in already: joined = tuple(pos for pos, p in pillars.items() if p.branch in r.group) out.append(Interaction(joined[0], PALACE[joined[0]], "branch", r, joined)) return out def dohwa_hits(chart: Chart, branch: str) -> list[dict]: return [{"basis": basis, "branch": branch} for basis, p in (("year", chart.year), ("day", chart.day)) if dohwa_of(p.branch) == branch] def love_facts(chart: Chart, today: Pillar, gender: Optional[Gender] = None) -> LoveFacts: """남자는 재성, 여자는 관성 날. 성별을 모르면 도화·홍염·일지 관계만.""" if gender not in (None, "male", "female"): raise ValueError(f"성별은 male·female·None: {gender}") reasons: list[dict] = [] stars, label = {"male": (WEALTH, "재성"), "female": (OFFICER, "관성")}.get(gender or "", ((), "")) for part, god in (("stem", ten_god(chart.day_stem, today.stem)), ("branch", branch_ten_god(chart.day_stem, today.branch))): if god in stars: reasons.append({"kind": label, "part": part, "ten_god": god}) reasons += [{"kind": "도화", **hit} for hit in dohwa_hits(chart, today.branch)] if hongyeom_of(chart.day_stem) == today.branch: reasons.append({"kind": "홍염", "branch": today.branch}) for r in branch_relations(today.branch, chart.day.branch): if r.kind in LOVE_DAY_RELATIONS: reasons.append({"kind": "일지", "relation": r.kind, "chars": r.chars}) return LoveFacts(bool(reasons), gender, tuple(reasons)) def score(chart: Chart, today: Pillar, found: list[Interaction], dohwa: list[dict], hongyeom: bool) -> tuple[int, list[Contribution]]: """사주 점수와 사실별 점수. 삼합·삼형이 완성되면 같은 묶음의 반합·형은 겹쳐 세지 않는다.""" w = SCORE_WEIGHTS stem_god, branch_god = ten_god(chart.day_stem, today.stem), branch_ten_god(chart.day_stem, today.branch) parts = [ Contribution(stem_god, today.stem, None, w["ten_god"][stem_god]), Contribution(branch_god, today.branch, None, w["ten_god"][branch_god] * w["branch_ten_god_share"]), ] triples = {(i.relation.kind, i.relation.group) for i in found if i.positions} absorbed = {("반합", g) for k, g in triples if k == "삼합"} | {("형", g) for k, g in triples if k == "삼형"} for i in found: r = i.relation if (r.kind, r.group) in absorbed: continue share = 1.0 if i.positions else w["position_share"][i.position] parts.append(Contribution(r.kind, r.chars, i.position, w["relation"][r.kind] * share)) parts += [Contribution("도화", hit["branch"], hit["basis"], w["도화"]) for hit in dohwa] if hongyeom: parts.append(Contribution("홍염", today.branch, None, w["홍염"])) parts = [Contribution(c.fact, c.chars, c.position, round(c.points, 2)) for c in parts] total = w["base"] + sum(c.points for c in parts) return round(max(0, min(100, total))), parts def daily_facts(chart: Chart, day: date, gender: Optional[Gender] = None) -> DailyFacts: today = day_pillar(day) found = interactions(chart, today) dohwa = dohwa_hits(chart, today.branch) hongyeom = hongyeom_of(chart.day_stem) == today.branch total, parts = score(chart, today, found, dohwa, hongyeom) return DailyFacts( day=day, pillar=today, stem_ten_god=ten_god(chart.day_stem, today.stem), branch_main_stem=main_stem(today.branch), branch_ten_god=branch_ten_god(chart.day_stem, today.branch), interactions=tuple(found), dohwa=tuple(dohwa), hongyeom=hongyeom, love=love_facts(chart, today, gender), score=total, contributions=tuple(parts), )