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'), | ||||
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) | ||||
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_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) | 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.") | ||||
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): | |||||
def read_question_data(self, file_name: str) -> pd.DataFrame: | |||||
try: | try: | ||||
data = pd.read_csv(file_name, delimiter=';') | data = pd.read_csv(file_name, delimiter=';') | ||||
except Error as err: | except Error as err: | ||||
print(err) | print(err) | ||||
return data | return data | ||||
## ToDO: get question, return type(dict) | |||||
def get_questions(): | |||||
pass | |||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
database = QuestionDataBase('Project/BackEnd/Database/EinsZweiOderDrei.db') | |||||
database = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db') | |||||
insert_table = database.insert_table(questions) | insert_table = database.insert_table(questions) | ||||
database.commit() | database.commit() | ||||
database.close_connection() | database.close_connection() | ||||
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; |
from Database.database import QuestionDataBase | from Database.database import QuestionDataBase | ||||
import random | |||||
class Game: | 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())) | ||||
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(): | |||||
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): | 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() | |||||
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(): | |||||
test_dict = {'TEST': 'Dies ist eine Testfrage', | |||||
'firstanswer': 'erste Antwort', | |||||
'secondanswer': 'zweite Antwort', | |||||
'thirdanswer': 'dritte Antwort'} | |||||
return jsonify(test_dict) | |||||
@app.route('/connection', methods=['GET']) | |||||
def connection(): | |||||
return Response(status=200) | |||||
def get_question(): | |||||
question = my_game.get_question() | |||||
return jsonify(question) | |||||
## USEFUL ? | |||||
# @app.route('/connection', methods=['GET']) | |||||
# 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) | |||||
print(type(team_size)) | |||||
my_game.set_teamsize(team_size.get('teamsize')) | |||||
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(): | ||||
@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 | ||||
## 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() | ||||