[N-Relay] Daily Collector
Collects the available awards for your account(s).
Creds:
@059
@Killer Be Killed
@kiwi5
Code:
import { ClaimDailyRewardMessage } from '../networking/packets/outgoing/claim-daily-reward-message';
import { GoToQuestRoomPacket } from '../networking/packets/outgoing/go-to-quest-room-packet';
import { Http } from '../services/http';
import { Client, Log, LogLevel, NrPlugin } from './../core/plugin-module';
const KEY_REGEX = /<key>(\w+)<\/key>/g;
@NRPlugin({
name: 'Collector',
author: 'tcrane+kiwi5',
enabled: true
})
class CollectorPlugin {
constructor() {
Client.on('ready', (pd, client: Client) => {
if (client.mapInfo.name === 'Nexus') {
Log('Collector', `Sending ${client.alias} to quest room`);
const gotoQuestRoom = new GoToQuestRoomPacket();
client.packetio.sendPacket(gotoQuestRoom);
} else {
Log('Collector', `Fetching calendar for ${client.alias}`);
Http.get('http://realmofthemadgodhrd.appspot.com/dailyLogin/fetchCalendar', {
guid: client.guid,
password: client.password
}).then((response) => {
Log('Collector', `Received calendar for ${client.alias}`, LogLevel.Success);
const items = this.getKeys(response);
if (items.length === 0) {
Log('Collector', `Nothing to collect for ${client.alias}!`, LogLevel.Success);
} else {
Log('Collector', `Collecting ${items.length} item${items.length === 1 ? '' : 's'} for ${client.alias}`);
this.collect(items, client)
.then(() => Log('Collector', `Finished collecting items for ${client.alias}!`, LogLevel.Success));
}
}).catch((error) => {
Log('Collector', `Error fetching calendar for ${client.alias}`);
Log('Collector', error.message, LogLevel.Warning);
});
}
});
}
private getKeys(xml: string): string[] {
const items: string[] = [];
let match = KEY_REGEX.exec(xml);
while (match !== null) {
items.push(match[1]);
match = KEY_REGEX.exec(xml);
}
return items;
}
private collect(collectables: string[], client: Client): Promise<void> {
return new Promise((resolve, reject) => {
const collect = (item: string) => {
Log('Collector', `Collecting item(s) for ${client.alias}`, LogLevel.Info);
const claim = new ClaimDailyRewardMessage();
claim.claimKey = item;
claim.claimType = 'nonconsecutive';
client.packetio.sendPacket(claim);
if (collectables.length > 0) {
setTimeout(() => {
collect(collectables.shift());
}, 1500);
} else {
resolve();
}
};
collect(collectables.shift());
});
}
}
@059
@Killer Be Killed
@kiwi5
Nice contribution. +Rep.