Last active
January 25, 2022 06:02
-
-
Save wirelessr/3555c40256e51ac7c1befa4db897941b to your computer and use it in GitHub Desktop.
How to Design in Clean Architecture Way, Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const expect = require("chai").expect; | |
const table = [10, 10, 15, 15, 30, 30, 100]; | |
const boxTable = [0, 1, 0, 0, 1, 0, 1]; | |
const dateDiff = (sD1, sD2) => { | |
const d1 = new Date(sD1); | |
const d2 = new Date(sD2); | |
d1.setHours(0, 0, 0, 0); | |
d2.setHours(0, 0, 0, 0); | |
return Math.abs(d1 - d2) / 86400000; | |
}; | |
class SignInRepo { | |
constructor(user) { | |
this.user = user; | |
this.counter = 0; | |
this.last = null; | |
this.lastBox = -1; | |
} | |
restoreSingInRecord(counter, last, lastBox) { | |
this.counter = counter; | |
this.last = last; | |
this.lastBox = lastBox; | |
} | |
update(now) { | |
this.counter++; | |
this.last = now; | |
} | |
reset() { | |
this.counter = 0; | |
this.last = null; | |
this.lastBox = -1; | |
} | |
setLastBox(lastBox) { | |
this.lastBox = lastBox; | |
} | |
} | |
class SignInService { | |
constructor(repo) { | |
this.repo = repo; | |
} | |
signIn(now) { | |
const diffDay = dateDiff(now, this.repo.last); | |
if (diffDay === 0) { | |
return 0; | |
} | |
if (diffDay > 1) { | |
this.repo.reset(); | |
} | |
this.repo.update(now); | |
return table[this.repo.counter - 1] || 0; | |
} | |
getTimeline() { | |
const ret = [0, 0, 0, 0, 0, 0, 0]; | |
if (!this.repo.counter) return ret; | |
for (let i = 0; i < 7; i++) { | |
if (i < this.repo.counter) ret[i] = 1; | |
} | |
return ret; | |
} | |
click() { | |
for (let i = this.repo.lastBox + 1; i < this.repo.counter; i++) { | |
if (boxTable[i] === 1) { | |
this.repo.setLastBox(i); | |
return 100; | |
} | |
} | |
return 0; | |
} | |
} | |
describe("step4", () => { | |
it("continuous 6d and signin 7th day", () => { | |
const user = "User A"; | |
const now = "2022-01-07 1:11:11"; | |
const repo = new SignInRepo(); | |
repo.restoreSingInRecord(6, "2022-01-06 5:55:55", 1); | |
const service = new SignInService(repo); | |
const timeline1 = service.getTimeline(); | |
expect(timeline1).to.deep.equal([1, 1, 1, 1, 1, 1, 0]); | |
const result = service.signIn(now); | |
expect(result).to.be.equal(100); | |
const timeline2 = service.getTimeline(); | |
expect(timeline2).to.deep.equal([1, 1, 1, 1, 1, 1, 1]); | |
}); | |
it("continuous 6d and click box", () => { | |
const user = "User A"; | |
const now = "2022-01-06 11:11:11"; | |
const repo = new SignInRepo(); | |
repo.restoreSingInRecord(6, "2022-01-06 5:55:55", 1); | |
const service = new SignInService(repo); | |
const boxReward1 = service.click(now); | |
expect(boxReward1).to.be.equal(100); | |
expect(repo.lastBox).to.be.equal(4); | |
const boxReward2 = service.click(now); | |
expect(boxReward2).to.be.equal(0); | |
expect(repo.lastBox).to.be.equal(4); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment