---
# System prepended metadata

title: API設計
tags: [遊戲後端]

---

# API設計
###### tags: `遊戲後端`

<!-- 頁首通用模組 -->
{%hackmd xcsnQTMQQ0SZQ9QF7YUp_g %}

---

:::info
:::spoiler 大綱
[TOC]
:::

---

## 構想
- 2020/10/15
    :::spoiler 構想
    ```
    // ======================================================
    // DB Struct
    // ======================================================

    Collection players: {
        Unsigned playerID,   // uniqe, primary key val
        String playerName,
        String bio,
        String leaderStone = stoneID
        Stone[] stones: {
            // 擁有的石頭
            {
                Unsigned stoneID,    // uniqe for each stone
                Unsigned stoneType,      // map<stoneName, stoneType>
                String stoneName,      // e.g. "長石"
                Action activityDoing,
                Time activityStartTime,

                Integer level,
                Integer STR,
                Integer AGI,
                ...
            },
            {
                Unsigned stoneID,
                ...
            },
            ...
        }
    }

    Collection basicStones: {
        // 各種石頭的基本數值
        {
            Unsigned stoneType,      // primary key val
            String stoneName,
            EvolveRule[] evolveRules {
                {
                    Unsined target = stoneType
                    Rule rule: {
                        // this rule refer to "(str > 78 or atk > 7) and agi < 63 and stoneType == 78763"
                        $and: {
                            $or: {
                                $gt: {
                                    STR: 78
                                },
                                $gt: {
                                    level: 7
                                }
                            },
                            $les: {
                                AGI: 63
                            },
                            $eq: {
                                stoneType: 78763
                            }
                        }
                    }
                }
            }
            Integer level
            Integer STR,    // init value of this type of stone
            Integer AGI,
            Integer LUK,
            Integer MAG,
            ...
        },
        ...
    }

    // ======================================================
    // API router
    // ======================================================
    ::
    req: POST /api/login
    res: Session cookie
    ::

    ::
    req: GET /api/myData
    res: player[session.playerID]
    ::

    ::
    req: GET /api/myData/qury="stone1.STR"
    res: player[session.playerID][stone1][STR]
    ::

    ::
    req: POST /api/action/$stone="stone1" & $action="reading"
    res: player[session.playerID][$stone].do($action)
    ::

    ::
    req: GET /api/playerData/$playerID="78763"
    res: player[$playerID]
    ::

    ::
    req: POST /api/attack/$opponent="78763" & $type="kill"
    res: challangePlayer(session.playerID, $opponent, $type).result
    ::

    ::
    req: GET /api/challange/$stage="78"
    res: challangeStage($stage).result
    ::
    ```
    :::
