Beispiel #1
0
    def initUI(self):
        # Set up the main grid
        self.setStyleSheet("QGroupBox { font-size: 32px; font-weight: bold; }")
        self.widgetLayout = QGridLayout(self)
        self.roundGroup = QGroupBox(self)
        self.widgetLayout.addWidget(self.roundGroup, 0, 0)
        self.matchGroup = QGroupBox(self)
        self.widgetLayout.addWidget(self.matchGroup, 0, 1)

        # Round Group
        self.roundLayout = QVBoxLayout(self.roundGroup)
        self.buttonGroupLayout = QHBoxLayout()
        self.roundLayout.addLayout(self.buttonGroupLayout)

        self.cancelMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.cancelMatchButton)
        self.cancelMatchButton.clicked.connect(self.cancelMatch)

        self.restartMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.restartMatchButton)
        self.restartMatchButton.clicked.connect(self.restartMatch)

        self.pauseMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.pauseMatchButton)
        self.pauseMatchButton.clicked.connect(self.pauseMatch)

        self.playerOrderButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.playerOrderButton)
        self.playerOrderButton.clicked.connect(self.changePlayerOrder)

        self.commitRoundButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.commitRoundButton)
        self.commitRoundButton.clicked.connect(self.commitRound)

        self.gameStatusLabel = QLabel(self.roundGroup)
        self.gameStatusLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.roundLayout.addWidget(self.gameStatusLabel)

        # Match Group
        self.matchGroupLayout = QVBoxLayout(self.matchGroup)

        self.clock = GameClock(self.engine.getGameSeconds(), self)
        self.clock.setMinimumHeight(100)
        self.matchGroupLayout.addWidget(self.clock)

        dpolicy = self.engine.getDealingPolicy()
        if dpolicy not in (self.engine.NoDealer, self.engine.StarterDealer):
            self.dealerPolicyCheckBox = QCheckBox(self.matchGroup)
            if self.engine.getDealingPolicy() == self.engine.WinnerDealer:
                self.dealerPolicyCheckBox.setChecked(True)
            else:
                self.dealerPolicyCheckBox.setChecked(False)
            self.dealerPolicyCheckBox.setStyleSheet(
                "QCheckBox { font-weight: bold; }")
            self.dealerPolicyCheckBox.stateChanged.connect(
                self.changeDealingPolicy)
            self.dealerPolicyCheckBox.setDisabled(
                self.engine.getNumRound() > 1)
            self.matchGroupLayout.addWidget(self.dealerPolicyCheckBox)
