Skip to content

Commit

Permalink
halloween event
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBThai committed Oct 31, 2023
1 parent 9442c17 commit 4a11571
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/commands/commandList/patreon/alterCowoncy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.alter = async function (p, id, text, info) {
}
};

async function checkDb(p, id, text, info) {
async function checkDb(p, info) {
const type = 'display';
const replacers = {
username: p.getName(info.user),
Expand Down
20 changes: 11 additions & 9 deletions src/commands/commandList/shop/util/itemUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ let lowestEventId = 22;
let eventItemId = lowestEventId;
for (let key in event) {
const eventItem = event[key].item;
items[eventItem.id] = {
id: eventItemId,
name: eventItem.name,
emoji: eventItem.emoji,
column: eventItem.id,
untradeable: true,
desc: eventItem.description,
};
eventItemId++;
if (eventItem) {
items[eventItem.id] = {
id: eventItemId,
name: eventItem.name,
emoji: eventItem.emoji,
column: eventItem.id,
untradeable: true,
desc: eventItem.description,
};
eventItemId++;
}
}

exports.getItems = async function (p) {
Expand Down
6 changes: 6 additions & 0 deletions src/data/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,11 @@
}
]
}
},
"2023_halloween": {
"start": 1698735600000,
"end": 1700035200000,
"type": "halloween",
"chance": 0.05
}
}
152 changes: 151 additions & 1 deletion src/utils/eventUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
const events = require('../data/event.json');
const itemUtil = require('../commands/commandList/shop/util/itemUtil.js');
const rewardUtil = require('./rewardUtil.js');
const lootboxUtil = require('../commands/commandList/zoo/lootboxUtil.js');
const dailyMax = 5;
const halloweenMax = 10;
const itemToEvents = {};
for (const key in events) {
const event = events[key];
itemToEvents[event.item.id] = event;
if (event.item) {
itemToEvents[event.item.id] = event;
}
}

