From 956435ba40f93a88a0f1f55405cf46abd8770b4a Mon Sep 17 00:00:00 2001 From: fullgream Date: Sun, 17 Aug 2025 18:50:21 +0300 Subject: [PATCH] add new tables --- server/database/dbModel.js | 114 ++++++++++++++++++++++++++++++++++++- server/database/index.js | 56 ++++++++++++++++++ 2 files changed, 168 insertions(+), 2 deletions(-) diff --git a/server/database/dbModel.js b/server/database/dbModel.js index 0875f61..5482d5b 100644 --- a/server/database/dbModel.js +++ b/server/database/dbModel.js @@ -54,11 +54,13 @@ module.exports = async function (sequelize, dbObj) { unique: false, comment: "Hash code of password", set: function (value) { + if (value === "") return this.setDataValue('password', ""); const hash = bcrypt.hashSync(value, getSalt()); this.setDataValue('password', hash); }, get: function () { const checkHash = this.getDataValue("password"); + if (checkHash === "") return Promise.resolve(false); return async function (password) { return await new Promise((rs, rj) => { bcrypt.compare(password, checkHash, (err, res) => { @@ -87,16 +89,124 @@ module.exports = async function (sequelize, dbObj) { primaryKey: true, comment: "ID of character", }, + ownerId: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "ID of character's owner", + }, name: { type: DataTypes.STRING, allowNull: false, unique: true, comment: "Name of character", }, - ownerId: { + nameMasked: { + type: DataTypes.STRING, + allowNull: false, + unique: false, + comment: "Masked name of character", + }, + avatar: { + type: DataTypes.BLOB, + allowNull: true, + comment: "Avatar of character", + }, + avatarMime: { + type: DataTypes.STRING, + allowNull: true, + comment: "Mimetype of character's avatar", + }, + descriptionPrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Description of character", + }, + appearancePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Appearance of character", + }, + naturePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Nature of character", + }, + prehistoryPrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Pre-history of character", + }, + communicationStylePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Communication style of character", + }, + }), + + npcCharacters: sequelize.define("npcCharacters", { + id: { type: DataTypes.INTEGER, allowNull: false, - comment: "ID of character's owner", + autoIncrement: true, + primaryKey: true, + comment: "ID of character", + }, + name: { + type: DataTypes.STRING, + allowNull: false, + unique: true, + comment: "Name of character", + }, + nameMasked: { + type: DataTypes.STRING, + allowNull: false, + unique: false, + comment: "Masked name of character", + }, + avatar: { + type: DataTypes.BLOB, + allowNull: true, + comment: "Avatar of character", + }, + avatarMime: { + type: DataTypes.STRING, + allowNull: true, + comment: "Mimetype of character's avatar", + }, + descriptionPrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Description of character", + }, + appearancePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Appearance of character", + }, + naturePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Nature of character", + }, + prehistoryPrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Pre-history of character", + }, + communicationStylePrompt: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + comment: "Communication style of character", }, }), }; diff --git a/server/database/index.js b/server/database/index.js index acf7424..867e836 100644 --- a/server/database/index.js +++ b/server/database/index.js @@ -59,6 +59,62 @@ class DatabaseCache { this.users.byId[addedData.id] = addedData; } } + + async userCharacters (rows, table) { + this.userCharacters.rows = rows; + + // Add new character at cache + this.userCharacters.add = async (row, isCreate = false) => { + if (isCreate) + await table.create(row); + + let addedData = null; + if (row.name) + addedData = await table.findAll({ + where: { + name: row.name + } + })[0]; + else if (row.id) + addedData = await table.findAll({ + where: { + id: row.id + } + })[0]; + + if (!addedData) throw new Error("Invalid row information"); + this.userCharacters.rows.push(addedData); + // By keys + } + } + + async npcCharacters (rows, table) { + this.npcCharacters.rows = rows; + + // Add new character at cache + this.npcCharacters.add = async (row, isCreate = false) => { + if (isCreate) + await table.create(row); + + let addedData = null; + if (row.name) + addedData = await table.findAll({ + where: { + name: row.name + } + })[0]; + else if (row.id) + addedData = await table.findAll({ + where: { + id: row.id + } + })[0]; + + if (!addedData) throw new Error("Invalid row information"); + this.npcCharacters.rows.push(addedData); + // By keys + } + } } class Database {