def __init__(self, defered, automatique, parent, periode, historique,
                 grilles):
        QtGui.QDialog.__init__(self, parent)

        self._defered = defered
        self._automatique = automatique
        self._grilles = grilles
        self._historique = GuiHistorique(self,
                                         historique,
                                         size=(HISTO_WIDTH, 500))

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        # period and history button
        wperiod = WPeriod(period=periode,
                          ecran_historique=self._historique,
                          parent=self)
        layout.addWidget(wperiod)

        explanation = WExplication(
            parent=self,
            text=texts_PGGS.get_text_explanation_grilles(),
            size=(600, 100))
        layout.addWidget(explanation)

        self._countdown = WCompterebours(self,
                                         temps=pms.TIME_TO_FILL_GRILLES,
                                         actionfin=self._accept)
        layout.addWidget(self._countdown)

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

        self._widgets_grilles = list()
        current_line = 0
        for i, g in enumerate(self._grilles):
            self._widgets_grilles.append(WGrid(g, self._automatique))
            grid_layout.addWidget(self._widgets_grilles[-1], current_line,
                                  i - current_line * pms.NB_GRILLES_PER_LINE)
            if i > 0 and (i + 1) % pms.NB_GRILLES_PER_LINE == 0:
                current_line += 1

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

        self.adjustSize()
        self.setFixedSize(self.size())
        self.setWindowTitle(trans_PGGS(u"Tasks"))