Beispiel #2
0
class GameWidget(Tab):

    def __init__(self, game, players, engine=None, parent=None):
        super(GameWidget, self).__init__(parent)
        self.game = game
        if engine is not None:
            self.engine = engine
            self.players = self.engine.getListPlayers()
        else:
            self.players = players
            self.createEngine()
            for nick in players:
                self.engine.addPlayer(nick)
            self.engine.begin()
        self.engine.printStats()
        self.gameInput = None
        self.finished = False
        self.toggleScreenLock()
        self.initUI()

    def initUI(self):
        # Set up the main grid
        self.setStyleSheet("QGroupBox { font-size: 32px; font-weight: bold; }")
        self.widgetLayout = QGridLayout(self)
        self.roundGroup = QGroupBox(self)
        self.widgetLayout.addWidget(self.roundGroup, 0, 0)
        self.matchGroup = QGroupBox(self)
        self.widgetLayout.addWidget(self.matchGroup, 0, 1)

        # Round Group
        self.roundLayout = QVBoxLayout(self.roundGroup)
        self.buttonGroupLayout = QHBoxLayout()
        self.roundLayout.addLayout(self.buttonGroupLayout)

        self.cancelMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.cancelMatchButton)
        self.cancelMatchButton.clicked.connect(self.cancelMatch)

        self.restartMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.restartMatchButton)
        self.restartMatchButton.clicked.connect(self.restartMatch)

        self.pauseMatchButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.pauseMatchButton)
        self.pauseMatchButton.clicked.connect(self.pauseMatch)

        self.playerOrderButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.playerOrderButton)
        self.playerOrderButton.clicked.connect(self.changePlayerOrder)

        self.commitRoundButton = QPushButton(self.roundGroup)
        self.buttonGroupLayout.addWidget(self.commitRoundButton)
        self.commitRoundButton.clicked.connect(self.commitRound)

        self.gameStatusLabel = QLabel(self.roundGroup)
        self.gameStatusLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.roundLayout.addWidget(self.gameStatusLabel)

        # Match Group
        self.matchGroupLayout = QVBoxLayout(self.matchGroup)

        self.clock = GameClock(self.engine.getGameSeconds(), self)
        self.clock.setMinimumHeight(100)
        self.matchGroupLayout.addWidget(self.clock)

        dpolicy = self.engine.getDealingPolicy()
        if dpolicy not in (self.engine.NoDealer, self.engine.StarterDealer):
            self.dealerPolicyCheckBox = QCheckBox(self.matchGroup)
            if self.engine.getDealingPolicy() == self.engine.WinnerDealer:
                self.dealerPolicyCheckBox.setChecked(True)
            else:
                self.dealerPolicyCheckBox.setChecked(False)
            self.dealerPolicyCheckBox.setStyleSheet(
                "QCheckBox { font-weight: bold; }")
            self.dealerPolicyCheckBox.stateChanged.connect(
                self.changeDealingPolicy)
            self.dealerPolicyCheckBox.setDisabled(
                self.engine.getNumRound() > 1)
            self.matchGroupLayout.addWidget(self.dealerPolicyCheckBox)

    def retranslateUI(self):
        self.setRoundTitle()
        self.pauseMatchButton.setText(
            i18n("GameWidget", "&Pause/Play"))
        self.cancelMatchButton.setText(
            i18n("GameWidget", "&Cancel Match"))
        self.restartMatchButton.setText(
            i18n("GameWidget", "Restart &Match"))
        self.commitRoundButton.setText(
            i18n("GameWidget", "Commit &Round"))
        self.playerOrderButton.setText(
            i18n("GameWidget", "Player &Order"))
        if self.engine.getDealingPolicy() not in (
                self.engine.NoDealer, self.engine.StarterDealer):
            self.dealerPolicyCheckBox.setText(
                i18n("GameWidget", "Winner deals"))
        self.updateGameStatusLabel()

    def updateGameStatusLabel(self):
        self.gameStatusLabel.setStyleSheet(
            "QLabel { font-size: 16px; font-weight:bold; color: red;}")
        winner = self.engine.getWinner()
        if winner:
            self.gameStatusLabel.setText(i18n(
                "GameWidget", "{} won this match!").format(winner))
        elif self.engine.isPaused():
            self.gameStatusLabel.setText(
                i18n("GameWidget", "Game is paused"))
        else:
            self.gameStatusLabel.setText(
                i18n("GameWidget", ""))

    def cancelMatch(self):
        if not self.isFinished():
            tit = i18n("GameWidget", 'Cancel Match')
            msg = i18n(
                'GameWidget', 'Do you want to save the current {} match?')
            msg = msg.format(self.game)
            ret = QMessageBox.question(self, tit, msg,
                                       QMessageBox.Yes | QMessageBox.No |
                                       QMessageBox.Cancel, QMessageBox.Cancel)

            if ret == QMessageBox.Cancel:
                return
            if ret == QMessageBox.No:
                self.closeMatch()
            else:
                self.saveMatch()

        self.toggleScreenLock(True)
        self.requestClose()

    def restartMatch(self):
        if not self.isFinished():
            tit = i18n("GameWidget", 'Restart Match')
            msg = i18n(
                'GameWidget', 'Do you want to save the current {} match?')
            msg = msg.format(self.game)
            ret = QMessageBox.question(self, tit, msg,
                                       QMessageBox.Yes | QMessageBox.No |
                                       QMessageBox.Cancel,
                                       QMessageBox.Cancel)

            if ret == QMessageBox.Cancel:
                return
            if ret == QMessageBox.Yes:
                self.saveMatch()
        self.toggleScreenLock(True)
        self.requestRestart()

    def pauseMatch(self):
        if self.engine.isPaused():
            self.clock.unpauseTimer()
            self.commitRoundButton.setEnabled(True)
            self.gameInput.setEnabled(True)
            self.engine.unpause()
            self.toggleScreenLock()
        else:
            self.clock.pauseTimer()
            self.commitRoundButton.setDisabled(True)
            self.gameInput.setDisabled(True)
            self.engine.pause()
            self.toggleScreenLock(True)
        self.updateGameStatusLabel()

    def commitRound(self):

        nround = self.engine.getNumRound()
        print("Opening round {}".format(nround))
        self.engine.openRound(nround)
        winner = self.gameInput.getWinner()
        if not winner:
            msg = i18n("GameWidget", "No winner selected")
            QMessageBox.warning(self, self.game, msg)

            return
        else:
            self.engine.setRoundWinner(winner)
        scores = self.gameInput.getScores()
        for player, score in scores.items():
            if not self.checkPlayerScore(player, score):
                msg = i18n(
                    "GameWidget", "{0} score is not valid").format(player)
                QMessageBox.warning(self, self.game, msg)
                return
            extras = self.getPlayerExtraInfo(player)
            if extras is None:
                return
            self.engine.addRoundInfo(player, score, extras)

        # Everything ok so far, let's confirm
        tit = i18n("GameWidget", 'Commit Round')
        msg = i18n(
            "GameWidget", "Are you sure you want to commit the current round?")
        ret = QMessageBox.question(self, tit, msg,
                                   QMessageBox.Yes | QMessageBox.No,
                                   QMessageBox.Yes)

        if ret == QMessageBox.No:
            return

        # Once here, we can commit round
        self.unsetDealer()
        self.engine.commitRound()
        self.engine.printStats()
        self.updatePanel()
        if not self.engine.getWinner():
            self.setDealer()

    def changeDealingPolicy(self, *args, **kwargs):
        if self.dealerPolicyCheckBox.isChecked():
            self.engine.setDealingPolicy(self.engine.WinnerDealer)
        else:
            self.engine.setDealingPolicy(self.engine.RRDealer)

    def closeMatch(self): self.engine.cancelMatch()

    def saveMatch(self): self.engine.save()

    def checkPlayerScore(self, player, score):
        if score >= 0:
            return True
        else:
            return False

    def setRoundTitle(self):
        try:
            nround = self.engine.getNumRound()
            self.roundGroup.setTitle(i18n(
                "GameWidget", "Round {0}").format(str(nround)))
        except AttributeError:
            pass

    def updatePanel(self):
        self.gameInput.reset()
        try:
            self.dealerPolicyCheckBox.setEnabled(False)
        except AttributeError:
            pass
        if self.engine.getWinner():
            self.setWinner()
        else:
            self.setRoundTitle()

    def getGameName(self): return self.game

    def isFinished(self): return self.finished

    # To be implemented in subclasses
    def createEngine(self): pass

    def getPlayerExtraInfo(self, player): return {}

    def unsetDealer(self): pass

    def setDealer(self): pass

    def setWinner(self):
        self.finished = True
        self.pauseMatchButton.setDisabled(True)
        self.clock.stopTimer()
        self.commitRoundButton.setDisabled(True)
        self.playerOrderButton.setDisabled(True)
        self.updateGameStatusLabel()
        self.gameInput.setDisabled(True)
        self.toggleScreenLock(True)

    def changePlayerOrder(self):
        originaldealer = self.engine.getDealer()
        pod = PlayerOrderDialog(self.engine, self)
#         pod.dealerChanged.connect(self.changedDealer)
        if pod.exec_():
            newdealer = pod.getNewDealer()
            neworder = pod.getNewOrder()
            if self.players != neworder:
                # Do something
                self.engine.setListPlayers(neworder)
                self.players = neworder
                self.updatePlayerOrder()
            if originaldealer != newdealer:
                self.unsetDealer()
                self.engine.setDealer(newdealer)
                self.setDealer()

    def updatePlayerOrder(self):
        self.gameInput.updatePlayerOrder()

    def toggleScreenLock(self, on=False):
        ES_CONTINUOUS = 0x80000000
        ES_DISPLAY_REQUIRED = 0x00000002
        try:
            if not on:
                ctypes.windll.kernel32.SetThreadExecutionState(
                    ES_CONTINUOUS | ES_DISPLAY_REQUIRED)
                print("Disabled Screensaver")
            else:
                ctypes.windll.kernel32.SetThreadExecutionState(0)
                print("Enabled Screensaver")
        except Exception:
            pass