Skip to content

Commit 1e0f5d9

Browse files
committed
Trading offers and counter offer windows implemented
1 parent d0beb1d commit 1e0f5d9

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

include/GameController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GameController {
3333
bool handleRoadButtonEvent(ScreenCoordinate);
3434
bool handleSettlementButtonEvent(ScreenCoordinate);
3535
bool handlePlayerClick(ScreenCoordinate, Player&);
36-
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving);
36+
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving, std::array<int, 5>);
3737
};
3838

3939
#endif

include/GameView.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,17 @@ class TradingView : public ViewElement {
184184
Player& initiating;
185185
Player& receiving;
186186

187-
std::array<int, 5> offer;
188-
189187
ViewButtonText trade;
190188
ViewButtonText cancel;
191189

190+
std::array<int, 5> offer;
191+
192192
TradingView(TradingView& o) = delete;
193193
TradingView& operator=(TradingView& o) = delete;
194194
protected:
195195
virtual bool clicked(ScreenCoordinate coord);
196196
public:
197-
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel);
197+
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
198198
virtual ~TradingView();
199199

200200
void render();

src/GameController.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ bool GameController::handleSettlementButtonEvent(ScreenCoordinate coord) {
8383
return true;
8484
}
8585

86+
template<int size>
87+
auto negativeArr(std::array<int, size> arr) -> std::array<int, size> {
88+
for(auto& it : arr) {
89+
it = -it;
90+
}
91+
return arr;
92+
}
93+
8694
/**
8795
* Handles a click on one of the Player names at the top right of the screen.
8896
* @param coord The coordinate clicked on.
@@ -94,13 +102,21 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
94102
Player& receiving = player;
95103
auto priority = -10;
96104

97-
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
105+
std::array<int, 5> initial = {0, 0, 0, 0, 0};
106+
107+
//std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
108+
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction([this, &initiating, &receiving](std::array<int, 5> offer, ScreenCoordinate coord) {
109+
std::array<int, 5> initial = {0, 0, 0, 0, 0};
110+
std::array<int, 5> reverseOffer = negativeArr<5>(offer);
111+
handleTradeOffer(coord, receiving, initial, receiving, reverseOffer);
112+
return true;
113+
});
98114
std::function<bool(ScreenCoordinate)> cancelFunction([this, priority](ScreenCoordinate coord) {
99115
view.removeElement(priority);
100116
return true;
101117
});
102118

103-
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction)));
119+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, initial)));
104120
std::cout << player.getName() << std::endl;
105121
return true;
106122
}
@@ -112,12 +128,25 @@ bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
112128
* @param offer The offer the player is giving.
113129
* @param receiving The other player in the trade.
114130
*/
115-
bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating, std::array<int, 5> offer, Player& receiving) {
116-
std::cout << "Received trade offer of ";
117-
for(auto& it : offer) {
118-
std::cout << it << " ";
131+
bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating, std::array<int, 5> offer, Player& receiving, std::array<int, 5> counterOffer) {
132+
auto priority = -10;
133+
if(offer == negativeArr<5>(counterOffer)) {
134+
view.removeElement(priority);
135+
//TODO: perform trade
136+
} else {
137+
//std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
138+
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction([this, &initiating, &receiving, counterOffer](std::array<int, 5> offer, ScreenCoordinate coord) {
139+
std::array<int, 5> reverseOffer = negativeArr<5>(offer);
140+
handleTradeOffer(coord, receiving, counterOffer, receiving, reverseOffer);
141+
return true;
142+
});
143+
std::function<bool(ScreenCoordinate)> cancelFunction([this, priority](ScreenCoordinate coord) {
144+
view.removeElement(priority);
145+
return true;
146+
});
147+
148+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction, counterOffer)));
119149
}
120-
std::cout << std::endl;
121150
return true;
122151
}
123152

src/GameView.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,12 @@ void DrawingGameVisitor::visit(DevelopmentCard& card) {
494494

495495
}
496496

497-
TradingView::TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel) : ViewElement({{0.1, 0.1},{0.9, 0.9}}), initiating(initiating), receiving(receiving),
497+
TradingView::TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> initialOffer) :
498+
ViewElement({{0.1, 0.1},{0.9, 0.9}}), initiating(initiating), receiving(receiving),
498499
trade(std::bind(trade, std::ref(offer), std::placeholders::_1), {{0.7, 0.1}, {0.9, 0.2}}, "resources/TypeWritersSubstitute-Black.ttf", 50, "Trade"),
499-
cancel(cancel, {{0.1, 0.1}, {0.3, 0.2}}, "resources/TypeWritersSubstitute-Black.ttf", 50, "Cancel") {
500+
cancel(cancel, {{0.1, 0.1}, {0.3, 0.2}}, "resources/TypeWritersSubstitute-Black.ttf", 50, "Cancel"),
501+
offer(initialOffer) {
500502

501-
for(auto& res : offer) {
502-
res = 0;
503-
}
504503
}
505504

506505
TradingView::~TradingView() {

0 commit comments

Comments
 (0)