Example #2
0
    def __init__(self, remote, defered):
        super(GuiDecision, self).__init__(remote.le2mclt.screen)

        # ----------------------------------------------------------------------
        # main attributes
        # ----------------------------------------------------------------------
        self.remote = remote
        self.defered = defered

        layout = QVBoxLayout(self)

        # ----------------------------------------------------------------------
        # HEAD AREA
        # ----------------------------------------------------------------------
        layout_head = QHBoxLayout()
        layout.addLayout(layout_head, 0)

        if pms.DYNAMIC_TYPE == pms.DISCRETE:
            self.label_period = QLabel(
                le2mtrans(u"Period") +
                u" {}".format(self.remote.currentperiod))
            layout_head.addWidget(self.label_period, 0, Qt.AlignLeft)

        self.compte_rebours = None
        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            self.compte_rebours = WCompterebours(self,
                                                 pms.CONTINUOUS_TIME_DURATION,
                                                 lambda: None)
        elif pms.DYNAMIC_TYPE == pms.DISCRETE:
            time = timedelta(seconds=7) if self.remote.le2mclt.automatique else \
                pms.DISCRETE_DECISION_TIME
            self.compte_rebours = WCompterebours(self, time, self.send_extrac)
        layout_head.addWidget(self.compte_rebours, 0, Qt.AlignLeft)
        layout_head.addStretch()

        # ----------------------------------------------------------------------
        # GRAPHICAL AREA
        # ----------------------------------------------------------------------

        self.plot_layout = QGridLayout()
        layout.addLayout(self.plot_layout)
        # extraction
        self.plot_extraction = PlotExtraction(self.remote.extractions)
        self.plot_layout.addWidget(self.plot_extraction, 0, 0)
        # part payoff
        self.plot_payoff = PlotPayoff(self.remote.payoff_part)
        self.plot_layout.addWidget(self.plot_payoff, 0, 1)
        # available resource
        self.plot_resource = PlotResource(self.remote.resource)
        self.plot_layout.addWidget(self.plot_resource, 1, 0)
        # value in text mode
        widget_infos = QWidget()
        widget_infos.setLayout(QVBoxLayout())
        self.textEdit_infos = QTextEdit()
        self.textEdit_infos.setReadOnly(True)
        self.textEdit_infos.setHtml(self.remote.text_infos)
        widget_infos.layout().addWidget(self.textEdit_infos)
        self.plot_layout.addWidget(widget_infos, 1, 1)
        self.plot_layout.setColumnStretch(0, 1)
        self.plot_layout.setColumnStretch(1, 1)

        # ----------------------------------------------------------------------
        # DECISION AREA
        # ----------------------------------------------------------------------

        self.extract_dec = MySlider()
        player_extrac = self.remote.extractions.ydata[-1]
        self.extract_dec.slider.setValue(player_extrac * 100)
        self.extract_dec.lcd.display(player_extrac)
        layout.addWidget(self.extract_dec)

        # ----------------------------------------------------------------------
        # FOOT AREA
        # ----------------------------------------------------------------------

        self.setWindowTitle(trans_CO(u"Decision"))

        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            self.extract_dec.slider.sliderReleased.connect(self.send_extrac)
            if self.remote.le2mclt.automatique:
                self.extract_dec.slider.valueChanged.connect(self.send_extrac)
            self.timer_continuous = QTimer()
            self.timer_continuous.timeout.connect(self.update_data_and_graphs)
            self.timer_continuous.start(
                int(pms.TIMER_UPDATE.total_seconds()) * 1000)

        if pms.DYNAMIC_TYPE == pms.DISCRETE and self.remote.le2mclt.automatique:
            self.extract_dec.slider.setValue(
                random.randint(pms.DECISION_MIN,
                               pms.DECISION_MAX * int(1 / pms.DECISION_STEP)))

        self.remote.end_of_time.connect(self.end_of_time)
    def __init__(self, defered, automatique, parent, period, historique,
                 remote):
        super(GuiDecision, self).__init__(parent)

        # variables
        self._defered = defered
        self._automatique = automatique
        self._historique = GuiHistorique(self, historique)
        self._remote = remote
        self._offer_items = {}

        layout = QtGui.QVBoxLayout(self)

        wperiod = WPeriod(period=period, ecran_historique=self._historique)
        layout.addWidget(wperiod)

        wexplanation = WExplication(text=texts_MC.get_text_explanation(
            self._remote.role, self._remote.value_or_cost),
                                    size=(450, 50),
                                    parent=self)
        layout.addWidget(wexplanation)

        self._compte_rebours = WCompterebours(parent=self,
                                              temps=pms.TEMPS,
                                              actionfin=self._accept)
        layout.addWidget(self._compte_rebours)

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

        CURRENT_LINE = 0

        gridlayout.addWidget(QtGui.QLabel(u"Offres de ventes"), CURRENT_LINE,
                             0)
        gridlayout.addWidget(QtGui.QLabel(u"Offres d'achats"), CURRENT_LINE, 1)
        gridlayout.addWidget(QtGui.QLabel(u"Transactions"), CURRENT_LINE, 2)

        CURRENT_LINE += 1

        self._model_ventes = QtGui.QStandardItemModel()
        self._listview_ventes = QtGui.QListView()
        self._listview_ventes.setModel(self._model_ventes)
        self._listview_ventes.setMaximumSize(300, 600)
        gridlayout.addWidget(self._listview_ventes, CURRENT_LINE, 0)

        self._model_achats = QtGui.QStandardItemModel()
        self._listview_achats = QtGui.QListView()
        self._listview_achats.setModel(self._model_achats)
        self._listview_achats.setMaximumSize(300, 600)
        gridlayout.addWidget(self._listview_achats, CURRENT_LINE, 1)

        self._listwidget_transactions = QtGui.QListWidget()
        self._listwidget_transactions.setMaximumSize(300, 600)
        gridlayout.addWidget(self._listwidget_transactions, CURRENT_LINE, 2)

        CURRENT_LINE += 1

        if self._remote.role == pms.VENDEUR:

            self._layout_offer = QtGui.QHBoxLayout()
            self._layout_offer.addWidget(QtGui.QLabel(u"Offre de vente"))
            self._spin_offer = QtGui.QDoubleSpinBox()
            self._spin_offer.setDecimals(pms.DECIMALES)
            self._spin_offer.setMinimum(0)
            self._spin_offer.setMaximum(pms.DECISION_MAX)
            self._spin_offer.setButtonSymbols(QtGui.QSpinBox.NoButtons)
            self._spin_offer.setMaximumWidth(50)
            self._layout_offer.addWidget(self._spin_offer)
            self._button_send_offer = QtGui.QPushButton(u"Envoyer")
            self._button_send_offer.setMaximumWidth(100)
            self._button_send_offer.clicked.connect(
                lambda _: self._send_offer())
            self._layout_offer.addWidget(self._button_send_offer)
            self._layout_offer.addSpacerItem(
                QtGui.QSpacerItem(20, 20, QtGui.QSizePolicy.Expanding,
                                  QtGui.QSizePolicy.Minimum))
            gridlayout.addLayout(self._layout_offer, CURRENT_LINE, 0)

        else:

            self._layout_offer = QtGui.QHBoxLayout()
            self._layout_offer.addWidget(QtGui.QLabel(u"Offre d'achat"))
            self._spin_offer = QtGui.QDoubleSpinBox()
            self._spin_offer.setDecimals(pms.DECIMALES)
            self._spin_offer.setMinimum(0)
            self._spin_offer.setMaximum(pms.DECISION_MAX)
            self._spin_offer.setButtonSymbols(QtGui.QSpinBox.NoButtons)
            self._spin_offer.setMaximumWidth(50)
            self._layout_offer.addWidget(self._spin_offer)
            self._button_send_offer = QtGui.QPushButton(u"Envoyer")
            self._button_send_offer.setMaximumWidth(100)
            self._button_send_offer.clicked.connect(
                lambda _: self._send_offer())
            self._layout_offer.addWidget(self._button_send_offer)
            self._layout_offer.addSpacerItem(
                QtGui.QSpacerItem(20, 20, QtGui.QSizePolicy.Expanding,
                                  QtGui.QSizePolicy.Minimum))
            gridlayout.addLayout(self._layout_offer, CURRENT_LINE, 1)

        self.setWindowTitle(trans_MC(u"Marché"))
        self.adjustSize()
        self.setFixedSize(self.size())

        if self._automatique:
            self._timer_automatique = QtCore.QTimer()
            self._timer_automatique.setSingleShot(False)
            self._timer_automatique.timeout.connect(self._play_auto)
            self._timer_automatique.start(5000)
