Skip to content

Commit

Permalink
Attempt at adding auction dialog functionality
Browse files Browse the repository at this point in the history
(cherry picked from commit b21df8308be3c62f0ff40203a685e485b3060516)
  • Loading branch information
brightrim committed Jun 18, 2024
1 parent 3334eba commit e134889
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,10 @@ void Character::requestMerchantDialog(MerchantDialog *merchantDialog) {
// Nothing to do here, overloaded in Player
}

void Character::requestAuctionDialog(AuctionDialog *auctionDialog) {
// Nothing to do here, overloaded in Player
}

void Character::requestSelectionDialog(SelectionDialog *selectionDialog) {
// Nothing to do here, overloaded in Player
}
Expand Down
2 changes: 2 additions & 0 deletions src/Character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "dialog/CraftingDialog.hpp"
#include "dialog/InputDialog.hpp"
#include "dialog/MerchantDialog.hpp"
#include "dialog/AuctionDialog.hpp"
#include "dialog/MessageDialog.hpp"
#include "dialog/SelectionDialog.hpp"
#include "tuningConstants.hpp"
Expand Down Expand Up @@ -428,6 +429,7 @@ class Character {
virtual void requestInputDialog(InputDialog *inputDialog);
virtual void requestMessageDialog(MessageDialog *messageDialog);
virtual void requestMerchantDialog(MerchantDialog *merchantDialog);
virtual void requestAuctionDialog(AuctionDialog *auctionDialog);
virtual void requestSelectionDialog(SelectionDialog *selectionDialog);
virtual void requestCraftingDialog(CraftingDialog *craftingDialog);
virtual void requestCraftingLookAt(unsigned int dialogId, ItemLookAt &lookAt);
Expand Down
48 changes: 48 additions & 0 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "dialog/CraftingDialog.hpp"
#include "dialog/InputDialog.hpp"
#include "dialog/MerchantDialog.hpp"
#include "dialog/AuctionDialog.hpp"
#include "dialog/MessageDialog.hpp"
#include "dialog/SelectionDialog.hpp"
#include "map/Field.hpp"
Expand Down Expand Up @@ -2545,6 +2546,53 @@ void Player::executeMerchantDialogLookAt(unsigned int dialogId, uint8_t list, ui
}
}

void Player::requestAuctionDialog(AuctionDialog *auctionDialog) {
requestDialog<AuctionDialog, AuctionDialogTC>(auctionDialog);
}

void Player::executeAuctionDialogAbort(unsigned int dialogId) {
auto auctionDialog = getDialog<AuctionDialog>(dialogId);

if (auctionDialog) {
auctionDialog->setResult(AuctionDialog::playerAborts);
auctionDialog->setPurchaseIndex(0);
auctionDialog->setPurchaseAmount(0);
ScriptItem item;
auctionDialog->setSaleItem(item);
LuaScript::executeDialogCallback(*auctionDialog);
}

dialogs.erase(dialogId);
}

void Player::executeAuctionDialogBid(unsigned int dialogId, AuctionDialog::index_type index,
Item::number_type amount) const {
auto auctionDialog = getDialog<AuctionDialog>(dialogId);

if (auctionDialog) {
auctionDialog->setResult(AuctionDialog::playerBids);
auctionDialog->setPurchaseIndex(index);
auctionDialog->setPurchaseAmount(amount);
ScriptItem item;
auctionDialog->setSaleItem(item);
LuaScript::executeDialogCallback(*auctionDialog);
}
}

void Player::executeAuctionDialogLookAt(unsigned int dialogId, uint8_t list, uint8_t slot) {
auto auctionDialog = getDialog<AuctionDialog>(dialogId);

if (auctionDialog) {
auctionDialog->setResult(AuctionDialog::playerLooksAt);
auctionDialog->setLookAtList(static_cast<AuctionDialog::ListType>(list));
auctionDialog->setPurchaseIndex(slot);
auto lookAt = LuaScript::executeDialogCallback<ItemLookAt>(*auctionDialog);

ServerCommandPointer cmd = std::make_shared<LookAtDialogGroupItemTC>(dialogId, list, slot, lookAt);
Connection->addCommand(cmd);
}
}

