Skip to content

Commit

Permalink
feat: Add trade request functionality to GameNetwork
Browse files Browse the repository at this point in the history
- Implemented the `requestTrade` method in `GameNetwork` to facilitate sending trade requests between players.
- The trade request now includes the trade offer details and the responder player's ID.
- Added the "game-client:requestTrade" event emission to handle the trade request data.
- Updated UI interactions to handle incoming trade requests, allowing players to accept or reject the trades.
- Integrated trade response handling to notify the server of the player's decision.
  • Loading branch information
TKanX committed Aug 28, 2024
1 parent cafd9dc commit cb16eb1
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions public/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ class GameNetwork {
this.socket.emit("game-client:toss", {});
}

/**
* Place a piece.
* @param {number} position - The position index.
* @param {string} pieceIndex - The piece index.
*/
placePiece(position, pieceIndex) {
this.socket.emit("game-client:placePiece", { position, pieceIndex });
}

/**
* Trade request.
* @param {Object} offer - The trade offer.
* @param {string} responder - The responder player ID.
*/
requestTrade(offer, responder) {
this.socket.emit("game-client:requestTrade", { offer, responder });
}

/**
* Trade response.
* @param {boolean} accepted - The trade accepted.
*/
respondTrade(accepted) {
this.socket.emit("game-client:tradeResponse", { accepted });
}

/**
* Add listener.
* @param {string} event - The event name.
Expand Down Expand Up @@ -1289,10 +1315,23 @@ class Game {

if (playerId === this.network.userId) {
if (type === "normal") {
this.ui.showTrade(this.pieces, (selectedPiece) => {
this.renderer.hideAvailablePositions();
this.ui.hideTrade();
});
this.ui.showTrade(
this.pieces,
(selectedPiece, selectedOtherPlayerPiece) => {
this.renderer.hideAvailablePositions();
this.ui.hideTrade();

const tradeOffer = {
requesterPieceIndex: selectedPiece.pieceIndex,
responderPieceIndex: selectedOtherPlayerPiece.pieceIndex,
};

this.network.requestTrade(
tradeOffer,
selectedOtherPlayerPiece.player
);
}
);

this.renderer.showAvailablePositions(availablePositions);
}
Expand All @@ -1307,10 +1346,37 @@ class Game {
});
});

this.network.addListener("game:tradeRequest", (data) => {
const requester = this.players.find(
(player) => player.id === data.requester
);
const requestedPiece = this.pieces.find(
(player) => player.id === this.network.userId
).pieces[data.offer.responderPieceIndex];
const offeredPiece = this.pieces.find(
(player) => player.id === data.requester
).pieces[data.offer.requesterPieceIndex];

this.ui.showTradeRequest(
requester,
requestedPiece,
offeredPiece,
() => {
this.network.respondTrade(true);
this.ui.hideTradeRequest();
},
() => {
this.network.respondTrade(false);
this.ui.hideTradeRequest();
}
);
});

this.network.addListener("game:turnEnd", (data) => {
this.ui.endPlayerTurn(data.player);
this.ui.hideTossButton();
this.ui.hideTrade();
this.ui.hideTradeRequest();
this.renderer.hideAvailablePositions();

this.ui.showGamePhase("Turn End", 2000);
Expand Down

0 comments on commit cb16eb1

Please sign in to comment.