[feat] better group chat

This commit is contained in:
kwaroran
2023-05-31 05:08:46 +09:00
parent 7805092992
commit 7ea768cb5b
9 changed files with 208 additions and 57 deletions

103
src/ts/process/group.ts Normal file
View File

@@ -0,0 +1,103 @@
import { shuffle } from "lodash";
import { findCharacterbyId } from "../util";
import { alertConfirm, alertError, alertSelectChar } from "../alert";
import { language } from "src/lang";
import { get } from "svelte/store";
import { DataBase, setDatabase } from "../storage/database";
import { selectedCharID } from "../stores";
export async function addGroupChar(){
let db = get(DataBase)
let selectedId = get(selectedCharID)
let group = db.characters[selectedId]
if(group.type === 'group'){
const res = await alertSelectChar()
if(res){
if(group.characters.includes(res)){
alertError(language.errors.alreadyCharInGroup)
}
else{
if(await alertConfirm(language.askLoadFirstMsg)){
group.chats[group.chatPage].message.push({
role:'char',
data: findCharacterbyId(res).firstMessage,
saying: res,
})
}
group.characters.push(res)
group.characterTalks.push(1 / 6 * 4)
group.characterActive.push(true)
}
}
setDatabase(db)
}
}
export function rmCharFromGroup(index:number){
let db = get(DataBase)
let selectedId = get(selectedCharID)
let group = db.characters[selectedId]
if(group.type === 'group'){
group.characters.splice(index, 1)
group.characterTalks.splice(index, 1)
group.characterActive.splice(index, 1)
setDatabase(db)
}
}
export type GroupOrder = {
id: string,
talkness: number,
index: number
}
export function groupOrder(chars:GroupOrder[], input:string):GroupOrder[] {
let order:GroupOrder[] = [];
if (input) {
const words = getWords(input)
for (const word of words) {
for (let char of chars) {
const charNameChunks = getWords(findCharacterbyId(char.id).name)
console.log(charNameChunks)
if (charNameChunks.includes(word)) {
order.push(char);
break;
}
}
}
}
const shuffled = shuffle(chars)
for (const char of shuffled) {
if(order.includes(char)){
continue
}
//TODO
const chance = 0.5
if (chance >= Math.random()) {
order.push(char);
}
}
while (order.length === 0) {
order.push(chars[Math.floor(Math.random() * chars.length)]);
}
return order;
}
function getWords(data:string){
const matches = data.match(/\b\w+\b/gmi)
let words:string[] = []
for(const match of matches){
words.push(match.toLocaleLowerCase())
}
return words
}