添加事件、分数更新、随机事件获取,学生被选择的概率现在和分数有关

master
cflsxjw 1 month ago
parent 6dee717023
commit 1d1839bcbe

8
.idea/.gitignore vendored

@ -1,8 +0,0 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="ROLLER_DB@localhost" uuid="9189e938-8553-4da1-b748-2f45425cc677">
<driver-ref>mariadb</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mariadb://localhost:3306/ROLLER_DB</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/roller-backend.iml" filepath="$PROJECT_DIR$/.idea/roller-backend.iml" />
</modules>
</component>
</project>

@ -1,10 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4"> <module version="4">
<component name="NewModuleRootManager"> <component name="PyDocumentationSettings">
<content url="file://$MODULE_DIR$"> <option name="format" value="PLAIN" />
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <option name="myDocStringFormat" value="Plain" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (roller-backend)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/main.py" dialect="MariaDB" />
<file url="PROJECT" dialect="MariaDB" /> <file url="PROJECT" dialect="MariaDB" />
</component> </component>
</project> </project>

@ -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
Loading…
Cancel
Save