Beispiel #1
0
class Ui_MainWindow(object):
    def __init__(self, socket, player_id):
        self.questions = Questions()
        self.WinnerChosen = False
        self.socketGUI = socket
        self.playerId = player_id

        self.thread_scores = threading.Thread(target=self.update_scoreboard,
                                              args=())
        self.thread_scores.daemon = True
        # self.answered_questions_t = threading.Thread(target=self.answered_questions, args=())
        # self.answered_questions_t.daemon = True
        # self.answered_questions_t.start()

    # each second asks for new scores
    def update_scoreboard(self):
        while True:
            new_scores = self.socketGUI.send("get_scores")
            print("New scores from server: ", new_scores)
            self.team1_score_lbl.setText(str(new_scores.get("p0")))
            self.team2_score_lbl.setText(str(new_scores.get("p1")))
            self.team3_score_lbl.setText(str(new_scores.get("p2")))
            sleep(1)

#   def answered_questions(self):
#        while True:
#answered = self.socketGUI.send("get_answered")
#print(answered)
# print("New scores from server: ", new_scores)
# self.team1_score_lbl.setText(str(new_scores.get("p0")))
# self.team2_score_lbl.setText(str(new_scores.get("p1")))
# self.team3_score_lbl.setText(str(new_scores.get("p2")))
#sleep(1)

    def openQuestion(self, btn):
        rcv = self.socketGUI.send("q_" + btn.objectName())  # ex. cat1.100

        dialog = QuestionDialog(rcv)
        ret_status = dialog.exec_()

        if ret_status == QtWidgets.QDialog.Accepted:
            print("Answered = ", dialog.getAnswer())
            #check answer
            answ = json.dumps({
                "question": "cat1.100",
                "answer": dialog.getAnswer()
            })
            print("Data back to server", answ)
            print(type(answ))
            rcv = self.socketGUI.send(answ)
            print("Received back (is correct): ", rcv.get("a"))
            btn.setEnabled(False)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(608, 392)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.cat1_100den = QtWidgets.QPushButton(self.centralwidget)
        self.cat1_100den.setGeometry(QtCore.QRect(30, 50, 101, 41))
        self.cat1_100den.setAutoFillBackground(True)
        self.cat1_100den.setCheckable(True)
        self.cat1_100den.setChecked(False)
        self.cat1_100den.setAutoRepeat(False)
        self.cat1_100den.setAutoExclusive(False)
        self.cat1_100den.setAutoDefault(True)
        self.cat1_100den.setDefault(False)
        self.cat1_100den.setFlat(False)
        self.cat1_100den.setObjectName("cat1.100")
        self.cat1_100den.clicked.connect(
            lambda: self.openQuestion(self.cat1_100den))

        #self.buttons.append(self.cat1_100den)

        self.cat2_100den = QtWidgets.QPushButton(self.centralwidget)
        self.cat2_100den.setGeometry(QtCore.QRect(140, 50, 101, 41))
        self.cat2_100den.setAutoFillBackground(True)
        self.cat2_100den.setCheckable(True)
        self.cat2_100den.setChecked(False)
        self.cat2_100den.setAutoRepeat(False)
        self.cat2_100den.setAutoExclusive(False)
        self.cat2_100den.setAutoDefault(True)
        self.cat2_100den.setDefault(False)
        self.cat2_100den.setFlat(False)
        self.cat2_100den.setObjectName("cat2.100")
        self.cat2_100den.clicked.connect(
            lambda: self.openQuestion(self.cat2_100den))

        self.cat3_100den = QtWidgets.QPushButton(self.centralwidget)
        self.cat3_100den.setGeometry(QtCore.QRect(250, 50, 101, 41))
        self.cat3_100den.setAutoFillBackground(True)
        self.cat3_100den.setCheckable(True)
        self.cat3_100den.setChecked(False)
        self.cat3_100den.setAutoRepeat(False)
        self.cat3_100den.setAutoExclusive(False)
        self.cat3_100den.setAutoDefault(True)
        self.cat3_100den.setDefault(False)
        self.cat3_100den.setFlat(False)
        self.cat3_100den.setObjectName("cat3.100")
        self.cat3_100den.clicked.connect(
            lambda: self.openQuestion(self.cat3_100den))

        self.cat4_100den = QtWidgets.QPushButton(self.centralwidget)
        self.cat4_100den.setGeometry(QtCore.QRect(360, 50, 101, 41))
        self.cat4_100den.setAutoFillBackground(True)
        self.cat4_100den.setCheckable(True)
        self.cat4_100den.setChecked(False)
        self.cat4_100den.setAutoRepeat(False)
        self.cat4_100den.setAutoExclusive(False)
        self.cat4_100den.setAutoDefault(True)
        self.cat4_100den.setDefault(False)
        self.cat4_100den.setFlat(False)
        self.cat4_100den.setObjectName("cat4.100")
        self.cat4_100den.clicked.connect(
            lambda: self.openQuestion(self.cat4_100den))

        self.cat5_100den = QtWidgets.QPushButton(self.centralwidget)
        self.cat5_100den.setGeometry(QtCore.QRect(470, 50, 101, 41))
        self.cat5_100den.setAutoFillBackground(True)
        self.cat5_100den.setCheckable(True)
        self.cat5_100den.setChecked(False)
        self.cat5_100den.setAutoRepeat(False)
        self.cat5_100den.setAutoExclusive(False)
        self.cat5_100den.setAutoDefault(True)
        self.cat5_100den.setDefault(False)
        self.cat5_100den.setFlat(False)
        self.cat5_100den.setObjectName("cat5.100")
        self.cat5_100den.clicked.connect(
            lambda: self.openQuestion(self.cat5_100den))

        self.cat4_200den = QtWidgets.QPushButton(self.centralwidget)
        self.cat4_200den.setGeometry(QtCore.QRect(360, 100, 101, 41))
        self.cat4_200den.setAutoFillBackground(True)
        self.cat4_200den.setCheckable(True)
        self.cat4_200den.setChecked(False)
        self.cat4_200den.setAutoRepeat(False)
        self.cat4_200den.setAutoExclusive(False)
        self.cat4_200den.setAutoDefault(True)
        self.cat4_200den.setDefault(False)
        self.cat4_200den.setFlat(False)
        self.cat4_200den.setObjectName("cat4.200")
        self.cat4_200den.clicked.connect(
            lambda: self.openQuestion(self.cat4_200den))

        self.cat3_200den = QtWidgets.QPushButton(self.centralwidget)
        self.cat3_200den.setGeometry(QtCore.QRect(250, 100, 101, 41))
        self.cat3_200den.setAutoFillBackground(True)
        self.cat3_200den.setCheckable(True)
        self.cat3_200den.setChecked(False)
        self.cat3_200den.setAutoRepeat(False)
        self.cat3_200den.setAutoExclusive(False)
        self.cat3_200den.setAutoDefault(True)
        self.cat3_200den.setDefault(False)
        self.cat3_200den.setFlat(False)
        self.cat3_200den.setObjectName("cat3.200")
        self.cat3_200den.clicked.connect(
            lambda: self.openQuestion(self.cat3_200den))

        self.cat2_200den = QtWidgets.QPushButton(self.centralwidget)
        self.cat2_200den.setGeometry(QtCore.QRect(140, 100, 101, 41))
        self.cat2_200den.setAutoFillBackground(True)
        self.cat2_200den.setCheckable(True)
        self.cat2_200den.setChecked(False)
        self.cat2_200den.setAutoRepeat(False)
        self.cat2_200den.setAutoExclusive(False)
        self.cat2_200den.setAutoDefault(True)
        self.cat2_200den.setDefault(False)
        self.cat2_200den.setFlat(False)
        self.cat2_200den.setObjectName("cat2.200")
        self.cat2_200den.clicked.connect(
            lambda: self.openQuestion(self.cat2_200den))

        self.cat5_200den = QtWidgets.QPushButton(self.centralwidget)
        self.cat5_200den.setGeometry(QtCore.QRect(470, 100, 101, 41))
        self.cat5_200den.setAutoFillBackground(True)
        self.cat5_200den.setCheckable(True)
        self.cat5_200den.setChecked(False)
        self.cat5_200den.setAutoRepeat(False)
        self.cat5_200den.setAutoExclusive(False)
        self.cat5_200den.setAutoDefault(True)
        self.cat5_200den.setDefault(False)
        self.cat5_200den.setFlat(False)
        self.cat5_200den.setObjectName("cat5.200")
        self.cat5_200den.clicked.connect(
            lambda: self.openQuestion(self.cat5_200den))

        self.cat1_200den = QtWidgets.QPushButton(self.centralwidget)
        self.cat1_200den.setGeometry(QtCore.QRect(30, 100, 101, 41))
        self.cat1_200den.setAutoFillBackground(True)
        self.cat1_200den.setCheckable(True)
        self.cat1_200den.setChecked(False)
        self.cat1_200den.setAutoRepeat(False)
        self.cat1_200den.setAutoExclusive(False)
        self.cat1_200den.setAutoDefault(True)
        self.cat1_200den.setDefault(False)
        self.cat1_200den.setFlat(False)
        self.cat1_200den.setObjectName("cat1.200")
        self.cat1_200den.clicked.connect(
            lambda: self.openQuestion(self.cat1_200den))

        self.cat4_400den = QtWidgets.QPushButton(self.centralwidget)
        self.cat4_400den.setGeometry(QtCore.QRect(360, 200, 101, 41))
        self.cat4_400den.setAutoFillBackground(True)
        self.cat4_400den.setCheckable(True)
        self.cat4_400den.setChecked(False)
        self.cat4_400den.setAutoRepeat(False)
        self.cat4_400den.setAutoExclusive(False)
        self.cat4_400den.setAutoDefault(True)
        self.cat4_400den.setDefault(False)
        self.cat4_400den.setFlat(False)
        self.cat4_400den.setObjectName("cat4.400")
        self.cat4_400den.clicked.connect(
            lambda: self.openQuestion(self.cat4_400den))

        self.cat4_300den = QtWidgets.QPushButton(self.centralwidget)
        self.cat4_300den.setGeometry(QtCore.QRect(360, 150, 101, 41))
        self.cat4_300den.setAutoFillBackground(True)
        self.cat4_300den.setCheckable(True)
        self.cat4_300den.setChecked(False)
        self.cat4_300den.setAutoRepeat(False)
        self.cat4_300den.setAutoExclusive(False)
        self.cat4_300den.setAutoDefault(True)
        self.cat4_300den.setDefault(False)
        self.cat4_300den.setFlat(False)
        self.cat4_300den.setObjectName("cat4.300")
        self.cat4_300den.clicked.connect(
            lambda: self.openQuestion(self.cat4_300den))

        self.cat2_400den = QtWidgets.QPushButton(self.centralwidget)
        self.cat2_400den.setGeometry(QtCore.QRect(140, 200, 101, 41))
        self.cat2_400den.setAutoFillBackground(True)
        self.cat2_400den.setCheckable(True)
        self.cat2_400den.setChecked(False)
        self.cat2_400den.setAutoRepeat(False)
        self.cat2_400den.setAutoExclusive(False)
        self.cat2_400den.setAutoDefault(True)
        self.cat2_400den.setDefault(False)
        self.cat2_400den.setFlat(False)
        self.cat2_400den.setObjectName("cat2.400")
        self.cat2_400den.clicked.connect(
            lambda: self.openQuestion(self.cat2_400den))

        self.cat3_300den = QtWidgets.QPushButton(self.centralwidget)
        self.cat3_300den.setGeometry(QtCore.QRect(250, 150, 101, 41))
        self.cat3_300den.setAutoFillBackground(True)
        self.cat3_300den.setCheckable(True)
        self.cat3_300den.setChecked(False)
        self.cat3_300den.setAutoRepeat(False)
        self.cat3_300den.setAutoExclusive(False)
        self.cat3_300den.setAutoDefault(True)
        self.cat3_300den.setDefault(False)
        self.cat3_300den.setFlat(False)
        self.cat3_300den.setObjectName("cat3.300")
        self.cat3_300den.clicked.connect(
            lambda: self.openQuestion(self.cat3_300den))

        self.cat2_300den = QtWidgets.QPushButton(self.centralwidget)
        self.cat2_300den.setGeometry(QtCore.QRect(140, 150, 101, 41))
        self.cat2_300den.setAutoFillBackground(True)
        self.cat2_300den.setCheckable(True)
        self.cat2_300den.setChecked(False)
        self.cat2_300den.setAutoRepeat(False)
        self.cat2_300den.setAutoExclusive(False)
        self.cat2_300den.setAutoDefault(True)
        self.cat2_300den.setDefault(False)
        self.cat2_300den.setFlat(False)
        self.cat2_300den.setObjectName("cat2.300")
        self.cat2_300den.clicked.connect(
            lambda: self.openQuestion(self.cat2_300den))

        self.cat1_400den = QtWidgets.QPushButton(self.centralwidget)
        self.cat1_400den.setGeometry(QtCore.QRect(30, 200, 101, 41))
        self.cat1_400den.setAutoFillBackground(True)
        self.cat1_400den.setCheckable(True)
        self.cat1_400den.setChecked(False)
        self.cat1_400den.setAutoRepeat(False)
        self.cat1_400den.setAutoExclusive(False)
        self.cat1_400den.setAutoDefault(True)
        self.cat1_400den.setDefault(False)
        self.cat1_400den.setFlat(False)
        self.cat1_400den.setObjectName("cat1.400")
        self.cat1_400den.clicked.connect(
            lambda: self.openQuestion(self.cat1_400den))

        self.cat5_300den = QtWidgets.QPushButton(self.centralwidget)
        self.cat5_300den.setGeometry(QtCore.QRect(470, 150, 101, 41))
        self.cat5_300den.setAutoFillBackground(True)
        self.cat5_300den.setCheckable(True)
        self.cat5_300den.setChecked(False)
        self.cat5_300den.setAutoRepeat(False)
        self.cat5_300den.setAutoExclusive(False)
        self.cat5_300den.setAutoDefault(True)
        self.cat5_300den.setDefault(False)
        self.cat5_300den.setFlat(False)
        self.cat5_300den.setObjectName("cat5.300")
        self.cat5_300den.clicked.connect(
            lambda: self.openQuestion(self.cat5_300den))

        self.cat5_400den = QtWidgets.QPushButton(self.centralwidget)
        self.cat5_400den.setGeometry(QtCore.QRect(470, 200, 101, 41))
        self.cat5_400den.setAutoFillBackground(True)
        self.cat5_400den.setCheckable(True)
        self.cat5_400den.setChecked(False)
        self.cat5_400den.setAutoRepeat(False)
        self.cat5_400den.setAutoExclusive(False)
        self.cat5_400den.setAutoDefault(True)
        self.cat5_400den.setDefault(False)
        self.cat5_400den.setFlat(False)
        self.cat5_400den.setObjectName("cat5.400")
        self.cat5_400den.clicked.connect(
            lambda: self.openQuestion(self.cat5_400den))

        self.cat3_400den = QtWidgets.QPushButton(self.centralwidget)
        self.cat3_400den.setGeometry(QtCore.QRect(250, 200, 101, 41))
        self.cat3_400den.setAutoFillBackground(True)
        self.cat3_400den.setCheckable(True)
        self.cat3_400den.setChecked(False)
        self.cat3_400den.setAutoRepeat(False)
        self.cat3_400den.setAutoExclusive(False)
        self.cat3_400den.setAutoDefault(True)
        self.cat3_400den.setDefault(False)
        self.cat3_400den.setFlat(False)
        self.cat3_400den.setObjectName("cat3.400")
        self.cat3_400den.clicked.connect(
            lambda: self.openQuestion(self.cat3_400den))

        self.cat1_300den = QtWidgets.QPushButton(self.centralwidget)
        self.cat1_300den.setGeometry(QtCore.QRect(30, 150, 101, 41))
        self.cat1_300den.setAutoFillBackground(True)
        self.cat1_300den.setCheckable(True)
        self.cat1_300den.setChecked(False)
        self.cat1_300den.setAutoRepeat(False)
        self.cat1_300den.setAutoExclusive(False)
        self.cat1_300den.setAutoDefault(True)
        self.cat1_300den.setDefault(False)
        self.cat1_300den.setFlat(False)
        self.cat1_300den.setObjectName("cat1.300")
        self.cat1_300den.clicked.connect(
            lambda: self.openQuestion(self.cat1_300den))

        self.cat3_500den = QtWidgets.QPushButton(self.centralwidget)
        self.cat3_500den.setGeometry(QtCore.QRect(250, 250, 101, 41))
        self.cat3_500den.setAutoFillBackground(True)
        self.cat3_500den.setCheckable(True)
        self.cat3_500den.setChecked(False)
        self.cat3_500den.setAutoRepeat(False)
        self.cat3_500den.setAutoExclusive(False)
        self.cat3_500den.setAutoDefault(True)
        self.cat3_500den.setDefault(False)
        self.cat3_500den.setFlat(False)
        self.cat3_500den.setObjectName("cat3.500")
        self.cat3_500den.clicked.connect(
            lambda: self.openQuestion(self.cat3_500den))

        self.cat2_500den = QtWidgets.QPushButton(self.centralwidget)
        self.cat2_500den.setGeometry(QtCore.QRect(140, 250, 101, 41))
        self.cat2_500den.setAutoFillBackground(True)
        self.cat2_500den.setCheckable(True)
        self.cat2_500den.setChecked(False)
        self.cat2_500den.setAutoRepeat(False)
        self.cat2_500den.setAutoExclusive(False)
        self.cat2_500den.setAutoDefault(True)
        self.cat2_500den.setDefault(False)
        self.cat2_500den.setFlat(False)
        self.cat2_500den.setObjectName("cat2.500")
        self.cat2_500den.clicked.connect(
            lambda: self.openQuestion(self.cat2_500den))

        self.cat4_500den = QtWidgets.QPushButton(self.centralwidget)
        self.cat4_500den.setGeometry(QtCore.QRect(360, 250, 101, 41))
        self.cat4_500den.setAutoFillBackground(True)
        self.cat4_500den.setCheckable(True)
        self.cat4_500den.setChecked(False)
        self.cat4_500den.setAutoRepeat(False)
        self.cat4_500den.setAutoExclusive(False)
        self.cat4_500den.setAutoDefault(True)
        self.cat4_500den.setDefault(False)
        self.cat4_500den.setFlat(False)
        self.cat4_500den.setObjectName("cat4.500")
        self.cat4_500den.clicked.connect(
            lambda: self.openQuestion(self.cat4_500den))

        self.cat5_500den = QtWidgets.QPushButton(self.centralwidget)
        self.cat5_500den.setGeometry(QtCore.QRect(470, 250, 101, 41))
        self.cat5_500den.setAutoFillBackground(True)
        self.cat5_500den.setCheckable(True)
        self.cat5_500den.setChecked(False)
        self.cat5_500den.setAutoRepeat(False)
        self.cat5_500den.setAutoExclusive(False)
        self.cat5_500den.setAutoDefault(True)
        self.cat5_500den.setDefault(False)
        self.cat5_500den.setFlat(False)
        self.cat5_500den.setObjectName("cat5.500")
        self.cat5_500den.clicked.connect(
            lambda: self.openQuestion(self.cat5_500den))

        self.cat1_500den = QtWidgets.QPushButton(self.centralwidget)
        self.cat1_500den.setGeometry(QtCore.QRect(30, 250, 101, 41))
        self.cat1_500den.setAutoFillBackground(True)
        self.cat1_500den.setCheckable(True)
        self.cat1_500den.setChecked(False)
        self.cat1_500den.setAutoRepeat(False)
        self.cat1_500den.setAutoExclusive(False)
        self.cat1_500den.setAutoDefault(True)
        self.cat1_500den.setDefault(False)
        self.cat1_500den.setFlat(False)
        self.cat1_500den.setObjectName("cat1.500")
        self.cat1_500den.clicked.connect(
            lambda: self.openQuestion(self.cat1_500den))

        self.category1_lbl = QtWidgets.QLabel(self.centralwidget)
        self.category1_lbl.setGeometry(QtCore.QRect(40, 20, 81, 16))
        self.category1_lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.category1_lbl.setIndent(0)
        self.category1_lbl.setObjectName("category1_lbl")
        self.category1_lbl.setText(self.questions.GetCategoryName(0))

        self.category2_lbl = QtWidgets.QLabel(self.centralwidget)
        self.category2_lbl.setGeometry(QtCore.QRect(150, 20, 81, 16))
        self.category2_lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.category2_lbl.setIndent(0)
        self.category2_lbl.setObjectName("category2_lbl")
        self.category2_lbl.setText(self.questions.GetCategoryName(1))

        self.category3_lbl = QtWidgets.QLabel(self.centralwidget)
        self.category3_lbl.setGeometry(QtCore.QRect(260, 20, 81, 16))
        self.category3_lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.category3_lbl.setIndent(0)
        self.category3_lbl.setObjectName("category3_lbl")
        self.category3_lbl.setText(self.questions.GetCategoryName(2))

        self.category4_lbl = QtWidgets.QLabel(self.centralwidget)
        self.category4_lbl.setGeometry(QtCore.QRect(370, 20, 81, 16))
        self.category4_lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.category4_lbl.setIndent(0)
        self.category4_lbl.setObjectName("category4_lbl")
        self.category4_lbl.setText(self.questions.GetCategoryName(3))

        self.category5_lbl = QtWidgets.QLabel(self.centralwidget)
        self.category5_lbl.setGeometry(QtCore.QRect(480, 20, 81, 16))
        self.category5_lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.category5_lbl.setIndent(0)
        self.category5_lbl.setObjectName("category5_lbl")
        self.category5_lbl.setText(self.questions.GetCategoryName(4))

        self.result_lbl = QtWidgets.QLabel(self.centralwidget)
        self.result_lbl.setGeometry(QtCore.QRect(30, 300, 61, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.result_lbl.setFont(font)
        self.result_lbl.setObjectName("result_lbl")

        self.team1_txt_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team1_txt_lbl.setGeometry(QtCore.QRect(30, 320, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team1_txt_lbl.setFont(font)
        self.team1_txt_lbl.setObjectName("team1_txt_lbl")

        self.team2_txt_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team2_txt_lbl.setGeometry(QtCore.QRect(30, 340, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team2_txt_lbl.setFont(font)
        self.team2_txt_lbl.setObjectName("team2_txt_lbl")

        self.team3_txt_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team3_txt_lbl.setGeometry(QtCore.QRect(30, 360, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team3_txt_lbl.setFont(font)
        self.team3_txt_lbl.setObjectName("team3_txt_lbl")

        self.team1_score_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team1_score_lbl.setGeometry(QtCore.QRect(80, 320, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team1_score_lbl.setFont(font)
        self.team1_score_lbl.setObjectName("team1_score_lbl")

        self.team2_score_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team2_score_lbl.setGeometry(QtCore.QRect(80, 340, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team2_score_lbl.setFont(font)
        self.team2_score_lbl.setObjectName("team2_score_lbl")

        self.team3_score_lbl = QtWidgets.QLabel(self.centralwidget)
        self.team3_score_lbl.setGeometry(QtCore.QRect(80, 360, 41, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.team3_score_lbl.setFont(font)
        self.team3_score_lbl.setObjectName("team3_score_lbl")

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(
            _translate(
                "MainWindow",
                "Џеопарди (Jeopardy) | Player: {}".format(str(self.playerId))))
        self.cat1_100den.setText(_translate("MainWindow", "100 ден."))
        self.cat2_100den.setText(_translate("MainWindow", "100 ден."))
        self.cat3_100den.setText(_translate("MainWindow", "100 ден."))
        self.cat4_100den.setText(_translate("MainWindow", "100 ден."))
        self.cat5_100den.setText(_translate("MainWindow", "100 ден."))
        self.cat4_200den.setText(_translate("MainWindow", "200 ден."))
        self.cat3_200den.setText(_translate("MainWindow", "200 ден."))
        self.cat2_200den.setText(_translate("MainWindow", "200 ден."))
        self.cat5_200den.setText(_translate("MainWindow", "200 ден."))
        self.cat1_200den.setText(_translate("MainWindow", "200 ден."))
        self.cat4_400den.setText(_translate("MainWindow", "400 ден."))
        self.cat4_300den.setText(_translate("MainWindow", "300 ден."))
        self.cat2_400den.setText(_translate("MainWindow", "400 ден."))
        self.cat3_300den.setText(_translate("MainWindow", "300 ден."))
        self.cat2_300den.setText(_translate("MainWindow", "300 ден."))
        self.cat1_400den.setText(_translate("MainWindow", "400 ден."))
        self.cat5_300den.setText(_translate("MainWindow", "300 ден."))
        self.cat5_400den.setText(_translate("MainWindow", "400 ден."))
        self.cat3_400den.setText(_translate("MainWindow", "400 ден."))
        self.cat1_300den.setText(_translate("MainWindow", "300 ден."))
        self.cat3_500den.setText(_translate("MainWindow", "500 ден."))
        self.cat2_500den.setText(_translate("MainWindow", "500 ден."))
        self.cat4_500den.setText(_translate("MainWindow", "500 ден."))
        self.cat5_500den.setText(_translate("MainWindow", "500 ден."))
        self.cat1_500den.setText(_translate("MainWindow", "500 ден."))
        self.result_lbl.setText(_translate("MainWindow", "Резултат:"))
        self.team1_txt_lbl.setText(_translate("MainWindow", "ТИМ 1: "))
        self.team2_txt_lbl.setText(_translate("MainWindow", "ТИМ 2:"))
        self.team3_txt_lbl.setText(_translate("MainWindow", "ТИМ 3: "))
        self.team1_score_lbl.setText(_translate("MainWindow", "0"))
        self.team2_score_lbl.setText(_translate("MainWindow", "0"))
        self.team3_score_lbl.setText(_translate("MainWindow", "0"))

        self.thread_scores.start()
Beispiel #2
0
class Client(threading.Thread):
    def __init__(self, socket, address, id, name, signal):
        threading.Thread.__init__(self)
        self.socket = socket
        self.address = address
        self.id = id
        self.name = name
        self.signal = signal
        self.score = 0
        self.questions = Questions()
        self.askedQuestionCtg = ""
        self.askedQuestionNum = ""
        self.askedQuestionCode = ""

    def __str__(self):
        return str(self.id) + " " + str(self.address)

    def id(self):
        return self.id

    def run(self):
        while self.signal:
            try:
                data = self.socket.recv(2048)
            except:
                print("Client " + str(self.address) + " has disconnected\n")
                self.signal = False
                connections.remove(self)
                break
            # ako stignalo neshto
            if data != "":
                decoded_data = str(data.decode("utf-8"))

                # return scores from all players
                if decoded_data == "get_scores":
                    data_rsp = json.dumps(scores)
                    #print("server response data= ", data_rsp)
                    connections[self.id].socket.sendall(data_rsp.encode())

                elif decoded_data == "get_answered_questions":
                    data_rsp = json.dumps(answered_questions)
                    connections[self.id].socket.sendall(data_rsp.encode())

                # answer response (in json format)
                elif is_json(data.decode("utf-8")):
                    json_data = json.loads(data.decode("utf-8"))

                    print("Received from client\n")
                    print(json_data)
                    print("Q: ", json_data.get("question"))
                    print("A: ", json_data.get("answer"))

                    corr_answer = self.questions.GetCorrectAnswer(self.askedQuestionCtgName, \
                                                                  int(self.askedQuestionNum), \
                                                                  self.askedQuestionCode)

                    print("Corr answer is: ", corr_answer)
                    print("User's answer is: ", json_data.get("answer"))

                    if json_data.get("answer") == corr_answer:
                        print("Correct answer!\n")
                        data_rsp = json.dumps({"a": "correct"})
                        connections[self.id].socket.sendall(data_rsp.encode())
                        # update global scores
                        scores["p" + str(self.id)] += 100
                    else:
                        print("Incorrect answer!\n")
                        data_rsp = json.dumps({"a": "incorrect"})
                        connections[self.id].socket.sendall(data_rsp.encode())
                        # update global scores
                        scores["p" + str(self.id)] -= 100

                    print("Answered ", self.askedQuestionCode)
                    answered = "cat" + str(
                        self.askedQuestionNum
                    ) + "." + self.askedQuestionCode[-3:]
                    answered_questions.append(answered)

                    print(scores)

                    print("ID " + str(self.id) + ": sent: " +
                          str(data.decode("utf-8")))

                # if set data has "q_" prefix then it is question from client!
                elif decoded_data[0:2] == "q_":
                    print("Decoded data: ", decoded_data)
                    cat_num = str(data.decode("utf-8"))[5]
                    print("Asked Question is from category: ", cat_num)

                    category_name = self.questions.GetCategoryName(
                        int(cat_num) - 1)
                    print("Question is from category: ", category_name)
                    self.askedQuestionCtgName = category_name

                    questions_cat = self.questions.GetAllQuestionsFromCategory(
                        category_name)
                    print("All questions from this category: ", questions_cat)

                    question_number = decoded_data[-3]
                    print("Question number is ", question_number)
                    self.askedQuestionNum = question_number

                    print("Question content is:")
                    q_str = "q" + question_number + "." + question_number + "00"
                    print(q_str)
                    self.askedQuestionCode = q_str

                    print("QA: ", questions_cat[int(question_number) - 1])
                    print("--------------------------------------")

                    question_content = questions_cat[int(question_number) -
                                                     1].get(str(q_str)).get(
                                                         "content")
                    print(question_content)

                    print("Possible answers:\n")
                    question_answers = questions_cat[int(question_number) -
                                                     1].get(str(q_str)).get(
                                                         "possible_answers")
                    print(list(question_answers))

                    # Send question
                    q_data = json.dumps({
                        "q": question_content,
                        "a": list(question_answers)[0],
                        "b": list(question_answers)[1],
                        "c": list(question_answers)[2]
                    })
                    print(q_data)
                    connections[self.id].socket.sendall(q_data.encode())
                # elif decoded_data == "get_answered":
                #     data_ = pickle.dumps(answered_questions)
                #     connections[self.id].socket.sendall(data_)
                else:
                    pass