@ginstone/nga-api
Version:
101 lines (100 loc) • 3.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BetData = exports.VoteData = exports.SuperVoteData = void 0;
const Option_1 = require("./Option");
const VoteType_1 = require("../../enums/VoteType");
class SuperVoteData {
constructor(s) {
/**
* 尚未解析的数据,留给子类
*/
this.map = new Map();
// 解析vote字段
const array = s.split("~");
this.totalCount = 0;
this.total = 0;
for (let i = 0; i < array.length - 1; i += 2) {
const key = array[i];
const value = array[i + 1];
if ("max_select" === (key)) {
this.maxSelect = Number(value);
}
else if ("priv" === (key)) {
this.reputation = Number(value.split("_")[1]);
}
else if ("end" === (key)) {
this.end = new Date(Number(value) * 1000);
}
else if ("opt" === (key)) {
this.result = Number(value);
}
else {
this.map.set(key, value);
}
}
}
}
exports.SuperVoteData = SuperVoteData;
class VoteData extends SuperVoteData {
constructor(s) {
super(s);
this.type = VoteType_1.VoteType.VOTE;
this.options = new Array();
this.map.forEach((v, k) => {
const id = Number(k);
if (!isNaN(id)) {
const moreData = this.map.get("_" + k);
if (moreData) {
const numbers = moreData.split(",").map(i => Number(i));
this.totalCount += numbers[0];
this.options.push(new Option_1.VoteOption(id, v, numbers[0]));
if (numbers[2] > 0) {
this.total = numbers[2];
}
}
}
});
// 计算占比
this.options.forEach(option => option.countPer = option.count / this.totalCount);
}
}
exports.VoteData = VoteData;
class BetData extends SuperVoteData {
constructor(s) {
super(s);
this.type = VoteType_1.VoteType.BET;
this.options = new Array();
this.totalMoney = 0;
this.map.forEach((v, k) => {
const id = Number(k);
if (!isNaN(id)) {
const moreData = this.map.get("_" + k);
if (moreData) {
const numbers = moreData.split(",").map(i => Number(i));
this.totalCount += numbers[0];
this.totalMoney += numbers[1];
this.options.push(new Option_1.BetOption(id, v, numbers[0], numbers[1]));
if (numbers[2] > 0) {
this.total = numbers[2];
}
}
}
else if ("min" === k) {
this.min = Number(v);
}
else if ("max" === k) {
this.max = Number(v);
}
else if ("done" === k) {
// 胜出项目
this.successId = Number(v);
}
});
// 计算占比
this.options.forEach(option => {
option.countPer = option.count / this.totalCount;
option.moneyPer = option.money / this.totalMoney;
});
}
}
exports.BetData = BetData;