Example #4
0
    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"))
Example #5
0
    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))
Example #6
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)
Example #7
0
class GuiDecision(QDialog):
    def __init__(self, remote, defered):
        super(GuiDecision, self).__init__(remote.le2mclt.screen)

        # ----------------------------------------------------------------------
        # main attributes
        # ----------------------------------------------------------------------
        self.remote = remote
        self.defered = defered

        layout = QVBoxLayout(self)

        # ----------------------------------------------------------------------
        # HEAD AREA
        # ----------------------------------------------------------------------
        layout_head = QHBoxLayout()
        layout.addLayout(layout_head, 0)

        if pms.DYNAMIC_TYPE == pms.DISCRETE:
            self.label_period = QLabel(
                le2mtrans(u"Period") +
                u" {}".format(self.remote.currentperiod))
            layout_head.addWidget(self.label_period, 0, Qt.AlignLeft)

        self.compte_rebours = None
        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            self.compte_rebours = WCompterebours(self,
                                                 pms.CONTINUOUS_TIME_DURATION,
                                                 lambda: None)
        elif pms.DYNAMIC_TYPE == pms.DISCRETE:
            time = timedelta(seconds=7) if self.remote.le2mclt.automatique else \
                pms.DISCRETE_DECISION_TIME
            self.compte_rebours = WCompterebours(self, time, self.send_extrac)
        layout_head.addWidget(self.compte_rebours, 0, Qt.AlignLeft)
        layout_head.addStretch()

        # ----------------------------------------------------------------------
        # GRAPHICAL AREA
        # ----------------------------------------------------------------------

        self.plot_layout = QGridLayout()
        layout.addLayout(self.plot_layout)

        # extractions (indiv + group)
        self.plot_extraction = PlotExtraction(self.remote.le2mclt.uid,
                                              self.remote.extractions_indiv,
                                              self.remote.extraction_group)
        self.plot_layout.addWidget(self.plot_extraction, 0, 0)

        # payoff indiv
        self.plot_payoff = PlotPayoff(self.remote.payoff_part)
        self.plot_layout.addWidget(self.plot_payoff, 0, 1)

        # resource
        self.plot_resource = PlotResource(self.remote.resource)
        self.plot_layout.addWidget(self.plot_resource, 1, 0)

        # value in text mode
        widget_infos = QWidget()
        widget_infos.setLayout(QVBoxLayout())
        self.textEdit_infos = QTextEdit()
        self.textEdit_infos.setReadOnly(True)
        self.textEdit_infos.setHtml(self.remote.text_infos)
        widget_infos.layout().addWidget(self.textEdit_infos)
        self.plot_layout.addWidget(widget_infos, 1, 1)
        self.plot_layout.setColumnStretch(0, 1)
        self.plot_layout.setColumnStretch(1, 1)

        # ----------------------------------------------------------------------
        # DECISION AREA
        # ----------------------------------------------------------------------

        self.extract_dec = MySlider()
        player_extrac = self.remote.extractions_indiv[
            self.remote.le2mclt.uid].ydata[-1]
        self.extract_dec.slider.setValue(player_extrac * 100)
        self.extract_dec.lcd.display(player_extrac)
        layout.addWidget(self.extract_dec)

        # ----------------------------------------------------------------------
        # FOOT AREA
        # ----------------------------------------------------------------------

        self.setWindowTitle(trans_DYNCPR(u"Decision"))

        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            self.extract_dec.slider.sliderReleased.connect(self.send_extrac)
            if self.remote.le2mclt.automatique:
                self.extract_dec.slider.valueChanged.connect(self.send_extrac)
            self.timer_continuous = QTimer()
            self.timer_continuous.timeout.connect(self.update_data_and_graphs)
            self.timer_continuous.start(
                int(pms.TIMER_UPDATE.total_seconds()) * 1000)

        if pms.DYNAMIC_TYPE == pms.DISCRETE and self.remote.le2mclt.automatique:
            self.extract_dec.slider.setValue(
                random.randint(pms.DECISION_MIN,
                               pms.DECISION_MAX * int(1 / pms.DECISION_STEP)))

        self.remote.end_of_time.connect(self.end_of_time)

    def reject(self):
        pass

    def send_extrac(self):
        dec = self.extract_dec.value()
        logger.info("{} send {}".format(self.remote.le2mclt, dec))
        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            self.remote.server_part.callRemote("new_extraction", dec)
        elif pms.DYNAMIC_TYPE == pms.DISCRETE:
            self.defered.callback(dec)

    def update_data_and_graphs(self):
        if self.remote.le2mclt.automatique:
            if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
                if random.random() < 0.33:
                    self.extract_dec.slider.setValue(
                        random.randint(
                            pms.DECISION_MIN,
                            pms.DECISION_MAX * int(1 / pms.DECISION_STEP)))
            elif pms.DYNAMIC_TYPE == pms.DISCRETE:
                self.extract_dec.slider.setValue(
                    random.randint(
                        pms.DECISION_MIN,
                        pms.DECISION_MAX * int(1 / pms.DECISION_STEP)))
        self.plot_extraction.canvas.draw()
        self.plot_resource.canvas.draw()
        self.plot_payoff.canvas.draw()
        self.textEdit_infos.setHtml(self.remote.text_infos)
        if pms.DYNAMIC_TYPE == pms.DISCRETE:
            self.label_period.setText(
                le2mtrans(u"Period") +
                u" {}".format(self.remote.currentperiod))
            self.compte_rebours.restart()

    def end_of_time(self):
        try:
            self.timer_continuous.stop()
        except AttributeError:  # if dynamic == discrete
            pass
        if pms.DYNAMIC_TYPE == pms.CONTINUOUS:
            try:
                self.defered.callback(None)
            except AlreadyCalledError as m:
                logger.warning(m.message)
        super(GuiDecision, self).accept()
