Skip to content

Instantly share code, notes, and snippets.

@snwfdhmp
Created October 25, 2020 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snwfdhmp/bd60177d698bcfaeed4dd4859cc05585 to your computer and use it in GitHub Desktop.
Save snwfdhmp/bd60177d698bcfaeed4dd4859cc05585 to your computer and use it in GitHub Desktop.
const startingTime = 4324;
function main() {
console.log(data.players[0].state);
for (let i = 0; i < data.players.length; i++) {
updatePlayer(data.players[i], startingTime + 200);
}
console.log(data.players[0].state);
}
function updatePlayer(player, gameTime) {
const timeStart = player.state.at;
const timeDiff = gameTime - timeStart;
let isTimeBreak = {};
for (const taskId in player.state.wip) {
if (player.state.wip[taskId].finishedAt <= gameTime) {
if (!isTimeBreak[player.state.wip[taskId].finishedAt]) {
isTimeBreak[player.state.wip[taskId].finishedAt] = [];
}
isTimeBreak[player.state.wip[taskId].finishedAt].push(taskId);
}
}
for (let i = 0; i < timeDiff; i++) {
// process wips
if (isTimeBreak[timeStart + i]) {
const task = player.state.wip[isTimeBreak[timeStart + i]];
for (const effectChange of task.effectsChanges) {
switch (effectChange.action) {
case "delete":
for (let j = 0; j < player.state.effects.length; j++) {
if (player.state.effects[j].id == effectChange.id) {
player.state.effects.splice(j, 1);
}
}
break;
case "add":
player.state.effects.push({ id: effectChange.id });
break;
}
}
player.state.wip.splice(isTimeBreak[timeStart + i], 1);
}
// process effects
for (const effect of player.state.effects) {
params = gameParams.effects[effect.id];
if (!params) {
console.warn({ msg: `effect '${effect.id}' is not declared` });
continue;
}
params.perTick(player);
}
player.state.at = timeStart + i + 1;
}
}
let data = {
players: [
{
id: "snwfdhmp",
state: {
at: 4324,
data: {
goldPossessed: 1205,
},
effects: [
{
id: "gold_production_lvl_1",
},
],
wip: [
{
finishedAt: 4507,
effectsChanges: [
{
action: "delete",
id: "gold_production_lvl_1",
},
{
action: "add",
id: "gold_production_lvl_2",
},
],
},
],
},
},
],
};
let gameParams = {
effects: {
gold_production_lvl_1: {
perTick: (player) => {
player.state.data.goldPossessed += 2;
},
},
gold_production_lvl_2: {
perTick: (player) => {
player.state.data.goldPossessed += 1000;
},
},
},
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment