Refactor characterFormatUpdate function to use getCharacterByIndex and setCharacterByIndex

This commit is contained in:
kwaroran
2024-10-25 18:40:47 +09:00
parent be90308f23
commit 75ec62bcc7
2 changed files with 29 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
import { get, writable } from "svelte/store";
import { saveImage, setDatabase, type character, type Chat, defaultSdDataFunc, type loreBook, getDatabase } from "./storage/database.svelte";
import { saveImage, setDatabase, type character, type Chat, defaultSdDataFunc, type loreBook, getDatabase, getCharacterByIndex, setCharacterByIndex } from "./storage/database.svelte";
import { alertAddCharacter, alertConfirm, alertError, alertNormal, alertSelect, alertStore, alertWait } from "./alert";
import { language } from "../lang";
import { decode as decodeMsgpack } from "msgpackr";
@@ -417,11 +417,10 @@ function formatTavernChat(chat:string, charName:string){
return chat.replace(/<([Uu]ser)>|\{\{([Uu]ser)\}\}/g, getUserName()).replace(/((\{\{)|<)([Cc]har)(=.+)?((\}\})|>)/g, charName)
}
export function characterFormatUpdate(index:number|character, arg:{
export function characterFormatUpdate(indexOrCharacter:number|character, arg:{
updateInteraction?:boolean,
} = {}){
let db = getDatabase()
let cha = typeof(index) === 'number' ? db.characters[index] : index
let cha = typeof(indexOrCharacter) === 'number' ? getCharacterByIndex(indexOrCharacter) : indexOrCharacter
if(cha.chats.length === 0){
cha.chats = [{
message: [],
@@ -512,9 +511,8 @@ export function characterFormatUpdate(index:number|character, arg:{
cha.customscript = []
}
cha.lastInteraction = Date.now()
if(typeof(index) === 'number'){
db.characters[index] = cha
setDatabase(db)
if(typeof(indexOrCharacter) === 'number'){
setCharacterByIndex(indexOrCharacter, cha)
}
cha.chats = cha.chats.map((v) => {
v.fmIndex ??= cha.firstMsgIndex ?? -1

View File

@@ -453,6 +453,7 @@ export function setDatabase(data:Database){
}
export function setDatabaseLite(data:Database){
console.trace('setDatabaseLite executed')
DBState.db = data
}
@@ -469,16 +470,34 @@ export function getDatabase(options:getDatabaseOptions = {}):Database{
export function getCurrentCharacter(options:getDatabaseOptions = {}):character|groupChat{
const db = getDatabase(options)
db.characters ??= []
if(!db.characters){
db.characters = []
}
const char = db.characters?.[get(selectedCharID)]
return char
}
export function setCurrentCharacter(char:character|groupChat){
const db = getDatabase()
db.characters ??= []
db.characters[get(selectedCharID)] = char
setDatabaseLite(db)
if(!DBState.db.characters){
DBState.db.characters = []
}
DBState.db.characters[get(selectedCharID)] = char
}
export function getCharacterByIndex(index:number,options:getDatabaseOptions = {}):character|groupChat{
const db = getDatabase(options)
if(!db.characters){
db.characters = []
}
const char = db.characters?.[index]
return char
}
export function setCharacterByIndex(index:number,char:character|groupChat){
if(!DBState.db.characters){
DBState.db.characters = []
}
DBState.db.characters[index] = char
}
export function getCurrentChat(){