add functions for shuffle questions and return the correct field
This commit is contained in:
parent
c3367fad31
commit
60b02019e2
@ -3,7 +3,7 @@ import pandas as pd
|
|||||||
from sqlite3 import Error
|
from sqlite3 import Error
|
||||||
|
|
||||||
|
|
||||||
questions = [('Wie heißt der längst Fluss auf der Welt?', 'Amazonas', 'Nil', 'Jangtsekiang', 'Nil'),
|
questions = [('Wie heißt der längste Fluss auf der Welt?', 'Amazonas', 'Nil', 'Jangtsekiang', 'Nil'),
|
||||||
('Aus welcher englischen Stadt stammt die Musikgruppe die Beatles?', 'London', 'Sheffield', 'Liverpool', 'Liverpool'),
|
('Aus welcher englischen Stadt stammt die Musikgruppe die Beatles?', 'London', 'Sheffield', 'Liverpool', 'Liverpool'),
|
||||||
('Zu welchem Land gehört Grönland?', 'Norwegen', 'Island', 'Dänemark', 'Dänemark'),
|
('Zu welchem Land gehört Grönland?', 'Norwegen', 'Island', 'Dänemark', 'Dänemark'),
|
||||||
('Wie heißen die ursprünglichen Bewohner Australiens?', 'Aborigines', 'Eingeborene', 'Australian Natives', 'Aborigines'),
|
('Wie heißen die ursprünglichen Bewohner Australiens?', 'Aborigines', 'Eingeborene', 'Australian Natives', 'Aborigines'),
|
||||||
@ -29,7 +29,7 @@ class QuestionDataBase:
|
|||||||
def __init__(self, file_name) -> None:
|
def __init__(self, file_name) -> None:
|
||||||
self.file_name = file_name
|
self.file_name = file_name
|
||||||
try:
|
try:
|
||||||
self.connection = sqlite3.connect(database=self.file_name)
|
self.connection = sqlite3.connect(database=self.file_name, check_same_thread=False)
|
||||||
print(f'Successfully connected to SQLite (Version: ' + sqlite3.version + ')..\n')
|
print(f'Successfully connected to SQLite (Version: ' + sqlite3.version + ')..\n')
|
||||||
except Error as err:
|
except Error as err:
|
||||||
print(err)
|
print(err)
|
||||||
@ -45,14 +45,13 @@ class QuestionDataBase:
|
|||||||
def create_table(self):
|
def create_table(self):
|
||||||
table_config = "CREATE TABLE IF NOT EXISTS Questions" \
|
table_config = "CREATE TABLE IF NOT EXISTS Questions" \
|
||||||
"(QuestionNr TEXT PRIMARY KEY," \
|
"(QuestionNr TEXT PRIMARY KEY," \
|
||||||
" Answeroption_1 \nTEXT," \
|
"Answeroption_1 \nTEXT," \
|
||||||
" Answeroption_2 TEXT," \
|
"Answeroption_2 TEXT," \
|
||||||
" Answeroption_3 TEXT," \
|
"Answeroption_3 TEXT," \
|
||||||
" Correct_answeroption TEXT)"
|
"Correct_answeroption TEXT)"
|
||||||
self.cursor.execute(table_config)
|
self.cursor.execute(table_config)
|
||||||
print("Successfully create tables.. ")
|
print("Successfully create tables.. ")
|
||||||
|
|
||||||
|
|
||||||
def insert_table(self, questions: list):
|
def insert_table(self, questions: list):
|
||||||
self.cursor.executemany("INSERT OR IGNORE INTO Questions VALUES (?, ?, ?, ?, ?)", questions)
|
self.cursor.executemany("INSERT OR IGNORE INTO Questions VALUES (?, ?, ?, ?, ?)", questions)
|
||||||
print("Successfully insert data into table.")
|
print("Successfully insert data into table.")
|
||||||
@ -61,12 +60,10 @@ class QuestionDataBase:
|
|||||||
self.cursor.execute("SELECT COUNT(*) FROM Questions")
|
self.cursor.execute("SELECT COUNT(*) FROM Questions")
|
||||||
return self.cursor.fetchone()[0]
|
return self.cursor.fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
def delete_data(self, table_name):
|
def delete_data(self, table_name):
|
||||||
self.cursor.execute(f"DELETE FROM {table_name}")
|
self.cursor.execute(f"DELETE FROM {table_name}")
|
||||||
|
|
||||||
|
def read_question_data(self, file_name: str) -> pd.DataFrame:
|
||||||
def read_question_data(self, file_name: str):
|
|
||||||
try:
|
try:
|
||||||
data = pd.read_csv(file_name, delimiter=';')
|
data = pd.read_csv(file_name, delimiter=';')
|
||||||
except Error as err:
|
except Error as err:
|
||||||
@ -74,16 +71,9 @@ class QuestionDataBase:
|
|||||||
print(err)
|
print(err)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
## ToDO: get question, return type(dict)
|
|
||||||
def get_questions():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
|
|
||||||
database = QuestionDataBase('Project/BackEnd/Database/EinsZweiOderDrei.db')
|
|
||||||
insert_table = database.insert_table(questions)
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
database = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
|
||||||
|
insert_table = database.insert_table(questions)
|
||||||
database.commit()
|
database.commit()
|
||||||
database.close_connection()
|
database.close_connection()
|
||||||
|
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
Question, Answer1, Answer2, Answer3, Right Answer
|
QuestionNr, Answeroption_1, Answeroption_2, Answeroption_3, Correct_answeroption;
|
||||||
('Wie heißt der längst Fluss auf der Welt?', 'Amazonas', 'Nil', 'Jangtsekiang', 'Nil'),
|
Wie heißt der längst Fluss auf der Welt?, Amazonas, Nil, Jangtsekiang, Nil;
|
||||||
('Aus welcher englischen Stadt stammt die Musikgruppe die Beatles?', 'London', 'Sheffield', 'Liverpool', 'Liverpool'),
|
Aus welcher englischen Stadt stammt die Musikgruppe die Beatles?, London, Sheffield, Liverpool, Liverpool;
|
||||||
('Zu welchem Land gehört Grönland?', 'Norwegen', 'Island', 'Dänemark', 'Dänemark'),
|
Zu welchem Land gehört Grönland?, Norwegen, Island, Dänemark, Dänemark
|
||||||
('Wie heißen die ursprünglichen Bewohner Australiens?', 'Aborigines', 'Eingeborene', 'Australian Natives', 'Aborigines'),
|
Wie heißen die ursprünglichen Bewohner Australiens?, Aborigines, Eingeborene, Australian Natives, Aborigines
|
||||||
('Mit welchem Korn verglich Jesus das Reich Gottes?', 'Senfkorn', 'Weizenkorn', 'Haferkorn', 'Senfkorn'),
|
Mit welchem Korn verglich Jesus das Reich Gottes?, Senfkorn, Weizenkorn, Haferkorn, Senfkorn
|
||||||
('Wie lange können Kaiserpinguine tauchen?', '5min', '10min', '20min', '20min'),
|
Wie lange können Kaiserpinguine tauchen?, 5min, 10min, 20min, 20min
|
||||||
('Wer war der Kommandant der Apollo 11?', 'Louis Armstrong', 'Neil Armstrong', 'Lance Armstrong', 'Neil Armstrong'),
|
Wer war der Kommandant der Apollo 11?, Louis Armstrong, Neil Armstrong, Lance Armstrong, Neil Armstrong
|
||||||
('Wie heißt die größte Stadt der Welt (nach Einwohnern)?', 'São Paulo', 'Tokio', 'Seoul', 'Tokio',),
|
Wie heißt die größte Stadt der Welt (nach Einwohnern)?, São Paulo, Tokio, Seoul, Tokio
|
||||||
('In welchem Land liegt Helsinki?', 'Finnland', 'Norwegen', 'Schweden', 'Finnland'),
|
In welchem Land liegt Helsinki?, Finnland, Norwegen, Schweden, Finnland
|
||||||
('Wie viele Bundesländer hat Deutschland?', '16', '17', '20', '16'),
|
Wie viele Bundesländer hat Deutschland?, 16, 17, 20, 16
|
||||||
('Wie heißt der höchste Berg Europas?', 'Zugspitze', 'Matterhorn', 'Montblanc', 'Montblanc'),
|
Wie heißt der höchste Berg Europas?, Zugspitze, Matterhorn, Montblanc, Montblanc
|
||||||
('Wie lange braucht das Licht in etwa von der Sonne zur Erde?', '80 Sekunden', '8 Minuten', '7 Tage', '8 Minuten'),
|
Wie lange braucht das Licht in etwa von der Sonne zur Erde?, 80 Sekunden, 8 Minuten, 7 Tage, 8 Minuten
|
||||||
('Was hat James Watt erfunden?', 'Glühbirne', 'Elektrischen Widerstand', 'Dampfmaschine', 'Dampfmaschine'),
|
Was hat James Watt erfunden?, Glühbirne, Elektrischen Widerstand, Dampfmaschine, Dampfmaschine
|
||||||
('Wie hieß der erste Deutsch Bundeskanzler?', 'Helmut Schmidt', 'Konrad Adenauer', 'Willy Brandt', 'Konrad Adenauer'),
|
Wie hieß der erste Deutsch Bundeskanzler?, Helmut Schmidt, Konrad Adenauer, Willy Brandt, Konrad Adenauer
|
||||||
('Von wem stammt das Gemälde der Mona Lisa?', 'Picasso', 'Vincent van Gogh', 'Leonardo da Vinci', 'Leonardo da Vinci'),
|
Von wem stammt das Gemälde der Mona Lisa?, Picasso, Vincent van Gogh, Leonardo da Vinci, Leonardo da Vinci
|
||||||
('Wie heißt der Erfinder der Jeanshose?', 'Tom Wrangler', 'Levi Strauss', 'Peter Diesel', 'Levi Strauss'),
|
Wie heißt der Erfinder der Jeanshose?, Tom Wrangler, Levi Strauss, Peter Diesel, Levi Strauss;
|
||||||
('Welches Land gewann 2014 die Fußball Weltmeisterschaft?', 'Brasilien', 'Argentien', 'Deutschland', 'Deutschland'),
|
Welches Land gewann 2014 die Fußball Weltmeisterschaft?, Brasilien, Argentien, Deutschland, Deutschland;
|
||||||
('Welcher ist der "rote Planet" unseres Sonnensystems?', 'Mars', 'Venus', 'Jupiter', 'Mars'),
|
Welcher ist der "rote Planet" unseres Sonnensystems?, Mars, Venus', Jupiter, Mars;
|
||||||
('Was bezeichnet die chemische Formel \'NaCl\'?', 'Kochsalz', 'Wasser', 'Stickstoff'),
|
Was bezeichnet die chemische Formel 'NaCl'?, Kochsalz, Wasser, Stickstoff, Kochsalz;
|
||||||
('Welche Form hat die Narbe auf Harry Potters Stirn?', 'Dreieck', 'Blitz', 'Kreuz', 'Blitz')
|
Welche Form hat die Narbe auf Harry Potters Stirn?, Dreieck, Blitz, Kreuz, Blitz;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
from Database.database import QuestionDataBase
|
from Database.database import QuestionDataBase
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -6,7 +8,7 @@ class Game:
|
|||||||
self.teamsize = teamsize
|
self.teamsize = teamsize
|
||||||
self.scoreboard = {'score_red': 0,
|
self.scoreboard = {'score_red': 0,
|
||||||
'score_blue': 0,
|
'score_blue': 0,
|
||||||
'score_yellow': 0}
|
'score_green': 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()))
|
||||||
@ -18,17 +20,40 @@ class Game:
|
|||||||
for key in self.scoreboard.keys():
|
for key in self.scoreboard.keys():
|
||||||
if key in current_score.keys():
|
if key in current_score.keys():
|
||||||
self.scoreboard[key] = self.scoreboard[key] + current_score[key]
|
self.scoreboard[key] = self.scoreboard[key] + current_score[key]
|
||||||
else:
|
|
||||||
pass
|
|
||||||
return self.scoreboard
|
return self.scoreboard
|
||||||
|
|
||||||
def random_question():
|
def set_scoreboard(self, current_scores: dict):
|
||||||
pass
|
self.scoreboard = self.add_score(current_scores)
|
||||||
## ToDo: select question and remove selected question from list
|
return self.scoreboard
|
||||||
|
|
||||||
def reset_game(self):
|
def reset_game(self):
|
||||||
self.__init__()
|
self.__init__()
|
||||||
|
|
||||||
|
def get_question(self) -> dict:
|
||||||
|
questions = self.questions.cursor.execute("Select * from Questions")
|
||||||
|
field_names = [i[0] for i in questions.description]
|
||||||
|
|
||||||
|
questions_data = questions.fetchall()
|
||||||
|
random_question_number = random.choice(self.available_questions)
|
||||||
|
self.available_questions.remove(random_question_number)
|
||||||
|
|
||||||
|
question= list(questions_data[random_question_number])
|
||||||
|
data = dict(zip(field_names, question))
|
||||||
|
add_data = my_game.correct_field(data)
|
||||||
|
return add_data
|
||||||
|
|
||||||
|
def correct_field(self, data_dict: dict) -> dict:
|
||||||
|
answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
|
||||||
|
for i in range(0, len(answeroption) -1):
|
||||||
|
if data_dict[answeroption[-1]] == data_dict[answeroption[i]]:
|
||||||
|
index = answeroption.index(answeroption[i]) + 1
|
||||||
|
break
|
||||||
|
data_dict.update([('Correct_field', index)])
|
||||||
|
return data_dict
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
my_game = Game()
|
||||||
|
test = my_game.get_question()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,31 +1,30 @@
|
|||||||
from flask import Flask, jsonify, Response, request
|
from flask import Flask, jsonify, Response, request
|
||||||
|
from game import Game
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
my_game = Game()
|
||||||
|
|
||||||
@app.route('/GETQUESTION', methods=['GET'])
|
@app.route('/GETQUESTION', methods=['GET'])
|
||||||
def test():
|
def get_question():
|
||||||
test_dict = {'TEST': 'Dies ist eine Testfrage',
|
question = my_game.get_question()
|
||||||
'firstanswer': 'erste Antwort',
|
return jsonify(question)
|
||||||
'secondanswer': 'zweite Antwort',
|
|
||||||
'thirdanswer': 'dritte Antwort'}
|
|
||||||
return jsonify(test_dict)
|
|
||||||
|
|
||||||
@app.route('/connection', methods=['GET'])
|
## USEFUL ?
|
||||||
def connection():
|
# @app.route('/connection', methods=['GET'])
|
||||||
return Response(status=200)
|
# def connection():
|
||||||
|
# return Response(status=200)
|
||||||
|
|
||||||
@app.route('/teamsize', methods=['POST'])
|
@app.route('/teamsize', methods=['POST'])
|
||||||
def teamsize():
|
def teamsize():
|
||||||
team_size = request.json
|
team_size = request.json
|
||||||
print(team_size)
|
my_game.set_teamsize(team_size.get('teamsize'))
|
||||||
print(type(team_size))
|
|
||||||
return Response(status=200)
|
return Response(status=200)
|
||||||
|
|
||||||
@app.route('/scoreboard', methods=['GET'])
|
@app.route('/scoreboard', methods=['GET'])
|
||||||
def scoreboard():
|
def scoreboard():
|
||||||
pass
|
new_score = my_game.scoreboard
|
||||||
|
return new_score
|
||||||
|
|
||||||
@app.route('/check', methods=['GET'])
|
@app.route('/check', methods=['GET'])
|
||||||
def check():
|
def check():
|
||||||
@ -33,8 +32,14 @@ def check():
|
|||||||
|
|
||||||
@app.route('/reset', methods=['GET'])
|
@app.route('/reset', methods=['GET'])
|
||||||
def reset():
|
def reset():
|
||||||
|
my_game.reset_game()
|
||||||
return Response(status=200)
|
return Response(status=200)
|
||||||
|
|
||||||
|
## IDEA
|
||||||
|
@app.route('/GETSCORES', methods=['POST'])
|
||||||
|
def get_scores():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
## ASUS Router
|
## ASUS Router
|
||||||
@ -42,8 +47,10 @@ def main():
|
|||||||
|
|
||||||
## 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…
x
Reference in New Issue
Block a user