Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Introduce Trade UI / Engine
  • Loading branch information
vthibault committed Jun 11, 2014
1 parent cef5be9 commit f78c687504844d866afc4d5783cf3e0bbd61a0c7
Showing 6 changed files with 802 additions and 10 deletions.
@@ -138,6 +138,7 @@ define(function( require )
require('./MapEngine/ChatRoom').call();
require('./MapEngine/Pet').call();
require('./MapEngine/Store').call();
require('./MapEngine/Trade').call();
}


@@ -0,0 +1,251 @@
/**
* Engine/MapEngine/Trade.js
*
* Manage Trade packets
* TODO: PACKET.CZ.REQ_EXCHANGE_ITEM
*
* This file is part of ROBrowser, Ragnarok Online in the Web Browser (http://www.robrowser.com/).
*
* @author Vincent Thibault
*/
define(function( require )
{
'use strict';


/**
* Load dependencies
*/
var DB = require('DB/DBManager');
var Network = require('Network/NetworkManager');
var PACKET = require('Network/PacketStructure');
var Trade = require('UI/Components/Trade/Trade');
var ChatBox = require('UI/Components/ChatBox/ChatBox');
var UIManager = require('UI/UIManager');


/**
* Convert GID to a random string
* It's used in the official client
*
* @param {number} GID
* @param {string} randrom string
*/
function TradeGIDEncoding(GID)
{
var table = 'ROHUTNASEW';
var str, out;
var i, count;

str = String(GID);
out = '';

for (i = 0, count = str.length; i < count; ++i) {
out += table[ str[i] ];
}

return out;
}


/**
* Someone ask to start a trade
* @param {object} pkt - PACKET.ZC.REQ_EXCHANGE_ITEM
*/
function onTradeRequest( pkt )
{
function answer(value) {
return function() {
var pkt = new PACKET.CZ.ACK_EXCHANGE_ITEM();
pkt.result = value;
Network.sendPacket(pkt);
};
}

var text = '(' + pkt.name + ') ' + DB.getMessage(93) + ('level' in pkt ? '\n' + 'Lv. ' + pkt.level : '');
Trade.title = pkt.name;

if ('level' in pkt) {
Trade.title += ' Lv' + pkt.level;
}

if ('GID' in pkt) {
Trade.title += ' (' + TradeGIDEncoding(pkt.GID) +')';
}

UIManager.showPromptBox( text, 'ok', 'cancel', answer(3), answer(4));
}


/**
* Result about trade ask
* @param {object} pkt - PACKET.ZC.ACK_EXCHANGE_ITEM
*/
function onTradeRequestAnswer( pkt )
{
switch (pkt.result) {
case 0: // Char is too far
ChatBox.addText( DB.getMessage(70), ChatBox.TYPE.ERROR);
break;


case 1: // Character does not exist
ChatBox.addText( DB.getMessage(71), ChatBox.TYPE.ERROR);
break;

case 2: // In another deal
ChatBox.addText( DB.getMessage(72), ChatBox.TYPE.ERROR);
break;

case 3:
Trade.append();
break;

case 4: // Cancel
ChatBox.addText( DB.getMessage(74), ChatBox.TYPE.ERROR);
break;

case 5: // AFK ?
break;
}
}


/**
* Try to add an item to the list
*
* @param {number} item index in inventory
* @param {number} count
*/
Trade.reqAddItem = function reqAddItem( index, count )
{
var pkt = new PACKET.CZ.ADD_EXCHANGE_ITEM();
pkt.index = index;
pkt.count = count;

Network.sendPacket(pkt);
};


/**
* Response from the server when requesting to add an item.
*
* @param {object} pkt - PACKET.ZC.ACK_ADD_EXCHANGE_ITEM
*/
function onAddItemResult( pkt )
{
switch (pkt.result) {
case 1: // overweight
ChatBox.addText( DB.getMessage(73), ChatBox.TYPE.ERROR);
break;

case 2: // trade canceled
ChatBox.addText( DB.getMessage(74), ChatBox.TYPE.ERROR);
break;
}

Trade.addItemFromInventory(pkt.Index, pkt.result === 0);
}


/**
* Oher user added another item
*
* @param {object} pkt - PACKET.ZC.ADD_EXCHANGE_ITEM
*/
function onItemAdded( pkt )
{
Trade.addItem(pkt);
}


/**
* Reject deal
*/
Trade.onCancel = function onCancel()
{
var pkt = new PACKET.CZ.CANCEL_EXCHANGE_ITEM();
Network.sendPacket(pkt);
};


/**
* Deal canceled
*
* @param {object} pkt - PACKET.ZC.CANCEL_EXCHANGE_ITEM
*/
function onTradeCancel( pkt )
{
ChatBox.addText( DB.getMessage(74), ChatBox.TYPE.ERROR);
Trade.remove();
}


/**
* Conclude the deal
*/
Trade.onConclude = function onConclude()
{
var pkt = new PACKET.CZ.CONCLUDE_EXCHANGE_ITEM();
Network.sendPacket(pkt);
};


/**
* Conclude a part of the trade
*
* @param {object} pkt - PACKET.ZC.CONCLUDE_EXCHANGE_ITEM
*/
function onTradeConclude( pkt )
{
Trade.conclude( pkt.who ? 'recv' : 'send' );
}


/**
* Submit the trade
*/
Trade.onTradeSubmit = function onTradeSubmit()
{
var pkt = new PACKET.CZ.EXEC_EXCHANGE_ITEM();
Network.sendPacket(pkt);
};


/**
* Result of the deal
*
* @param {object} pkt - PACKET.ZC.EXEC_EXCHANGE_ITEM
*/
function onTradeSubmitAnswer( pkt )
{
// Fail
if (pkt.result === 1) {
ChatBox.addText( DB.getMessage(76), ChatBox.TYPE.ERROR);
Trade.remove();
return;
}

//TODO: Give items...
ChatBox.addText( DB.getMessage(75), ChatBox.TYPE.BLUE);
Trade.remove();
}


/**
* Initialize
*/
return function MainEngine()
{
Network.hookPacket(PACKET.ZC.REQ_EXCHANGE_ITEM, onTradeRequest);
Network.hookPacket(PACKET.ZC.REQ_EXCHANGE_ITEM2, onTradeRequest);
Network.hookPacket(PACKET.ZC.ACK_EXCHANGE_ITEM, onTradeRequestAnswer);
Network.hookPacket(PACKET.ZC.ACK_EXCHANGE_ITEM2, onTradeRequestAnswer);
Network.hookPacket(PACKET.ZC.ACK_ADD_EXCHANGE_ITEM, onAddItemResult);
Network.hookPacket(PACKET.ZC.ADD_EXCHANGE_ITEM, onItemAdded);
Network.hookPacket(PACKET.ZC.ADD_EXCHANGE_ITEM2, onItemAdded);
Network.hookPacket(PACKET.ZC.CANCEL_EXCHANGE_ITEM, onTradeCancel);
Network.hookPacket(PACKET.ZC.CONCLUDE_EXCHANGE_ITEM, onTradeConclude);
Network.hookPacket(PACKET.ZC.EXEC_EXCHANGE_ITEM, onTradeSubmitAnswer);
};
});
@@ -96,13 +96,13 @@ define( ['./PacketStructure' ], function( PACKET )
0xdd: PACKET.ZC.MEMBER_EXIT, // ok
0xdf: PACKET.ZC.CHANGE_CHATROOM, // ok
0xe1: PACKET.ZC.ROLE_CHANGE, // ok
0xe5: PACKET.ZC.REQ_EXCHANGE_ITEM,
0xe7: PACKET.ZC.ACK_EXCHANGE_ITEM,
0xe9: PACKET.ZC.ADD_EXCHANGE_ITEM,
0xea: PACKET.ZC.ACK_ADD_EXCHANGE_ITEM,
0xec: PACKET.ZC.CONCLUDE_EXCHANGE_ITEM,
0xee: PACKET.ZC.CANCEL_EXCHANGE_ITEM,
0xf0: PACKET.ZC.EXEC_EXCHANGE_ITEM,
0xe5: PACKET.ZC.REQ_EXCHANGE_ITEM, // ok
0xe7: PACKET.ZC.ACK_EXCHANGE_ITEM, //ok
0xe9: PACKET.ZC.ADD_EXCHANGE_ITEM, // ok
0xea: PACKET.ZC.ACK_ADD_EXCHANGE_ITEM, // ok
0xec: PACKET.ZC.CONCLUDE_EXCHANGE_ITEM, // ok
0xee: PACKET.ZC.CANCEL_EXCHANGE_ITEM, // ok
0xf0: PACKET.ZC.EXEC_EXCHANGE_ITEM, // ok
0xf1: PACKET.ZC.EXCHANGEITEM_UNDO,
0xf2: PACKET.ZC.NOTIFY_STOREITEM_COUNTINFO, // ok
0xf4: PACKET.ZC.ADD_ITEM_TO_STORE, // ok
@@ -270,8 +270,8 @@ define( ['./PacketStructure' ], function( PACKET )
0x1f1: PACKET.AC.NOTIFY_ERROR,
0x1f2: PACKET.ZC.UPDATE_CHARSTAT2,
0x1f3: PACKET.ZC.NOTIFY_EFFECT2, // ok
0x1f4: PACKET.ZC.REQ_EXCHANGE_ITEM2,
0x1f5: PACKET.ZC.ACK_EXCHANGE_ITEM2,
0x1f4: PACKET.ZC.REQ_EXCHANGE_ITEM2, // ok
0x1f5: PACKET.ZC.ACK_EXCHANGE_ITEM2, // ok
0x1f6: PACKET.ZC.REQ_BABY,
0x1f8: PACKET.ZC.START_BABY,
0x1fc: PACKET.ZC.REPAIRITEMLIST,
@@ -479,7 +479,7 @@ define( ['./PacketStructure' ], function( PACKET )
0x80b: PACKET.ZC.PARTY_BOOKING_NOTIFY_DELETE,
0x80d: PACKET.ZC.SIMPLE_CASH_BTNSHOW,
0x80e: PACKET.ZC.NOTIFY_HP_TO_GROUPM_R2, // ok
0x80f: PACKET.ZC.ADD_EXCHANGE_ITEM2,
0x80f: PACKET.ZC.ADD_EXCHANGE_ITEM2, // ok
0x810: PACKET.ZC.OPEN_BUYING_STORE,
0x812: PACKET.ZC.FAILED_OPEN_BUYING_STORE_TO_BUYER,
0x813: PACKET.ZC.MYITEMLIST_BUYING_STORE,

0 comments on commit f78c687

Please sign in to comment.