Server::Server(QWidget *parent) :
    QDialog(parent)
{

    InitRawInput();


    statusLabel = new QLabel(tr("Click 'Start' to start the UDP server"));

    startButton = new QPushButton(tr("&Start"));
    quitButton = new QPushButton(tr("&Quit"));

    buttonBox = new QDialogButtonBox;
    buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

    //UDPServer initialisation
    udpServer = new UDPServer(false, this);
    udpServer->setOtherNodePort(50001);

    connect(startButton, SIGNAL(clicked()), udpServer, SLOT(startSocket()));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(Mapper::instance(), SIGNAL(actionHappened(QString)), udpServer, SLOT(sendControllerAction(QString)));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);

    setWindowTitle(tr("UDP Protocol Based Server"));
}
Exemplo n.º 2
0
void LoadBackpackPage::actionPerformed( GUIComponent *inTarget ) {
    if( inTarget == &mDoneButton ) {
        mDone = true;
        }
    else if( inTarget == &mToolPicker ) {
        if( mToolPicker.shouldShowGridView() ) {
            mShowGridToolPicker = true;
            }
        else {
            checkBuyButtonStatus();
            }
        }
    else if( inTarget == &mUndoButton ) {
        undoActionOrActionGroup();
        
        checkBuyButtonStatus();
        
        checkSellModeStatus();
        
        checkUndoStatus();

        actionHappened();
        }
    else if( inTarget == &mBuyButton ) {
        mChangeHappened = true;
        
        int selectedObject = mToolPicker.getSelectedObject();
        
        int price = mToolPicker.getPrice( selectedObject );
        int sellPrice = mToolPicker.getSellBackPrice( selectedObject );;
        
        // first look for matching slot
        char found = false;
        
        for( int i=0; i<NUM_PACK_SLOTS; i++ ) {
            if( mPackSlots[i]->getObject() == selectedObject ) {
                found = true;
                
                mPackSlots[i]->addToQuantity( 1 );
                mLootValue -= price;
                
                mToolPicker.useSelectedObject();
                
                addToQuantity( &mPurchaseRecords, selectedObject );
                
                PurchaseHistoryRecord historyRecord = { selectedObject, 1, 0,
                                                        false };
                mPurchaseHistory.push_back( historyRecord );
                
                checkUndoStatus();

                break;
                }
            }
        
        if( ! found ) {
            // check for empty slot instead
            for( int i=0; i<NUM_PACK_SLOTS; i++ ) {
                if( mPackSlots[i]->getObject() == -1 ) {
                    // empty slot!
                    mPackSlots[i]->setObject( selectedObject );
                    mPackSlots[i]->setSellPrice( sellPrice );
                    mLootValue -= price;
                
                    mToolPicker.useSelectedObject();

                    addToQuantity( &mPurchaseRecords, selectedObject );

                    PurchaseHistoryRecord historyRecord = { selectedObject, 1,
                                                            0, false };
                    mPurchaseHistory.push_back( historyRecord );
                
                    checkUndoStatus();

                    break;
                    }
                }
            }
        
        checkBuyButtonStatus();
        actionHappened();
        }
    else if( inTarget == &mSellModeButton ) {
        mSellMode = true;
        checkSellModeStatus();
        }
    else if( inTarget == &mSellHalfButton ) {
        mSellHalfMode = true;
        checkSellModeStatus();
        }
    else if( inTarget == &mSellOneButton ) {
        mSellHalfMode = false;
        checkSellModeStatus();
        }
    else if( inTarget == &mSellAllButton ) {
        char firstRecordInChain = true;

        // process slots in reverse order so that when we undo back
        // through the chain, the original item order is restored
        for( int i=NUM_PACK_SLOTS - 1; i>=0; i-- ) {
            int id = mPackSlots[i]->getObject();
                
            if( id != -1 ) {
                int quantitySold = mPackSlots[i]->getQuantity();
                    
                mPackSlots[i]->setQuantity( 0 );
                    
                mLootValue += 
                    quantitySold * 
                    mToolPicker.getSellBackPrice( id );
                    
                addToQuantity( &mSellRecords, id, quantitySold );

                PurchaseHistoryRecord historyRecord = 
                    { id, -quantitySold, 0, ! firstRecordInChain };
                mPurchaseHistory.push_back( historyRecord );
                    
                firstRecordInChain = false;
                }
            }
        for( int i=NUM_VAULT_SLOTS - 1; i>=0; i-- ) {
            int id = mVaultSlots[i]->getObject();
                
            if( id != -1 ) {
                int quantitySold = mVaultSlots[i]->getQuantity();
                    
                mVaultSlots[i]->setQuantity( 0 );
                    
                mLootValue += 
                    quantitySold * 
                    mToolPicker.getSellBackPrice( id );
                    
                addToQuantity( &mSellRecords, id, quantitySold );

                PurchaseHistoryRecord historyRecord = 
                    { id, -quantitySold, 1, ! firstRecordInChain };
                mPurchaseHistory.push_back( historyRecord );
                    
                firstRecordInChain = false;
                }
            }

        checkSellModeStatus();
        
        checkUndoStatus();
        }
    else if( inTarget == &mBuyModeButton ) {
        mSellMode = false;
        mSellHalfMode = false;
        mMoveHalfMode = false;
        checkSellModeStatus();
        }
    else if( inTarget == &mMoveHalfButton ) {
        mMoveHalfMode = true;
        checkSellModeStatus();
        }
    else if( inTarget == &mMoveOneButton ) {
        mMoveHalfMode = false;
        checkSellModeStatus();
        }
    else {
        
        // check if backpack slot clicked
        char foundHit = false;
        
        for( int i=0; i<NUM_PACK_SLOTS; i++ ) {
            if( inTarget == mPackSlots[i] ) {
                int id = mPackSlots[i]->getObject();
                
                if( id != -1 ) {
                    // click on a non-empty pack slot counts as an action
                    mChangeHappened = true;

                    actionHappened();
                    
                    
                    
                    if( mSellMode ) {

                        int quantitySold = 1;
                            
                        if( mSellHalfMode ) {
                            quantitySold = mPackSlots[i]->getQuantity() / 2;
                            }
                        if( quantitySold < 1 ) {
                            quantitySold = 1;
                            }
                        mPackSlots[i]->addToQuantity( - quantitySold );

                        mLootValue += 
                            quantitySold * 
                            mToolPicker.getSellBackPrice( id );
                        
                        addToQuantity( &mSellRecords, id, quantitySold );
                        PurchaseHistoryRecord historyRecord = 
                            { id, -quantitySold, 0, false };
                        mPurchaseHistory.push_back( historyRecord );

                        checkSellModeStatus();
                        checkUndoStatus();
                        }
                    else {
                        int quantityMoved = 1;
                        if( mMoveHalfMode ) {
                            quantityMoved = mPackSlots[i]->getQuantity() / 2;
                            }
                        if( quantityMoved < 1 ) {
                            quantityMoved = 1;
                            }
                        
                        mPackSlots[i]->addToQuantity( - quantityMoved );

                        char foundInVault = false;

                        for( int j=0; j<NUM_VAULT_SLOTS; j++ ) {
                            if( mVaultSlots[j]->getObject() == id ) {
                                mVaultSlots[j]->addToQuantity( quantityMoved );
                                foundInVault = true;
                                break;
                                }
                            }
                        if( !foundInVault ) {
                            // find empty slot and stick it there
                            for( int j=0; j<NUM_VAULT_SLOTS; j++ ) {
                                if( mVaultSlots[j]->getObject() == -1 ) {
                                    mVaultSlots[j]->setObject( id );
                                    mVaultSlots[j]->setQuantity( 
                                        quantityMoved );
                                    mVaultSlots[j]->setSellPrice(
                                        mToolPicker.getSellBackPrice( id ) );
                                    break;
                                    }
                                }
                            }
                        }
                    
                    checkBuyButtonStatus();
                    }
                
                foundHit = true;
                break;
                }
            }
        
        if( !foundHit ) {
            // check for clicks of vault slots

            for( int i=0; i<NUM_VAULT_SLOTS; i++ ) {
                if( inTarget == mVaultSlots[i] ) {
                    int id = mVaultSlots[i]->getObject();
                
                    if( id != -1 ) {
                        // click on a non-empty vault slot counts as an action
                        mChangeHappened = true;

                        actionHappened();
                        
                        if( mSellMode ) {
                            int quantitySold = 1;
                            
                            if( mSellHalfMode ) {
                                quantitySold = 
                                    mVaultSlots[i]->getQuantity() / 2;
                                }
                            if( quantitySold < 1 ) {
                                quantitySold = 1;
                                }
                            

                            mVaultSlots[i]->setQuantity( 
                                mVaultSlots[i]->getQuantity()
                                - quantitySold );
                            
                            mLootValue += 
                                quantitySold * 
                                mToolPicker.getSellBackPrice( id );
                        
                            addToQuantity( &mSellRecords, id, quantitySold );
                            PurchaseHistoryRecord historyRecord = 
                                { id, -quantitySold, 1, false };
                            mPurchaseHistory.push_back( historyRecord );
                        
                            checkSellModeStatus();
                            checkUndoStatus();
                            }
                        else {
                            // ignore transfer click if backpack full
                            // and if it has no matching slot
                            int foundIndex = -1;
                            char empty = false;
                            for( int j=0; j<NUM_PACK_SLOTS; j++ ) {
                                if( mPackSlots[j]->getObject() == id ) {
                                    foundIndex = j;
                                    break;
                                    }
                                }
                            if( foundIndex == -1 ) {    
                                for( int j=0; j<NUM_PACK_SLOTS; j++ ) {
                                    if( mPackSlots[j]->getObject() == -1 ) {
                                        foundIndex = j;
                                        empty = true;
                                        break;
                                        }
                                    }
                                }
                        
                            if( foundIndex != -1 ) {

                                int quantityMoved = 1;
                                if( mMoveHalfMode ) {
                                    quantityMoved = 
                                        mVaultSlots[i]->getQuantity() / 2;
                                    }
                                if( quantityMoved < 1 ) {
                                    quantityMoved = 1;
                                    }

                                mVaultSlots[i]->addToQuantity( 
                                    - quantityMoved );
                                                                
                                if( empty ) {
                                    mPackSlots[foundIndex]->setObject( id );
                                    mPackSlots[foundIndex]->setQuantity( 
                                        quantityMoved );
                                    
                                    mPackSlots[foundIndex]->setSellPrice(
                                        mToolPicker.getSellBackPrice( id ) );
                                    }
                                else {
                                    mPackSlots[foundIndex]->addToQuantity( 
                                        quantityMoved );
                                    }
                                
                                checkBuyButtonStatus();
                                }
                            }
                        }
                    break;
                    }
                }
            }

        
        }
    
    
    }