mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-04-14 19:56:23 +00:00
fix world state
This commit is contained in:
parent
96fed21fbd
commit
f835cf2761
4 changed files with 79 additions and 41 deletions
|
|
@ -18,7 +18,9 @@ describe('GET /world-state', () => {
|
|||
});
|
||||
|
||||
it('returns Tavern quest data when world boss is active', async () => {
|
||||
await updateDocument('groups', { _id: TAVERN_ID }, { quest: { active: true, key: 'dysheartener', progress: { hp: 50000, rage: 9999 } } });
|
||||
sinon.stub(worldState, 'getWorldBoss').returns({
|
||||
active: true, extra: {}, key: 'dysheartener', progress: { hp: 50000, rage: 9999, collect: {} },
|
||||
});
|
||||
|
||||
const res = await requester().get('/world-state');
|
||||
expect(res).to.have.nested.property('worldBoss');
|
||||
|
|
@ -33,15 +35,29 @@ describe('GET /world-state', () => {
|
|||
rage: 9999,
|
||||
},
|
||||
});
|
||||
worldState.getWorldBoss.restore();
|
||||
});
|
||||
|
||||
it('calls getRepeatingEvents for data', async () => {
|
||||
const getRepeatingEventsOnDate = sinon.stub(common.content, 'getRepeatingEventsOnDate').returns([]);
|
||||
const getCurrentGalaEvent = sinon.stub(common.schedule, 'getCurrentGalaEvent').returns({});
|
||||
|
||||
await requester().get('/world-state');
|
||||
|
||||
expect(getRepeatingEventsOnDate).to.have.been.calledOnce;
|
||||
expect(getCurrentGalaEvent).to.have.been.calledOnce;
|
||||
|
||||
getRepeatingEventsOnDate.restore();
|
||||
getCurrentGalaEvent.restore();
|
||||
});
|
||||
|
||||
context('no current event', () => {
|
||||
beforeEach(async () => {
|
||||
sinon.stub(worldState, 'getCurrentEvent').returns(null);
|
||||
sinon.stub(worldState, 'getCurrentEventList').returns([]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
worldState.getCurrentEvent.restore();
|
||||
worldState.getCurrentEventList.restore();
|
||||
});
|
||||
|
||||
it('returns null for the current event when there is none active', async () => {
|
||||
|
|
@ -51,18 +67,18 @@ describe('GET /world-state', () => {
|
|||
});
|
||||
});
|
||||
|
||||
context('no current event', () => {
|
||||
context('active event', () => {
|
||||
const evt = {
|
||||
...common.content.events.fall2020,
|
||||
event: 'fall2020',
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
sinon.stub(worldState, 'getCurrentEvent').returns(evt);
|
||||
sinon.stub(worldState, 'getCurrentEventList').returns([evt]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
worldState.getCurrentEvent.restore();
|
||||
worldState.getCurrentEventList.restore();
|
||||
});
|
||||
|
||||
it('returns the current event when there is an active one', async () => {
|
||||
|
|
@ -71,4 +87,45 @@ describe('GET /world-state', () => {
|
|||
expect(res.currentEvent).to.eql(evt);
|
||||
});
|
||||
});
|
||||
|
||||
context('active event with NPC image suffix', () => {
|
||||
const evt = {
|
||||
...common.content.events.fall2020,
|
||||
event: 'fall2020',
|
||||
npcImageSuffix: 'fall',
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
sinon.stub(worldState, 'getCurrentEventList').returns([evt]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
worldState.getCurrentEventList.restore();
|
||||
});
|
||||
|
||||
it('returns the NPC image suffix when present', async () => {
|
||||
const res = await requester().get('/world-state');
|
||||
|
||||
expect(res.npcImageSuffix).to.equal('fall');
|
||||
});
|
||||
|
||||
it('returns the NPC image suffix with multiple events present', async () => {
|
||||
const evt2 = {
|
||||
...common.content.events.winter2020,
|
||||
event: 'test',
|
||||
};
|
||||
|
||||
const evt3 = {
|
||||
...common.content.events.winter2020,
|
||||
event: 'winter2020',
|
||||
npcImageSuffix: 'winter',
|
||||
};
|
||||
|
||||
worldState.getCurrentEventList.returns([evt, evt2, evt3]);
|
||||
|
||||
const res = await requester().get('/world-state');
|
||||
|
||||
expect(res.npcImageSuffix).to.equal('winter');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ api.armoire = {
|
|||
|
||||
api.events = EVENTS;
|
||||
api.repeatingEvents = REPEATING_EVENTS;
|
||||
api.getRepeatingEventsOnDate = getRepeatingEvents;
|
||||
|
||||
api.classes = CLASSES;
|
||||
|
||||
|
|
|
|||
|
|
@ -27,15 +27,20 @@ api.getWorldState = {
|
|||
method: 'GET',
|
||||
url: '/world-state',
|
||||
async handler (req, res) {
|
||||
const worldState = {};
|
||||
const worldState = { npcImageSuffix: '', currentEvent: null };
|
||||
|
||||
worldState.worldBoss = await getWorldBoss();
|
||||
worldState.currentEventList = getCurrentEventList();
|
||||
if (worldState.currentEventList.length > 0) {
|
||||
[worldState.currentEvent] = worldState.currentEventList;
|
||||
worldState.npcImageSuffix = worldState.currentEvent ? worldState.currentEvent.npcImageSuffix : '';
|
||||
}
|
||||
|
||||
worldState.currentEventList.forEach(event => {
|
||||
if (event.npcImageSuffix) {
|
||||
worldState.npcImageSuffix = event.npcImageSuffix;
|
||||
}
|
||||
});
|
||||
|
||||
res.respond(200, worldState);
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -18,46 +18,21 @@ export async function getWorldBoss () {
|
|||
|
||||
export function getCurrentEvent () {
|
||||
const now = moment();
|
||||
const currEvtKey = Object.keys(common.content.repeatingEvents).find(
|
||||
evtKey => {
|
||||
const event = common.content.repeatingEvents[evtKey];
|
||||
const startDate = event.start.replace('1970', now.year());
|
||||
const endDate = event.end.replace('1970', now.year());
|
||||
const currentEvents = common.content.getRepeatingEventsOnDate(now);
|
||||
|
||||
return now.isBetween(startDate, endDate);
|
||||
},
|
||||
);
|
||||
|
||||
if (!currEvtKey) {
|
||||
if (currentEvents.length === 0) {
|
||||
return common.schedule.getCurrentGalaEvent();
|
||||
}
|
||||
return {
|
||||
event: currEvtKey,
|
||||
...common.content.repeatingEvents[currEvtKey],
|
||||
event: currentEvents[0].key,
|
||||
...currentEvents[0],
|
||||
};
|
||||
}
|
||||
|
||||
export function getCurrentEventList () {
|
||||
const now = moment();
|
||||
const currentEventKeys = filter(
|
||||
Object.keys(common.content.repeatingEvents),
|
||||
eventKey => {
|
||||
const eventData = common.content.repeatingEvents[eventKey];
|
||||
const startDate = eventData.start.replace('1970', now.year());
|
||||
const endDate = eventData.end.replace('1970', now.year());
|
||||
|
||||
return now.isBetween(startDate, endDate);
|
||||
},
|
||||
);
|
||||
|
||||
const currentEventList = [];
|
||||
|
||||
currentEventKeys.forEach(key => {
|
||||
currentEventList.push({
|
||||
event: key,
|
||||
...common.content.repeatingEvents[key],
|
||||
});
|
||||
});
|
||||
currentEventList.push(common.schedule.getCurrentGalaEvent());
|
||||
return currentEventList;
|
||||
console.log(common.content.getRepeatingEventsOnDate);
|
||||
const currentEvents = common.content.getRepeatingEventsOnDate(now);
|
||||
currentEvents.push(common.schedule.getCurrentGalaEvent());
|
||||
return currentEvents;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue