示例#1
0
class GuiRecapitulatif(QtGui.QDialog):
    """
    Dialog for the summary, for repeated game or one-shot.
    If ecran_historique is set it replaces the default GuiHistorique
    """

    def __init__(self, remote, defered, automatique, parent, period, historique,
                 summary_text, triangle_transactions, star_transactions):
        """

        :param defered:
        :param automatique:
        :param parent:
        :param period:
        :param historique:
        :param summary_text:
        :param history_screen:
        :param size_histo: the size of the history table. The width of the
        explanation area will be the same than the width of the history table
        :return:
        """
        super(GuiRecapitulatif, self).__init__(parent)

        self._remote = remote
        self._defered = defered
        self._automatique = automatique

        layout = QtGui.QVBoxLayout(self)

        # period label and history button --------------------------------------
        self.ecran_historique = GuiHistorique(
            self, historique, size=SIZE_HISTO)
        layout_period = QtGui.QHBoxLayout()
        label_period = QtGui.QLabel(le2mtrans("Period") + " {}".format(period))
        layout_period.addWidget(label_period)
        layout_period.addSpacerItem(
            QtGui.QSpacerItem(20, 5, QtGui.QSizePolicy.Expanding,
                              QtGui.QSizePolicy.Fixed))
        button_history = QtGui.QPushButton(le2mtrans("History"))
        button_history.clicked.connect(self.ecran_historique.show)
        layout_period.addWidget(button_history)
        layout.addLayout(layout_period)

        # timer
        self._compte_rebours = WCompterebours(
            parent=self, temps=pms.SUMMARY_TIME, actionfin=self._display_warning)
        layout.addWidget(self._compte_rebours)

        # explanation zone -----------------------------------------------------
        self.widexplication = WExplication(text=summary_text, parent=self,
                                           size=(SIZE_HISTO[0], 80))
        layout.addWidget(self.widexplication)

        # transactions ---------------------------------------------------------
        try:
            max_triangle_price = max(triangle_transactions.MRI_trans_price)
        except ValueError:  # no transaction
            max_triangle_price = 1
        try:
            max_star_price = max(star_transactions.MRI_trans_price)
        except ValueError:
            max_star_price = 1
        max_price = max(max_triangle_price, max_star_price)

        transactions_layout = QtGui.QGridLayout()
        layout.addLayout(transactions_layout)

        # triangle ---
        triangle_label = QtGui.QLabel(trans_MRI(u"Triangle"))
        triangle_label.setStyleSheet("font-weight: bold;")
        transactions_layout.addWidget(triangle_label, 0, 0)
        self._triangle_transaction_zone = TransactionZone(zone_size=(450, 250))
        try:
            for i, item in triangle_transactions.iterrows():
                price = item.MRI_trans_price
                buyer = item.MRI_trans_buyer
                seller = item.MRI_trans_seller
                implied, buyer_or_seller = False, None
                if buyer == self._remote.le2mclt.uid or \
                                seller == self._remote.le2mclt.uid:
                    implied = True
                    buyer_or_seller = pms.BUYER if \
                        buyer == self._remote.le2mclt.uid else pms.SELLER
                color = "blue" if implied else "black"
                self._triangle_transaction_zone.add_transaction(
                    price, buyer_or_seller, color)
        except ValueError:  # no transactions
            pass
        transactions_layout.addWidget(self._triangle_transaction_zone, 1, 0)
        self._triangle_transactions_graph = GraphicalZone(
            triangle_transactions, max_price, pms.TRIANGLE, zone_size=(450, 250))
        transactions_layout.addWidget(self._triangle_transactions_graph, 2, 0)

        # star ---
        star_label = QtGui.QLabel(trans_MRI(u"Star"))
        star_label.setStyleSheet("font-weight: bold;")
        transactions_layout.addWidget(star_label, 0, 2)
        self._star_transaction_zone = TransactionZone(zone_size=(450, 250))
        try:
            for i, item in star_transactions.iterrows():
                price = item.MRI_trans_price
                buyer = item.MRI_trans_buyer
                seller = item.MRI_trans_seller
                implied, buyer_or_seller = False, None
                if buyer == self._remote.le2mclt.uid or \
                                seller == self._remote.le2mclt.uid:
                    implied = True
                    buyer_or_seller = pms.BUYER if \
                        buyer == self._remote.le2mclt.uid else pms.SELLER
                color = "blue" if implied else "black"
                self._star_transaction_zone.add_transaction(
                    price, buyer_or_seller, color)
        except ValueError:  # no transactions
            pass
        transactions_layout.addWidget(self._star_transaction_zone, 1, 2)
        self._star_transactions_graph = GraphicalZone(
            star_transactions, max_price, pms.STAR, zone_size=(450, 250))
        transactions_layout.addWidget(self._star_transactions_graph, 2, 2)

        separator = QtGui.QFrame()
        separator.setFrameShape(QtGui.QFrame.VLine)
        transactions_layout.addWidget(
            separator, 0, 1, transactions_layout.rowCount(), 1)

        # history table --------------------------------------------------------
        # in this screen we only keep the header and the last line of the
        # history
        histo_recap = [historique[0], historique[-1]]
        self.tablemodel = TableModelHistorique(histo_recap)
        self.widtableview = WTableview(parent=self, tablemodel=self.tablemodel,
                                       size=(SIZE_HISTO[0], 100))
        self.widtableview.ui.tableView.verticalHeader().setResizeMode(
            QtGui.QHeaderView.Stretch)
        layout.addWidget(self.widtableview)

        buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok)
        buttons.accepted.connect(self._accept)
        layout.addWidget(buttons)

        # automatique
        if self._automatique:
            self._timer_automatique = QtCore.QTimer()
            self._timer_automatique.timeout.connect(
                buttons.button(QtGui.QDialogButtonBox.Ok).click)
            self._timer_automatique.start(7000)

        # taille et titre
        self.setWindowTitle(le2mtrans(u"Summary"))
        # self.adjustSize()
        # self.setFixedSize(self.size())

    def _accept(self):
        """
        :return:
        """
        try:
            self._timer_automatique.stop()
        except AttributeError:
            pass
        try:
            self._compte_rebours.stop()
        except AttributeError:
            pass
        logger.info(u"callback: Ok summary")
        self._defered.callback(1)
        self.accept()

    def reject(self):
        pass

    def _display_warning(self):
        self._compte_rebours.setStyleSheet("color: red;")
