forked from IBM-Blockchain-Archive/marbles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_part2.js
154 lines (144 loc) · 4.07 KB
/
ws_part2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// ==================================
// Part 2 - incoming messages, look for type
// ==================================
var ibc = {};
var chaincode = {};
var async = require('async');
module.exports.setup = function(sdk, cc){
ibc = sdk;
chaincode = cc;
};
module.exports.process_msg = function(ws, data){
if(data.v === 2){
if(data.type == 'create'){
console.log('its a create!');
if(data.name && data.color && data.size && data.user){
chaincode.init_marble([data.name, data.color, data.size, data.user], cb_invoked); //create a new marble
}
}
else if(data.type == 'get'){
console.log('get marbles msg');
chaincode.read('_marbleindex', cb_got_index);
}
else if(data.type == 'transfer'){
console.log('transfering msg');
if(data.name && data.user){
chaincode.set_user([data.name, data.user]);
}
}
else if(data.type == 'remove'){
console.log('removing msg');
if(data.name){
chaincode.remove(data.name);
}
}
else if(data.type == 'chainstats'){
console.log('chainstats msg');
ibc.chain_stats(cb_chainstats);
}
else if(data.type == 'open_trade'){
console.log('open_trade msg');
if(!data.willing || data.willing.length < 0){
console.log('error, "willing" is empty');
}
else if(!data.want){
console.log('error, "want" is empty');
}
else{
var args = [data.user, data.want.color, data.want.size];
for(var i in data.willing){
args.push(data.willing[i].color);
args.push(data.willing[i].size);
}
chaincode.open_trade(args);
}
}
else if(data.type == 'get_open_trades'){
console.log('get open trades msg');
chaincode.read('_opentrades', cb_got_trades);
}
else if(data.type == 'perform_trade'){
console.log('perform trade msg');
chaincode.perform_trade([data.id, data.closer.user, data.closer.name, data.opener.user, data.opener.color, data.opener.size]);
}
else if(data.type == 'remove_trade'){
console.log('remove trade msg');
chaincode.remove_trade([data.id]);
}
}
//got the marble index, lets get each marble
function cb_got_index(e, index){
if(e != null) console.log('error:', e);
else{
try{
var json = JSON.parse(index);
for(var i in json){
console.log('!', i, json[i]);
chaincode.read(json[i], cb_got_marble); //iter over each, read their values
}
}
catch(e){
console.log('error:', e);
}
}
}
//call back for getting a marble, lets send a message
function cb_got_marble(e, marble){
if(e != null) console.log('error:', e);
else {
sendMsg({msg: 'marbles', marble: marble});
}
}
function cb_invoked(e, a){
console.log('response: ', e, a);
}
//call back for getting the blockchain stats, lets get the block height now
var chain_stats = {};
function cb_chainstats(e, stats){
chain_stats = stats;
if(stats && stats.height){
var list = [];
for(var i = stats.height - 1; i >= 1; i--){ //create a list of heights we need
list.push(i);
if(list.length >= 8) break;
}
list.reverse(); //flip it so order is correct in UI
console.log(list);
async.eachLimit(list, 1, function(key, cb) { //iter through each one, and send it
ibc.block_stats(key, function(e, stats){
if(e == null){
stats.height = key;
sendMsg({msg: 'chainstats', e: e, chainstats: chain_stats, blockstats: stats});
}
cb(null);
});
}, function() {
});
}
}
//call bacak for getting a block's stats, lets send the chain/block stats
function cb_blockstats(e, stats){
if(chain_stats.height) stats.height = chain_stats.height - 1;
sendMsg({msg: 'chainstats', e: e, chainstats: chain_stats, blockstats: stats});
}
//call back for getting open trades, lets send the trades
function cb_got_trades(e, trades){
if(e != null) console.log('error:', e);
else {
if(trades && trades.open_trades){
sendMsg({msg: 'open_trades', open_trades: trades.open_trades});
}
}
}
//send a message, socket might be closed...
function sendMsg(json){
if(ws){
try{
ws.send(JSON.stringify(json));
}
catch(e){
console.log('error ws', e);
}
}
}
};