{
  "openapi": "3.1.0",
  "info": {
    "title": "Brake Master Open Competition API",
    "version": "0.1.0",
    "description": "Humans and programs compete under identical deterministic rules. Public submissions are replayable. No browser automation required. Bulk search should run locally; evaluate does not write storage. One new successful save per player per second. The game model is not real vehicle calibration."
  },
  "servers": [
    {
      "url": "https://brake-master-dq.pages.dev"
    }
  ],
  "paths": {
    "/api/challenges": {
      "get": {
        "operationId": "getChallenges",
        "summary": "Current rules, replay limits, and available challenges",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/evaluate": {
      "post": {
        "operationId": "evaluateReplay",
        "summary": "Simulate without reading or writing D1",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "result": {
                      "$ref": "#/components/schemas/Result"
                    },
                    "saved": {
                      "const": false
                    },
                    "replay": {
                      "type": "string"
                    },
                    "rulesVersion": {
                      "type": "string"
                    },
                    "challengeId": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Entry"
              }
            }
          }
        }
      }
    },
    "/api/players": {
      "post": {
        "operationId": "createPlayer",
        "summary": "Create an anonymous player; token returned once",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "nickname": {
                      "type": "string"
                    },
                    "token": {
                      "type": "string"
                    },
                    "note": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "nickname"
                ],
                "properties": {
                  "nickname": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 20
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/runs": {
      "post": {
        "operationId": "submitReplay",
        "summary": "Server recomputes and saves successful runs; duplicate replay is idempotent",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "saved": {
                      "type": "boolean"
                    },
                    "duplicate": {
                      "type": "boolean"
                    },
                    "result": {
                      "$ref": "#/components/schemas/Result"
                    },
                    "replay": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "saved": {
                      "type": "boolean"
                    },
                    "duplicate": {
                      "type": "boolean"
                    },
                    "result": {
                      "$ref": "#/components/schemas/Result"
                    },
                    "replay": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Entry"
              }
            }
          }
        },
        "security": [
          {
            "playerToken": []
          }
        ]
      }
    },
    "/api/leaderboard": {
      "get": {
        "operationId": "getLeaderboard",
        "summary": "Best run per player; top 50; equal ticks share rank",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "query",
            "name": "challengeId",
            "schema": {
              "type": "string",
              "enum": [
                "dry-v1",
                "wet-v1"
              ],
              "default": "dry-v1"
            }
          }
        ]
      }
    },
    "/api/runs/{id}": {
      "get": {
        "operationId": "getReplay",
        "summary": "Public replay and authoritative result",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-f0-9]{32}$"
            }
          }
        ]
      }
    },
    "/api/me/runs": {
      "get": {
        "operationId": "getMyRuns",
        "summary": "Latest 20 stored runs for this player",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "security": [
          {
            "playerToken": []
          }
        ]
      }
    },
    "/api/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "D1 connectivity and rules version",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "default": {
            "description": "Invalid input, version, identity, rate limit, or unavailable storage",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "playerToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "Token returned by POST /api/players; keep it secret."
      }
    },
    "schemas": {
      "Entry": {
        "type": "object",
        "required": [
          "challengeId",
          "rulesVersion",
          "replay"
        ],
        "properties": {
          "challengeId": {
            "type": "string",
            "enum": [
              "dry-v1",
              "wet-v1"
            ]
          },
          "rulesVersion": {
            "type": "string",
            "const": "brake-1.0.0"
          },
          "replay": {
            "type": "string",
            "maxLength": 6144,
            "description": "BM1| followed by strictly increasing absolute base36 ticks with + / -, separated by semicolons. 10 ms/tick; starts released; at most 512 events, 3000 ticks. No events after termination."
          }
        }
      },
      "Result": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "stopped",
              "broken",
              "timeout"
            ]
          },
          "timeMs": {
            "type": "integer"
          },
          "ticks": {
            "type": "integer"
          },
          "distanceMm": {
            "type": "integer"
          },
          "peakForceN": {
            "type": "integer"
          },
          "damage": {
            "type": "integer"
          },
          "eventCount": {
            "type": "integer"
          }
        }
      }
    }
  }
}