示例#2
0
class GuiDecision(QtGui.QDialog):
    def __init__(self, defered, automatique, parent, period, historique, remote):
        super(GuiDecision, self).__init__(parent)

        # variables
        self._defered = defered
        self._automatique = automatique
        self._remote = remote

        layout = QtGui.QVBoxLayout(self)

        self._historique = GuiHistorique(self, historique, size=SIZE_HISTO)
        wperiod = WPeriod(
            period=period, ecran_historique=self._historique)
        layout.addWidget(wperiod)

        wexplanation = WExplication(
            text=texts_MRI.get_text_explanation(
                self._remote.balance_if_triangle, self._remote.balance_if_star),
            size=(SIZE_HISTO[0], 70), parent=self)
        layout.addWidget(wexplanation)

        # Compte à rebours =====================================================
        self._compte_rebours = WCompterebours(
            parent=self, temps=pms.MARKET_TIME, actionfin=self._accept)
        self._compte_rebours.setStyleSheet("color: blue;")
        layout.addWidget(self._compte_rebours)

        # zone information actualisée ==========================================
        self._information = InformationZone()
        layout.addWidget(self._information)

        # market ===============================================================
        market_layout = QtGui.QGridLayout()
        layout.addLayout(market_layout)

        # triangle
        triangle_label = QtGui.QLabel(trans_MRI(u"Triangle"))
        triangle_label.setStyleSheet("color: red;")
        market_layout.addWidget(triangle_label, 0, 0, 1, 2)
        self._triangle_purchase_zone = OfferZone(pms.BUY, zone_size=(350, 250))
        market_layout.addWidget(self._triangle_purchase_zone, 1, 0)
        self._triangle_sell_zone = OfferZone(pms.SELL, zone_size=(350, 250))
        market_layout.addWidget(self._triangle_sell_zone, 1, 1)
        self._triangle_transactions = TransactionZone(zone_size=(350, 250))
        market_layout.addWidget(self._triangle_transactions, 2, 0, 1, 2)

        # star
        star_label = QtGui.QLabel(trans_MRI(u"Star"))
        star_label.setStyleSheet("color: red;")
        market_layout.addWidget(star_label, 0, 3, 1, 2)
        self._star_purchase_zone = OfferZone(pms.BUY, zone_size=(350, 250))
        market_layout.addWidget(self._star_purchase_zone, 1, 3)
        self._star_sell_zone = OfferZone(pms.SELL, zone_size=(350, 250))
        market_layout.addWidget(self._star_sell_zone, 1, 4)
        self._star_transactions = TransactionZone(zone_size=(350, 250))
        market_layout.addWidget(self._star_transactions, 2, 3, 1, 2)

        separator = QtGui.QFrame()
        separator.setFrameShape(QtGui.QFrame.VLine)
        market_layout.addWidget(separator, 0, 2, market_layout.rowCount(), 1)

        # finalisation =========================================================
        layout.addSpacing(50)
        self._make_connections()
        self.setWindowTitle(trans_MRI(u"Market"))
        # self.adjustSize()
        # self.setFixedSize(self.size())

        if self._automatique:
            self._timer_automatique = QtCore.QTimer()
            self._timer_automatique.timeout.connect(self._play_auto)
            self._timer_automatique.start(randint(2000, 7000))
                
    def _make_connections(self):
        """
        Connect the pushbutton of the different offer zones to the method
        of GUIDecision (_add_offer)
        :return:
        """
        # send offer ===========================================================
        # TRIANGLE
        self._triangle_purchase_zone.pushbutton_send.clicked.connect(
            lambda _: self._add_offer(
                pms.TRIANGLE, pms.BUY,
                self._triangle_purchase_zone.spin_offer.value(),
        self._triangle_purchase_zone.spin_offer))
        self._triangle_purchase_zone.spin_offer.valueChanged.connect(
            lambda _: self._triangle_purchase_zone.pushbutton_send.setToolTip(
                self._remote.get_hypothetical_balance(
                    {"MRI_offer_contract": pms.TRIANGLE,
                     "MRI_offer_type": pms.BUY,
                     "MRI_offer_price": self._triangle_purchase_zone.spin_offer.value()})
            ))
        self._triangle_sell_zone.pushbutton_send.clicked.connect(
            lambda _: self._add_offer(
                pms.TRIANGLE, pms.SELL,
                self._triangle_sell_zone.spin_offer.value(),
                self._triangle_sell_zone.spin_offer))
        self._triangle_sell_zone.spin_offer.valueChanged.connect(
            lambda _: self._triangle_sell_zone.pushbutton_send.setToolTip(
                self._remote.get_hypothetical_balance(
                    {"MRI_offer_contract": pms.TRIANGLE,
                     "MRI_offer_type": pms.SELL,
                     "MRI_offer_price": self._triangle_sell_zone.spin_offer.value()}
                )))
        # STAR
        self._star_purchase_zone.pushbutton_send.clicked.connect(
            lambda _: self._add_offer(
                pms.STAR, pms.BUY,
                self._star_purchase_zone.spin_offer.value(),
                self._star_purchase_zone.spin_offer))
        self._star_purchase_zone.spin_offer.valueChanged.connect(
            lambda _: self._star_purchase_zone.pushbutton_send.setToolTip(
                self._remote.get_hypothetical_balance(
                    {"MRI_offer_contract": pms.STAR,
                     "MRI_offer_type": pms.BUY,
                     "MRI_offer_price": self._star_purchase_zone.spin_offer.value()}
                )))
        self._star_sell_zone.pushbutton_send.clicked.connect(
            lambda _: self._add_offer(
                pms.STAR, pms.SELL, self._star_sell_zone.spin_offer.value(),
                self._star_sell_zone.spin_offer))
        self._star_sell_zone.spin_offer.valueChanged.connect(
            lambda _: self._star_sell_zone.pushbutton_send.setToolTip(
                self._remote.get_hypothetical_balance(
                    {"MRI_offer_contract": pms.STAR,
                     "MRI_offer_type": pms.SELL,
                     "MRI_offer_price": self._star_sell_zone.spin_offer.value()}
                )))
        
        # remove offer =========================================================
        self._triangle_purchase_zone.pushbutton_remove.clicked.connect(
            lambda _: self._remove_offer(pms.TRIANGLE, pms.BUY))
        self._triangle_sell_zone.pushbutton_remove.clicked.connect(
            lambda _: self._remove_offer(pms.TRIANGLE, pms.SELL))
        self._star_purchase_zone.pushbutton_remove.clicked.connect(
            lambda _: self._remove_offer(pms.STAR, pms.BUY))
        self._star_sell_zone.pushbutton_remove.clicked.connect(
            lambda _: self._remove_offer(pms.STAR, pms.SELL))

        # offer selected =======================================================
        self._triangle_purchase_zone.offer_selected.connect(
            lambda offer: self._triangle_purchase_zone.pushbutton_accept.setToolTip(
                self._remote.get_hypothetical_balance(offer, accept=True)))
        self._triangle_sell_zone.offer_selected.connect(
            lambda offer: self._triangle_sell_zone.pushbutton_accept.setToolTip(
                self._remote.get_hypothetical_balance(offer, accept=True)))
        self._star_purchase_zone.offer_selected.connect(
            lambda offer: self._star_purchase_zone.pushbutton_accept.setToolTip(
                self._remote.get_hypothetical_balance(offer, accept=True)))
        self._star_sell_zone.offer_selected.connect(
            lambda offer: self._star_sell_zone.pushbutton_accept.setToolTip(
                self._remote.get_hypothetical_balance(offer, accept=True)))

        # accept selected offer ================================================
        self._triangle_purchase_zone.pushbutton_accept.clicked.connect(
            lambda _: self._accept_selected_offer(
                pms.TRIANGLE, self._triangle_purchase_zone.current_offer))
        self._triangle_sell_zone.pushbutton_accept.clicked.connect(
            lambda _: self._accept_selected_offer(
                pms.TRIANGLE, self._triangle_sell_zone.current_offer))
        self._star_purchase_zone.pushbutton_accept.clicked.connect(
            lambda _: self._accept_selected_offer(
                pms.STAR, self._star_purchase_zone.current_offer))
        self._star_sell_zone.pushbutton_accept.clicked.connect(
            lambda _: self._accept_selected_offer(
                pms.STAR, self._star_sell_zone.current_offer))

    def reject(self):
        pass
    
    def _accept(self):
        try:
            self._timer_automatique.stop()
        except AttributeError:
            pass
        logger.info(u"Ok")
        self.accept()
        self._defered.callback(True)

    @defer.inlineCallbacks
    def _add_offer(self, triangle_or_star, buy_or_sell, price,
                   spin_offer_sender=None):
        """
        send the offer to the server
        called by pushbutton_send of the offer zone
        """
        logger.info("add_offer: contract {} - type {} - price {}".format(
            triangle_or_star, buy_or_sell, price))
        # we test whether there exists an offer with the same price
        if triangle_or_star == pms.TRIANGLE:
            if buy_or_sell == pms.BUY:
                existing_offer = self._triangle_sell_zone.exists_offer(
                    price, self._remote.le2mclt.uid)
            else:
                existing_offer = self._triangle_purchase_zone.exists_offer(
                    price, self._remote.le2mclt.uid)
        else:
            if buy_or_sell == pms.BUY:
                existing_offer = self._star_sell_zone.exists_offer(
                    price, self._remote.le2mclt.uid)
            else:
                existing_offer = self._star_purchase_zone.exists_offer(
                    price, self._remote.le2mclt.uid)

        # if the existing is player's own offer we cancel this existing offer
        if existing_offer:
            if existing_offer["MRI_offer_sender"] == self._remote.le2mclt.uid:
                existing_offer = False

        if existing_offer:
            existing_offer["MRI_offer_contract"] = triangle_or_star
            existing_offer["MRI_offer_type"] = \
                pms.SELL if buy_or_sell == pms.BUY else pms.BUY
            yield (self._accept_selected_offer(triangle_or_star,
                                               existing_offer))
        else:
            offer = {"MRI_offer_contract": triangle_or_star,
                     "MRI_offer_type": buy_or_sell,
                     "MRI_offer_price": price,
                     "MRI_offer_sender_type": pms.PRICE_MAKER}
            if self._remote.is_offer_ok(offer):
                yield (self._remote.add_offer(offer))
            else:
                if not self._automatique:
                    self._display_offer_failure(
                        trans_MRI(u"You can't make this offer"))
        try:
            spin_offer_sender.setValue(0)
        except AttributeError:
            pass

    def add_offer(self, offer):
        """
        add the offer to the list
        called by remote
        :param offer:
        :return:
        """
        sender = offer["MRI_offer_sender"]
        color = "blue" if sender == self._remote.le2mclt.uid else "black"
        if offer["MRI_offer_contract"] == pms.TRIANGLE:
            if offer["MRI_offer_type"] == pms.BUY:
                self._triangle_purchase_zone.add_offer(sender, offer, color)
            else:  # purchase
                self._triangle_sell_zone.add_offer(sender, offer, color)
        else:  # star
            if offer["MRI_offer_type"] == pms.BUY:
                self._star_purchase_zone.add_offer(sender, offer, color)
            else:  # purchase
                self._star_sell_zone.add_offer(sender, offer, color)
        
    @defer.inlineCallbacks
    def _remove_offer(self, triangle_or_star, buy_or_sell):
        """
        Called by pushbutton_remove_offer from the offer zone
        :param triangle_or_star: 
        :param buy_or_sell: 
        :return:
        """
        offer = {"MRI_offer_contract": triangle_or_star,
                 "MRI_offer_type": buy_or_sell}
        yield (self._remote.remove_offer(offer))

    def remove_offer(self, offer):
        """
        remove the offer from the list
        called by remote
        :param offer:
        :return:
        """
        sender = offer["MRI_offer_sender"]
        if offer["MRI_offer_contract"] == pms.TRIANGLE:
            if offer["MRI_offer_type"] == pms.BUY:
                self._triangle_purchase_zone.remove_offer(sender)
            else:  # purchase
                self._triangle_sell_zone.remove_offer(sender)
        else:  # star
            if offer["MRI_offer_type"] == pms.BUY:
                self._star_purchase_zone.remove_offer(sender)
            else:  # purchase
                self._star_sell_zone.remove_offer(sender)

    @defer.inlineCallbacks
    def _accept_selected_offer(self, triangle_or_star, existing_offer):
        """
        Called by pushbutton accept_selected_offer from the offer zone
        Complete the offer and if all is fine add a new transaction
        :param triangle_or_star:
        :param existing_offer:
        :return:
        """
        if existing_offer is None:
            return
        try:
            if existing_offer["MRI_offer_sender"] != self._remote.le2mclt.uid:
                existing_offer["MRI_offer_contract"] = triangle_or_star
                new_offer = dict()
                new_offer["MRI_offer_contract"] = \
                    existing_offer["MRI_offer_contract"]
                new_offer["MRI_offer_price"] = existing_offer["MRI_offer_price"]
                new_offer["MRI_offer_sender_type"] = pms.PRICE_TAKER
                if existing_offer["MRI_offer_type"] == pms.BUY:
                    new_offer["MRI_offer_type"] = pms.SELL
                else:
                    new_offer["MRI_offer_type"] = pms.BUY
                if self._remote.is_offer_ok(new_offer):
                    yield (self._remote.add_transaction(existing_offer,
                                                        new_offer))
                else:
                    if not self._automatique:
                        self._display_offer_failure(
                            trans_MRI(u"You can't accept this offer"))
        except (TypeError, KeyError):  # if no item selected
            pass

    def add_transaction(self, transaction):
        """
        Add the transaction in the transaction zone
        :param transaction:
        :return:
        """
        price = transaction["MRI_trans_price"]
        buyer = transaction["MRI_trans_buyer"]
        seller = transaction["MRI_trans_seller"]
        implied, buyer_or_seller = False, None
        if buyer == self._remote.le2mclt.uid or \
                    seller == self._remote.le2mclt.uid:
            implied = True
            buyer_or_seller = pms.BUYER if buyer == self._remote.le2mclt.uid \
                else pms.SELLER
        color = "blue" if implied else "black"
        if transaction["MRI_trans_contract"] == pms.TRIANGLE:
            self._triangle_transactions.add_transaction(price, buyer_or_seller,
                                                        color)
        else:
            self._star_transactions.add_transaction(price, buyer_or_seller,
                                                    color)

    @defer.inlineCallbacks
    def update_balance(self, balance_if_triangle, balance_if_star):
        """
        Display the new balances and test each player's offers in the list.
        If an offer is no more possible then removes it
        :param balance:
        :param balance_if_triangle:
        :param balance_if_star:
        :return:
        """
        self._information.label_balance_if_triangle.setText(
            str(balance_if_triangle))
        self._information.label_balance_if_star.setText(str(balance_if_star))

        triangle_purchase_sender_offer = \
            self._triangle_purchase_zone.get_sender_offer(self._remote.le2mclt.uid)
        if triangle_purchase_sender_offer:
            if not self._remote.is_offer_ok(triangle_purchase_sender_offer):
                yield (self._remove_offer(pms.TRIANGLE, pms.BUY))
        triangle_sell_sender_offer = \
            self._triangle_sell_zone.get_sender_offer(self._remote.le2mclt.uid)
        if triangle_sell_sender_offer:
            if not self._remote.is_offer_ok(triangle_sell_sender_offer):
                yield (self._remove_offer(pms.TRIANGLE, pms.SELL))
        star_purchase_sender_offer = \
            self._star_purchase_zone.get_sender_offer(self._remote.le2mclt.uid)
        if star_purchase_sender_offer:
            if not self._remote.is_offer_ok(star_purchase_sender_offer):
                yield (self._remove_offer(pms.STAR, pms.BUY))
        star_sell_sender_offer = \
            self._star_sell_zone.get_sender_offer(self._remote.le2mclt.uid)
        if star_sell_sender_offer:
            if not self._remote.is_offer_ok(star_sell_sender_offer):
                yield (self._remove_offer(pms.STAR, pms.SELL))

    def _display_offer_failure(self, message):
        QtGui.QMessageBox.warning(self, trans_MRI(u"Be careful"), message)
        return

    def _play_auto(self):
        """
        called by the timer when the program play automatically
        :return:
        """
        # make an offer
        def make_offer(the_list):
            random_offer = float("{:.2f}".format(random() + randint(0, 1)))
            the_list.spin_offer.setValue(random_offer)
            the_list.pushbutton_send.click()

        def accept_offer(the_list):
            the_list.select_an_item()
            the_list.pushbutton_accept.click()

        triangle_or_star = choice([pms.TRIANGLE, pms.STAR])
        buy_or_sell = choice([pms.BUY, pms.SELL])
        selected_function = choice([make_offer, accept_offer])

        if triangle_or_star == pms.TRIANGLE:
            if buy_or_sell == pms.BUY:
                selected_function(self._triangle_purchase_zone)
            else:
                selected_function(self._triangle_sell_zone)
        else:
            if buy_or_sell == pms.BUY:
                selected_function(self._star_purchase_zone)
            else:
                selected_function(self._star_sell_zone)