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
|
||||
|
||||
|
||||
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'),
|
||||
('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'),
|
||||
@ -29,7 +29,7 @@ class QuestionDataBase:
|
||||
def __init__(self, file_name) -> None:
|
||||
self.file_name = file_name
|
||||
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')
|
||||
except Error as err:
|
||||
print(err)
|
||||
@ -45,14 +45,13 @@ class QuestionDataBase:
|
||||
def create_table(self):
|
||||
table_config = "CREATE TABLE IF NOT EXISTS Questions" \
|
||||
"(QuestionNr TEXT PRIMARY KEY," \
|
||||
" Answeroption_1 \nTEXT," \
|
||||
" Answeroption_2 TEXT," \
|
||||
" Answeroption_3 TEXT," \
|
||||
" Correct_answeroption TEXT)"
|
||||
"Answeroption_1 \nTEXT," \
|
||||
"Answeroption_2 TEXT," \
|
||||
"Answeroption_3 TEXT," \
|
||||
"Correct_answeroption TEXT)"
|
||||
self.cursor.execute(table_config)
|
||||
print("Successfully create tables.. ")
|
||||
|
||||
|
||||
def insert_table(self, questions: list):
|
||||
self.cursor.executemany("INSERT OR IGNORE INTO Questions VALUES (?, ?, ?, ?, ?)", questions)
|
||||
print("Successfully insert data into table.")
|
||||
@ -61,12 +60,10 @@ class QuestionDataBase:
|
||||
self.cursor.execute("SELECT COUNT(*) FROM Questions")
|
||||
return self.cursor.fetchone()[0]
|
||||
|
||||
|
||||
def delete_data(self, table_name):
|
||||
self.cursor.execute(f"DELETE FROM {table_name}")
|
||||
|
||||
|
||||
def read_question_data(self, file_name: str):
|
||||
def read_question_data(self, file_name: str) -> pd.DataFrame:
|
||||
try:
|
||||
data = pd.read_csv(file_name, delimiter=';')
|
||||
except Error as err:
|
||||
@ -74,16 +71,9 @@ class QuestionDataBase:
|
||||
print(err)
|
||||
return data
|
||||
|
||||
## ToDO: get question, return type(dict)
|
||||
def get_questions():
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
database = QuestionDataBase('Project/BackEnd/Database/EinsZweiOderDrei.db')
|
||||
database = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
|
||||
insert_table = database.insert_table(questions)
|
||||
|
||||
database.commit()
|
||||
database.close_connection()
|
||||
|
||||
|
@ -1,21 +1,21 @@
|
||||
Question, Answer1, Answer2, Answer3, Right Answer
|
||||
('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'),
|
||||
('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'),
|
||||
('Mit welchem Korn verglich Jesus das Reich Gottes?', 'Senfkorn', 'Weizenkorn', 'Haferkorn', 'Senfkorn'),
|
||||
('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'),
|
||||
('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'),
|
||||
('Wie viele Bundesländer hat Deutschland?', '16', '17', '20', '16'),
|
||||
('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'),
|
||||
('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'),
|
||||
('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'),
|
||||
('Welches Land gewann 2014 die Fußball Weltmeisterschaft?', 'Brasilien', 'Argentien', 'Deutschland', 'Deutschland'),
|
||||
('Welcher ist der "rote Planet" unseres Sonnensystems?', 'Mars', 'Venus', 'Jupiter', 'Mars'),
|
||||
('Was bezeichnet die chemische Formel \'NaCl\'?', 'Kochsalz', 'Wasser', 'Stickstoff'),
|
||||
('Welche Form hat die Narbe auf Harry Potters Stirn?', 'Dreieck', 'Blitz', 'Kreuz', 'Blitz')
|
||||
QuestionNr, Answeroption_1, Answeroption_2, Answeroption_3, Correct_answeroption;
|
||||
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;
|
||||
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
|
||||
Mit welchem Korn verglich Jesus das Reich Gottes?, Senfkorn, Weizenkorn, Haferkorn, Senfkorn
|
||||
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
|
||||
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
|
||||
Wie viele Bundesländer hat Deutschland?, 16, 17, 20, 16
|
||||
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
|
||||
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
|
||||
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;
|
||||
Welches Land gewann 2014 die Fußball Weltmeisterschaft?, Brasilien, Argentien, Deutschland, Deutschland;
|
||||
Welcher ist der "rote Planet" unseres Sonnensystems?, Mars, Venus', Jupiter, Mars;
|
||||
Was bezeichnet die chemische Formel 'NaCl'?, Kochsalz, Wasser, Stickstoff, Kochsalz;
|
||||
Welche Form hat die Narbe auf Harry Potters Stirn?, Dreieck, Blitz, Kreuz, Blitz;
|
||||
|
@ -1,4 +1,6 @@
|
||||
from Database.database import QuestionDataBase
|
||||
import random
|
||||
|
||||
|
||||
|
||||
class Game:
|
||||
@ -6,7 +8,7 @@ class Game:
|
||||
self.teamsize = teamsize
|
||||
self.scoreboard = {'score_red': 0,
|
||||
'score_blue': 0,
|
||||
'score_yellow': 0}
|
||||
'score_green': 0}
|
||||
|
||||
self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
|
||||
self.available_questions = list(range(1, self.questions.num_rows()))
|
||||
@ -18,17 +20,40 @@ class Game:
|
||||
for key in self.scoreboard.keys():
|
||||
if key in current_score.keys():
|
||||
self.scoreboard[key] = self.scoreboard[key] + current_score[key]
|
||||
else:
|
||||
pass
|
||||
return self.scoreboard
|
||||
|
||||
def random_question():
|
||||
pass
|
||||
## ToDo: select question and remove selected question from list
|
||||
def set_scoreboard(self, current_scores: dict):
|
||||
self.scoreboard = self.add_score(current_scores)
|
||||
return self.scoreboard
|
||||
|
||||
def reset_game(self):
|
||||
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 game import Game
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
my_game = Game()
|
||||
|
||||
@app.route('/GETQUESTION', methods=['GET'])
|
||||
def test():
|
||||
test_dict = {'TEST': 'Dies ist eine Testfrage',
|
||||
'firstanswer': 'erste Antwort',
|
||||
'secondanswer': 'zweite Antwort',
|
||||
'thirdanswer': 'dritte Antwort'}
|
||||
return jsonify(test_dict)
|
||||
def get_question():
|
||||
question = my_game.get_question()
|
||||
return jsonify(question)
|
||||
|
||||
@app.route('/connection', methods=['GET'])
|
||||
def connection():
|
||||
return Response(status=200)
|
||||
## USEFUL ?
|
||||
# @app.route('/connection', methods=['GET'])
|
||||
# def connection():
|
||||
# return Response(status=200)
|
||||
|
||||
@app.route('/teamsize', methods=['POST'])
|
||||
def teamsize():
|
||||
team_size = request.json
|
||||
print(team_size)
|
||||
print(type(team_size))
|
||||
my_game.set_teamsize(team_size.get('teamsize'))
|
||||
return Response(status=200)
|
||||
|
||||
@app.route('/scoreboard', methods=['GET'])
|
||||
def scoreboard():
|
||||
pass
|
||||
new_score = my_game.scoreboard
|
||||
return new_score
|
||||
|
||||
@app.route('/check', methods=['GET'])
|
||||
def check():
|
||||
@ -33,8 +32,14 @@ def check():
|
||||
|
||||
@app.route('/reset', methods=['GET'])
|
||||
def reset():
|
||||
my_game.reset_game()
|
||||
return Response(status=200)
|
||||
|
||||
## IDEA
|
||||
@app.route('/GETSCORES', methods=['POST'])
|
||||
def get_scores():
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
## ASUS Router
|
||||
@ -43,6 +48,8 @@ def main():
|
||||
## Postman
|
||||
app.run(host='127.0.0.1', port=5555, debug=True)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user