|
|
@ -1,10 +1,10 @@
|
|
|
|
from math import floor
|
|
|
|
from random import randint, random
|
|
|
|
from random import uniform
|
|
|
|
|
|
|
|
from fastapi import FastAPI, UploadFile
|
|
|
|
from fastapi import FastAPI, UploadFile
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy
|
|
|
|
from sqlalchemy import text
|
|
|
|
from sqlalchemy import text
|
|
|
|
import pandas as pd
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
@ -19,7 +19,7 @@ app.add_middleware(
|
|
|
|
allow_headers=["*"]
|
|
|
|
allow_headers=["*"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/ROLLER_DB")
|
|
|
|
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://root:1212@127.0.0.1:3306/ROLLER_DB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
@app.get("/")
|
|
|
@ -30,14 +30,34 @@ async def root():
|
|
|
|
@app.get("/api/get/get_random_stu")
|
|
|
|
@app.get("/api/get/get_random_stu")
|
|
|
|
async def get_random_stu():
|
|
|
|
async def get_random_stu():
|
|
|
|
with engine.connect() as c:
|
|
|
|
with engine.connect() as c:
|
|
|
|
stu = c.execute(text("SELECT sno, sname FROM stu_info")).fetchall()
|
|
|
|
stu = c.execute(text("SELECT sno, sname, score FROM stu_info;")).fetchall()
|
|
|
|
|
|
|
|
avg = (c.execute(text(f"SELECT AVG(score) FROM stu_info;")).fetchall())[0][0]
|
|
|
|
size = stu.__len__()
|
|
|
|
size = stu.__len__()
|
|
|
|
return {"sno": stu[floor(uniform(0, size - 1))][0], "sname": stu[floor(uniform(0, size - 1))][1]}
|
|
|
|
while 1:
|
|
|
|
|
|
|
|
no = randint(0, size - 1)
|
|
|
|
|
|
|
|
diff = stu[no][2] - avg
|
|
|
|
|
|
|
|
if diff > 7:
|
|
|
|
|
|
|
|
chance = 0.2
|
|
|
|
|
|
|
|
elif diff > 3:
|
|
|
|
|
|
|
|
chance = 0.7 - (diff-3) / 4 * 0.5
|
|
|
|
|
|
|
|
elif diff > 1:
|
|
|
|
|
|
|
|
chance = 0.9 - (diff-1) / 2 * 0.2
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
chance = 1
|
|
|
|
|
|
|
|
if random() < chance:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
return {"sno": stu[no][0], "sname": stu[no][1]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/get/get_random_event")
|
|
|
|
@app.get("/api/get/get_random_event")
|
|
|
|
async def get_event():
|
|
|
|
async def get_event():
|
|
|
|
return {"eventId": 10, "eventDescription": "疯狂星期四!/v我50:P"}
|
|
|
|
with engine.connect() as c:
|
|
|
|
|
|
|
|
res = c.execute(text("SELECT event_id, event_description FROM events")).fetchall()
|
|
|
|
|
|
|
|
if randint(0, 100) < 15:
|
|
|
|
|
|
|
|
event_id = randint(1, 2)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
event_id = 0
|
|
|
|
|
|
|
|
return {"event_id": res[event_id][0], "event_description": res[event_id][1]}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/post/upload_stu_info")
|
|
|
|
@app.post("/api/post/upload_stu_info")
|
|
|
|
async def upload_stu_info(file: UploadFile):
|
|
|
|
async def upload_stu_info(file: UploadFile):
|
|
|
@ -47,5 +67,15 @@ async def upload_stu_info(file: UploadFile):
|
|
|
|
c.execute(text("TRUNCATE TABLE stu_info"))
|
|
|
|
c.execute(text("TRUNCATE TABLE stu_info"))
|
|
|
|
for i in data.index.values:
|
|
|
|
for i in data.index.values:
|
|
|
|
with engine.begin() as c:
|
|
|
|
with engine.begin() as c:
|
|
|
|
c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0);"))
|
|
|
|
c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0, 0);"))
|
|
|
|
return file.filename
|
|
|
|
return file.filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangedScore(BaseModel):
|
|
|
|
|
|
|
|
sno: str
|
|
|
|
|
|
|
|
variation: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/post/update_score")
|
|
|
|
|
|
|
|
async def update_score(changed_score: ChangedScore):
|
|
|
|
|
|
|
|
with engine.begin() as c:
|
|
|
|
|
|
|
|
c.execute(text(f"UPDATE stu_info SET score = score + {changed_score.variation} WHERE sno = '{changed_score.sno}'"))
|
|
|
|
|
|
|
|
return changed_score
|