0.1.4 • Published 5 years ago

tradr v0.1.4

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

tradr

Tradr is a simple package to facilitate buying and selling of "items" between users.

Install

npm install -s tradr

It's as simple as that. Nothing hard.

Usage

Users

Creating a user is simple with the User class. A user has 1 required parameter, the id parameter. This keeps track of who is buying what from whom, and ensures that a user can't buy from itself. You can keep track of the ID on your own. You can also define an inventory object, which is an object with indices of numbers (item IDs), as well as a money number, which keeps track of the user's current money.

import { User } from 'tradr';

// User ID 0 with 10 of item 0,.
const user1 = new User (0, {0: 10});
// User ID 1, with 5 of item 0, but with $100.
const user2 = new User (0, {0: 5}, 100);

Creating a trade

To create a trade, first you must decide whether a trade is a buy trade or a sell trade. Buy trades are created with the BuyTrade class and sell trades are created with the SellTrade class.

Both trade types have 4 parameters; id, item, price, and owner. The id is a number type. The item type is an object with keys and values of numbers. The keys represent the item and the values represent the amount of each. The price is the maximum price in the case of a buy order and the minimum price in the case of a sell order. Finally, the owner parameter represents the owner, which is a User class.

import { BuyTrade, SellTrade } from "tradr";

// Buying 10 item 0's for up to 100 dollars.
const buyTrade = new BuyTrade (0, {0: 10}, 100, user1);
// Selling 10 item 0's for at least 50 dollars.
const sellTrade = new SellTrade (0, {0: 10}, 50, user2);

Trade fulfillment

Trades can be fulfilled by either a buyer or a seller, so long as they are compatible. The fulfiller is the one "doing the favor" for the fulfillee; if the fulfiller can get a better price than they bargained for, they will.

// Buyer is buying 10 items for up to 100 dollars, seller is selling items for at least 50 dollars.

buyTrade.fulfillTrade(sellTrade);
// The buyer is "doing the favor", so he will try to get the best bargain and pay only 50 dollars.

sellTrade.fulfillTrade(buyTrade);
// The seller is "doing the favor", so he will try to get the best bargain and make 100 dollars.