void Player::requestSelectionDialog(SelectionDialog *selectionDialog) {
requestDialog<SelectionDialog, SelectionDialogTC>(selectionDialog);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Item.hpp"
#include "Showcase.hpp"
#include "dialog/MerchantDialog.hpp"
#include "dialog/AuctionDialog.hpp"
#include "dialog/SelectionDialog.hpp"
#include "netinterface/BasicServerCommand.hpp"
#include "netinterface/NetInterface.hpp"
Expand Down Expand Up @@ -509,6 +510,12 @@ class Player : public Character {
Item::number_type amount);
void executeMerchantDialogLookAt(unsigned int dialogId, uint8_t list, uint8_t slot);

void requestAuctionDialog(AuctionDialog *auctionDialog) override;
void executeAuctionDialogAbort(unsigned int dialogId);
void executeAuctionDialogBid(unsigned int dialogId, AuctionDialog::index_type index,
Item::number_type amount) const;
void executeAuctionDialogLookAt(unsigned int dialogId, uint8_t list, uint8_t slot);

void requestSelectionDialog(SelectionDialog *selectionDialog) override;
void executeSelectionDialog(unsigned int dialogId, bool success, SelectionDialog::index_type index);

Expand Down
103 changes: 103 additions & 0 deletions src/dialog/AuctionDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Illarionserver - server for the game Illarion
* Copyright 2011 Illarion e.V.
*
* This file is part of Illarionserver.
*
* Illarionserver is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Illarionserver is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* Illarionserver. If not, see <http://www.gnu.org/licenses/>.
*/

#include "dialog/AuctionDialog.hpp"

#include "data/Data.hpp"

AuctionDialog::AuctionDialog(const string &title, const luabind::object &callback)
: Dialog(title, "AuctionDialog", callback) {}

auto AuctionDialog::getOffersSize() const -> index_type { return offers.size(); }

auto AuctionDialog::getOffersBegin() const -> offer_iterator { return offers.cbegin(); }

auto AuctionDialog::getOffersEnd() const -> offer_iterator { return offers.cend(); }

void AuctionDialog::addOffer(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price) {
const auto &itemStruct = Data::items()[item];
addOffer(item, name, price, itemStruct.BuyStack);
}

void AuctionDialog::addOffer(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price, TYPE_OF_BUY_STACK stack) {
if (canAddOffer()) {
offers.emplace_back(item, name, price, stack);
}
}

auto AuctionDialog::getPrimaryRequestsSize() const -> index_type { return getProductsSize(primaryRequests); }

auto AuctionDialog::getPrimaryRequestsBegin() const -> product_iterator { return getProductsBegin(primaryRequests); }

auto AuctionDialog::getPrimaryRequestsEnd() const -> product_iterator { return getProductsEnd(primaryRequests); }

void AuctionDialog::addPrimaryRequest(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price) {
addProduct(primaryRequests, item, name, price);
}

auto AuctionDialog::getSecondaryRequestsSize() const -> index_type { return getProductsSize(secondaryRequests); }

auto AuctionDialog::getSecondaryRequestsBegin() const -> product_iterator {
return getProductsBegin(secondaryRequests);
}

auto AuctionDialog::getSecondaryRequestsEnd() const -> product_iterator { return getProductsEnd(secondaryRequests); }

void AuctionDialog::addSecondaryRequest(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price) {
addProduct(secondaryRequests, item, name, price);
}

auto AuctionDialog::getResult() const -> Result { return result; }

void AuctionDialog::setResult(Result result) { this->result = result; }

auto AuctionDialog::getPurchaseIndex() const -> index_type { return purchaseIndex; }

void AuctionDialog::setPurchaseIndex(index_type index) { purchaseIndex = index; }

auto AuctionDialog::getPurchaseAmount() const -> Item::number_type { return purchaseAmount; }

void AuctionDialog::setPurchaseAmount(Item::number_type amount) { purchaseAmount = amount; }

auto AuctionDialog::getSaleItem() const -> const ScriptItem & { return saleItem; }

void AuctionDialog::setSaleItem(const ScriptItem &item) { saleItem = item; }

auto AuctionDialog::getLookAtList() const -> ListType { return lookAtList; }

void AuctionDialog::setLookAtList(ListType list) { lookAtList = list; }

auto AuctionDialog::closeOnMove() const -> bool { return true; }

auto AuctionDialog::getProductsSize(const product_list &products) -> index_type { return products.size(); }

auto AuctionDialog::getProductsBegin(const product_list &products) -> product_iterator { return products.cbegin(); }

auto AuctionDialog::getProductsEnd(const product_list &products) -> product_iterator { return products.cend(); }

void AuctionDialog::addProduct(product_list &products, TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price) {
if (canAddProduct(products)) {
products.emplace_back(item, name, price);
}
}

auto AuctionDialog::canAddOffer() const -> bool { return offers.size() < MAXPRODUCTS; }

auto AuctionDialog::canAddProduct(const product_list &products) -> bool { return products.size() < MAXPRODUCTS; }
126 changes: 126 additions & 0 deletions src/dialog/AuctionDialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Illarionserver - server for the game Illarion
* Copyright 2011 Illarion e.V.
*
* This file is part of Illarionserver.
*
* Illarionserver is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Illarionserver is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* Illarionserver. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef AUCTION_DIALOG_HPP
#define AUCTION_DIALOG_HPP

#include "Item.hpp"
#include "dialog/Dialog.hpp"

#include <utility>
#include <vector>

using std::vector;

class Product {
private:
TYPE_OF_ITEM_ID item;
string name;
TYPE_OF_WORTH price;

public:
Product(TYPE_OF_ITEM_ID item, string name, TYPE_OF_WORTH price) : item(item), name(std::move(name)), price(price){};
[[nodiscard]] auto getItem() const -> TYPE_OF_ITEM_ID { return item; };
[[nodiscard]] auto getName() const -> const string & { return name; };
[[nodiscard]] auto getPrice() const -> TYPE_OF_WORTH { return price; };
};

class OfferProduct : public Product {
private:
TYPE_OF_BUY_STACK stack;

public:
OfferProduct(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price, TYPE_OF_BUY_STACK stack)
: Product(item, name, price), stack(stack){};
[[nodiscard]] auto getStack() const -> TYPE_OF_BUY_STACK { return stack; };
};

class AuctionDialog : public Dialog {
public:
using index_type = uint8_t;
using product_list = vector<Product>;
using product_iterator = product_list::const_iterator;
using offer_list = vector<OfferProduct>;
using offer_iterator = offer_list::const_iterator;

enum Result { playerAborts = 0, playerBids = 1, playerLooksAt = 2 };

enum ListType { listBid = 1 };

private:
static const uint32_t MAXPRODUCTS = 256;
offer_list offers;
product_list primaryRequests;
product_list secondaryRequests;

Result result{playerAborts};

index_type purchaseIndex{0};
Item::number_type purchaseAmount{0};

ScriptItem saleItem;

ListType lookAtList{listSell};

public:
AuctionDialog(const string &title, const luabind::object &callback);

auto getOffersSize() const -> index_type;
auto getOffersBegin() const -> offer_iterator;
auto getOffersEnd() const -> offer_iterator;
void addOffer(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price);
void addOffer(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price, TYPE_OF_BUY_STACK stack);

auto getPrimaryRequestsSize() const -> index_type;
auto getPrimaryRequestsBegin() const -> product_iterator;
auto getPrimaryRequestsEnd() const -> product_iterator;
void addPrimaryRequest(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price);

auto getSecondaryRequestsSize() const -> index_type;
auto getSecondaryRequestsBegin() const -> product_iterator;
auto getSecondaryRequestsEnd() const -> product_iterator;
void addSecondaryRequest(TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price);

auto getResult() const -> Result;
void setResult(Result result);

auto getPurchaseIndex() const -> index_type;
void setPurchaseIndex(index_type index);
auto getPurchaseAmount() const -> Item::number_type;
void setPurchaseAmount(Item::number_type amount);

auto getSaleItem() const -> const ScriptItem &;
void setSaleItem(const ScriptItem &item);

auto getLookAtList() const -> ListType;
void setLookAtList(ListType list);

auto closeOnMove() const -> bool override;

private:
static auto getProductsSize(const product_list &products) -> index_type;
static auto getProductsBegin(const product_list &products) -> product_iterator;
static auto getProductsEnd(const product_list &products) -> product_iterator;
static void addProduct(product_list &products, TYPE_OF_ITEM_ID item, const string &name, TYPE_OF_WORTH price);
auto canAddOffer() const -> bool;
static auto canAddProduct(const product_list &products) -> bool;
};

#endif
1 change: 1 addition & 0 deletions src/dialog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target_sources( dialog
Dialog.cpp
InputDialog.cpp
MerchantDialog.cpp
AuctionDialog.cpp
MessageDialog.cpp
SelectionDialog.cpp
)
Expand Down
1 change: 1 addition & 0 deletions src/netinterface/CommandFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CommandFactory::CommandFactory() {
templateList[C_MESSAGEDIALOG_TS] = std::make_unique<MessageDialogTS>();
templateList[C_INPUTDIALOG_TS] = std::make_unique<InputDialogTS>();
templateList[C_MERCHANTDIALOG_TS] = std::make_unique<MerchantDialogTS>();
templateList[C_AUCTIONDIALOG_TS] = std::make_unique<AuctionDialogTS>();
templateList[C_SELECTIONDIALOG_TS] = std::make_unique<SelectionDialogTS>();
templateList[C_CRAFTINGDIALOG_TS] = std::make_unique<CraftingDialogTS>();
templateList[C_LOGIN_TS] = std::make_unique<LoginCommandTS>();
Expand Down
Loading

0 comments on commit e134889

Please sign in to comment.