-
Notifications
You must be signed in to change notification settings - Fork 1
/
bankserver.em
282 lines (247 loc) · 8.76 KB
/
bankserver.em
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
system.require('std/core/bind.em');
if (typeof(bank) === 'undefined')
bank = {};
bank = system.Class.extend({
init: function(bankKey) {
system.print('Init bank');
this.userPwordMap = {};
this.presUserMap = {};
this.acctBalanceMap = {};
this.presOwnMap = {};
this.connected = false;
this.key = bankKey;
this.connectedBanks = [];
std.core.bind(this.loginRequest, this) << {'action':'login':};
std.core.bind(this.createRequest, this) << {'action':'create':};
std.core.bind(this.getRequest, this) << {'action':'get':};
std.core.bind(this.giveRequest, this) << {'action':'give':};
std.core.bind(this.tradeRequest, this) << {'action':'trade':};
// std.core.bind(this.connectBank, this) << {'request':'bankConnect':};
},
loginRequest: function (msg, sender) {
system.print('Received login request.');
var newMsg = {};
if (msg.username in this.userPwordMap) {
if (this.userPwordMap[msg.username].password == msg.password) {
newMsg.print = 'Successfully logged in.';
this.userPwordMap[msg.username].presence == sender.toString();
this.presUserMap[sender.toString()] = msg.username;
if(!(sender.toString()) in this.presOwnMap)
this.presOwnMap[sender.toString()] = msg.username;
} else {
newMsg.error = 'Incorrect password.';
}
} else {
newMsg.print = 'I do not recognize this username. Try creating a new account.';
newMsg.error = 'Unrecognized username.';
//this.createRequest(msg, sender);
}
msg.makeReply(newMsg) >> [];
},
createRequest: function (msg, sender) {
system.print('Received create request.');
var newMsg = {};
if (msg.username in this.userPwordMap) {
newMsg.error = 'Username already exists.';
} else {
var account = {};
account.password = msg.password;
account.presence = sender.toString();
this.userPwordMap[msg.username] = account;
this.presUserMap[sender.toString()] = msg.username;
if(!(sender.toString()) in this.presOwnMap)
this.presOwnMap[sender.toString()] = msg.username;
this.acctBalanceMap[msg.username] = 100;
newMsg.print = 'Successfully created account.';
if (msg.username in this.userPwordMap)
system.print('Really successfully created account.');
}
msg.makeReply(newMsg) >> [];
},
checkTransaction: function(sender, what, recipient, inExchangeFor) {
if (!(sender.toString() in this.presUserMap))
return 'Not logged in.';
if (!(recipient in this.userPwordMap))
return 'Unknown recipient username "' + recipient + '".';
if (what.type == 'money' && what.value >
this.acctBalanceMap[this.presUserMap[sender.toString()]]) {
return 'You do not have $' + what.value +
' in your account.';
} else if(what.type == 'presence' &&
(what.value in this.presOwnMap &&
this.presOwnMap[what.value] !=
this.presUserMap[sender.toString()])) {
return what.value + ' belongs to ' + this.presOwnMap[what.value] +
'.';
}
if (typeof(inExchangeFor) === 'undefined')
return true;
if (inExchangeFor.type == 'money' &&
inExchangeFor.value > this.acctBalanceMap[recipient]) {
return recipient + ' does not have $' + what.value +
' in his/her account.';
} else if(inExchangeFor.type == 'presence' &&
(inExchangeFor.value in this.presOwnMap &&
this.presOwnMap[inExchangeFor.value] != recipient)) {
return inExchangeFor.value + ' belongs to ' +
this.presOwnMap[inExchangeFor.value] + '.';
}
return true;
},
give: function(what, from, to) {
if(what.type == 'money') {
this.acctBalanceMap[from] -= msg.what.value;
this.acctBalanceMap[msg.to] += msg.what.value;
return from + ' successfully gave $' + what.value +
' to ' + to + '.';
} else if(what.type == 'presence') {
this.presOwnMap[what.value] = to;
return from + ' successfully gave ' + what.value + ' to ' +
to + '.';
} else {
throw('Unrecognized property type: "' + msg.what.type + '".');
}
},
giveRequest: function (msg, sender) {
system.print('Received give request.');
var replyMsg = {};
var status = this.checkTransaction(sender, msg.what, msg.to);
if (typeof(status) === 'string') {
replyMsg.error = status;
msg.makeReply(replyMsg) >> [];
return;
}
try {
replyMsg.print = this.give(msg.what,
this.presUserMap[sender.toString()], msg.to);
} catch(e) {
replyMsg.error = e;
}
msg.makeReply(replyMsg) >> [];
},
tradeRequest: function(msg, sender) {
system.print('Received trade request.');
var replyMsg = {};
var status = this.checkTransaction(sender, msg.what, msg.to, msg['for']);
if (typeof(status) === 'string') {
replyMsg.error = status;
msg.makeReply(replyMsg) >> [];
return;
}
replyMsg.seqNo = msg.sequenceNo;
replyMsg.id = msg.streamid;
var selfid = system.self.toString();
var line = 'var bank ="' + selfid + '"; \n';
function clientGUI() {
var bankVis = system.createVisible(bank);
var x = @sirikata.ui(
'trade',
function() {
$('<div id="trade-ui" title="Trade">' +
' <button id="agree-button">Yes!</button>' +
' <button id="decline-button">No!</button>' +
'</div>').appendTo('body');
var window = new sirikata.ui.window(
"#trade-ui",
{
width: 100,
height: 'auto'
}
);
window.show();
sirikata.ui.button('#agree-button').click(acceptTrade);
sirikata.ui.button('#decline-button').click(declineTrade);
function acceptTrade() {
window.toggle();
sirikata.event('trade', 'accept');
}
function declineTrade() {
window.toggle();
sirikata.event('trade', 'decline');
}
}
);@;
system.require('std/core/bind.em');
function z(cmd) {
var rmsg = new Object();
rmsg.resp = cmd;
rmsg >> bankVis >> [];
}
simulator._simulator.addGUITextModule("trade", x, function(gui) {gui.bind("trade", std.core.bind(z,this));});
}
var GUIstring = clientGUI.toString();
var GUIStr = line + GUIstring;
GUIStr += "\n clientGUI();";
var request = {
request : 'script',
script : GUIStr
};
var user = this.presUserMap[sender.toString()];
var targetVis = system.createVisible(this.userPwordMap[msg.to].presence);
std.core.bind(this.acceptTrade, this) << {'resp':'accept':} << targetVis;
std.core.bind(this.declineTrade, this) << {'resp':'decline':} << targetVis;
request >> targetVis >> [];
msg.makeReply(replyMsg) >> [];
/*
var approvalHandle;
var onTradeApproved = function(approvalMsg, approvalSender) {
if(this.presUserMap[approvalSender.toString()] == msg.to) {
confirmMsg = {status: 'success', seqNo: replyMsg.seqNo,
id: replyMsg.id};
try {
confirmMsg.print = this.give(msg.what, user, msg.to) + '\n' +
this.give(msg['for'], msg.to, user);
} catch(e) {
confirmMsg.status = 'failure';
confirmMsg.error = e;
}
confirmMsg >> sender >> [];
approvalHandle.cancel();
}
};
approvalHandle = (onTradeApproved << [{'action':'trade':},
{'what':msg['for']:},
{'to':user:},
{'for':msg.what:}]);*/
},
connectBank: function (bank, bankKey, bankKeyConnect) {
if (bankKey == this.key) {
var msg = {};
msg.request = 'bankConnect';
msg.key = bankKeyConnect;
msg >> bank >> [std.core.bind(this.setConnection, this)];
}
},
setConnection: function (msg, sender) {
this.connected = true;
this.connectedBanks.push(sender.toString());
},
handleConnection: function (msg, sender) {
if (msg.key == this.key) {
this.connected = true;
this.connectedBanks.push(sender.toString());
var reply = {};
reply.msg = 'Connected.';
msg.makeReply(reply) >> [];
}
},
getRequest: function (msg, sender) {
var user = this.presUserMap[sender.toString()];
system.print('Received get request from ' + user + '.\n');
system.print(this.userPwordMap);
var replyMsg = {};
if (this.userPwordMap[user].presence == sender.toString()) {
if(msg.key.type == 'money') {
replyMsg.value = this.acctBalanceMap[user];
} else if(msg.key.type == 'presence') {
if(msg.key.value in this.presOwnMap)
replyMsg.value = this.presOwnMap[msg.key.value];
else
replyMsg.value = '';
}
} else {
replyMsg.error = 'Not logged in.';
}
msg.makeReply(replyMsg) >> [];
}
});