TP TipPlatforma Spat na overview

Aktivne zdrojove diagramy

Scoring Engine Flow V2

Aktivny zdrojovy diagram projektu v HTML obale, zladeny s overview dizajnom.

Mermaid sourceOverview-ready HTML09_scoring_engine_flow_v2.mermaid

Mermaid zdroj

Diagram je ponechany ako zdrojovy text, ale otvoren v plnohodnotnom HTML obale, ktory sedí s overview dizajnom.

flowchart TD
    START(["Admin sets is_final = true\non match_result"])

    START --> INC{"Was is_final\nalready true?"}
    INC -->|"Yes — correction"| VER["Increment result_version\n(audit trail: old version kept)"]
    INC -->|"No — first time"| FETCH

    VER --> FETCH

    FETCH["Fetch all predictions\nfor this match"]

    FETCH --> LOOP["For each prediction\n(user × group)"]

    LOOP --> TEMPLATE["Load scoring_template\nassigned to the match"]

    TEMPLATE --> COMP["For each template component"]

    COMP --> TYPE{Component type?}

    TYPE -->|"winner"| W["Compare\npredicted_winner\nvs match_result.winner"]
    TYPE -->|"exact_score"| E["Compare\npredicted home:away\nvs result home:away"]
    TYPE -->|"advancing_team"| A["Compare\npredicted team\nvs extra_data.advancing_team"]

    W --> WR{Correct?}
    E --> ER{Correct?}
    A --> AR{Correct?}

    WR -->|"Yes"| ADD_W["+ component.points"]
    WR -->|"No"| ZERO_W["+ 0"]
    ER -->|"Yes"| ADD_E["+ component.points"]
    ER -->|"No"| ZERO_E["+ 0"]
    AR -->|"Yes"| ADD_A["+ component.points"]
    AR -->|"No"| ZERO_A["+ 0"]

    ADD_W --> SUM
    ZERO_W --> SUM
    ADD_E --> SUM
    ZERO_E --> SUM
    ADD_A --> SUM
    ZERO_A --> SUM

    SUM["Sum points across all components\nBuild breakdown JSON\n{winner: N, exact_score: N, ...}"]

    SUM --> IDEM{"Row exists for\n(prediction_id,\nresult_version)?"}
    IDEM -->|"Yes — skip\n(idempotent run)"| MORE
    IDEM -->|"No"| SAVE["INSERT prediction_scores\n{prediction_id, result_version,\npoints_awarded, breakdown, calculated_at}"]

    SAVE --> MORE{More\npredictions?}
    MORE -->|"Yes"| LOOP
    MORE -->|"No"| LDR["Recalculate group leaderboards\n(SUM points WHERE result_version =\nMAX per prediction — no double-count)"]

    LDR --> RT["Propagate via\nSupabase Realtime\n(WebSocket → all clients)"]

    RT --> NOTIF["Trigger notification service\n(email to all group players)\n(push: Phase 2)"]

    NOTIF --> END(["Scoring complete"])

    style START fill:#dbeafe,stroke:#3b82f6
    style END fill:#dcfce7,stroke:#22c55e
    style INC fill:#fef9c3,stroke:#eab308
    style TYPE fill:#fce7f3,stroke:#ec4899
    style WR fill:#f1f5f9,stroke:#94a3b8
    style ER fill:#f1f5f9,stroke:#94a3b8
    style AR fill:#f1f5f9,stroke:#94a3b8
    style LDR fill:#e0e7ff,stroke:#6366f1
    style RT fill:#fee2e2,stroke:#ef4444