Browse Source

shuffle answeroptions and show final results desc

master
David Engert 1 year ago
parent
commit
694062420d

+ 0
- 1
src_folder/BackEnd/CameraDetection/CameraDetection.py View File

# Fehlerbehandlung, wenn kein Bild gelesen werden kann # Fehlerbehandlung, wenn kein Bild gelesen werden kann
if not ret: if not ret:
break break

# Farben erkennen # Farben erkennen
farben_img, ergebnisse, count_red, count_green, count_blue = erkennung_farben(frame) farben_img, ergebnisse, count_red, count_green, count_blue = erkennung_farben(frame)



+ 28
- 8
src_folder/BackEnd/game.py View File

from Database.database import QuestionDataBase from Database.database import QuestionDataBase
import random import random
import re




class Game: class Game:
def __init__(self, teamsize=0, correct_field=0): def __init__(self, teamsize=0, correct_field=0):
self.teamsize = teamsize self.teamsize = teamsize
self.scoreboard = {'score_red': 0, self.scoreboard = {'score_red': 0,
'score_blue': 0,
'score_green': 0}
'score_green': 0,
'score_blue': 0
}


self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db') self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
self.available_questions = list(range(1, self.questions.num_rows())) self.available_questions = list(range(1, self.questions.num_rows()))
self.field = correct_field self.field = correct_field
self.question = {} self.question = {}
self.final_score = {}


def set_teamsize(self, teamsize: int): def set_teamsize(self, teamsize: int):
self.teamsize = teamsize self.teamsize = teamsize
def set_scoreboard(self, current_scores: dict): def set_scoreboard(self, current_scores: dict):
self.scoreboard = self.add_score(current_scores) self.scoreboard = self.add_score(current_scores)
return self.scoreboard return self.scoreboard
def reset_game(self): def reset_game(self):
self.__init__()
self.scoreboard = {'score_red': 0,
'score_green': 0,
'score_blue': 0
}


def get_question(self) -> dict:
def get_question(self):
questions = self.questions.cursor.execute("Select * from Questions") questions = self.questions.cursor.execute("Select * from Questions")
field_names = [i[0] for i in questions.description] field_names = [i[0] for i in questions.description]


questions_data = questions.fetchall() questions_data = questions.fetchall()
random_question_number = random.choice(self.available_questions) random_question_number = random.choice(self.available_questions)
self.available_questions.remove(random_question_number) self.available_questions.remove(random_question_number)

question= list(questions_data[random_question_number]) question= list(questions_data[random_question_number])
self.question = dict(zip(field_names, question)) self.question = dict(zip(field_names, question))
self.shuffle_answeroptions()
self.add_correct_field() self.add_correct_field()
return self.question

def shuffle_answeroptions(self):
answeroptions = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3']
keys = []
print(self.question)

for answer in answeroptions:
keys.append(self.question[answer])
random.shuffle(keys)

for index, answer in enumerate(answeroptions):
self.question[answer] = keys[index]


def add_correct_field(self): def add_correct_field(self):
answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption'] answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
def set_correct_field(self, correct_field: int): def set_correct_field(self, correct_field: int):
self.field = correct_field self.field = correct_field


def final_result(self):
self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True))





+ 13
- 3
src_folder/BackEnd/router.py View File

@app.route('/question', methods=['GET']) @app.route('/question', methods=['GET'])
def get_question(): def get_question():
my_game.get_question() my_game.get_question()
print(my_game.question)
return jsonify(my_game.question) return jsonify(my_game.question)


@app.route('/teamsize', methods=['POST']) @app.route('/teamsize', methods=['POST'])
def teamsize(): def teamsize():
team_size = request.json team_size = request.json
my_game.set_teamsize(team_size.get('teamsize')) my_game.set_teamsize(team_size.get('teamsize'))
print(f'Teamsize: {my_game.teamsize}\n')
return Response(status=200) return Response(status=200)


@app.route('/scoreboard', methods=['GET']) @app.route('/scoreboard', methods=['GET'])
def scoreboard(): def scoreboard():
return my_game.scoreboard, Response(status=200)
my_game.set_scoreboard()
print(my_game.scoreboard)
return jsonify(my_game.scoreboard)


@app.route('/check', methods=['GET']) @app.route('/check', methods=['GET'])
def check(): def check():
my_game.add_score(scores) my_game.add_score(scores)
return Response(status=200) return Response(status=200)


@app.route('/winner', methods=['GET'])
def set_winner():
my_game.final_result()
return jsonify(my_game.final_score)

def main(): def main():
## ASUS Router
## Router
# app.run(host='192.168.50.79', port=5555, debug=True) # app.run(host='192.168.50.79', port=5555, debug=True)


## Postman ## Postman
app.run(host='127.0.0.1', port=5555, debug=True)
app.run(host='127.0.0.1', port=5555, debug=True)




if __name__ == '__main__': if __name__ == '__main__':
main() main()




Loading…
Cancel
Save