Player trading

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


Top

1 Objective

We want player to be able to interact with NPC, and to be able to exchange items (trade). The requirements are following:

There are also other requirements, such as player might not move to another map, while trading, etc. but they are not listed here, because the list would be very long...

2 Network messages

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:

The message in the following tables is just verbal description, it has nothing to do with class names or code names ×

Server to client messages:

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

3 Server side representation

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).


3.1 Item representation

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.

5 Client side representation


5.1 Item displayment

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.


5.2 User input

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)