Refactor characterFormatUpdate function to use getCharacterByIndex and setCharacterByIndex
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { get, writable } from "svelte/store";
|
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 { alertAddCharacter, alertConfirm, alertError, alertNormal, alertSelect, alertStore, alertWait } from "./alert";
|
||||||
import { language } from "../lang";
|
import { language } from "../lang";
|
||||||
import { decode as decodeMsgpack } from "msgpackr";
|
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)
|
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,
|
updateInteraction?:boolean,
|
||||||
} = {}){
|
} = {}){
|
||||||
let db = getDatabase()
|
let cha = typeof(indexOrCharacter) === 'number' ? getCharacterByIndex(indexOrCharacter) : indexOrCharacter
|
||||||
let cha = typeof(index) === 'number' ? db.characters[index] : index
|
|
||||||
if(cha.chats.length === 0){
|
if(cha.chats.length === 0){
|
||||||
cha.chats = [{
|
cha.chats = [{
|
||||||
message: [],
|
message: [],
|
||||||
@@ -512,9 +511,8 @@ export function characterFormatUpdate(index:number|character, arg:{
|
|||||||
cha.customscript = []
|
cha.customscript = []
|
||||||
}
|
}
|
||||||
cha.lastInteraction = Date.now()
|
cha.lastInteraction = Date.now()
|
||||||
if(typeof(index) === 'number'){
|
if(typeof(indexOrCharacter) === 'number'){
|
||||||
db.characters[index] = cha
|
setCharacterByIndex(indexOrCharacter, cha)
|
||||||
setDatabase(db)
|
|
||||||
}
|
}
|
||||||
cha.chats = cha.chats.map((v) => {
|
cha.chats = cha.chats.map((v) => {
|
||||||
v.fmIndex ??= cha.firstMsgIndex ?? -1
|
v.fmIndex ??= cha.firstMsgIndex ?? -1
|
||||||
|
|||||||
@@ -453,6 +453,7 @@ export function setDatabase(data:Database){
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function setDatabaseLite(data:Database){
|
export function setDatabaseLite(data:Database){
|
||||||
|
console.trace('setDatabaseLite executed')
|
||||||
DBState.db = data
|
DBState.db = data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,16 +470,34 @@ export function getDatabase(options:getDatabaseOptions = {}):Database{
|
|||||||
|
|
||||||
export function getCurrentCharacter(options:getDatabaseOptions = {}):character|groupChat{
|
export function getCurrentCharacter(options:getDatabaseOptions = {}):character|groupChat{
|
||||||
const db = getDatabase(options)
|
const db = getDatabase(options)
|
||||||
db.characters ??= []
|
if(!db.characters){
|
||||||
|
db.characters = []
|
||||||
|
}
|
||||||
const char = db.characters?.[get(selectedCharID)]
|
const char = db.characters?.[get(selectedCharID)]
|
||||||
return char
|
return char
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setCurrentCharacter(char:character|groupChat){
|
export function setCurrentCharacter(char:character|groupChat){
|
||||||
const db = getDatabase()
|
if(!DBState.db.characters){
|
||||||
db.characters ??= []
|
DBState.db.characters = []
|
||||||
db.characters[get(selectedCharID)] = char
|
}
|
||||||
setDatabaseLite(db)
|
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(){
|
export function getCurrentChat(){
|
||||||
|
|||||||
Reference in New Issue
Block a user