forked from ElementsProject/lightning-charge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sb_websocket_client.js
203 lines (175 loc) · 5.14 KB
/
sb_websocket_client.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
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
//import { join } from 'path'
//import WebSocket from 'ws'
module.exports = (ln) => {
//these can be initalized with a 'sendInfoMsg' call
var seasonYear = null;
var seasonType = null;
var week = null;
const handleMsg = message => {
console.log("handleMsg " + message);
const json = JSON.parse(message)
if (json.hasOwnProperty('invoice')) {
pay(json['invoice']);
} else if (json.hasOwnProperty('seasonType')) {
//info channel
handleInfoMsg(json)
} else {
console.log(message);
}
}
const info = wsp => {
return sendMessage(wsp,{"channel" : "info"});
}
const sendMessage = (wsp, msg) => {
const uuidv1 = require('uuid/v1');
const invoiceId = uuidv1();
const invoiceP = wsp.sendRequest(msg, {requestId: invoiceId});
//invoiceP.then(invoice => console.log("LN Invoice : " + JSON.stringify(invoice) + "\n"));
const payLoadP = invoiceP.then(() => createSyntheticRequest(wsp, invoiceId));
const paidP = invoiceP.then(i => pay(i));
//paidP.then(p => console.log("LN Invoice Payment: " + JSON.stringify(p) + "\n"));
return payLoadP.then(payload => payload['data']);
}
const createSyntheticRequest = (wsp,uuid) => {
//this is a hack to add another request to the queue
//this is how we actually return the right Promise
//back to the caller of an API
return wsp._requests
.create(uuid, () => {
return ;
}, 0);
}
const pay = payload => {
const invoice = payload['invoice'];
//console.log("invoice " + invoice);
var paid = null;
try {
paid = ln.pay(invoice);
} catch (e) {
console.log("error paying invoice");
console.log("response from sb-api " + payload);
throw e;
}
return paid;
}
const team_roster = (wsp, teamId) => {
const msg = {
"channel" : "team",
"teamId" : teamId,
"retrieve" : "roster"
};
return sendMessage(wsp,msg);
}
const team_schedule = (wsp, teamId, year) => {
const msg = {
"channel" : "team",
"teamId" : teamId,
"retrieve" : "schedule",
"year" : year
};
return sendMessage(wsp,msg);
}
const games = (wsp,week,seasonPhase,year,teamId) => {
const msg = {
"channel" : "games",
"week" : week,
"seasonPhase" : seasonPhase,
"year" : year,
"teamId": teamId,
"realtime" : false,
};
return sendMessage(wsp,msg);
}
const realtime_games = (wsp, teamId) => {
const metaP = info(wsp);
const games = metaP.then(meta =>
sendMessage(wsp,build_realtime_msg(meta,teamId))
)
return games;
}
const games_this_week = (wsp) => {
//{"version":"8","lastRosterDownload":"20180801T153829.816Z","seasonType":"Regular","seasonYear":2017,"week":"NflWeek17"}
const metaP = info(wsp);
const thisWeek = metaP
.then(m => games(wsp,
parseWeek(m['week']),
m['seasonType'],
m['seasonYear']));
return thisWeek;
}
const games_today = (wsp) => {
const this_weekP = games_this_week(wsp);
return this_weekP.then(this_week => filter_today(this_week));
}
const player = (wsp, lastname,firstname) => {
const msg = {
"channel" : "players",
"lastName" : lastname,
"firstName" : firstname
};
return sendMessage(wsp,msg);
}
const stats_game_player_id = (wsp, stat_type, gameid, playerid) => {
const msg = {
"channel" : "stats",
"statType" : stat_type,
"gameId" : gameid,
"playerId" : playerid
};
return sendMessage(wsp,msg);
}
const stats_name_week = (wsp, stat_type, year, week, seasonPhase, lastname, firstname) => {
const msg = {
"channel" : "stats",
"statType" : stat_type,
"year" : year,
"week" : week,
"seasonPhase" : seasonPhase,
"lastName" : lastname,
"firstName" : firstname
};
return sendMessage(wsp,msg);
}
/** Currently the api returns in the format of 'NflWeekX'
* This is really annoying, this breaks on the 'k' in week
* and then parses the number
*/
function parseWeek(week) {
const weekString = week.split('k')[1];
return parseInt(weekString);
}
/** Takes in a list of games and finds ones that happenign today */
function filter_today(this_week) {
const today = new Date()
const timezoneOffset = today.getTimezoneOffset();
var gamesToday = [];
for (var i = 0; i < this_week.length; i++) {
var date = new Date(this_week[i]['startTime']);
//convert UTC timezone to user's timezone
//so they see expected games
const locale = date.toLocaleDateString();
if (locale === today.toDateString()) {
gamesToday += this_week[i];
}
}
return gamesToday;
}
function build_realtime_msg(meta,teamId) {
const msg = {
"channel" : "games",
"week" : parseWeek(meta['week']),
"seasonPhase" : meta['seasonType'],
"year" : meta['seasonYear'],
"teamId": teamId,
"realtime" : true,
};
return msg;
}
return { info,
team_roster, team_schedule,
games, realtime_games, games_this_week, games_today,
player,
stats_game_player_id,
stats_name_week
};
}