Skip to content

Commit

Permalink
feat(town): add worship feature. add worship leaderboard. add worship…
Browse files Browse the repository at this point in the history
… to headless runner
  • Loading branch information
seiyria committed Sep 21, 2023
1 parent 2cb92c9 commit 94edd14
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/src/modules/content/constants.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class ConstantsService {

this.worshipDuration = +this.configService.get<number>(
'WORSHIP_DURATION',
24,
1,
);

this.worshipCoinBoost = +this.configService.get<number>(
Expand Down
4 changes: 4 additions & 0 deletions server/src/modules/content/content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export class ContentService {
return Object.values(this.resources ?? {});
}

public isResource(resourceId: string): boolean {
return !!this.resources[resourceId];
}

public getResource(resourceId: string): IResource {
const resourceRef = this.resources[resourceId];
if (!resourceRef)
Expand Down
2 changes: 1 addition & 1 deletion server/src/modules/gameplay/worship.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class WorshipService {

await this.statsService.incrementStat(userId, 'worships' as TrackedStat, 1);
await this.statsService.incrementStat(
deity,
userId,
`worship${capitalize(deity)}` as TrackedStat,
1,
);
Expand Down
11 changes: 6 additions & 5 deletions server/src/modules/market/market.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export class MarketService {
const user = await this.userService.findUserById(userId);
if (!user) throw new NotFoundException(`User ${userId} not found`);

const isResource = this.contentService.getResource(instanceId);
const isResource = this.contentService.isResource(instanceId);
let itemRef: IItem | undefined;
let itemId = '';

if (isResource) {
itemRef = isResource;
itemId = isResource.itemId;
const resourceRef = this.contentService.getResource(instanceId);
itemRef = resourceRef;
itemId = resourceRef.itemId;
} else {
const inventoryItem = await this.inventoryService.getInventoryItemForUser(
userId,
Expand Down Expand Up @@ -341,7 +342,7 @@ export class MarketService {
const user = await this.userService.findUserById(userId);
if (!user) throw new NotFoundException(`User ${userId} not found`);

const isResource = this.contentService.getResource(listing.itemId);
const isResource = this.contentService.isResource(listing.itemId);

if (!this.playerHelper.hasCoins(player, listing.price))
return userError('Not enough coins');
Expand Down Expand Up @@ -487,7 +488,7 @@ export class MarketService {
if (listing.userId !== userId)
throw new BadRequestException('You cannot remove this listing');

const isResource = this.contentService.getResource(listing.itemId);
const isResource = this.contentService.isResource(listing.itemId);
const isInventoryFull = await this.inventoryService.isInventoryFull(userId);
if (!isResource && isInventoryFull)
return userError('Your inventory is full!');
Expand Down
49 changes: 49 additions & 0 deletions server/src/utils/leaderboard-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,53 @@ export const leaderboardQueries = [
};
},
})),
...[
{
name: 'Worship: Most Faithful',
singleUserName: 'Total Worships',
stat: 'worships',
},
{
name: 'Worship: Most Buibui Worships',
singleUserName: 'Buibui Worships',
stat: 'worshipCoins',
},
{
name: 'Worship: Most Eindew Worships',
singleUserName: 'Eindew Worships',
stat: 'worshipXp',
},
{
name: `Worship: Most Gra'Chl Worships`,
singleUserName: `Gra'Chl Worships`,
stat: 'worshipDefense',
},
{
name: 'Worship: Most Parthe Worships',
singleUserName: 'Parthe Worships',
stat: 'worshipTravel',
},
{
name: 'Worship: Most Ruspoo Worships',
singleUserName: 'Ruspoo Worships',
stat: 'worshipOffense',
},
{
name: 'Worship: Most Spoodles Worships',
singleUserName: 'Spoodles Worships',
stat: 'worshipNothing',
},
].map(({ name, singleUserName, stat }) => ({
name,
singleUserName,
query: { [`stats.${stat}`]: { $gt: 0 } },
fields: { ...alwaysFields, [`stats.${stat}`]: 1 },
params: { sort: { [`stats.${stat}`]: -1 }, limit: numPlayersPerCategory },
formatter: (data) => {
return {
...alwaysData(data),
value: data.stats?.[stat]?.toLocaleString() ?? '0',
};
},
})),
];
5 changes: 5 additions & 0 deletions utils/headless-runner/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
sellRandomItem,
sellRandomResource,
} from './market';
import { canWorship, worshipRandomDeity } from './worship';

export async function gameloop(playerApi: PlayerApi) {
try {
Expand All @@ -28,6 +29,10 @@ export async function gameloop(playerApi: PlayerApi) {
console.error('Error... server down? Skipping action...');
}

if (canWorship(playerApi)) {
await worshipRandomDeity(playerApi);
}

if (hasResources(playerApi) && hasEnoughCoins(playerApi)) {
await sellRandomResource(playerApi);
}
Expand Down
17 changes: 17 additions & 0 deletions utils/headless-runner/worship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PlayerApi } from './_types';

export function canWorship(playerApi: PlayerApi): boolean {
return (playerApi.player.player.deityPrayerCooldown ?? 0) < Date.now();
}

export async function worshipRandomDeity(playerApi: PlayerApi): Promise<any> {
const deities = ['travel', 'xp', 'coins', 'offense', 'defense', 'nothing'];
const randomDeity = deities[Math.floor(Math.random() * deities.length)];

console.info(`${playerApi.name} is worshipping: ${randomDeity}...`);

return playerApi.api
.url(`/gameplay/worship`)
.post({ deity: randomDeity })
.json();
}

0 comments on commit 94edd14

Please sign in to comment.