Files
pirate-corsaire-server/Callbacks/die.js
T
2026-08-24 22:20:36 +02:00

253 lines
9.0 KiB
JavaScript

const Reward = require("../Game/Reward");
const DoubleLinkedList = require("../Utility/DoubleLinkedList");
const admiralTable = require("../Data/admiralTable");
const leaderboards = require("../Data/leaderboard");
const { TASK_TYPES, TYPES } = require("../Data/constants");
const rewardCallback = require("./reward");
const { TASK_TYPE_DESTROY } = TASK_TYPES;
const { ENTITY_TYPE_PLAYER, ENTITY_TYPE_NPC, ENTITY_TYPE_MONSTER } = TYPES;
const Game = require("../Game/Game");
const { players } = Game;
const percentages = [0.40, 0.25, 0.15, 0.10, 0.07, 0.03];
const amounts = [1, 3, 5, 10, 20, 25, 35];
const totalAmount = amounts.reduce((acc, cur) => acc + cur, 0);
let aa = new Array(amounts.length);
setTimeout(async () => {
const q = await execute("SELECT * FROM leaderboard_1 ORDER BY amount DESC LIMIT 1099");
const batches = [];
const values = q.data;
let totalSize = 0;
for (let i = 0; i < percentages.length; i++) { // this loop is for slicing based on percentages
if (values.length - totalAmount < 0) {
batches.push([]);
continue;
};
const batchSize = (values.length - totalAmount) * percentages[i];
const batch = values.slice(totalSize, totalSize + batchSize);
batches.push(batch);
totalSize += batchSize;
};
let rest = values.slice(totalSize, totalSize + totalAmount);
//require("../Miscellaneous/winston").error(`Total length: ${values.length} (these are from DB, raw)`)
// require("../Miscellaneous/winston").error(`Rest length: ${rest.length} (these are based on fixed amounts instead of percentages)`)
let x = 0;
for (let i = 0; i < amounts.length; i++) {
const batchSize = amounts[i];
const batch = rest.slice(totalAmount - (totalAmount - x), totalAmount - (totalAmount - x - batchSize));
// require("../Miscellaneous/winston").error(`amounts array index: ${i} | slicing between: ${totalAmount - (totalAmount - x)} - ${totalAmount - (totalAmount - x - batchSize)}`)
aa[amounts.length - 1 - i] = batch;
x += batchSize;
};
batches.push(...aa);
const thresholds = new Array(batches.length);
for (let j = 0; j < batches.length; j++) {
const batch = batches[j];
const lastElemWithValue = batch.filter(e => e.id);
// require("../Miscellaneous/winston").error(`Batch ${j}: length is ${batch.length}, last elem with value: ${lastElemWithValue[lastElemWithValue.length - 1]?.id} ${lastElemWithValue[lastElemWithValue.length - 1]?.amount}`)
thresholds[j] = lastElemWithValue[lastElemWithValue.length - 1] ? lastElemWithValue[lastElemWithValue.length - 1].amount : 0;
for (let k = 0; k < batch.length; k++)
db.execute("UPDATE playerdata SET globalrank = ? WHERE playerID = ?", [j, batch[k].id]);
};
global.thresholds = thresholds;
// require("../Miscellaneous/winston").error(thresholds);
}, 2000)
const tracker = new DoubleLinkedList();
function globalDieCallback(source) {
const { typeID, entityTypeID, parentEntityTypeID, isAdmiral, authorID, authorTypeID } = source;
const id = `${typeID}_${isAdmiral ? entityTypeID : parentEntityTypeID}`;
const ids = {
typeID,
parentEntityTypeID
};
console.trace("death", source);
const player = players[authorID];
switch (typeID) {
case 1:
leaderboards[4].updatePosition(authorID, 1);
break;
case 2:
leaderboards[5].updatePosition(authorID, 1);
break;
case 3:
leaderboards[6].updatePosition(authorID, 1);
break;
};
if (authorTypeID === ENTITY_TYPE_PLAYER && typeID === ENTITY_TYPE_PLAYER) {
const targetPlayer = players[source.id];
const keyAuthor = `${player.id}-${targetPlayer.id}`;
const killCountAuthor = tracker.getElement(keyAuthor);
if (killCountAuthor === 2) return console.info("reached threshold");
const keyTarget = `${targetPlayer.id}-${player.id}`;
const killCountTarget = tracker.getElement(keyTarget);
if (killCountTarget) tracker.accumulateItem(keyTarget, -1);
tracker.accumulateItem(keyAuthor, 1);
const amount = 10;
const reward = new Reward({
distributionType: 3,
baseReward: [
{
categoryID: 6,
id: 4,
amountMinimum: amount
}
]
});
reward.dynamic(rewards => rewardCallback({ isRegular: true, authorID, source, rewards }));
const battlePointsTarget = targetPlayer.statusPoints[3] && targetPlayer.statusPoints[3].amount;
console.info(battlePointsTarget, amount, "sp")
if (battlePointsTarget >= amount) {
const reward_2 = new Reward({
distributionType: 3,
baseReward: [
{
categoryID: 6,
id: 4,
amountMinimum: amount * -1
}
]
});
reward_2.dynamic(rewards => rewardCallback({ isRegular: true, authorID: source.id, source, rewards }));
};
};
if (typeID === ENTITY_TYPE_NPC) {
} else if (typeID === ENTITY_TYPE_MONSTER) {
};
const toBeAdjusted = [{
taskCategoryID: TASK_TYPE_DESTROY,
typeID,
entityTypeID
}];
for (questID in player.quests.list) player.quests.list[questID].adjustTask(toBeAdjusted, 1);
if (this.reward) {
const playerInventory = player.inventory.getPosessions();
switch (this.reward.distributionType) {
case 1:
this.reward.multiple({
playerInventory,
damage: this.damageReceived,
maximum: this.totalDamageReceived,
lastShotAuthorID: authorID
}, (ids, rewards) => this.rewardCallback({ isRegular: true, authorID: ids.id, source, rewards }));
break;
case 2:
this.reward.single(1, playerInventory, rewards => this.rewardCallback({ isRegular: true, authorID, source, rewards }));
break;
};
};
const playerInventory = player.inventory.getPosessions();
if (isAdmiral) return admiralTable.descreaseAdmiralCount(id);
const isAdmiralSpawnReady = parentEntityTypeID ? admiralTable.increaseCounter(ids) : false;
return isAdmiralSpawnReady;
};
module.exports = globalDieCallback;
//const globalPoints = Math.floor(player.statusPoints[0] / 10000 + player.statusPoints[1] / 2000 + player.statusPoints[2] / 2000);
//if (globalPoints) leaderboards[0].updatePosition(authorID, globalPoints, true);
/*
let aa = new Array(amounts.length);
setTimeout(async () => {
const q = await execute("SELECT * FROM leaderboard_1 ORDER BY amount DESC");
const batches = [];
const values = q.data;
let totalSize = 0;
for (let i = 0; i < percentages.length; i++) { // this loop is for slicing based on percentages
if (values.length - totalAmount < 0) {
batches.push([]);
continue;
};
const batchSize = (values.length - totalAmount) * percentages[i];
const batch = values.slice(totalSize, totalSize + batchSize);
batches.push(batch);
totalSize += batchSize;
};
let rest = values.slice(totalSize, totalSize + totalAmount);
require("../Miscellaneous/winston").error(`Total length: ${values.length} (these are from DB, raw)`)
require("../Miscellaneous/winston").error(`Rest length: ${rest.length} (these are based on fixed amounts instead of percentages)`)
let x = 0;
for (let i = 0; i < amounts.length; i++) {
const batchSize = amounts[i];
const batch = rest.slice(totalAmount - (totalAmount - x), totalAmount - (totalAmount - x - batchSize));
require("../Miscellaneous/winston").error(`amounts array index: ${i} | slicing between: ${totalAmount - (totalAmount - x)} - ${totalAmount - (totalAmount - x - batchSize)}`)
aa[amounts.length - 1 - i] = batch;
x += batchSize;
};
batches.push(...aa);
const thresholds = new Array(batches.length);
for (let j = 0; j < batches.length; j++) {
const batch = batches[j];
const lastElemWithValue = batch.filter(e => e.id);
require("../Miscellaneous/winston").error(`Batch ${j}: length is ${batch.length}, last elem with value: ${lastElemWithValue[lastElemWithValue.length - 1]?.id} ${lastElemWithValue[lastElemWithValue.length - 1]?.amount}`)
thresholds[j] = lastElemWithValue[lastElemWithValue.length - 1] ? lastElemWithValue[lastElemWithValue.length - 1].amount : 0;
for (let k = 0; k < batch.length; k++)
db.execute("UPDATE playerdata SET globalrank = ? WHERE playerID = ?", [j, batch[k].id]);
};
global.thresholds = thresholds;
require("../Miscellaneous/winston").error(thresholds);
}, 2000)
*/