Skip to content

Commit

Permalink
feat: RPG (#780)
Browse files Browse the repository at this point in the history
* feat: Added RPG table schemas

* sql: Refined the schema even more, added interfaces

* misc: Added PG#fetchRpgUser

* perf: How 'bout a huge nanoboost for DatabaseInit?

* feat: Clean up and do more progress

* misc: Change the schema a lot more

* feat: Added battles

* feat: Add more fields, changed some datatypes

* feat: Added selected weapons, CREATE RULE guards, etc

* fix: Resolved bug in JSON#insertRpgGuild
  • Loading branch information
kyranet committed Feb 7, 2020
1 parent dbcbd8d commit 9bdf769
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/lib/queries/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RawMemberSettings } from '@lib/types/settings/raw/RawMemberSettings';
import { RawModerationSettings } from '@lib/types/settings/raw/RawModerationSettings';
import { RawStarboardSettings } from '@lib/types/settings/raw/RawStarboardSettings';
import { RawTwitchStreamSubscriptionSettings } from '@lib/types/settings/raw/RawTwitchStreamSubscriptionSettings';
import { RawRpgGuild, RawRpgGuildRank, RawRpgClass, RawRpgItem } from '@lib/types/settings/raw/RawGameSettings';

export interface CommonQuery {
deleteUserEntries(userID: string): Promise<unknown>;
Expand Down Expand Up @@ -32,11 +33,13 @@ export interface CommonQuery {
fetchStarsFromUser(guildID: string, userID: string, minimum: number): Promise<RawStarboardSettings[]>;
fetchTwitchStreamSubscription(streamerID: string): Promise<TwitchStreamSubscriptionSettings | null>;
fetchTwitchStreamsByGuild(guildID: string): Promise<TwitchStreamSubscriptionSettings[]>;
retrieveRandomItem(luck: number): Promise<RawRpgItem>;
insertCommandUseCounter(command: string): Promise<unknown>;
insertDashboardUser(entry: RawDashboardUserSettings): Promise<unknown>;
insertGiveaway(entry: RawGiveawaySettings): Promise<unknown>;
insertModerationLog(entry: RawModerationSettings): Promise<unknown>;
insertStar(entry: RawStarboardSettings): Promise<unknown>;
insertRpgGuild(leaderID: string, name: string): Promise<unknown>;
updateModerationLog(entry: RawModerationSettings): Promise<unknown>;
updateModerationLogReasonBulk(guildID: string, cases: readonly number[], reason: string | null): Promise<unknown>;
updateStar(entry: RawStarboardSettings): Promise<unknown>;
Expand Down Expand Up @@ -64,6 +67,26 @@ export interface UpsertMemberSettingsReturningDifference {
new_value: number;
}

export interface RpgUserEntryItem extends RawRpgItem {
durability: string;
}

export interface RpgUserEntry {
id: string;
name: string;
win_count: string;
guild: RawRpgGuild | null;
guild_rank: RawRpgGuildRank | null;
class: RawRpgClass | null;
items: RpgUserEntryItem[];
death_count: string;
crate_common_count: number;
crate_uncommon_count: number;
crate_rare_count: number;
crate_legendary_count: number;
luck: number;
}

export interface TwitchStreamSubscriptionSettings {
id: string;
is_streaming: boolean;
Expand Down
53 changes: 53 additions & 0 deletions src/lib/queries/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RawModerationSettings } from '@lib/types/settings/raw/RawModerationSett
import { RawStarboardSettings } from '@lib/types/settings/raw/RawStarboardSettings';
import { RawTwitchStreamSubscriptionSettings } from '@lib/types/settings/raw/RawTwitchStreamSubscriptionSettings';
import { RawUserSettings } from '@lib/types/settings/raw/RawUserSettings';
import { RawRpgItem } from '@lib/types/settings/raw/RawGameSettings';
import { JsonProvider } from '@lib/types/util';
import { Client } from 'discord.js';
import { CommonQuery, LeaderboardEntry, TwitchStreamSubscriptionSettings, UpdatePurgeTwitchStreamReturning, UpsertMemberSettingsReturningDifference } from './common';
Expand Down Expand Up @@ -233,6 +234,23 @@ export class JsonCommonQuery implements CommonQuery {
return values.filter(value => value.guild_ids.includes(guildID));
}

public async retrieveRandomItem(luck: number) {
const entries = (await this.provider.getAll(Databases.RpgItems) as RawRpgItem[])
.sort((a, b) => a.id - b.id);

const count = entries.reduce((acc, entry) => acc + Number(entry.rarity), 0);
const percentage = luck === 0 ? 1 : 1 / luck;
const maximum = Math.random() * count * percentage;

let counter = 0;
for (const entry of entries) {
counter += Number(entry.rarity);
if (counter >= maximum) return entry;
}

return entries[entries.length - 1];
}

public async insertCommandUseCounter(command: string) {
const value = await this.provider.get(Databases.CommandCounter, command) as { id: string; uses: number };
if (value) await this.provider.update(Databases.CommandCounter, command, { uses: value.uses + 1 });
Expand All @@ -255,6 +273,36 @@ export class JsonCommonQuery implements CommonQuery {
return this.provider.create(Databases.Moderation, `${entry.guild_id}.${entry.message_id}`, entry);
}

public async insertRpgGuild(leaderID: string, name: string) {
// Retrieve counter from "meta" entry in database
let meta = await this.provider.get(Databases.RpgGuilds, 'meta') as RpgGuildMetaEntry | null;
if (meta === null) {
meta = { id: 'meta', counter: 1 };
await this.provider.create(Databases.RpgGuilds, 'meta', meta);
} else {
++meta.counter;
await this.provider.update(Databases.RpgGuilds, 'meta', meta);
}

// Create the guild data and update information
const id = meta.counter;
await Promise.all([
this.provider.create(Databases.RpgGuilds, `${id}`, {
id,
name,
description: null,
leader: leaderID,
member_limit: 5,
win_count: 0,
lose_count: 0,
money_count: 0,
bank_limit: 50000,
upgrade: 0
}),
this.provider.update(Databases.RpgUsers, leaderID, { guild_id: id })
]);
}

public updateModerationLog(entry: RawModerationSettings) {
return this.provider.update(Databases.Moderation, `${entry.guild_id}.${entry.case_id}`, entry);
}
Expand Down Expand Up @@ -337,3 +385,8 @@ export class JsonCommonQuery implements CommonQuery {
}

}

interface RpgGuildMetaEntry {
id: string;
counter: number;
}
35 changes: 35 additions & 0 deletions src/lib/queries/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RawTwitchStreamSubscriptionSettings } from '@lib/types/settings/raw/Raw
import { Client } from 'discord.js';
import PostgresProvider from 'src/providers/postgres';
import { CommonQuery, UpdatePurgeTwitchStreamReturning, UpsertMemberSettingsReturningDifference } from './common';
import { RawRpgItem } from '@lib/types/settings/raw/RawGameSettings';

export class PostgresCommonQuery implements CommonQuery {

Expand Down Expand Up @@ -371,6 +372,24 @@ export class PostgresCommonQuery implements CommonQuery {
}));
}

public retrieveRandomItem(luck: number) {
const { provider } = this;
const percentage = luck === 0 ? '' : ` * (1.0 / ${provider.cNumber(luck)})`;
return provider.runOne<RawRpgItem>(/* sql */`
WITH CTE AS (
SELECT RANDOM()${percentage} * (SELECT SUM(rarity) FROM rpg_items) R
)
SELECT "id", "name", "rarity"
FROM (
SELECT rpg_items.*, SUM(rarity) OVER (ORDER BY id) S, R
FROM rpg_items CROSS JOIN CTE
) Q
WHERE S >= R
ORDER BY id
LIMIT 1;
`);
}

public insertCommandUseCounter(command: string) {
return this.provider.run(/* sql */`
INSERT
Expand Down Expand Up @@ -431,6 +450,22 @@ export class PostgresCommonQuery implements CommonQuery {
`, [entry.enabled, entry.user_id, entry.message_id, entry.channel_id, entry.guild_id, entry.star_message_id, entry.stars]);
}

public insertRpgGuild(leaderID: string, name: string) {
const { provider } = this;
const cLeader = provider.cString(leaderID);
const cName = provider.cString(name);
return provider.run(/* sql */`
WITH g AS (
INSERT INTO rpg_guilds ("name", "leader")
VALUES (${cName}, ${cLeader})
RETURNING id
)
UPDATE rpg_users
SET guild_id = (SELECT id FROM g)
WHERE id = ${cLeader};
`);
}

public updateModerationLog(entry: RawModerationSettings) {
return this.provider.run(/* sql */`
UPDATE moderation
Expand Down
13 changes: 10 additions & 3 deletions src/lib/types/constants/Constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
export const enum Databases {
Banners = 'banners',
CommandCounter = 'command_counter',
DashboardUsers = 'dashboard_users',
Giveaway = 'giveaway',
Guilds = 'guilds',
Members = 'members',
Moderation = 'moderation',
RpgClass = 'rpg_class',
RpgGuildRank = 'rpg_guild_rank',
RpgGuilds = 'rpg_guilds',
RpgItems = 'rpg_items',
RpgItemRanks = 'rpg_item_ranks',
RpgUserItems = 'rpg_user_items',
RpgUsers = 'rpg_users',
Starboard = 'starboard',
Users = 'users',
CommandCounter = 'command_counter',
TwitchStreamSubscriptions = 'twitch_stream_subscriptions',
DashboardUsers = 'dashboard_users'
Users = 'users'
}

export const enum Colors {
Expand Down
Loading

0 comments on commit 9bdf769

Please sign in to comment.