Skip to content

Commit

Permalink
fully rewritten items handler
Browse files Browse the repository at this point in the history
  • Loading branch information
morozoffnor committed Feb 17, 2024
1 parent 0a02aac commit e10200e
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 93 deletions.
21 changes: 15 additions & 6 deletions app/api/items.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express from 'express'
import {tokenChecker} from "./tools/APItokenChecker.mjs";
import {logger} from "../tools/logger.mjs";
import {useDebuffItem, useItem} from "../tools/items/tools/useItem.mjs";
import {getActiveUsers} from "../database/database.mjs";
import {getActiveUsers, getUserById} from "../database/database.mjs";
import {getAllItemObjects} from "../tools/items/tools/itemsHandler.mjs";
import {ItemsHandler} from "../items/ItemsHandler.mjs";

let ItemsRouter = express.Router()

Expand All @@ -15,17 +15,26 @@ let ItemsRouter = express.Router()
ItemsRouter.use(tokenChecker)
ItemsRouter.post('/use', express.json({type: 'application/json'}), async (req, res) => {
const usedItem = req.body

if (await useItem(usedItem)) {
let handler = new ItemsHandler({
user: await getUserById(usedItem['userId']),
itemId: usedItem['itemId']
})
if (await handler.activateItem()) {
res.status(200).send('Hello World!');
return
} else {
res.status(500).send('ЧТО-ТО ПОШЛО НЕ ТАК БЛЯТЬ');
}
res.status(500).send('ЧТО-ТО ПОШЛО НЕ ТАК БЛЯТЬ');
});

ItemsRouter.post('/use/debuff', express.json({type: 'application/json'}), async (req, res) => {
const usedItem = req.body
if (await useDebuffItem(usedItem)) {
const handler = new ItemsHandler({
user: await getUserById(parseInt(usedItem['userId'])),
target: await getUserById(parseInt(usedItem['target'])),
itemId: usedItem['itemId']
})
if (await handler.debuffUser()) {
res.status(200).send('Hello World!');
return
}
Expand Down
120 changes: 120 additions & 0 deletions app/items/ItemsHandler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {getActiveUsers, IncUserStats} from "../database/database.mjs";
import {bot} from "../main.mjs";
import {config} from "../config.mjs";
import {ChinesePump} from "../tools/items/chinesePump.mjs";
import {Gondonfedi} from "../tools/items/gondonfedi.mjs";
import {Minimizer300} from "../tools/items/minimizer300.mjs";
import {Monolith} from "../tools/items/monolith.mjs";
import {Bolt} from "../tools/items/bolt.mjs";

export class ItemsHandler {

stats = new IncUserStats()
constructor(info) {
if (info.user) {
this.user = info.user
}
if(info.target) {
this.target = info.target
}
if (info.size) {
this.size = info.size
}
if (info.itemId) {
this.itemId = info.itemId
}
if (info.item) {
this.item = info.item
}
}

async #sendMessage(message) {
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
}


async activateItem() {
// console.log(this.user)
console.log(this.item)
for (let i = 0; i < this.user.items.length; i++) {
if (this.user.items[i]._id == this.itemId) {
this.item = this.user.items[i]
// detecting random monolith
if (this.item.name === "Монолит" && this.item.effectInfo.randomize) {
console.log('activating instant monolith')
if (await this.#activateMonolith()) {
// save user and collect stats if ok
this.user.items.splice(i, 1);
await this.stats.items(this.user.userid)
this.user.save()
return true
} else {
return false
}
} else {
// base item activation flow
console.log('setting activate item for user')
this.user.activatedItem = this.user.items[i]
const message = `@${this.user.userName} использовал ${this.user.items[i].name} [${this.user.items[i].rarity}]`
this.user.items.splice(i, 1);
await this.user.save()
await this.stats.items(this.user.userid)
await this.#sendMessage(message)
return true
}

}
}
}

async useItem() {
switch (this.item.name) {
case "Уменьшатор 3000":
return await new Minimizer300(this.item).activateItem(this.size)
case "Гондон Феди":
return await new Gondonfedi(this.item).activateItem(this.user)
case "Китайская помпа":
return await new ChinesePump(this.item).activateItem(this.size)
case "Монолит":
return await new Monolith(this.item).activateItem(this.size)
case "Болтище":
return await new Bolt(this.item).activateItem(this.size)
}
}

async debuffUser() {
for (let i = 0; i < this.user.items.length; i++) {
if (this.user.items[i]._id == this.itemId) {
this.item = this.user.items[i]
this.size = this.target.cockStats.currentSize
console.log('using bolt')
this.target.cockStats.currentSize = await this.useItem()
const message = `@${this.user.userName} использовал ${this.user.items[i].name} [${this.user.items[i].rarity}] на @${this.target.userName}!\nТеперь его хуй равен ${this.target.cockStats.currentSize}`
await this.target.save()
this.user.items.splice(i, 1);
await this.user.save()
await this.stats.items(this.user.userid)
await this.#sendMessage(message)
return true
}
}
}

async #activateMonolith() {
const activeUsers = await getActiveUsers()
if (activeUsers.length < 1) {
return false
} else {
let randomUser = activeUsers[Math.floor(Math.random() * activeUsers.length)]
this.size = randomUser.cockStats.currentSize
randomUser.cockStats.currentSize = await this.useItem()
await randomUser.save()
const message = `@${this.user.userName} использовал <b>${this.item.name} [${this.item.rarity}]</b>! Боги рандома выбрали целью @${randomUser.userName}. Теперь его хуй - ${randomUser.cockStats.currentSize}см`
await this.#sendMessage(message)
return true
}


}

}
9 changes: 7 additions & 2 deletions app/sizes/inlineQuery.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createQuery, getLastAttempt, getLastQuery, getUserById} from "../databas
import {createNewUser} from "../tools/createNewUser.mjs";
import {config} from "../config.mjs";
import {getCockStatsString} from "../stats/sizeStats.mjs";
import {handleItem} from "../tools/items/tools/itemsHandler.mjs";
import {ItemsHandler} from "../items/ItemsHandler.mjs";

/**
* Creates inline for the user. Creates a new user if necessary
Expand Down Expand Up @@ -102,7 +102,12 @@ async function createQueryData(ctx){
if (user.activatedItem != null) {
console.log('found activated item in user')
console.log(user.activatedItem)
const affectedSize = await handleItem(user.activatedItem, await getRandomSize(), user)
let handler = new ItemsHandler({
user: user,
item: user.activatedItem,
size: await getRandomSize()
})
const affectedSize = await handler.useItem()
return {
userId: ctx.from.id,
size: affectedSize,
Expand Down
4 changes: 2 additions & 2 deletions app/tools/items/monolith.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Monolith extends BaseItem{
this.effect = {
descriptions: ["умножает размер хуя на 0.7"],
alert: "",
title: ""
title: "Умножает хуй при следующем замере"
}
this.icon = "monolithCommon.png"
this.effectInfo = {
Expand All @@ -42,7 +42,7 @@ export class Monolith extends BaseItem{
this.effect = {
descriptions: ["умножает размер хуя на 0.5"],
alert: "",
title: ""
title: "Умножает хуй при следующем замере"
}
this.icon = "monolithUncommon.png"
this.effectInfo = {
Expand Down
165 changes: 82 additions & 83 deletions app/tools/items/tools/useItem.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,87 @@ import {getActiveUsers, getUserById, IncUserStats} from "../../../database/datab
import {bot} from "../../../main.mjs";
import {config} from "../../../config.mjs";
import {Bolt} from "../bolt.mjs";
import {handleItem} from "./itemsHandler.mjs";

// TODO rewrite this horrible mess
export async function useItem(usedItem) {
let user = await getUserById(parseInt(usedItem['userId']))
const stats = new IncUserStats()
console.log(user.items.length)
for (let i = 0; i < user.items.length; i++) {
console.log('iterating through item ' + user.items[i]._id)
if (user.items[i]._id == usedItem['itemId']) {
// console.log('found item in user ' + user.id)
if (user.items[i].effectInfo){
if (user.items[i].effectInfo.randomize) {
const targets = await getActiveUsers()
if (targets.length < 1) {
return false
}
const target = targets[Math.floor(Math.random() * targets.length)]
const targetSize = target.cockStats.currentSize
const newTargetSize = await handleItem(user.items[i], targetSize)
target.cockStats.currentSize = await newTargetSize
const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]! Боги рандома выбрали целью @${target.userName}. Теперь его хуй - ${target.cockStats.currentSize}`
user.items.splice(i, 1);
await user.save()
await target.save()
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
} else {
console.log('setting activate item for user')
user.activatedItem = user.items[i]
const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]`
user.items.splice(i, 1);
await user.save()
await stats.items(user.userid)
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
}
} else {
console.log('setting activate item for user')
user.activatedItem = user.items[i]
const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]`
user.items.splice(i, 1);
await user.save()
await stats.items(user.userid)
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
}
return true
}
}
return false
}

export async function useDebuffItem(usedItem) {
let user = await getUserById(parseInt(usedItem['userId']))
let target = await getUserById(parseInt(usedItem['target']))
const stats = new IncUserStats()
await stats.items(user.userid)
console.log(user.items.length)
for (let i = 0; i < user.items.length; i++) {
console.log('iterating through item ' + user.items[i]._id)
if (user.items[i]._id == usedItem['itemId']) {
// console.log('found item in user ' + user.id)
console.log('debuffing other user')
if (user.items[i].effectInfo.instant) {
const targetSize = target.cockStats.currentSize
const newTargetSize = await handleItem(user.items[i], targetSize)
target.cockStats.currentSize = await newTargetSize
const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}] на @${target.userName}!\nТеперь его хуй равен ${target.cockStats.currentSize}`
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
}
else {
target.activatedItem = user.items[i]
const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}] на @${target.userName}`
await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
}
user.items.splice(i, 1);
await user.save()
await target.save()
return true
}
}
return false
}
// export async function useItem(usedItem) {
// let user = await getUserById(parseInt(usedItem['userId']))
// const stats = new IncUserStats()
// console.log(user.items.length)
// for (let i = 0; i < user.items.length; i++) {
// console.log('iterating through item ' + user.items[i]._id)
// if (user.items[i]._id == usedItem['itemId']) {
// // console.log('found item in user ' + user.id)
// if (user.items[i].effectInfo){
// if (user.items[i].effectInfo.randomize) {
// const targets = await getActiveUsers()
// if (targets.length < 1) {
// return false
// }
// const target = targets[Math.floor(Math.random() * targets.length)]
// const targetSize = target.cockStats.currentSize
// const newTargetSize = await handleItem(user.items[i], targetSize)
// target.cockStats.currentSize = await newTargetSize
// const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]! Боги рандома выбрали целью @${target.userName}. Теперь его хуй - ${target.cockStats.currentSize}`
// user.items.splice(i, 1);
// await user.save()
// await target.save()
// await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
// } else {
// console.log('setting activate item for user')
// user.activatedItem = user.items[i]
// const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]`
// user.items.splice(i, 1);
// await user.save()
// await stats.items(user.userid)
// await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
// }
//
// } else {
// console.log('setting activate item for user')
// user.activatedItem = user.items[i]
// const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}]`
// user.items.splice(i, 1);
// await user.save()
// await stats.items(user.userid)
// await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
// }
//
// return true
// }
// }
// return false
// }
//
// export async function useDebuffItem(usedItem) {
// let user = await getUserById(parseInt(usedItem['userId']))
// let target = await getUserById(parseInt(usedItem['target']))
// const stats = new IncUserStats()
// await stats.items(user.userid)
// console.log(user.items.length)
// for (let i = 0; i < user.items.length; i++) {
// console.log('iterating through item ' + user.items[i]._id)
// if (user.items[i]._id == usedItem['itemId']) {
// // console.log('found item in user ' + user.id)
// console.log('debuffing other user')
//
// if (user.items[i].effectInfo.instant) {
// const targetSize = target.cockStats.currentSize
// const newTargetSize = await handleItem(user.items[i], targetSize)
// target.cockStats.currentSize = await newTargetSize
// const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}] на @${target.userName}!\nТеперь его хуй равен ${target.cockStats.currentSize}`
// await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
// }
// else {
// target.activatedItem = user.items[i]
// const message = `@${user.userName} использовал ${user.items[i].name} [${user.items[i].rarity}] на @${target.userName}`
// await bot.telegram.sendMessage(config.chatId, message, {parse_mode: "HTML"})
// }
// user.items.splice(i, 1);
// await user.save()
// await target.save()
// return true
//
// }
// }
// return false
// }

0 comments on commit e10200e

Please sign in to comment.