Skip to content

Commit

Permalink
basic webserver implementation for user stats requests
Browse files Browse the repository at this point in the history
  • Loading branch information
morozoffnor committed Dec 30, 2023
1 parent 8a3dc66 commit 38c377b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
12 changes: 11 additions & 1 deletion app/api/users.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getUserById} from "../database/database.mjs";
import {getAllUsers, getUserById} from "../database/database.mjs";
import {logger} from "../tools/logger.mjs";

export async function apiGetUserById(req, res) {
Expand All @@ -10,4 +10,14 @@ export async function apiGetUserById(req, res) {
logger.error('User not found', e)
res.status(404).send('User not found')
}
}

export async function apiGetAllUsers(req, res) {
const users = await getAllUsers()
try {
res.send(users)
} catch (e) {
logger.error('Users not found', e)
res.status(404).send('Users not found')
}
}
13 changes: 9 additions & 4 deletions app/database/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export async function cleanAllCurrentSizes() {
const query = User.updateMany({}, {'cockStats.currentSize': null})
await query.exec()
}

export async function getAllUsers() {
const users = User.find()
return await users.exec()
}
// Properties
export async function getProperties() {
return await Prop.findOne({}).exec()
}
// User stats class
export class IncUserStats {

Expand Down Expand Up @@ -166,8 +175,4 @@ export class IncUserStats {
}}
).exec()
}
}

export async function getProperties() {
return await Prop.findOne({}).exec()
}
7 changes: 6 additions & 1 deletion app/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {detectYakuza} from "./tools/yakuzaDetector.mjs";
import {logger} from "./tools/logger.mjs";
import express from "express";
import {tokenChecker} from "./api/tools/APItokenChecker.mjs";
import {apiGetUserById} from "./api/users.mjs";
import {apiGetAllUsers, apiGetUserById} from "./api/users.mjs";

// connect to DB
await connect()
Expand All @@ -37,6 +37,11 @@ api.get('/user/:id', async (req, res) => {
await apiGetUserById(req, res)
})

api.get('/users/', async (req, res) => {
await apiGetAllUsers(req, res)
logger.info(req.headers)
})

// Bot
export const bot = new Telegraf(config.botToken)
await initDays()
Expand Down
24 changes: 24 additions & 0 deletions app/tools/timeConverter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


/**
* Converts time to human-readable string
* @param {Number} number - time in seconds
* @returns {Promise<String>} - time in human-readable format
*/
export async function convertNumberToTimeString(number) {
const date = new Date(number * 1000)
const hours = date.getUTCHours()
const minutes = date.getUTCMinutes()
const seconds = date.getUTCSeconds()
let timeString = ''
if (hours > 0) {
timeString += hours + 'ч '
}
if (minutes > 0) {
timeString += minutes + 'м '
}
if (seconds > 0) {
timeString += seconds + 'с'
}
return timeString
}
6 changes: 4 additions & 2 deletions app/tools/yakuzaDetector.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getProperties} from "../database/database.mjs";
import {convertNumberToTimeString} from "./timeConverter.mjs";

const YakuzaMessageCooldown = 1000 * 60 * 60 // 1 hour

Expand All @@ -11,7 +12,7 @@ async function updateYakuza() {

export async function detectYakuza(ctx){
let yakuzaz = ['якуза', 'якузе', 'якузу', 'якуза', 'якузе', 'якузу', 'якудза', 'якудзе', 'якудзу', 'якудза', 'якудзе', 'якудзу', 'кирио', 'кирию']
if (yakuzaz.some(v => ctx.message.text.includes(v))) {
if (yakuzaz.some(v => ctx.message.text.toLowerCase().includes(v))) {
await sendMessage(ctx)
await updateYakuza()
}
Expand Down Expand Up @@ -40,6 +41,7 @@ async function getTimeDifference() {
async function sendMessage(ctx) {
const timeDifference = await getTimeDifference()
if (timeDifference > YakuzaMessageCooldown) {
await ctx.reply(`Времени без упоминания якузы: ${timeDifference / 1000 / 60 / 60} час(ов)`)

await ctx.reply(`Времени без упоминания якузы: ${await convertNumberToTimeString(timeDifference)}`)
}
}

0 comments on commit 38c377b

Please sign in to comment.