[feat] added calc
This commit is contained in:
@@ -36,7 +36,7 @@ function toRPN(expression:string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function calculateRPN(expression:string) {
|
function calculateRPN(expression:string) {
|
||||||
let stack = [];
|
let stack:number[] = [];
|
||||||
|
|
||||||
expression.split(' ').forEach(token => {
|
expression.split(' ').forEach(token => {
|
||||||
if (parseFloat(token) || token === '0') {
|
if (parseFloat(token) || token === '0') {
|
||||||
@@ -54,30 +54,8 @@ function calculateRPN(expression:string) {
|
|||||||
|
|
||||||
return stack.pop();
|
return stack.pop();
|
||||||
}
|
}
|
||||||
function parseAndExecuteFunctions(s: string): string {
|
|
||||||
const functionRegex = /\$(\w+)\((.*?)\)/g;
|
|
||||||
|
|
||||||
let match;
|
export function calcString(args:string) {
|
||||||
while ((match = functionRegex.exec(s)) !== null) {
|
|
||||||
let [fullMatch, funcName, args] = match;
|
|
||||||
|
|
||||||
let result = '';
|
|
||||||
switch (funcName) {
|
|
||||||
case 'calc':
|
|
||||||
result = calc(parseAndExecuteFunctions(args));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
s = s.replace(fullMatch, result.toString());
|
|
||||||
functionRegex.lastIndex = 0; // Reset the regex
|
|
||||||
}
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
function calc(args:string) {
|
|
||||||
const expression = toRPN(args);
|
const expression = toRPN(args);
|
||||||
const evaluated = calculateRPN(expression);
|
const evaluated = calculateRPN(expression);
|
||||||
return evaluated
|
return evaluated
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { downloadFile } from "../storage/globalApi";
|
|||||||
import { alertError, alertNormal } from "../alert";
|
import { alertError, alertNormal } from "../alert";
|
||||||
import { language } from "src/lang";
|
import { language } from "src/lang";
|
||||||
import { findCharacterbyId, selectSingleFile } from "../util";
|
import { findCharacterbyId, selectSingleFile } from "../util";
|
||||||
|
import { calcString } from "./infunctions";
|
||||||
|
|
||||||
const dreg = /{{data}}/g
|
const dreg = /{{data}}/g
|
||||||
const randomness = /\|\|\|/g
|
const randomness = /\|\|\|/g
|
||||||
@@ -110,6 +111,7 @@ export function processScriptFull(char:character|groupChat, data:string, mode:Sc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
let mOut = outScript.replace(dreg, "$&")
|
||||||
if(chatID !== -1){
|
if(chatID !== -1){
|
||||||
const selchar = db.characters[get(selectedCharID)]
|
const selchar = db.characters[get(selectedCharID)]
|
||||||
const chat = selchar.chats[selchar.chatPage]
|
const chat = selchar.chats[selchar.chatPage]
|
||||||
@@ -134,6 +136,10 @@ export function processScriptFull(char:character|groupChat, data:string, mode:Sc
|
|||||||
}
|
}
|
||||||
return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex]
|
return selchar.firstMsgIndex === -1 ? selchar.firstMessage : selchar.alternateGreetings[selchar.firstMsgIndex]
|
||||||
}
|
}
|
||||||
|
if(p1.startsWith('calc')){
|
||||||
|
const v = p1.split("::")[1]
|
||||||
|
return calcString(v).toString()
|
||||||
|
}
|
||||||
return v
|
return v
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -141,7 +147,7 @@ export function processScriptFull(char:character|groupChat, data:string, mode:Sc
|
|||||||
const list = data.split('|||')
|
const list = data.split('|||')
|
||||||
data = list[Math.floor(Math.random()*list.length)];
|
data = list[Math.floor(Math.random()*list.length)];
|
||||||
}
|
}
|
||||||
data = data.replace(reg, outScript.replace(dreg, "$&"))
|
data = data.replace(reg, mOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user