Skip to content

Commit

Permalink
Quiz class no longer makes a Team object
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjinDedic committed Mar 13, 2023
1 parent d6d32a1 commit 1a0a07b
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 51 deletions.
176 changes: 176 additions & 0 deletions LEGACY/oo_package_split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
from fastapi.testclient import TestClient
from main import app
import json
import os

client = TestClient(app=app)

class ApiInterface:
def __init__(self,team_name,team_password):
self.team_name = team_name
self.team_password = team_password
self.__logged_in = False

def login(self):
#__logged_in is a private variable it is mirrored inside the Quiz class once login() is called
response = client.post("/login", json={"name": self.team_name, "password": self.team_password})
if response.status_code == 200:
solved_qs = response.json()['solved_questions']
solved_qs = json.loads(solved_qs)
print('solved_qs:',solved_qs, type(solved_qs))
self.__logged_in = True
print('logged in')
return True

else:
print('login failed')
return False


def get_question(self,question_num):
if self.__logged_in:
response = client.get("/get_question/"+str(question_num))
if response.status_code == 200:
return response.json()['question']
else:
return 'Question not found'
if self.__logged_in == False:
print('login first')
return

def submit_answer(self,question_num, answer):
if self.__logged_in:
response = client.post("/submit_answer", json={"id": str(question_num), "answer": answer, "team_name": self.team_name})
if response.status_code == 200:
return response.json()
else:
return 'API Connection error'
if self.__logged_in == False:
print('login first')
return

def get_teams_table(self):
if self.__logged_in == False:
print('login first')
return
response = client.get("/get_teams_table")
if response.status_code == 200:
return response.json()
else:
return 'API Connection error cannot get teams table'


class Quiz(ApiInterface):
def __init__(self,team_name,team_password):
super().__init__(team_name,team_password)
self.__logged_in = self.login()
response = client.get("/")
self.total_questions = response.json()["length"]
print('CONN ESTABLISHED, TOTAL QUESTIONS:', self.total_questions)


def print_rankings(self):
if self.__logged_in == False:
print('login first')
return
teams_data = self.get_teams_table()
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_rankings()


def print_status(self):
if self.__logged_in == False:
print('login first')
return
teams_data = self.get_teams_table()
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_status(self.team_name)

def interactive_menu(self):
if self.__logged_in == False:
print('login first')
return
#os.system('cls' if os.name == 'nt' else 'clear')
print('Welcome to VCC 2023', self.team_name,'What would you like to do?')
print('1. View Question')
print('2. Submit Answer')
print('3. View Scoreboard')
print('4. View Answered Questions')
print('5. Exit')
choice = input('Enter your choice:')
if choice == '1':
question_num = input('Enter question number:')
question = self.get_question(question_num)
print(question)
input('Press enter to continue')
self.interactive_menu()
elif choice == '2':
question_num = input('Enter question number:')
print('Question:',self.get_question(question_num))
answer = input('Enter answer:')
result = self.submit_answer(question_num,answer)
print(result)
input('Press enter to continue')
self.interactive_menu()
elif choice == '3':
self.print_rankings()
input('Press enter to continue')
self.interactive_menu()
elif choice == '4':
print('Here is the list of the questions you have answered:')
self.print_status()
input('Press enter to continue')
self.interactive_menu()
elif choice == '5':
print('Goodbye')
return


class Team():
def __init__(self, name, score, solved_questions=[]):
self.name = name
self.score = score
self.solved_questions = solved_questions


def __str__(self):
if len(self.name) < 8:
answer = "TEAM NAME: "+ self.name + "\t\t SCORE: " + str(self.score) + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
else:
answer = "TEAM NAME: "+ self.name + "\t SCORE: " + str(self.score) + + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
return answer


class Scoreboard():
def __init__(self,teams_data):
self.teams = []
self.teams_data = teams_data
self.load_teams()

def load_teams(self):
data_example = {'teams': [
['team1', 'team1', 0, '[]', 'red', 0],
['team2', 'team2', 0, '[]', 'blue', 0],
['team3', 'team3', 0, '[]', 'green', 0],
['team4', 'team4', 0, '[]', 'yellow', 0]]}
print(self.teams_data)
for team in self.teams_data['teams']:
solved_qs = json.loads(team[3])
self.teams.append(Team(name=team[0],score=team[2],solved_questions=solved_qs))


def print_rankings(self):
#this function needs to add colors to the teams which are read from the json file
#os.system('cls' if os.name == 'nt' else 'clear')
ordered_teams = sorted(self.teams, key=lambda x: x.score, reverse=True)
for team in ordered_teams:
print(team)

def print_status(self,team_name):
for team in self.teams:
if team.name == team_name:
print(team_name,'score:',team.score,'solved questions:',team.solved_questions)

if __name__ == "__main__":
quiz = Quiz('team1','team1')
quiz.interactive_menu()
Binary file modified __pycache__/main.cpython-311.pyc
Binary file not shown.
25 changes: 13 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ async def login(team: Login):
return {"message": "Login successful", "score": team[2], "solved_questions": team[3], "color": team[4],"connected": team[5]}


