wuhu-torn-helper/src/class/action/CompanyHelper.ts
2022-10-21 10:12:33 +08:00

61 lines
2.4 KiB
TypeScript

import WuhuBase from "../WuhuBase";
import WuhuConfig from "../WuhuConfig";
import Log from "../Log";
import CommonUtils from "../utils/CommonUtils";
import FetchUtils from "../utils/FetchUtils";
import InfoUtils from "../utils/InfoUtils";
import Alert from "../utils/Alert";
/**
* 公司助手
*/
export default class CompanyHelper extends WuhuBase {
className = 'CompanyHelper';
public constructor() {
super();
WuhuConfig.get('CHTrainsDetectSwitch') && this.trainsDetect().then();
}
/**
* 火车检测
* 每日判断一次,非公司老板跳过检测
* TODO 优化、URL判断
* @private
*/
private async trainsDetect(): Promise<null> {
// 通过用户的icon判断公司老板
if ((await InfoUtils.getInstance().getSessionData()).statusIcons.icons.company.iconID !== 'icon73') {
Log.info('火车检测跳过:非公司老板');
return;
}
// 上次检测时间戳
let lastDetect: number = WuhuConfig.get('CHTrainsDetect') || 0;
// 检测是否过了一天
if (CommonUtils.getInstance().isNewDay(lastDetect, -6)) {
WuhuConfig.set('CHTrainsDetect', Date.now());
FetchUtils.getInstance().fetchText('/companies.php')
.then(res => {
let tmp: HTMLElement = document.createElement('div');
// TODO 未去除body标签
tmp.innerHTML = res.split('</head>')[1].replace('</html>', '').trim();
let trains: number = parseInt(tmp.querySelector('span.trains').innerText);
let stars: number = tmp.querySelectorAll('.company-rating .active').length / 2 || 1;
Log.info('火车/星级: ' + trains + '/' + stars);
if (trains + stars > 20) {
new Alert(`公司助手<br/><br/>火车检测:火车明日将溢出!${ trains }/20火车`, {
timeout: 15,
force: true,
sysNotify: true
});
}
})
.catch(error => {
Log.error('火车检测出错', error);
WuhuConfig.set('CHTrainsDetect', 0);
});
} else {
Log.info('火车检测:今日已提醒,跳过');
}
}
}