2022-10-17 15:19:18 +08:00

91 lines
3.8 KiB
TypeScript

import WuhuBase from "../WuhuBase";
import XUNZHAOMUZHUANG_HTML from "../../static/html/xunzhaomuzhuang/index.html";
import * as MUZHUANG_ID_LIST_JSON from "../../static/json/muzhuang_id_list.json";
import Log from "../Log";
import CommonUtils from "../utils/CommonUtils";
import XUNZHAOMUZHUANG_CSS from "../../static/css/xunzhaomuzhuang.css";
import TornStyleBlock from "../utils/TornStyleBlock";
/**
* 寻找木桩
* /item.php?temp=4#xunzhaomuzhuang
*/
export default class XZMZ extends WuhuBase {
className = 'XZMZ';
private readonly mainRoleContainer: HTMLElement;
private readonly IDList: number[];
private readonly btn: HTMLButtonElement;
private readonly tips: HTMLElement;
private counter: number;
public constructor() {
super();
CommonUtils.addStyle(XUNZHAOMUZHUANG_CSS);
this.mainRoleContainer = document.querySelector('div[role="main"]');
let block = new TornStyleBlock('寻找木桩');
block.setContent(XUNZHAOMUZHUANG_HTML);
this.mainRoleContainer.append(block.getBase());
this.IDList = MUZHUANG_ID_LIST_JSON.default;
this.btn = block.querySelector('#deadman-start-btn') as HTMLButtonElement;
this.tips = block.querySelector('#deadman_tips');
block.querySelector('#deadman-start-btn').addEventListener('click', () => {
this.mainRoleContainer.querySelector('#table-body').innerHTML = '';
this.btn.disabled = true;
this.StartSearch();
});
}
private StartSearch() {
this.counter = this.IDList.length;
const tips = this.tips;
tips.innerText = "---寻找中,长按目标名字弹出迷你档案---";
for (const id of this.IDList) {
this.SearchDeadman(id);
}
}
private SearchDeadman(id) {
CommonUtils.ajaxFetch({
url: window.addRFC('https://www.torn.com/profiles.php?step=getProfileData&XID=' + id),
method: 'GET'
}).then((res) =>
res.json()
)
.then((res) => {
this.counter--;
if (res.userStatus.status.type === 'ok') {
this.addRow({
player_id: res.user.userID,
name: res.userInformation.name,
level: res.userInformation.level
});
}
if (this.counter === 0) {
this.tips.innerText = "---寻找结束,等待60秒后重试---";
let countdown = 60;
let timer = window.setInterval(() => {
this.btn.innerHTML = (countdown--).toString();
if (countdown === 0) {
window.clearInterval(timer);
this.btn.disabled = false;
this.btn.innerHTML = '重新寻找';
}
}, 1000)
}
})
.catch(e => {
Log.error(e)
});
}
private addRow(data) {
let $parentNode = this.mainRoleContainer.querySelector('#table-body'),
$node = '<tr data-id="' + data.player_id + '"></tr>';
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;">' + data.player_id + '</td>'
$node += `<td style="border: 1px solid darkgray;padding:5px;text-align:center;"><a href="/profiles.php?XID=${ data.player_id }">${ data.name }</a></td>`;
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;">' + data.level + '</td>'
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;"><a href="https://www.torn.com/loader.php?sid=attack&user2ID=' + data.player_id + '" target="_blank" class="torn-btn">攻击</a></td>'
$parentNode.insertAdjacentHTML('beforeend', $node)
}
}