//constants
var URL = 'https://cryptokingdom.me/api/';

//dependencies
var request = require('request');

//Constructor
function CryptoKingdom(username, password, deviceID){
        this.username = username,
        this.password = password,
        this.deviceID = deviceID
}


CryptoKingdom.prototype = {
    constructor: CryptoKingdom,

    //general API request
    request: function(options, cb){
        if(!('headers' in options)){
            options.headers = {};
        }
        options.headers['content-type'] = 'application/json';
        options.json = true;
        request(options, function(err, response, body){
            cb(err, body);
        });
        return this;
    },

    //public API request
    public: function(item, cb){
        var options = {
            method: 'GET',
            url: URL + 'getStateForItem/' + item,
        };
        return this.request(options, cb);
    },

    //private API request
    private: function(params, cb){
        var options = {
            method: 'POST',
            url: URL + 'post-command',
            body: params
        };
        if(!params.command){
            options.url = URL + 'itemOwnership';
        }
        return this.request(options, cb);
    },

    //PUBLIC METHODS

    getStateForItem: function(item, cb){
        return this.public(item, cb);
    },

    //PRIVATE METHODS

    //login to receive token
    getToken: function(params, cb){
        var options = {
            method: 'POST',
            url: URL + 'login',
            body: params
        };
        options.body.jsonrpc = '2.0';
        options.body.id = '0';
        return this.request(options, cb);
    },

    //place buy order (bid)
    buy: function(token, item, quantity, price, cb){
        var params = {
            token: token,
            command: 'buy',
            itemID: item,
            price: price,
            amount: quantity
        };
        return this.private(params, cb);
    },

    //place sell order (ask)
    sell: function(token, item, quantity, price, cb){
        var params = {
            token: token,
            command: 'sell',
            itemID: item,
            price: price,
            amount: quantity
        };
        return this.private(params, cb);
    },

    //cancel ALL orders for a certain item
    cancelAll: function(token, item, cb){
        var params = {
            token: token,
            command: 'cancelall',
            itemID: item
        };
        return this.private(params, cb);
    },

    cancelBid: function(token, item, amount, price, cb){
        var params = {
            token: token,
            command: 'cancelbid',
            itemID: item,
            amount: amount,
            price: price
        };
        return this.private(params, cb);
    },

    cancelAsk: function(token, item, amount, price, cb){
        var params = {
            token: token,
            command: 'cancelask',
            itemID: item,
            amount: amount,
            price: price
        };
        return this.private(params, cb);
    },

    give: function(token, item, amount, recipient, cb){
        var params = {
            token: token,
            command: 'give',
            itemID: item,
            amount: amount,
            characterID: recipient
        };
        return this.private(params, cb);
    },

    consume: function(token, item, amount, note, cb){
        var params = {
            token: token,
            command: 'consume',
            itemID: item,
            amount: amount,
            note: note
        };
        return this.private(params, cb);
    },

    create: function(token, item, amount, recipient, note, cb){
        var params = {
            token: token,
            command: 'create',
            itemID: item,
            amount: amount,
            characterID: recipient,
            note: note
        };
        return this.private(params, cb);
    },

    transfer: function(token, item, amount, source, recipient, cb){
        var params = {
            token: token,
            command: 'transfer',
            itemID: item,
            amount: amount,
            transferFrom: source,
            transferTo: recipient
        };
        return this.private(params, cb);
    },
    // Returns a list of all items owned by the character
    itemOwnership: function(token, item, cb) {
        var params = {
            token: token,
            itemID: item
        };
        return this.private(params, cb);
    }

};

module.exports = CryptoKingdom;
@@ -639,22 +639,156 @@
     
-cb(err, body);
+if(options.body.command == 'owns') %7B%0A                cb(err, transformOwns(body));%0A            %7D else %7B%0A                cb(err, body);%0A            %7D
 %0A   
@@ -977,25 +977,24 @@
 tem/' + item
-,
 %0A        %7D;%0A
@@ -4660,41 +4660,1093 @@
    %7D
-%0A%0A%7D;%0A%0Amodule.exports = CryptoKingdom;
+,%0A    %0A    owns: function(token, item, cb) %7B%0A        var params = %7B%0A            token: token,%0A            command: 'owns',%0A            itemID: item%0A        %7D;%0A        return this.private(params, cb);%0A    %7D%0A%0A%7D;%0A%0Amodule.exports = CryptoKingdom;%0A%0A// helper function for parsing HTML and sorting player owns list%0Afunction transformOwns(data)%7B%0A    let owns = data.split('%3Cbr%3E');%0A    let filtered = owns.filter(function(item)%7B%0A        if(item.startsWith('%3Cstrong%3E'))%7B%0A            return item;%0A        %7D%0A    %7D);%0A    let arr = filtered.map(function(item) %7B%0A        let player = %7B%7D;%0A        let temp = item.split('%3Cstrong%3E');%0A        temp.shift();%0A        let data = temp%5B0%5D.split('%3CSTRONG%3E');%0A        data%5B1%5D = data%5B1%5D.split(':').pop().trim();%0A        player.name = data%5B0%5D;%0A        player.balance = parseFloat(data%5B1%5D.replace(/,/g, ''));%0A        return player;%0A    %7D);%0A%0A    let list = arr.sort(function(a, b) %7B%0A        if(a.balance %3C b.balance)%7B%0A            return 1;%0A        %7D%0A        if(a.balance %3E b.balance)%7B%0A            return -1;%0A        %7D%0A        return 0;%0A    %7D);%0A    %0A    return list;%0A%7D%0A

add owns method