Compare commits

...

2 Commits

Author SHA1 Message Date
Jill 5be9dbfe21
health groundwork 2023-11-21 11:40:44 +03:00
Jill 249390cf5d
fixin stuff i didn't catch on lapop 2023-11-20 18:20:12 +03:00
8 changed files with 52 additions and 14 deletions

View File

@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema
.createTable('initHealth', table =>
table.string('user').notNullable().unique()
);
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema
.dropTable('initHealth');
};

View File

@ -86,4 +86,7 @@ export interface Session {
accessToken: string,
refreshToken: string,
expiresAt: number,
}
export interface InitHealth {
user: string,
}

View File

@ -1,5 +1,6 @@
import { AutocompleteInteraction } from 'discord.js';
import { CustomItem, ItemInventory, db } from '../db';
import { MAX_HEALTH } from './pvp';
export type DefaultItem = Omit<CustomItem, 'guild'>; // uses negative IDs
export type Item = DefaultItem | CustomItem;
@ -117,7 +118,7 @@ export const defaultItems: DefaultItem[] = [
description: 'ow',
emoji: '🩸',
type: 'plain',
maxStack: 1024,
maxStack: MAX_HEALTH,
untradable: false
},
{

15
src/lib/rpg/pvp.ts Normal file
View File

@ -0,0 +1,15 @@
import { InitHealth, db } from '../db';
import { DefaultItems, getDefaultItem, giveItem } from './items';
export const MAX_HEALTH = 100;
export async function initHealth(user: string) {
const isInitialized = await db<InitHealth>('initHealth')
.where('user', user)
.first();
if (!isInitialized) {
giveItem(user, getDefaultItem(DefaultItems.BLOOD), MAX_HEALTH);
await db<InitHealth>('initHealth').insert({ user });
}
}

View File

@ -36,7 +36,7 @@ export function updateCookie(res: Response, sessionId: string) {
export async function getToken(bot: Client, code: string) {
try {
return await got.post(DISCORD_ENDPOINT + Routes.oauth2TokenExchange(), {
return await got.post(DISCORD_ENDPOINT + Routes.oauth2TokenExchange(), {
form: {
client_id: bot.config.clientId,
client_secret: bot.config.clientSecret,
@ -46,7 +46,7 @@ export async function getToken(bot: Client, code: string) {
} satisfies RESTPostOAuth2AccessTokenURLEncodedData
// if you're looking to change this then you are blissfully unaware of the past
// and have learnt 0 lessons
}).json() as RESTPostOAuth2AccessTokenResult
}).json() as RESTPostOAuth2AccessTokenResult;
} catch(err) {
log.error(err);
return;
@ -97,6 +97,7 @@ export async function getSession(bot: Client, headers: IncomingHttpHeaders) {
if (Date.now() < session.expiresAt) return session;
const newSession = refreshToken(bot, session.id, session.refreshToken);
return newSession;
}
export async function setSession(sessionId: string, sessionData: Omit<Session, 'id'>) {

View File

@ -1,4 +1,4 @@
import { Session, db } from '../lib/db';
import { Session } from '../lib/db';
import * as log from '../lib/log';
import got from 'got';
import { APIPartialGuild, APIUser, Routes } from 'discord.js';

View File

@ -1,7 +1,7 @@
import express from 'express';
import { create } from 'express-handlebars';
import * as log from '../lib/log';
import { CustomItem, Counter, Session, CustomCraftingRecipe, db } from '../lib/db';
import { CustomItem, Counter, CustomCraftingRecipe, db } from '../lib/db';
import { defaultItems } from '../lib/rpg/items';
import { Client, CDN } from 'discord.js';
import { getToken, getSessionString, getSession, setSession, updateCookie } from './oauth2';
@ -12,22 +12,22 @@ async function getGuildInfo(bot: Client, id: string) {
if (!guild) return;
const items = await db<CustomItem>('customItems')
.where('guild', guild)
.where('guild', guild.id)
.count({count: '*'});
const counters = await db<Counter>('counters')
.where('guild', guild)
.where('guild', guild.id)
.count({count: '*'});
const recipes = await db<CustomCraftingRecipe>('customCraftingRecipes')
.where('guild', guild)
.where('guild', guild.id)
.count({count: '*'});
return {
items: items[0].count as number,
counters: counters[0].count as number,
recipes: recipes[0].count as number,
}
};
}
export async function startServer(bot: Client, port: number) {
@ -39,7 +39,7 @@ export async function startServer(bot: Client, port: number) {
avatar: (id: string, hash: string) => (id && hash) ? cdn.avatar(id, hash, { size: 128 }) : '/assets/avatar.png',
icon: (id: string, hash: string) => (id && hash) ? cdn.icon(id, hash, { size: 128, forceStatic: true }) : '/assets/avatar.png',
}
})
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
@ -99,8 +99,7 @@ export async function startServer(bot: Client, port: number) {
res.render('home', {
signedIn: session !== undefined,
username: user?.global_name,
avatar: user?.avatar ? cdn.avatar(user.id, user.avatar, { size: 128 }) : '/assets/avatar.png',
user: user,
layout: false,
});
});

View File

@ -18,11 +18,11 @@
<body>
<div id="login" onclick="window.location = '/profile'">
{{#if signedIn}}
<div class="username">{{username}}</div>
<div class="username">{{user.global_name}}</div>
{{else}}
<div class="username logged-out">log in</div>
{{/if}}
<img class="avatar" src="{{avatar}}" width="128" height="128">
<img class="avatar" src="{{avatar user.id user.avatar}}" width="128" height="128">
</div>
<div id="main">
<img src="/assets/jillo.png" width="150" height="200">