feat: 계절 각 — 각마다 제철 달(months), 제철 각에 지금 계절 표시·철 지난 각은 순위 밖 맨 끝, 새 각 12개(꽃구경·이불빨래·빙수·실내건조·한강밤바람·단풍·억새·군고구마·해돋이·눈사람·붕어빵·귤까먹기), 해 뜰 무렵 시간대·일출 점수·눈 조건
This commit is contained in:
1 parent
43e1925d70
commit
5c05824ce0
14 files changed
+1130
-66
No files matched your search
@@ -42,6 +42,26 @@ WEATHER_SCORE_DEF = SimpleNamespace(
|
||||
)
|
||||
|
||||
|
||||
SEASONS = ("겨울", "봄", "여름", "가을")
|
||||
|
||||
|
||||
def season_of(month: int) -> str:
|
||||
"""봄 3–5 · 여름 6–8 · 가을 9–11 · 겨울 12–2."""
|
||||
return SEASONS[month % 12 // 3]
|
||||
|
||||
|
||||
def in_season(defn, month: int) -> bool:
|
||||
"""제철 달이 없으면 일 년 내내."""
|
||||
months = getattr(defn, "months", None)
|
||||
return not months or month in months
|
||||
|
||||
|
||||
def season_badge(defn, month: int) -> Optional[str]:
|
||||
"""제철이 정해진 각이 지금 제철이면 그 계절 이름. 일 년 내내인 각은 없음."""
|
||||
months = getattr(defn, "months", None)
|
||||
return season_of(month) if months and month in months else None
|
||||
|
||||
|
||||
@dataclass
|
||||
class IndexItem:
|
||||
definition: IndexDefinition
|
||||
@@ -52,6 +72,8 @@ class IndexItem:
|
||||
slots: dict[str, int] = field(default_factory=dict)
|
||||
slot_ranks: dict[str, int] = field(default_factory=dict) # 구간별 순위. 그 구간과 안 겹치는 각은 없음
|
||||
active: bool = True # 지금 구간이 이 각의 시간대와 겹치는지. 아니면 순위·피드에서 빼고 목록 끝에 하루 점수로 둔다
|
||||
season: Optional[str] = None # 지금 제철이면 계절 이름 (화면 표시)
|
||||
in_season: bool = True # 제철이 아니면 순위·피드에서 빼고 목록 맨 끝에 둔다
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -92,11 +114,12 @@ def slot_label(day: date, today: date) -> str:
|
||||
|
||||
|
||||
def window_hours(defn, day: date, region: Region) -> list[list[int]]:
|
||||
"""시간대 정의를 그날 기준 시각 목록으로. 24 이상은 다음 날 시각."""
|
||||
"""시간대 정의를 그날 기준 시각 목록으로. 24 이상은 다음 날 시각. around 는 일몰·일출 시각 ±hours."""
|
||||
out = []
|
||||
for w in defn.windows:
|
||||
if w.get("around") == "sunset":
|
||||
h = sun_times(day, float(region.lat), float(region.lon)).sunset_at.astimezone(KST).hour
|
||||
if w.get("around") in ("sunset", "sunrise"):
|
||||
sun = sun_times(day, float(region.lat), float(region.lon))
|
||||
h = (sun.sunset_at if w["around"] == "sunset" else sun.sunrise_at).astimezone(KST).hour
|
||||
out.append(list(range(h - w.get("hours", 1), h + w.get("hours", 1) + 1)))
|
||||
else:
|
||||
out.append(list(range(w["from"], w["to"])))
|
||||
@@ -272,6 +295,9 @@ class IndexService:
|
||||
if ErrorType.DB_ERROR in (err, err2):
|
||||
return ErrorType.DB_ERROR, None
|
||||
by_key = {(x.index_id, x.score_date): x for x in scores}
|
||||
month = today.month
|
||||
out_season = [d for d in defs if not in_season(d, month)]
|
||||
defs = [d for d in defs if in_season(d, month)]
|
||||
# 오늘 점수는 지금 구간 점수. 구간 점수가 없는 행(예전 계산분)은 하루 점수 그대로, 지금 구간과 안 겹치는 각은 뺀다
|
||||
now_score: dict[int, IndexScore] = {}
|
||||
for d in defs:
|
||||
@@ -290,7 +316,7 @@ class IndexService:
|
||||
items.append(IndexItem(
|
||||
definition=d, score=now_score[d.index_id], rank=i + 1,
|
||||
trend=[now_score[d.index_id] if day == today else by_key[(d.index_id, day)] for day in slots if (d.index_id, day) in by_key],
|
||||
slot=shown_slot(cur), slots=parts,
|
||||
slot=shown_slot(cur), slots=parts, season=season_badge(d, month),
|
||||
))
|
||||
off = sorted(
|
||||
(d for d in defs if d.index_id not in now_score and (d.index_id, today) in by_key),
|
||||
@@ -301,12 +327,19 @@ class IndexService:
|
||||
items.append(IndexItem(
|
||||
definition=d, score=row, rank=len(items) + 1,
|
||||
trend=[by_key[(d.index_id, day)] for day in slots if (d.index_id, day) in by_key],
|
||||
slots=_shown_parts({k: v["score"] for k, v in row.slots.items()}, cur), active=False,
|
||||
slots=_shown_parts({k: v["score"] for k, v in row.slots.items()}, cur), active=False, season=season_badge(d, month),
|
||||
))
|
||||
for k in SLOTS:
|
||||
in_slot = sorted((i for i in items if k in i.slots), key=lambda i: rank_score(i.slots[k], i.definition.priority), reverse=True)
|
||||
for n, i in enumerate(in_slot):
|
||||
i.slot_ranks[k] = n + 1
|
||||
# 제철이 아닌 각은 구간 순위 없이 맨 끝에
|
||||
for d in sorted((d for d in out_season if (d.index_id, today) in by_key), key=lambda d: d.sort_order):
|
||||
row = by_key[(d.index_id, today)]
|
||||
items.append(IndexItem(
|
||||
definition=d, score=row, rank=len(items) + 1,
|
||||
trend=[by_key[(d.index_id, day)] for day in slots if (d.index_id, day) in by_key], active=False, in_season=False,
|
||||
))
|
||||
return ErrorType.SUCCESS, IndexBoard(
|
||||
region=region, day=today, weather_score=weather_parts.get(cur), items=items, weather_slots=_shown_parts(weather_parts, cur),
|
||||
)
|
||||
@@ -359,6 +392,7 @@ class IndexService:
|
||||
_, holidays = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.holidays(s, today, days[-1]))
|
||||
_, sky_scores = await DB_SESSION_MNG.execute_lambda(lambda s: self.sky.scores(s, [region.code], today))
|
||||
sunset = {x.score_date: x.score for x in sky_scores or [] if x.sky_type == SkyType.SUNSET.value}
|
||||
sunrise = {x.score_date: x.score for x in sky_scores or [] if x.sky_type == SkyType.SUNRISE.value}
|
||||
if err is not ErrorType.SUCCESS:
|
||||
return err, None
|
||||
out = []
|
||||
@@ -366,7 +400,7 @@ class IndexService:
|
||||
m = night(day, float(region.lat), float(region.lon))
|
||||
ctx = Context(
|
||||
day=day, is_holiday=day in (holidays or set()), moon=m.lit * up_ratio(m, _STARS_HOURS), moon_note=f"{m.name}, {view_line(m, None)}",
|
||||
sunset_score=sunset.get(day), lookahead_rain_prob=0, **base_ctx,
|
||||
sunset_score=sunset.get(day), sunrise_score=sunrise.get(day), lookahead_rain_prob=0, **base_ctx,
|
||||
)
|
||||
for d in defs:
|
||||
result = day_result(d, day, region, by_at, daily, ctx, short=day < today + timedelta(days=DAYS), by_om=by_om)
|
||||
|
||||
Reference in new issue
Block a user