Skip to content

Instantly share code, notes, and snippets.

@jdanthdavis
Last active June 9, 2024 00:40
Show Gist options
  • Save jdanthdavis/647ab73453373f2381f357c9652b58b7 to your computer and use it in GitHub Desktop.
Save jdanthdavis/647ab73453373f2381f357c9652b58b7 to your computer and use it in GitHub Desktop.
import { checkKc, createFormData } from './utils.js';
export default {
async fetch(request, env) {
if (!isValidAgent(request.headers.get('User-Agent'))) {
return new Response();
}
const form = await request.clone().formData();
const payload = JSON.parse(form.get('payload_json'));
const file = form.get('file');
const extra = payload.extra;
const playerName = payload.playerName;
const bossName = extra.boss;
const killCount = extra.count;
const time = extra.time;
const isPb = extra.isPersonalBest;
if (
payload.type === 'KILL_COUNT' &&
checkKc(bossName, killCount, playerName)
) {
let msgMap = createFormData(
bossName,
killCount,
playerName,
time,
isPb,
env
);
for (const [url, msg] of msgMap.entries()) {
let formData = new FormData();
formData.append('payload_json', JSON.stringify({ content: msg }));
// since the screenshots would be taken so close to each other we are fine with sending the first one twice
formData.append('file', file);
await fetch(url, {
method: 'post',
body: formData,
});
}
}
return new Response();
},
};
function isValidAgent(ua) {
if (typeof ua !== 'string') return false;
if (!ua.startsWith('RuneLite/') && !ua.startsWith('HDOS/')) return false;
return ua.includes('Dink/');
}
/**
*
* @param bossName
* @param killCount
* @param playerName
* @returns
*/
export function checkKc(bossName, killCount, playerName) {
const theBoys = [
'LSX SWAP',
'MOOREI',
'GOUT HAVER',
'GLASSFACE',
'Z4M',
'Z4M I',
'THEMILDEST1',
'BG S',
];
const bossMap = new Map([
['TZKAL-ZUK', 5],
['SOL HEREDIT', 5],
['THEATRE OF BLOOD HARD MODE', 10],
['CHAMBERS OF XERIC CHALLENGE MODE', 10],
["PHOSANI'S NIGHTMARE", 25],
['THE NIGHTMARE', 25],
['CORPOREAL BEAST', 50],
['SARACHNIS', 1],
]);
const bossInterval = bossMap.get(bossName.toUpperCase());
// if KC is noteable
if (bossMap.has(bossName.toUpperCase()) && killCount % bossInterval === 0)
return true;
// base bossInterval of 100
if (killCount % 100 === 0) return true;
// special occasion
if (
(theBoys.includes(playerName.toUpperCase()) &&
bossName.toUpperCase() === 'SOL HEREDIT' &&
killCount === 1) ||
(theBoys.includes(playerName.toUpperCase()) &&
bossName.toUpperCase() === 'TZKAL-ZUK' &&
killCount === 1) ||
(theBoys.includes(playerName.toUpperCase()) &&
bossName.toUpperCase() === 'TZTOK-JAD' &&
killCount === 1)
) {
return true;
}
return false;
}
/**
*
* @param bossName
* @param killCount
* @param playerName
* @param time
* @param isPb
* @param env
* @returns
*/
export function createFormData(
bossName,
killCount,
playerName,
time,
isPb,
env
) {
const { KC_URL, PB_URL } = env;
let msgMap = new Map([]);
if (isPb) {
msgMap.set(
PB_URL,
`**${playerName}** has defeated **${bossName}** with a new personal best of **${time}**`
);
}
msgMap.set(
KC_URL,
`**${playerName}** has defeated **${bossName}** with a completion count of **${killCount}**`
);
return msgMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment