This describes how trading between players is implemented.
Make sure you have red player trading is implemented, because
this solution uses some of the player trading features
This solution also uses principles explained in previous parts of documentation
This is sub-layout for documentation pages
We want player to be able to interact with NPC, and to be able to exchange items (trade). The requirements are following:
As another step of analysis, let's have a look at messages, that will have to be sent between server and client to accomplish this task:
Message | Explanation |
---|---|
Request to use an useable item | Informs server that player wants to interact with the object (NPC) |
Trade window open | Informs client that trade window shaould be opened. Also send along with that all items currently in the stock |
Trade window close | Client will close trade window |
Add / remove item from inventory | Explained in inventory item management |
Client to server messages:
Message | Explanation |
---|---|
Request to use an useable item | Informs server that player wants to interact with the object (NPC) |
TradeSellItem / TradeBuyItem | Requests to buy / sell an item |
The requirement here is, that the shop stock must be same for all players in current map.
Therefore, the stock data must be held somewhere on map level (= same for all players in the map).
We could create an inventory instance in server side, allowing us to create an inventory grid,
and move items around.
However, this is memory heavy, and really not needed,
since items inside the shop shock will never be moving back and forth as in normal inventory.
We can be happy with only a linked list (or hashMap) of items in stock,
where then we just display it on client side in the inventory grid.
In this case, we want to display the nice, styled inventory grid.
Therefore, a new instance of inventoryHtmlInstance is created, with fixed size.
Then, we add there all the items received from the server (in same order),
achieving an effect of same displayment for all clients.
For recognizing, whether user click on item in inventory shall allow sell option,
we simply ask, whether the HTML NPC trade window is visible.
If so, we allow such an option and send rfequest to the server.
This can be seemed as INSECURE, but it really is not, because server will validate, whether player is
close enough to the NPC to sell items (or whatever validation)