class DEffort(QtGui.QDialog):
    def __init__(self, defered, automatique, parent, periode, historique,
                 grilles):
        QtGui.QDialog.__init__(self, parent)

        self._defered = defered
        self._automatique = automatique
        self._grilles = grilles
        self._historique = GuiHistorique(self,
                                         historique,
                                         size=(HISTO_WIDTH, 500))

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        # period and history button
        wperiod = WPeriod(period=periode,
                          ecran_historique=self._historique,
                          parent=self)
        layout.addWidget(wperiod)

        explanation = WExplication(
            parent=self,
            text=texts_PGGS.get_text_explanation_grilles(),
            size=(600, 100))
        layout.addWidget(explanation)

        self._countdown = WCompterebours(self,
                                         temps=pms.TIME_TO_FILL_GRILLES,
                                         actionfin=self._accept)
        layout.addWidget(self._countdown)

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

        self._widgets_grilles = list()
        current_line = 0
        for i, g in enumerate(self._grilles):
            self._widgets_grilles.append(WGrid(g, self._automatique))
            grid_layout.addWidget(self._widgets_grilles[-1], current_line,
                                  i - current_line * pms.NB_GRILLES_PER_LINE)
            if i > 0 and (i + 1) % pms.NB_GRILLES_PER_LINE == 0:
                current_line += 1

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

        self.adjustSize()
        self.setFixedSize(self.size())
        self.setWindowTitle(trans_PGGS(u"Tasks"))

    def reject(self):
        pass

    def _accept(self):
        if self._countdown.is_running():
            confirmation = QtGui.QMessageBox.question(
                self, "Confirmation",
                trans_PGGS(
                    u"Do you want to quit before the end of the timer?"),
                QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Yes)
            if confirmation != QtGui.QMessageBox.Yes:
                return
            else:
                self._countdown.stop()
        answers = sum([int(g.is_ok()) for g in self._widgets_grilles])
        if not self._automatique:
            QtGui.QMessageBox.information(
                self, "Information",
                trans_PGGS(u"You've found {} grids.").format(answers))
        logger.info("send back {}".format(answers))
        self.accept()
        self._defered.callback(answers)