let activeEvents = {};
Expand Down Expand Up @@ -63,6 +67,11 @@ exports.useItem = async function (item) {
exports.getEventItem = async function () {
const event = getCurrentActive();
if (!event) return;
if (event?.type === 'halloween') {
checkHalloween.bind(this)(event);
return;
}
if (!event.item) return;

// Cache if user is done today
let today = new Date();
Expand Down Expand Up @@ -202,3 +211,144 @@ async function parseReward(reward, con) {
const uid = await this.global.getUserUid(this.msg.author);
return rewardUtil.getReward(this.msg.author.id, uid, con, reward.type, reward.id, reward.count);
}

async function checkHalloween(event) {
const random = Math.random();
if (random >= event.chance) {
return;
}
// Cache if user is done today
let today = new Date();
today = today.toLocaleDateString();
if (this.msg.author.eventItemDone) {
const date = this.msg.author.eventItemDone;
if (date === today) {
return;
} else {
delete this.msg.author.eventItemDone;
}
}
const uid = await this.global.getUserUid(this.msg.author);

const con = await this.startTransaction();
let claimed;
try {
let sql = `SELECT * FROM user_event WHERE uid = ${uid} AND name = 'halloween';`;
const result = await con.query(sql);
claimed = (result[0]?.claim_count || 0) + 1;

const reset = this.dateUtil.afterMidnight(result[0]?.claim_reset);
if (result[0] && result[0]?.claim_count >= halloweenMax && !reset.after) {
this.msg.author.eventItemDone = today;
con.rollback();
return;
}
if (reset.after) {
claimed = 1;
}

sql = `INSERT INTO user_event (uid, name, claim_reset, claim_count) VALUES
(${uid}, 'halloween', ${reset.sql}, 1) ON DUPLICATE KEY UPDATE
claim_count = ${claimed}, claim_reset = ${reset.sql};`;
await con.query(sql);

const { rewardSql, rewardTxt } = await getHalloweenRewards.bind(this)(this.msg.author);
const candies = [
'<:candy1:1168784857002946611>',
'<:candy2:1168784855736270849>',
'<:candy3:1168784853538451488>',
'🍭',
];
const emoji = candies[Math.floor(Math.random() * candies.length)];
await con.query(rewardSql);
this.send(`${emoji} **|** \`[${claimed}/${halloweenMax}]\` ${rewardTxt}`);

await con.commit();
} catch (err) {
console.error(err);
con.rollback();
return;
}

/*
event.item.foundText.replaceAll('?emoji?', event.item.emoji) +
` \`[${claimed}/${dailyMax}]\`\n${this.config.emoji.blank} **|** To use this item, type \`owo use ${item.id}\``;
*/
}

async function getHalloweenRewards(user) {
const id = user.id;
const uid = await this.global.getUserUid(user);
let rand = Math.random();

if (rand <= 0.15) {
// Cowoncy
let rewardCount = 1000;
rewardCount = Math.floor(rewardCount + Math.random() * 4000);
return {
rewardTxt: `You’ve eaten too much candy and sit down. What’s that on the floor? You pocket an extra **${this.global.toFancyNum(
rewardCount
)} ${this.config.emoji.cowoncy} Cowoncy** you found!`,
rewardSql: `INSERT INTO cowoncy (id,money) VALUES (${id}, ${rewardCount}) ON DUPLICATE KEY UPDATE money = money + ${rewardCount};`,
};
} else if (rand <= 0.3) {
// Shard
let rewardCount = 500;
rewardCount = Math.floor(rewardCount + Math.random() * 2500);
return {
rewardTxt: `You open the candy wrapper, but what's this? This isn't candy! You found some **${this.global.toFancyNum(
rewardCount
)} ${this.config.emoji.shards} Weapon Shards** in your candy bag!`,
rewardSql: `INSERT INTO shards (uid,count) VALUES (${uid},${rewardCount}) ON DUPLICATE KEY UPDATE count = count + ${rewardCount};`,
};
} else if (rand <= 0.45) {
// Lootbox
let rewardCount = 1;
rewardCount = Math.floor(rewardCount + Math.random() * 2);
return {
rewardTxt: `You look through your bag of candies, but suprise! You found **${rewardCount} ${
this.config.emoji.lootbox
} Lootbox${rewardCount > 1 ? 'es' : ''}** instead!`,
rewardSql: `INSERT INTO lootbox (id,boxcount,claimcount,claim) VALUES (${id},${rewardCount},0,'2017-01-01') ON DUPLICATE KEY UPDATE boxcount = boxcount + ${rewardCount};`,
};
} else if (rand <= 0.6) {
// Crate
let rewardCount = 1;
rewardCount = Math.floor(rewardCount + Math.random() * 2);
return {
rewardTxt: `You look through your bag of candies, but suprise! You found **${rewardCount} ${
this.config.emoji.crate
} Weapon Crate${rewardCount > 1 ? 's' : ''}** instead!`,
rewardSql: `INSERT INTO crate (uid,cratetype,boxcount,claimcount,claim) VALUES (${uid},0,${rewardCount},0,'2017-01-01') ON DUPLICATE KEY UPDATE boxcount = boxcount + ${rewardCount};`,
};
} else if (rand <= 0.75) {
// Cookie
return {
rewardTxt: `You try to eat a piece of candy. Huh? It’s just a cookie in a wrapper! You got an extra **${this.config.emoji.cookie} Cookie** for your profile!`,
rewardSql: `INSERT INTO rep (id, count) VALUES (${id},1) ON DUPLICATE KEY UPDATE count = count + 1;`,
};
} else if (rand <= 0.9) {
// Special Gem
const specialGems = [79, 80, 81, 82, 83, 84, 85];
const gemId = specialGems[Math.floor(Math.random() * specialGems.length)];
let gem = lootboxUtil.getRandomGems(uid, 1, { gid: gemId });
let gemSql = gem.sql;
gem = Object.values(gem.gems)[0].gem;
return {
rewardTxt: `You walk down a dark and creepy street. A tiny sparkle catches your eye! You found ${this.global.getA(
gem.rank
)} **${gem.emoji} ${gem.rank} ${gem.type} Gem**!`,
rewardSql: gemSql,
};
} else {
// Special Pet
let animal = this.global.validAnimal('2023halloween_owo');
return {
rewardTxt: `You knock on a door and shout "Trick or Treat"! OwO? What's this? It's ${animal.value} OwO herself that oppened the door!`,
rewardSql: `INSERT INTO animal (count, totalcount, id, name) VALUES (1,1,${id},'${animal.value}')
ON DUPLICATE KEY UPDATE count = count + 1, totalcount = totalcount + 1;
INSERT INTO animal_count (id, ${animal.rank}) VALUES (${id}, 1)
ON DUPLICATE KEY UPDATE ${animal.rank} = ${animal.rank} + 1;`,
};
}
}

0 comments on commit 4a11571

Please sign in to comment.