UNPKG

koishi-plugin-w-vpet

Version:

基于w-messagedb的电子宠物插件

361 lines (356 loc) 13.4 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Config: () => Config, VPetService: () => VPetService, apply: () => apply, inject: () => inject, name: () => name }); module.exports = __toCommonJS(src_exports); var import_koishi = require("koishi"); // src/pet-types.ts var landAnimals = [ { name: "小狗", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小猫", category: "animal_land" /* ANIMAL_LAND */ }, { name: "仓鼠", category: "animal_land" /* ANIMAL_LAND */ }, { name: "兔子", category: "animal_land" /* ANIMAL_LAND */ }, { name: "刺猬", category: "animal_land" /* ANIMAL_LAND */ }, { name: "松鼠", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小熊", category: "animal_land" /* ANIMAL_LAND */ }, { name: "浣熊", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小狐狸", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小鹿", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小猪", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小羊", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小马", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小象", category: "animal_land" /* ANIMAL_LAND */ }, { name: "小猴", category: "animal_land" /* ANIMAL_LAND */ } ]; var waterAnimals = [ { name: "金鱼", category: "animal_water" /* ANIMAL_WATER */ }, { name: "热带鱼", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小乌龟", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小章鱼", category: "animal_water" /* ANIMAL_WATER */ }, { name: "海豚", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小鲸鱼", category: "animal_water" /* ANIMAL_WATER */ }, { name: "水母", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小虾", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小蟹", category: "animal_water" /* ANIMAL_WATER */ }, { name: "海星", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小海马", category: "animal_water" /* ANIMAL_WATER */ }, { name: "小鳄鱼", category: "animal_water" /* ANIMAL_WATER */ }, { name: "河豚", category: "animal_water" /* ANIMAL_WATER */ } ]; var trees = [ { name: "小松树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小橡树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小樱花", category: "plant_tree" /* PLANT_TREE */ }, { name: "小苹果树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小柳树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小枫树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小桃树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小梨树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小杉树", category: "plant_tree" /* PLANT_TREE */ }, { name: "小银杏", category: "plant_tree" /* PLANT_TREE */ }, { name: "小枇杷树", category: "plant_tree" /* PLANT_TREE */ } ]; var shrubs = [ { name: "小玫瑰", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小茉莉", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小杜鹃", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小栀子花", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小月季", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小紫荆", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小丁香", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小茶花", category: "plant_shrub" /* PLANT_SHRUB */ }, { name: "小绣球", category: "plant_shrub" /* PLANT_SHRUB */ } ]; var mosses = [ { name: "小青苔", category: "plant_moss" /* PLANT_MOSS */ }, { name: "小地衣", category: "plant_moss" /* PLANT_MOSS */ }, { name: "小石松", category: "plant_moss" /* PLANT_MOSS */ }, { name: "小泥炭藓", category: "plant_moss" /* PLANT_MOSS */ }, { name: "小卷柏", category: "plant_moss" /* PLANT_MOSS */ }, { name: "小角苔", category: "plant_moss" /* PLANT_MOSS */ } ]; var fungi = [ { name: "小蘑菇", category: "fungus" /* FUNGUS */ }, { name: "小木耳", category: "fungus" /* FUNGUS */ }, { name: "小灵芝", category: "fungus" /* FUNGUS */ }, { name: "小松露", category: "fungus" /* FUNGUS */ }, { name: "小牛肝菌", category: "fungus" /* FUNGUS */ }, { name: "小羊肚菌", category: "fungus" /* FUNGUS */ }, { name: "小银耳", category: "fungus" /* FUNGUS */ }, { name: "小猴头菇", category: "fungus" /* FUNGUS */ } ]; var petTypes = [ ...landAnimals, ...waterAnimals, ...trees, ...shrubs, ...mosses, ...fungi ]; // src/index.ts var name = "w-vpet"; var inject = ["messageDb", "database"]; var Config = import_koishi.z.object({ healthDecreasePerDay: import_koishi.z.number().default(50).description("宠物每天健康值下降量"), healthIncreasePerMessage: import_koishi.z.number().default(10).description("每条消息增加的健康值"), maxHealth: import_koishi.z.number().default(100).description("宠物最大健康值") }); var VPetService = class extends import_koishi.Service { constructor(ctx, config) { super(ctx, "vpet"); this.config = config; ctx.model.extend("w-vpet", { userId: "string", platform: "string", guildId: "string", name: "string", type: "string", category: "string", health: "integer", growth: "integer", adoptTime: "integer", lastInteractTime: "integer" }, { primary: ["userId", "platform", "guildId"] }); ctx.command("vpet").option("name", "-n <name:string>").action(async ({ session, options }) => { if (!session.guildId) { return "请在群聊中使用此命令"; } const { userId, platform, guildId } = session; const pet = await this.getPet(userId, platform, guildId); if (!pet) { const newPet = await this.adoptPet(userId, platform, guildId, options.name); return this.renderPetInfo(newPet, "恭喜你领养了一只新宠物!"); } if (pet.health <= 0) { await this.ctx.database.remove("w-vpet", { userId, platform, guildId }); const newPet = await this.adoptPet(userId, platform, guildId, options.name); return this.renderPetInfo(newPet, `你的宠物 ${pet.name} 已经死亡,已为你领养一只新宠物!`); } if (options.name) { const renamedPet = await this.renamePet(userId, platform, guildId, options.name); return this.renderPetInfo(renamedPet, `你的宠物已重命名为 ${options.name}!`); } const updatedPet = await this.interactWithPet(userId, platform, guildId); return this.renderPetInfo(updatedPet, "你的宠物很高兴见到你!"); }); } static { __name(this, "VPetService"); } static inject = ["database", "messageDb"]; logger = this.ctx.logger("w-vpet"); // 获取用户宠物 async getPet(userId, platform, guildId) { const pets = await this.ctx.database.get("w-vpet", { userId, platform, guildId }); if (pets.length === 0) { return null; } const pet = pets[0]; return this.updatePetStatus(pet); } // 领养新宠物 async adoptPet(userId, platform, guildId, customName) { const petType = this.getRandomPetType(); const now = Date.now(); const pet = { userId, platform, guildId, name: customName || petType.name, type: petType.name, category: petType.category, health: this.config.maxHealth, growth: 0, adoptTime: now, lastInteractTime: now }; await this.ctx.database.create("w-vpet", pet); return pet; } // 与宠物互动(更新健康值) async interactWithPet(userId, platform, guildId) { const pet = await this.getPet(userId, platform, guildId); if (!pet) { throw new Error("宠物不存在"); } if (pet.health <= 0) { return pet; } const messageCount = await this.getMessageCountSinceLastInteraction(userId, platform, guildId, pet.lastInteractTime); let newHealth = pet.health + messageCount * this.config.healthIncreasePerMessage; if (newHealth > this.config.maxHealth) { newHealth = this.config.maxHealth; } const extraGrowth = Math.floor(messageCount / 10); const newGrowth = pet.growth + extraGrowth; const now = Date.now(); const updatedPet = { ...pet, health: newHealth, growth: newGrowth, lastInteractTime: now }; await this.ctx.database.set("w-vpet", { userId, platform, guildId }, { health: updatedPet.health, growth: updatedPet.growth, lastInteractTime: updatedPet.lastInteractTime }); return updatedPet; } // 更新宠物状态(健康值和成长值) async updatePetStatus(pet) { const now = Date.now(); const daysSinceLastInteraction = Math.floor((now - pet.lastInteractTime) / (24 * 60 * 60 * 1e3)); if (daysSinceLastInteraction <= 0) { return pet; } const messageCount = await this.getMessageCountSinceLastInteraction(pet.userId, pet.platform, pet.guildId, pet.lastInteractTime); const healthDecrease = daysSinceLastInteraction * this.config.healthDecreasePerDay; const healthIncrease = messageCount * this.config.healthIncreasePerMessage; let newHealth = pet.health - healthDecrease + healthIncrease; newHealth = Math.max(0, Math.min(newHealth, this.config.maxHealth)); const extraGrowth = Math.floor(messageCount / 10); const newGrowth = pet.growth + daysSinceLastInteraction + extraGrowth; const updatedPet = { ...pet, health: newHealth, growth: newGrowth }; await this.ctx.database.set("w-vpet", { userId: pet.userId, platform: pet.platform, guildId: pet.guildId }, { health: updatedPet.health, growth: updatedPet.growth }); return updatedPet; } // 重命名宠物 async renamePet(userId, platform, guildId, newName) { const pet = await this.getPet(userId, platform, guildId); if (!pet) { return null; } if (pet.health <= 0) { return pet; } await this.ctx.database.set("w-vpet", { userId, platform, guildId }, { name: newName }); return { ...pet, name: newName }; } // 获取自上次互动以来的消息数量 async getMessageCountSinceLastInteraction(userId, platform, guildId, lastInteractTime) { const messages = await this.ctx.database.select("w-message").where({ platform, guildId, userId, timestamp: { $gt: lastInteractTime } }).execute(); return messages.length; } // 随机选择宠物类型 getRandomPetType() { const index = Math.floor(Math.random() * petTypes.length); return petTypes[index]; } // 渲染宠物信息 renderPetInfo(pet, message) { const healthStatus = this.getHealthStatus(pet.health); const growthStage = this.getGrowthStage(pet.growth); return `${message} 【宠物信息】 名称:${pet.name} 类型:${pet.type} 健康值:${pet.health}/100 (${healthStatus}) 成长值:${pet.growth} (${growthStage}) 领养时间:${new Date(pet.adoptTime).toLocaleString()} `; } // 获取健康状态描述 getHealthStatus(health) { if (health <= 0) return "死亡"; if (health < 20) return "奄奄一息"; if (health < 40) return "状态不佳"; if (health < 60) return "一般"; if (health < 80) return "健康"; return "非常健康"; } // 获取成长阶段描述 getGrowthStage(growth) { if (growth < 7) return "幼年期"; if (growth < 30) return "成长期"; if (growth < 90) return "成熟期"; if (growth < 365) return "壮年期"; if (growth < 1e3) return "蜕变期"; if (growth < 5e3) return "练气期"; if (growth < 15e3) return "筑基期"; if (growth < 5e4) return "金丹期"; if (growth < 1e5) return "元婴期"; if (growth < 2e5) return "化神期"; if (growth < 5e5) return "大乘期"; if (growth < 1e6) return "渡劫期"; if (growth < 2e6) return "飞升期"; if (growth < 5e6) return "仙兽"; return "永恒"; } }; function apply(ctx, config) { ctx.plugin(VPetService, config); } __name(apply, "apply"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, VPetService, apply, inject, name });