|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import {BaseDice} from './base' |
| 4 | +import FormData from 'form-data'; |
| 5 | +import {APIError} from '../errors/APIError'; |
| 6 | +import steem from 'steem'; |
| 7 | +import request from 'request'; |
| 8 | +import fetch from 'isomorphic-fetch'; |
| 9 | + |
| 10 | +export class KryptoGames extends BaseDice { |
| 11 | + constructor(){ |
| 12 | + super(); |
| 13 | + this.url = 'https://kryptogames.io'; |
| 14 | + this.benefit = '?ref=mydicebot' |
| 15 | + this.currencys = ["steem","sbd"]; |
| 16 | + steem.api.setOptions({url:'https://api.steemit.com'}); |
| 17 | + } |
| 18 | + |
| 19 | + async login(userName, password, twoFactor ,apiKey, req) { |
| 20 | + req.session.accessToken = apiKey; |
| 21 | + req.session.username = userName; |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + async getUserInfo(req) { |
| 26 | + let info = req.session.info; |
| 27 | + if(typeof info != 'undefined'){ |
| 28 | + return true; |
| 29 | + } |
| 30 | + let userName = req.session.username; |
| 31 | + let ret = await steem.api.getAccountsAsync([userName]); |
| 32 | + let userinfo = { |
| 33 | + 'bets' : 0, |
| 34 | + 'wins' : 0, |
| 35 | + 'losses' : 0, |
| 36 | + 'profit' : 0, |
| 37 | + 'wagered' : 0, |
| 38 | + 'balance' : 0, |
| 39 | + }; |
| 40 | + for(let k in ret){ |
| 41 | + let sbd = ret[k]['sbd_balance'].split(' '); |
| 42 | + let steem_balance = ret[k]['balance'].split(' '); |
| 43 | + userinfo.balance = parseFloat(steem_balance[0]); |
| 44 | + } |
| 45 | + info = {}; |
| 46 | + let currentInfo = userinfo; |
| 47 | + info.info = userinfo; |
| 48 | + req.session.info = info; |
| 49 | + console.log(req.session.info); |
| 50 | + return info; |
| 51 | + } |
| 52 | + |
| 53 | + async refresh(req) { |
| 54 | + let info = req.session.info; |
| 55 | + if(info){ |
| 56 | + return info; |
| 57 | + } |
| 58 | + let userName = req.session.username; |
| 59 | + let ret = await steem.api.getAccountsAsync([userName]); |
| 60 | + for(let k in ret){ |
| 61 | + let balance = new Array(); |
| 62 | + balance['sbd'] = ret[k]['sbd_balance'].split(' '); |
| 63 | + balance['steem'] = ret[k]['balance'].split(' '); |
| 64 | + info.info.balance = parseFloat(balance[req.query.currency][0]); |
| 65 | + } |
| 66 | + req.session.info = info; |
| 67 | + return info; |
| 68 | + } |
| 69 | + |
| 70 | + async clear(req) { |
| 71 | + let userName = req.session.username; |
| 72 | + let ret = await steem.api.getAccountsAsync([userName]); |
| 73 | + let info = {}; |
| 74 | + info.info = { |
| 75 | + 'bets' : 0, |
| 76 | + 'wins' : 0, |
| 77 | + 'losses' : 0, |
| 78 | + 'profit' : 0, |
| 79 | + 'wagered' : 0, |
| 80 | + 'balance' : 0, |
| 81 | + }; |
| 82 | + info.currentInfo = { |
| 83 | + 'bets' : 0, |
| 84 | + 'wins' : 0, |
| 85 | + 'losses' : 0, |
| 86 | + 'profit' : 0, |
| 87 | + 'wagered' : 0, |
| 88 | + 'balance' : 0, |
| 89 | + } |
| 90 | + for(let k in ret){ |
| 91 | + let balance = new Array(); |
| 92 | + balance['sbd'] = ret[k]['sbd_balance'].split(' '); |
| 93 | + balance['steem'] = ret[k]['balance'].split(' '); |
| 94 | + info.info.balance = parseFloat(balance[req.query.currency][0]); |
| 95 | + info.currentInfo.balance = parseFloat(balance[req.query.currency][0]); |
| 96 | + info.info.success = 'true'; |
| 97 | + } |
| 98 | + req.session.info = info; |
| 99 | + return info; |
| 100 | + } |
| 101 | + |
| 102 | + async bet(req) { |
| 103 | + req.setTimeout(500000); |
| 104 | + let info = req.session.info; |
| 105 | + let amount = (req.body.PayIn/100000000).toFixed(3); |
| 106 | + let condition = 'under'; |
| 107 | + let currency = req.body.Currency.toLowerCase(); |
| 108 | + let target = 0; |
| 109 | + target = Math.floor(req.body.Chance) + 1; |
| 110 | + let cseed = Math.random().toString(36).substring(2); |
| 111 | + let memo = 'BRoll ' + condition + ' ' + target + ' '+ cseed; |
| 112 | + let bet = amount + ' '+ req.body.Currency.toUpperCase(); |
| 113 | + let userName = req.session.username; |
| 114 | + let token = req.session.accessToken; |
| 115 | + let kryptoGamesDice = 'kryptogames'; |
| 116 | + try{ |
| 117 | + let ret = await this._transfer(token, userName, kryptoGamesDice, bet, memo); |
| 118 | + let data = await this._getBetInfo(ret.id, userName, cseed); |
| 119 | + if(typeof data._id == "undefined") { |
| 120 | + data = await this._getBetInfoFromUser(userName,ret.id, cseed); |
| 121 | + } |
| 122 | + if(typeof data._id != "undefined") { |
| 123 | + data.amount = amount; |
| 124 | + let betInfo = {}; |
| 125 | + betInfo.id = data._id; |
| 126 | + betInfo.condition = '<'; |
| 127 | + betInfo.target = target; |
| 128 | + betInfo.profit = (parseFloat(data.payout) - parseFloat(data.amount)).toFixed(8); |
| 129 | + betInfo.roll_number = data.diceRoll; |
| 130 | + betInfo.payout = parseFloat(data.payout).toFixed(8); |
| 131 | + betInfo.amount = parseFloat(data.amount).toFixed(8); |
| 132 | + info.info.balance = (parseFloat(info.info.balance) + parseFloat(betInfo.profit)).toFixed(8); |
| 133 | + info.currentInfo.balance = (parseFloat(info.currentInfo.balance) + parseFloat(betInfo.profit)).toFixed(8); |
| 134 | + info.info.bets++; |
| 135 | + info.currentInfo.bets++; |
| 136 | + info.info.profit = (parseFloat(info.info.profit) + parseFloat(betInfo.profit)).toFixed(8); |
| 137 | + info.info.wagered = (parseFloat(info.info.wagered) + parseFloat(amount)).toFixed(8); |
| 138 | + info.currentInfo.wagered = (parseFloat(info.currentInfo.wagered) + parseFloat(amount)).toFixed(8); |
| 139 | + info.currentInfo.profit = (parseFloat(info.currentInfo.profit) + parseFloat(betInfo.profit)).toFixed(8); |
| 140 | + if(data.won){ |
| 141 | + betInfo.win = true; |
| 142 | + info.info.wins++; |
| 143 | + info.currentInfo.wins++; |
| 144 | + } else { |
| 145 | + betInfo.win = false; |
| 146 | + info.info.losses++; |
| 147 | + info.currentInfo.losses++; |
| 148 | + } |
| 149 | + let returnInfo = {}; |
| 150 | + returnInfo.betInfo= betInfo; |
| 151 | + returnInfo.info = info; |
| 152 | + req.session.info = info; |
| 153 | + return returnInfo; |
| 154 | + } else { |
| 155 | + throw new Error('bet data is null'); |
| 156 | + } |
| 157 | + } catch(e) { |
| 158 | + throw e; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + async _getBetInfoFromUser(account, id, cseed){ |
| 163 | + let memoRegEx = /\{(.*)/; |
| 164 | + return new Promise(async (resolve, reject) => { |
| 165 | + try { |
| 166 | + let options = { |
| 167 | + url: ' https://api.steemit.com', |
| 168 | + method: 'POST', |
| 169 | + json: { |
| 170 | + jsonrpc: '2.0', |
| 171 | + method: 'condenser_api.get_account_history', |
| 172 | + params: [account, -1, 1], |
| 173 | + id: 1 |
| 174 | + }, |
| 175 | + timeout:10000 |
| 176 | + }; |
| 177 | + for(let tryQueryCount=0; tryQueryCount<20; tryQueryCount++) { |
| 178 | + let data = await this._queryUserInfo(options,id,cseed); |
| 179 | + if(data !== undefined){ |
| 180 | + tryQueryCount = 999; |
| 181 | + console.log(data); |
| 182 | + resolve(data) |
| 183 | + } else { |
| 184 | + console.log('Waiting for blockchain packing.....'); |
| 185 | + await this._sleep(15000); |
| 186 | + } |
| 187 | + } |
| 188 | + resolve('not found') |
| 189 | + } catch (e) { |
| 190 | + reject( e ); |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + async _getBetInfo(id, userName, cseed){ |
| 198 | + let memoRegEx = /\{(.*)/; |
| 199 | + let tryQueryCount = 0; |
| 200 | + return new Promise(( resolve, reject ) => { |
| 201 | + let release = steem.api.streamOperations(async function (err, op) { |
| 202 | + if (err) { |
| 203 | + reject( err ); |
| 204 | + } else { |
| 205 | + if (op[0] === "transfer" && op[1].to === userName) { |
| 206 | + if (op[1].from === "kryptogames" && op[1].memo.startsWith("You")) { |
| 207 | + tryQueryCount++; |
| 208 | + try { |
| 209 | + memoRegEx = /Client Seed: ([A-Za-z0-9]+),/; |
| 210 | + let clientSeed = memoRegEx.exec(op[1].memo)[1] ; |
| 211 | + if(clientSeed == cseed ){ |
| 212 | + release(); |
| 213 | + let memo = op[1].memo; |
| 214 | + let steems = op[1].amount.split(' '); |
| 215 | + let data = {}; |
| 216 | + console.log(memo); |
| 217 | + data.payout = steems[0]; |
| 218 | + data._id = id; |
| 219 | + memoRegEx = /Result: ([0-9]+),/; |
| 220 | + data.diceRoll = memoRegEx.exec(op[1].memo)[1] ; |
| 221 | + data.won = false; |
| 222 | + if (memo.indexOf("Won")>0) { |
| 223 | + data.won = true; |
| 224 | + } |
| 225 | + resolve(data); |
| 226 | + } |
| 227 | + } catch (e) { |
| 228 | + reject( e ); |
| 229 | + } |
| 230 | + } |
| 231 | + if (op[1].from === "kryptogames" && !op[1].memo.startsWith("You")) { |
| 232 | + release(); |
| 233 | + let memo = op[1].memo; |
| 234 | + console.log(memo); |
| 235 | + reject(memo); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + if(tryQueryCount>=100){ |
| 240 | + release(); |
| 241 | + resolve({}); |
| 242 | + } |
| 243 | + }); |
| 244 | + }); |
| 245 | + } |
| 246 | + |
| 247 | + async _transfer(p,u,t,s,m){ |
| 248 | + return new Promise(( resolve, reject ) => { |
| 249 | + steem.broadcast.transfer(p, u, t, s, m, function(err, result){ |
| 250 | + if(err) { |
| 251 | + reject( err ); |
| 252 | + } else { |
| 253 | + resolve( result ); |
| 254 | + } |
| 255 | + }); |
| 256 | + }); |
| 257 | + } |
| 258 | + async _sleep(ms) { |
| 259 | + return new Promise(resolve => setTimeout(resolve, ms)) |
| 260 | + } |
| 261 | + |
| 262 | + async _queryUserInfo(options, id, cseed){ |
| 263 | + let memoRegEx = /\{(.*)/; |
| 264 | + return new Promise(( resolve, reject ) => { |
| 265 | + let req = request.post(options,function (e, r, body) { |
| 266 | + if(e) { |
| 267 | + console.log('reject error'); |
| 268 | + reject( e ); |
| 269 | + } else { |
| 270 | + if(body) { |
| 271 | + let res = body.result; |
| 272 | + for(let k in res) { |
| 273 | + let tran = res[k][1].op; |
| 274 | + try { |
| 275 | + if (tran[0] == "transfer" && tran[1].from == "kryptogames" && tran[1].memo.startsWith("You")) { |
| 276 | + memoRegEx = /Client Seed: ([A-Za-z0-9]+),/; |
| 277 | + let clientSeed = memoRegEx.exec(tran[1].memo)[1] ; |
| 278 | + console.log(clientSeed, cseed); |
| 279 | + if(clientSeed == cseed ){ |
| 280 | + let memo = tran[1].memo; |
| 281 | + let steems = tran[1].amount.split(' '); |
| 282 | + let data = {}; |
| 283 | + console.log(memo); |
| 284 | + data.payout = steems[0]; |
| 285 | + data._id = id; |
| 286 | + memoRegEx = /Result: ([0-9]+),/; |
| 287 | + data.diceRoll = memoRegEx.exec(tran[1].memo)[1] ; |
| 288 | + data.won = false; |
| 289 | + if (memo.indexOf("Won")>0) { |
| 290 | + data.won = true; |
| 291 | + } |
| 292 | + resolve(data); |
| 293 | + } |
| 294 | + } |
| 295 | + } catch (e) { |
| 296 | + reject( e ); |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + resolve(); |
| 301 | + } |
| 302 | + }); |
| 303 | + }); |
| 304 | + } |
| 305 | +} |
0 commit comments