@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_question/{id}")
async def get_question(id: str):
attachment = ''
Expand All @@ -70,6 +58,18 @@ async def get_question(id: str):
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')
Expand All @@ -96,6 +96,7 @@ async def submit_answer(a: Answer):
return {"message": "Incorrect"}



def update_teams_table(question_id, points_won, team_name):
conn = sqlite3.connect('database.db')
c = conn.cursor()
Expand Down
82 changes: 43 additions & 39 deletions oo_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,63 @@ def __init__(self,team_name,team_password):

def login(self):
response = client.post("/login", json={"name": self.team_name, "password": self.team_password})

if response.status_code == 200:
score = response.json()['score']
solved_qs = response.json()['solved_questions']
solved_qs = json.loads(solved_qs)
#print('solved_qs:',solved_qs, type(solved_qs))
self.team = Team(name=self.team_name, score=score, solved_questions=sorted(solved_qs))
self.__logged_in = True
print('logged in')
else:
print('login failed')

def get_question(self,question_num):
if self.__logged_in:
response = client.get("/get_question/"+str(question_num))
if response.status_code == 200:
return response.json()['question']
else:
return 'Question not found'
if self.__logged_in == False:
print('login first')
return
return self.team.get_question(question_num)

def submit_answer(self,question_num,answer):

def submit_answer(self,question_num, answer):
if self.__logged_in:
response = client.post("/submit_answer", json={"id": str(question_num), "answer": answer, "team_name": self.team_name})
if response.status_code == 200:
return response.json()
else:
return 'API Connection error'
if self.__logged_in == False:
print('login first')
return
return self.team.submit_answer(question_num,answer)

def print_rankings(self):

def get_teams_table(self):
if self.__logged_in == False:
print('login first')
return
response = client.get("/get_teams_table")
if response.status_code == 200:
scoreboard = Scoreboard(teams_data = response.json())
scoreboard.print_rankings()
return response.json()
else:
return 'API Connection error'
return 'API Connection error cannot get teams table'

def print_rankings(self):
if self.__logged_in == False:
print('login first')
return
teams_data = self.get_teams_table()
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_rankings()


def print_status(self):
if self.__logged_in == False:
print('login first')
return
teams_data = self.get_teams_table()
scoreboard = Scoreboard(teams_data = teams_data)
scoreboard.print_status(self.team_name)

def interactive_menu(self):
if self.__logged_in == False:
Expand All @@ -63,7 +86,7 @@ def interactive_menu(self):
print('1. View Question')
print('2. Submit Answer')
print('3. View Scoreboard')
print('4. View answered questions')
print('4. View Answered Questions')
print('5. Exit')
choice = input('Enter your choice:')
if choice == '1':
Expand All @@ -86,44 +109,27 @@ def interactive_menu(self):
self.interactive_menu()
elif choice == '4':
print('Here is the list of the questions you have answered:')
print(self.team.solved_questions)
self.print_status()
input('Press enter to continue')
self.interactive_menu()
elif choice == '5':
print('Goodbye')
return



class Team():
def __init__(self, name, score, solved_questions=[]):
self.name = name
self.score = score
self.solved_questions = solved_questions


def __str__(self):
if len(self.name) < 8:
answer = "TEAM NAME: "+ self.name + "\t\t SCORE: " + str(self.score) + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
else:
answer = "TEAM NAME: "+ self.name + "\t SCORE: " + str(self.score) + + "\t ANSWERED QUESTIONS: " + str(len(self.solved_questions))
return answer

def get_question(self,num):
response = client.get("/get_question/"+str(num))
if response.status_code == 200:
return response.json()['question']
else:
return 'Question not found'


def submit_answer(self,question_num, answer):
response = client.post("/submit_answer", json={"id": str(question_num), "answer": answer, "team_name": self.name})
if response.status_code == 200:
return response.json()
else:
return 'API Connection error'


class Scoreboard():
def __init__(self,teams_data):
Expand All @@ -132,23 +138,21 @@ def __init__(self,teams_data):
self.load_teams()

def load_teams(self):
data_example = {'teams': [
['team1', 'team1', 0, '[]', 'red', 0],
['team2', 'team2', 0, '[]', 'blue', 0],
['team3', 'team3', 0, '[]', 'green', 0],
['team4', 'team4', 0, '[]', 'yellow', 0]]}
print(self.teams_data)
for team in self.teams_data['teams']:
solved_qs = json.loads(team[3])
self.teams.append(Team(name=team[0],score=team[2],solved_questions=solved_qs))

self.teams.append(Team(name=team[0],score=team[2],solved_questions=solved_qs))

def print_rankings(self):
#this function needs to add colors to the teams which are read from the json file
#os.system('cls' if os.name == 'nt' else 'clear')
ordered_teams = sorted(self.teams, key=lambda x: x.score, reverse=True)
for team in ordered_teams:
print(team)

def print_status(self,team_name):
for team in self.teams:
if team.name == team_name:
print(team_name,'score:',team.score,'solved questions:',team.solved_questions)

if __name__ == "__main__":
quiz = Quiz('team1','team1')
Expand Down

0 comments on commit 1a0a07b

Please sign in to comment.