|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import {BaseDice} from './base' |
| 4 | +import fetch from 'isomorphic-fetch'; |
| 5 | +import FormData from 'form-data'; |
| 6 | +import {APIError} from '../errors/APIError' |
| 7 | + |
| 8 | +export class DuckDice extends BaseDice { |
| 9 | + constructor(){ |
| 10 | + super(); |
| 11 | + this.url = 'https://duckdice.io'; |
| 12 | + this.benefit = '&c=ab61534783' |
| 13 | + } |
| 14 | + |
| 15 | + async login(userName, password, twoFactor ,apiKey, req) { |
| 16 | + let ret = await this._send('stat/btc?api_key='+ apiKey, 'GET', '', ''); |
| 17 | + req.session.accessToken = apiKey; |
| 18 | + req.session.username = apiKey; |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + async getUserInfo(req) { |
| 23 | + return true |
| 24 | + } |
| 25 | + |
| 26 | + async refresh(req) { |
| 27 | + let formData = new FormData(); |
| 28 | + let info = req.session.info; |
| 29 | + if(info){ |
| 30 | + console.log("info is not null"); |
| 31 | + return info; |
| 32 | + } |
| 33 | + let accessToken = req.session.accessToken; |
| 34 | + let ret = await this._send('stat/'+req.query.currency+'?api_key='+ accessToken, 'GET', '',''); |
| 35 | + let userinfo = {}; |
| 36 | + userinfo.bets = ret.bets; |
| 37 | + userinfo.wins = ret.wins; |
| 38 | + userinfo.losses = ret.bets - ret.wins; |
| 39 | + userinfo.profit = ret.profit; |
| 40 | + userinfo.wagered = ret.volume; |
| 41 | + ret = await this._send('load/'+req.query.currency+'?api_key='+ accessToken, 'GET', '',''); |
| 42 | + userinfo.balance = ret.user.balance; |
| 43 | + userinfo.success = true; |
| 44 | + info.info = userinfo; |
| 45 | + req.session.info = info; |
| 46 | + return info; |
| 47 | + } |
| 48 | + |
| 49 | + async clear(req) { |
| 50 | + let accessToken = req.session.accessToken; |
| 51 | + let info = {}; |
| 52 | + let ret = await this._send('stat/'+req.query.currency+'?api_key='+ accessToken, 'GET', '',''); |
| 53 | + let userinfo = {}; |
| 54 | + userinfo.bets = ret.bets; |
| 55 | + userinfo.wins = ret.wins; |
| 56 | + userinfo.losses = ret.bets - ret.wins; |
| 57 | + userinfo.profit = ret.profit; |
| 58 | + userinfo.wagered = ret.volume; |
| 59 | + ret = await this._send('load/'+req.query.currency+'?api_key='+ accessToken, 'GET', '',''); |
| 60 | + userinfo.balance = ret.user.balance; |
| 61 | + userinfo.success = true; |
| 62 | + info.info = userinfo; |
| 63 | + info.currentInfo = {}; |
| 64 | + info.currentInfo.balance = ret.user.balance; |
| 65 | + info.currentInfo.bets = 0; |
| 66 | + info.currentInfo.wins = 0; |
| 67 | + info.currentInfo.losses = 0; |
| 68 | + info.currentInfo.profit = 0; |
| 69 | + info.currentInfo.wagered = 0; |
| 70 | + req.session.info = info; |
| 71 | + return info; |
| 72 | + } |
| 73 | + |
| 74 | + async bet(req) { |
| 75 | + let data = {}; |
| 76 | + let accessToken = req.session.accessToken; |
| 77 | + data.amount = parseFloat(req.body.PayIn/100000000).toFixed(8); |
| 78 | + data.symbol = req.body.Currency.toLowerCase(); |
| 79 | + let condition = req.body.High == "true"?true:false; |
| 80 | + let game = 0; |
| 81 | + if(req.body.High == "true"){ |
| 82 | + game = 9999-Math.floor((req.body.Chance*100)); |
| 83 | + } else { |
| 84 | + game = Math.floor((req.body.Chance*100))-1; |
| 85 | + } |
| 86 | + data.chance = parseFloat(req.body.Chance).toFixed(2); |
| 87 | + data.isHigh = condition; |
| 88 | + |
| 89 | + let ret = await this._send('play?api_key='+ accessToken, 'POST',data,''); |
| 90 | + let info = req.session.info; |
| 91 | + let betInfo = ret.bet; |
| 92 | + betInfo.condition = req.body.High == "true"?'>':'<'; |
| 93 | + betInfo.id = betInfo.hash; |
| 94 | + betInfo.target = parseFloat(game/100).toFixed(2);; |
| 95 | + betInfo.result = parseFloat(betInfo.number/100).toFixed(2); |
| 96 | + betInfo.amount = betInfo.betAmount; |
| 97 | + info.info.bets++; |
| 98 | + info.currentInfo.bets++; |
| 99 | + info.info.profit = parseFloat(info.info.profit) + parseFloat(betInfo.profit); |
| 100 | + info.info.balance = ret.user.balance; |
| 101 | + info.currentInfo.balance = ret.user.balance; |
| 102 | + info.info.wagered = parseFloat(info.info.wagered) + parseFloat(betInfo.amount); |
| 103 | + info.currentInfo.wagered = parseFloat(info.currentInfo.wagered) + parseFloat(betInfo.amount); |
| 104 | + info.currentInfo.profit = parseFloat(info.currentInfo.profit) + parseFloat(betInfo.profit); |
| 105 | + if(betInfo.profit>0){ |
| 106 | + betInfo.win = true; |
| 107 | + info.info.wins++; |
| 108 | + info.currentInfo.wins++; |
| 109 | + } else { |
| 110 | + betInfo.win = false; |
| 111 | + info.info.losses++; |
| 112 | + info.currentInfo.losses++; |
| 113 | + } |
| 114 | + let returnInfo = {}; |
| 115 | + returnInfo.betInfo= betInfo; |
| 116 | + returnInfo.info = info; |
| 117 | + req.session.info = info; |
| 118 | + console.log(returnInfo.betInfo); |
| 119 | + return returnInfo; |
| 120 | + } |
| 121 | + |
| 122 | + async _send(route, method, body, accessToken){ |
| 123 | + let url = `${this.url}/api/${route}${this.benefit}`; |
| 124 | + let res = null; |
| 125 | + if(method == "POST"){ |
| 126 | + res = await fetch(url, { |
| 127 | + method, |
| 128 | + headers: { |
| 129 | + Accept: 'application/json', |
| 130 | + 'User-Agent': 'MyDiceBot', |
| 131 | + 'Content-Type': 'application/json', |
| 132 | + }, |
| 133 | + body: JSON.stringify(body), |
| 134 | + }); |
| 135 | + } else { |
| 136 | + res = await fetch(url, { |
| 137 | + method, |
| 138 | + headers: { |
| 139 | + 'User-Agent': 'MyDiceBot', |
| 140 | + }, |
| 141 | + }); |
| 142 | + } |
| 143 | + let data = await res.json(); |
| 144 | + if (data.error) { |
| 145 | + let errs = new Error(data.error); |
| 146 | + errs.value = data.error; |
| 147 | + throw new APIError(data.error ,errs); |
| 148 | + } |
| 149 | + return data; |
| 150 | + } |
| 151 | +} |
0 commit comments