Initial Commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import random
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI(title="MileageDraw")
|
||||
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
# Prize Probabilities:
|
||||
# 1st Prize: 20%
|
||||
# 2nd Prize: 30%
|
||||
# 3rd Prize: 50%
|
||||
PRIZES = [
|
||||
{"rank": 1, "name": "1st Prize", "miles": 10000, "probability": 0.10},
|
||||
{"rank": 2, "name": "2nd Prize", "miles": 5000, "probability": 0.30},
|
||||
{"rank": 3, "name": "3rd Prize", "miles": 2000, "probability": 0.60},
|
||||
]
|
||||
|
||||
class DrawResult(BaseModel):
|
||||
rank: int
|
||||
name: str
|
||||
miles: int
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request=request, name="index.html"
|
||||
)
|
||||
|
||||
@app.post("/api/draw", response_model=DrawResult)
|
||||
async def draw_prize():
|
||||
rand_val = random.random()
|
||||
cumulative = 0.0
|
||||
for prize in PRIZES:
|
||||
cumulative += prize["probability"]
|
||||
if rand_val <= cumulative:
|
||||
return DrawResult(
|
||||
rank=prize["rank"],
|
||||
name=prize["name"],
|
||||
miles=prize["miles"]
|
||||
)
|
||||
|
||||
# Fallback in case of floating point precision issues
|
||||
last = PRIZES[-1]
|
||||
return DrawResult(
|
||||
rank=last["rank"],
|
||||
name=last["name"],
|
||||
miles=last["miles"]
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=10000, reload=True)
|
||||
|
||||
# import random
|
||||
# def sum(L, n):
|
||||
# if n == 0:
|
||||
# return L[0]
|
||||
# else:
|
||||
# return L[n] + sum(L, n-1)
|
||||
|
||||
# L = [random.randint(1, 100) for _ in range(10)]
|
||||
# print(L)
|
||||
|
||||
# s = sum(L, len(L)-1)
|
||||
# print(s)
|
||||
|
||||
Reference in New Issue
Block a user