-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
130 lines (108 loc) · 4.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from fastapi import FastAPI
from fastapi.responses import FileResponse
from pydantic import BaseModel
import sqlite3
import json
app = FastAPI()
class Answer(BaseModel):
id: str
answer: str
team_name: str
class Login(BaseModel):
name: str
password: str
@app.get("/")
async def home():
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM questions")
questions = c.fetchall()
return {"length": len(questions)}
@app.post("/login")
async def login(team: Login):
team_name = team.name
team_password = team.password
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM teams WHERE name = ? AND password = ?", (team_name, team_password))
team = c.fetchone()
if team == None:
return {"message": "Login failed"}
else:
return {"message": "Login successful", "score": team[2], "solved_questions": team[3], "color": team[4],"connected": team[5]}
@app.get("/get_question/{id}")
async def get_question(id: str):
attachment = ''
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM questions WHERE id = ?", (id,))
question = c.fetchone()
#print(question)
if question == None:
return {"message": "Question not found"}
else:
if question[4] != '':
attachment = '\n' + 'Download starter code ' + question[4] + '\n'
if question[5] != '':
attachment += 'Download input file ' + question[5]
return {"question": question[1] + attachment}
@app.get("/download_starter_code/{id}",response_class=FileResponse)
async def download_starter_code(id: str):
return 'questions/' + id + '_starter.py'
#See if we can download the file from database?
#See if we can attach headers to this response that contain the file name
@app.get("/download_input_file/{id}", response_class=FileResponse)
async def download_input_file(id: str):
return 'questions/' + id + '_input.txt'
@app.get("/get_teams_table")
async def get_teams_table():
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM teams")
teams = c.fetchall()
return {"teams": teams}
#just need to figure out how to get the solved questions list updated in the database
@app.post("/submit_answer")
async def submit_answer(a: Answer):
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM questions WHERE id = ?", (a.id,))
question = c.fetchone()
if question == None:
return {"message": "Question not found"}
elif question[2] == a.answer:
#print('correct answer for an existing question, points won: ', question[3])
resp = update_teams_table(question_id=a.id, team_name=a.team_name, points_won=question[3])
return resp
else:
return {"message": "Incorrect"}
def update_teams_table(question_id, points_won, team_name):
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT * FROM teams WHERE name = ?", (team_name,))
team = c.fetchone()
if team == None:
return {"message": "Team not found"}
#parse the team info
try:
solved_questions = json.loads(team[3])
#print(question_id, type(question_id), solved_questions, type(solved_questions))
if int(question_id) in solved_questions:
return {"message": "Already solved"}
except:
print("Cannot load team data")
try:
points_total = team[2]
points_total += points_won
solved_questions = json.loads(team[3])
solved_questions.append(int(question_id))
solved_questions = json.dumps(solved_questions)
#print("solved questions: ", solved_questions)
#update the database with the new values for score and solved questions
c.execute("UPDATE teams SET score = ?, solved_questions = ? WHERE name = ?", (points_total, solved_questions, team_name))
conn.commit()
#print("team found attempting to update db for question_id", question_id)
return {"message": "Correct", "points": points_won, "team_score": points_total}
except:
return {"message": "Error updating database"}
return {"message": "Something went very wrong"}