diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410ca..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..383bd5d --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mariadb + true + org.mariadb.jdbc.Driver + jdbc:mariadb://localhost:3306/ROLLER_DB + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7ec26bc..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/roller-backend.iml b/.idea/roller-backend.iml index 810e892..ec63674 100644 --- a/.idea/roller-backend.iml +++ b/.idea/roller-backend.iml @@ -1,10 +1,7 @@ - - - - - - - + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 63772a3..9309581 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/main.py b/main.py index 3317488..b08b32f 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,10 @@ -from math import floor -from random import uniform +from random import randint, random from fastapi import FastAPI, UploadFile from fastapi.middleware.cors import CORSMiddleware import sqlalchemy from sqlalchemy import text import pandas as pd +from pydantic import BaseModel app = FastAPI() @@ -19,7 +19,7 @@ app.add_middleware( 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("/") @@ -30,14 +30,34 @@ async def root(): @app.get("/api/get/get_random_stu") async def get_random_stu(): 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__() - 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") 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") 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")) for i in data.index.values: with engine.begin() as c: - c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0);")) - return file.filename \ No newline at end of file + c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0, 0);")) + 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 \ No newline at end of file