[refactor] chat parser
This commit is contained in:
@@ -283,30 +283,11 @@ function wppParser(data:string){
|
||||
|
||||
|
||||
const rgx = /(?:{{|<)(.+?)(?:}}|>)/gm
|
||||
export function risuChatParser(da:string, arg:{
|
||||
chatID?:number
|
||||
db?:Database
|
||||
chara?:string|character|groupChat
|
||||
rmVar?:boolean
|
||||
} = {}):string{
|
||||
const chatID = arg.chatID ?? -1
|
||||
const db = arg.db ?? get(DataBase)
|
||||
const aChara = arg.chara
|
||||
let chara:character|string = null
|
||||
|
||||
if(aChara){
|
||||
if(typeof(aChara) !== 'string' && aChara.type === 'group'){
|
||||
const gc = findCharacterbyId(aChara.chats[aChara.chatPage].message.at(-1).saying ?? '')
|
||||
if(gc.name !== 'Unknown Character'){
|
||||
chara = gc
|
||||
}
|
||||
}
|
||||
else{
|
||||
chara = aChara
|
||||
}
|
||||
}
|
||||
const matcher = (p1:string) => {
|
||||
const matcher = (p1:string,matcherArg:{chatID:number,db:Database,chara:character|string,rmVar:boolean}) => {
|
||||
const lowerCased = p1.toLocaleLowerCase()
|
||||
const chatID = matcherArg.chatID
|
||||
const db = matcherArg.db
|
||||
const chara = matcherArg.chara
|
||||
switch(lowerCased){
|
||||
case 'previous_char_chat':{
|
||||
if(chatID !== -1){
|
||||
@@ -449,7 +430,7 @@ export function risuChatParser(da:string, arg:{
|
||||
}
|
||||
case 'addvar':
|
||||
case 'setvar':{
|
||||
if(arg.rmVar){
|
||||
if(matcherArg.rmVar){
|
||||
return ''
|
||||
}
|
||||
break
|
||||
@@ -476,10 +457,68 @@ export function risuChatParser(da:string, arg:{
|
||||
return null
|
||||
}
|
||||
|
||||
const smMatcher = (p1:string,matcherArg:{db:Database,chara:character|string,rmVar:boolean}) => {
|
||||
const lowerCased = p1.toLocaleLowerCase()
|
||||
const db = matcherArg.db
|
||||
const chara = matcherArg.chara
|
||||
switch(lowerCased){
|
||||
case 'char':
|
||||
case 'bot':{
|
||||
let selectedChar = get(selectedCharID)
|
||||
let currentChar = db.characters[selectedChar]
|
||||
if(currentChar.type !== 'group'){
|
||||
return currentChar.name
|
||||
}
|
||||
if(chara){
|
||||
if(typeof(chara) === 'string'){
|
||||
return chara
|
||||
}
|
||||
else{
|
||||
return chara.name
|
||||
}
|
||||
}
|
||||
return currentChar.name
|
||||
}
|
||||
case 'user':{
|
||||
return db.username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function risuChatParser(da:string, arg:{
|
||||
chatID?:number
|
||||
db?:Database
|
||||
chara?:string|character|groupChat
|
||||
rmVar?:boolean
|
||||
} = {}):string{
|
||||
const chatID = arg.chatID ?? -1
|
||||
const db = arg.db ?? get(DataBase)
|
||||
const aChara = arg.chara
|
||||
let chara:character|string = null
|
||||
|
||||
if(aChara){
|
||||
if(typeof(aChara) !== 'string' && aChara.type === 'group'){
|
||||
const gc = findCharacterbyId(aChara.chats[aChara.chatPage].message.at(-1).saying ?? '')
|
||||
if(gc.name !== 'Unknown Character'){
|
||||
chara = gc
|
||||
}
|
||||
}
|
||||
else{
|
||||
chara = aChara
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let pointer = 0;
|
||||
let nested:string[] = [""]
|
||||
let pf = performance.now()
|
||||
let v = new Uint8Array(255)
|
||||
const matcherObj = {
|
||||
chatID: chatID,
|
||||
chara: chara,
|
||||
rmVar: arg.rmVar ?? false,
|
||||
db: db
|
||||
}
|
||||
while(pointer < da.length){
|
||||
switch(da[pointer]){
|
||||
case '{':{
|
||||
@@ -504,7 +543,7 @@ export function risuChatParser(da:string, arg:{
|
||||
}
|
||||
pointer++
|
||||
const dat = nested.shift()
|
||||
const mc = matcher(dat)
|
||||
const mc = matcher(dat, matcherObj)
|
||||
nested[0] += mc ?? `{{${dat}}}`
|
||||
break
|
||||
}
|
||||
@@ -513,7 +552,7 @@ export function risuChatParser(da:string, arg:{
|
||||
break
|
||||
}
|
||||
const dat = nested.shift()
|
||||
const mc = matcher(dat)
|
||||
const mc = smMatcher(dat, matcherObj)
|
||||
nested[0] += mc ?? `<${dat}>`
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user