@@ -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
0 commit comments