Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.idea/
57 changes: 57 additions & 0 deletions drugs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"drugs": [
{
"name": "Doliprane",
"expiresIn": 20,
"benefit": 30,
"benefitEffect": "decrease",
"benefitMultiplier": 1,
"hasExpirationDate": true,
"hasBenefitAfterExpiration" : true,
"benefitMultipliersByRemainingDay": []
},
{
"name": "Herbal Tea",
"expiresIn": 10,
"benefit": 5,
"benefitEffect": "increase",
"benefitMultiplier": 1,
"hasExpirationDate": true,
"hasBenefitAfterExpiration" : true,
"benefitMultipliersByRemainingDay": []
},
{
"name": "Fervex",
"expiresIn": 5,
"benefit": 40,
"benefitEffect": "increase",
"benefitMultiplier": 1,
"hasExpirationDate": true,
"hasBenefitAfterExpiration" : false,
"benefitMultipliersByRemainingDay": [
{ "expiresIn": 10, "benefitMultiplier": 2 },
{ "expiresIn": 5, "benefitMultiplier": 3 }
]
},
{
"name": "Magic Pill",
"expiresIn": 15,
"benefit": 40,
"benefitEffect": null,
"benefitMultiplier": 1,
"hasExpirationDate": false,
"hasBenefitAfterExpiration" : true,
"benefitMultipliersByRemainingDay": []
},
{
"name": "Dafalgan",
"expiresIn": 20,
"benefit": 30,
"benefitEffect": "decrease",
"benefitMultiplier": 2,
"hasExpirationDate": true,
"hasBenefitAfterExpiration" : true,
"benefitMultipliersByRemainingDay": []
}
]
}
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Drug, Pharmacy } from "./pharmacy";
import * as store from "./drugs.json";

import fs from "fs";

const drugs = [
new Drug("Doliprane", 20, 30),
new Drug("Herbal Tea", 10, 5),
new Drug("Fervex", 5, 40),
new Drug("Magic Pill", 15, 40)
];
const drugs = [];
store.drugs.map(drug => {
drugs.push(
new Drug(
drug.name,
drug.expiresIn,
drug.benefit,
drug.benefitEffect,
drug.benefitMultiplier,
drug.hasExpirationDate,
drug.hasBenefitAfterExpiration,
drug.benefitMultipliersByRemainingDay
)
);
});
const trial = new Pharmacy(drugs);

const log = [];
Expand Down
100 changes: 58 additions & 42 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
export class Drug {
constructor(name, expiresIn, benefit) {
constructor(
name,
expiresIn,
benefit,
benefitEffect = "decrease",
benefitMultiplier = 1,
hasExpirationDate = true,
hasBenefitAfterExpiration = true,
benefitMultipliersByRemainingDay = []
) {
this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
this.benefitEffect = benefitEffect;
this.benefitMultiplier = benefitMultiplier;
this.hasExpirationDate = hasExpirationDate;
this.hasBenefitAfterExpiration = hasBenefitAfterExpiration;
this.benefitMultipliersByRemainingDay = benefitMultipliersByRemainingDay;
}
}

export class Pharmacy {
constructor(drugs = []) {
this.drugs = drugs;
}

updateBenefitValue() {
for (var i = 0; i < this.drugs.length; i++) {
if (
this.drugs[i].name != "Herbal Tea" &&
this.drugs[i].name != "Fervex"
) {
if (this.drugs[i].benefit > 0) {
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].benefit = this.drugs[i].benefit - 1;
this.drugs.forEach(drug => {
if (0 < drug.benefit && drug.benefit <= 50) {
let benefitMultiplier = drug.benefitMultiplier;
let hasBenefitEnded = false;

if (drug.expiresIn <= 0) {
benefitMultiplier = 2;

if (!drug.hasBenefitAfterExpiration) {
drug.benefit = 0;
hasBenefitEnded = true;
}
}
} else {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
if (this.drugs[i].name == "Fervex") {
if (this.drugs[i].expiresIn < 11) {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
}
}
if (this.drugs[i].expiresIn < 6) {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;

if (
drug.benefitMultipliersByRemainingDay.length > 0 &&
!hasBenefitEnded
) {
drug.benefitMultipliersByRemainingDay
.sort((a, b) => {
return b.expiresIn - a.expiresIn;
})
.map(element => {
if (drug.expiresIn <= element.expiresIn) {
benefitMultiplier = element.benefitMultiplier;
}
}
}
});
}
}
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1;
}
if (this.drugs[i].expiresIn < 0) {
if (this.drugs[i].name != "Herbal Tea") {
if (this.drugs[i].name != "Fervex") {
if (this.drugs[i].benefit > 0) {
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].benefit = this.drugs[i].benefit - 1;
}
}

if (drug.benefitEffect !== null && !hasBenefitEnded) {
let benefitValue = drug.benefit;

benefitValue =
benefitValue +
(drug.benefitEffect === "decrease" ? -1 : 1) * benefitMultiplier;

if (benefitValue < 0) {
drug.benefit = 0;
} else if (benefitValue > 50) {
drug.benefit = 50;
} else {
this.drugs[i].benefit =
this.drugs[i].benefit - this.drugs[i].benefit;
}
} else {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
drug.benefit = benefitValue;
}
}
}
}

if (drug.hasExpirationDate) {
drug.expiresIn--;
}
});

return this.drugs;
}
Expand Down
Loading