From 2dd359a34441f40b2dff98517e48f918c64f844b Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (pragmatismo.io)" Date: Sun, 2 Dec 2018 19:59:27 -0200 Subject: [PATCH] fix(gbdialog): VBA is running. --- .prettierrc | 1 - packages/core.gbapp/services/GBVMService.ts | 72 +++++----- packages/core.gbapp/services/TSCompiler.ts | 3 +- packages/default.gbdialog/bot.vbs | 139 ++++++++++++++++---- src/app.ts | 16 ++- 5 files changed, 158 insertions(+), 73 deletions(-) diff --git a/.prettierrc b/.prettierrc index af8db0fa..c1e1114c 100644 --- a/.prettierrc +++ b/.prettierrc @@ -5,5 +5,4 @@ "arrowParens": "avoid", "semi": true, "singleQuote": true - } \ No newline at end of file diff --git a/packages/core.gbapp/services/GBVMService.ts b/packages/core.gbapp/services/GBVMService.ts index a281aeb0..42c9292e 100644 --- a/packages/core.gbapp/services/GBVMService.ts +++ b/packages/core.gbapp/services/GBVMService.ts @@ -73,12 +73,13 @@ export class GBVMService implements IGBCoreService { public async run(source: any, path: string, min: any, deployer: GBDeployer, filename: string) { // Converts VBS into TS. - //vb2ts.convertFile(source); + vb2ts.convertFile(source); // Convert TS into JS. const tsfile = `bot.ts`; const tsc = new TSCompiler(); - //tsc.compile([UrlJoin(path, tsfile)]); + tsc.compile([UrlJoin(path, tsfile)]); + // Run JS into the GB context. const jsfile = `bot.js`; let localPath = UrlJoin(path, jsfile); @@ -86,34 +87,38 @@ export class GBVMService implements IGBCoreService { if (fs.existsSync(localPath)) { let code: string = fs.readFileSync(localPath, 'utf8'); code = code.replace(/^.*exports.*$/gm, ''); - let match1; - let match2; - let finalCode = ''; - let pos = 0; - let nextCode = code; + + // Finds all hear calls. + + let parsedCode = code; let hearExp = /(\w+).*hear.*\(\)/; - while (match1 = hearExp.exec(nextCode)) { + let match1; - // Write async body. + while ((match1 = hearExp.exec(code))) { + + let pos = 0; + + // Writes async body. const variable = match1[1]; // variable = hear(); - finalCode += code.substring(pos, pos + match1.index); - finalCode += `hear (async (${variable}) => {\n`; + parsedCode = code.substring(pos, pos + match1.index); + parsedCode += `hear (async (${variable}) => {\n`; - // Skip old construction and point to the async block. + // Skips old construction and point to the async block. pos = pos + match1.index; - nextCode = code.substring(pos + match1[0].length + 1); + let tempCode = code.substring(pos + match1[0].length + 1); let start = pos; - // Find last } + // Balances code blocks and checks for exits. let right = 0; let left = 1; - while ((match2 = /\{|\}/.exec(nextCode))) { - const c = nextCode.substring(match2.index, match2.index + 1); + let match2; + while ((match2 = /\{|\}/.exec(tempCode))) { + const c = tempCode.substring(match2.index, match2.index + 1); if (c === '}') { right++; @@ -121,48 +126,43 @@ export class GBVMService implements IGBCoreService { left++; } - let match3 - if (match3 = hearExp.exec(nextCode)) - { - nextCode = nextCode.substring(match3.index + 1); - pos += match3.index; - break; - } - - nextCode = nextCode.substring(match2.index + 1); + tempCode = tempCode.substring(match2.index + 1); pos += match2.index + 1; if (left === right) { break; } - } - finalCode += code.substring(start + match1[0].length + 1, pos + match1[0].length); - finalCode += '});\n'; + parsedCode += code.substring(start + match1[0].length + 1, pos + match1[0].length); + parsedCode += '});\n'; + parsedCode += code.substring(pos + match1[0].length); - nextCode = code.substring(pos + match1[0].length); + // A interaction will be made for each hear. + + code = parsedCode; } - finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) { + parsedCode = parsedCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) { return $1 == undefined ? 'this.talk' : $1; }); - finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) { + parsedCode = parsedCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) { return $1 == undefined ? 'this.hear' : $1; }); - finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, function($0, $1) { + parsedCode = parsedCode.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, function($0, $1) { return $1 == undefined ? 'this.sendEmail' : $1; }); - finalCode = finalCode.replace(/this\./gm, 'await this.'); - finalCode = finalCode.replace(/function/gm, 'async function'); - console.log(finalCode); + parsedCode = parsedCode.replace(/this\./gm, 'await this.'); + parsedCode = parsedCode.replace(/function/gm, 'async function'); + + fs.writeFileSync(localPath, parsedCode); const sandbox: DialogClass = new DialogClass(min); const context = vm.createContext(sandbox); - vm.runInContext(finalCode, context); + vm.runInContext(parsedCode, context); min.sandbox = sandbox; await deployer.deployScriptToStorage(min.instanceId, filename); logger.info(`[GBVMService] Finished loading of ${filename}`); diff --git a/packages/core.gbapp/services/TSCompiler.ts b/packages/core.gbapp/services/TSCompiler.ts index 60bbe85b..4cb7e63a 100644 --- a/packages/core.gbapp/services/TSCompiler.ts +++ b/packages/core.gbapp/services/TSCompiler.ts @@ -43,6 +43,8 @@ export class TSCompiler { public compile( fileNames: string[], options: ts.CompilerOptions = { + noStrictGenericChecks: true, + noImplicitUseStrict: true, noEmitOnError: false, noImplicitAny: true, target: ts.ScriptTarget.ES5, @@ -51,7 +53,6 @@ export class TSCompiler { noEmitHelpers: true, maxNodeModuleJsDepth: 0, esModuleInterop: false - } ) { const program = ts.createProgram(fileNames, options); diff --git a/packages/default.gbdialog/bot.vbs b/packages/default.gbdialog/bot.vbs index e065b770..e6fe6d00 100644 --- a/packages/default.gbdialog/bot.vbs +++ b/packages/default.gbdialog/bot.vbs @@ -31,34 +31,117 @@ ' '**************************************************************************** - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - talk ("Please, what's your e-mail address?") - email = hear () - talk("Thanks, sending e-mail to: " + email ) - sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.") +talk ("How many installments do you want to pay your Credit?") +installments = hear () - name = hear () - talk("Hey " + name + "!") - +talk ("What is the amount requested?") +ammount = hear () + +talk ("What is the initial payment value?") +valorEntrada = hear () + + +talk ("What is the best due date?") +dueDate = hear () + +juros =0 +coeficiente1=0 + +if installments <12 then + juros = 1.60 + coeficiente = 0.09748 +end if + +if installments > 12 and installments< 18 then + juros = 1.66 + coeficiente = 0.06869 +end if + +if installments > 18 and installments< 36 then + juros = 1.64 + coeficiente = 0.05397 +end if + +if installments > 36 and installments< 48 then + juros = 1.62 + coeficiente = 0.03931 +end if + +if installments > 48 and installments< 60 then + juros = 1.70 + coeficiente = 0.03270 +end if + +if installments =60 then + juros = 1.79 + coeficiente = 0.02916 +end if + +if installments > 60 then + talk ("The maximum number of payments is 60") +end if + + +nInstallments = parseInt(installments) +vAmmount = parseFloat(ammount) +first = parseFloat(vAmmount) * 0.3 ' 30% of the value +tac = 800 +coeficiente = 1.3 + +taxaJuros = parseFloat(juros) +valorFinanciado = ammount - valorEntrada + tac +valorParcela = valorFinanciado * coeficiente +valorTotalDoBem = valorParcela * nInstallments + valorEntrada + +talk("Your credit analysis is done.") + +talk("First payment" + valorEntrada) +talk("valorParcela" + valor) +talk("taxaJuros" + taxaJuros) +talk("valorFinanciado" + valorFinanciado) +talk("valorTotalDoBem" + valorTotalDoBem) + + +text = hear() + +if email = "" then + + text = hear() + + + if email = "" then + talk () + text = hear() + + i1 = 10 + end if +else + text = hear() + if email = "" then + talk () + text = hear() + + i2 = 10 + + talk () + text = hear() + + talk () + text = hear() + end if +end if + +talk () +text = hear() + +i = 10 + +if i > 10 then + text = hear() + text = hear() +else + text = hear() + text = hear() +end if %> \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 86da8a7f..6a60a044 100644 --- a/src/app.ts +++ b/src/app.ts @@ -150,10 +150,12 @@ export class GBServer { } } -// // First line to run. - const path = 'packages/default.gbdialog'; - const file = 'bot.vbs'; - const source =(path + '/' + file); - let s = new GBVMService(); - s.run(source, path, null, null, null) -//GBServer.run(); +// First line to run. + +// const path = 'packages/default.gbdialog'; +// const file = 'bot.vbs'; +// const source =(path + '/' + file); +// let s = new GBVMService(); +// s.run(source, path, null, null, null) + +GBServer.run();