# -*- coding: utf-8 -*-
"""游戏形式注册表（V2）：11 种游戏形式的元数据【单一数据源】。

供后端 API（app/api/views.py 的 game-start/game-submit）与后台 admin（exams.py 选题页）
共用，避免两处重复定义 + 循环依赖。

不建数据库表——11 种形式是固定枚举，运营无需后台增删；未来要调参再迁 Setting 表。
前端 h5/game.html 另有一份对应的 RENDERERS 注册表（内容需与本表一致）。
"""

# game_code → {name(显示名), qtypes(该形式允许的题型), config(默认玩法参数), sort(选题页顺序)}
GAME_TEMPLATES = {
    "match":    {"name": "翻牌配对",   "qtypes": ["配对"],           "config": {"time": 60},              "sort": 1},
    "link":     {"name": "连连看",     "qtypes": ["配对"],           "config": {"time": 90},              "sort": 2},
    "judge":    {"name": "判断对错",   "qtypes": ["判断"],           "config": {"time": 4, "lives": 3},   "sort": 3},
    "balloon":  {"name": "气球答题",   "qtypes": ["单选", "判断"],    "config": {"time": 60, "lives": 3},  "sort": 4},
    "rapid":    {"name": "快问快答",   "qtypes": ["单选"],           "config": {"time": 5, "lives": 3},   "sort": 5},
    "classify": {"name": "分类归类",   "qtypes": ["分类"],           "config": {"time": 90},              "sort": 6},
    "puzzle":   {"name": "拼图还原",   "qtypes": ["配对"],           "config": {"time": 120, "grid": 3},  "sort": 7},
    "shoot":    {"name": "射击答题",   "qtypes": ["单选", "判断"],    "config": {"time": 60, "lives": 3},  "sort": 8},
    "runner":   {"name": "跑酷问答",   "qtypes": ["单选"],           "config": {"lives": 3},              "sort": 9},
    "mole":     {"name": "打地鼠",     "qtypes": ["单选"],           "config": {"time": 60, "lives": 3},  "sort": 10},
    "match3":   {"name": "消消乐",     "qtypes": ["配对"],           "config": {"target": 10},            "sort": 11},
    "merge":    {"name": "合成大西瓜", "qtypes": ["配对"],           "config": {"target": 5},             "sort": 12},
}

# 每个 game_code 对应的 payload 形状（game-start 按此序列化，前端按 game_code 选渲染器）：
#   pairs     → {pairs:[{id,a,b,topic,image}]}      配对类：翻牌/连连看/拼图/消消乐/合成
#   items     → {items:[{id,stem,answer,image}]}     判断类
#   questions → {questions:[{id,stem,opts,answer,image,points}]}  单选/分类/图片类
PAYLOAD_SHAPE = {
    "match": "pairs", "link": "pairs", "puzzle": "pairs", "match3": "pairs", "merge": "pairs",
    "judge": "items",
    "balloon": "questions", "rapid": "questions", "shoot": "questions",
    "mole": "questions", "runner": "questions", "classify": "questions",
}

# 老版本（V1）4 关 round 号 → game_code 兜底映射（game_code 为空时按 round 推断，向后兼容）
LEGACY_ROUND_CODE = {1: "match", 2: "judge", 3: "runner", 4: "mole"}


def game_name(code):
    return GAME_TEMPLATES.get(code, {}).get("name", code or "")


def game_config(code):
    return GAME_TEMPLATES.get(code, {}).get("config", {})


def game_qtypes(code):
    return GAME_TEMPLATES.get(code, {}).get("qtypes", [])
