diff --git a/AABrowserAPI/API.js b/AABrowserAPI/API.js index 67a3b3660..9bead5e6c 100644 --- a/AABrowserAPI/API.js +++ b/AABrowserAPI/API.js @@ -15,9 +15,9 @@ } - function saveBotCode(pSourceCode, callBackFunction) { + function saveBotCode(pDevTeam, pSource, pRepo, pPath, pSourceCode, callBackFunction) { - writeData("AAMasters", "AAMariam-Trading-Bot", "Trading-Process/User.Bot.js", pSourceCode, onDataWritten); + writeData(pDevTeam + "/" + pSource, pRepo, pPath, pSourceCode, onDataWritten); function onDataWritten(err) { diff --git a/BreakpointsBar.js b/BreakpointsBar.js index 7107b24d1..749b9d7b0 100644 --- a/BreakpointsBar.js +++ b/BreakpointsBar.js @@ -148,7 +148,7 @@ function newBreakpointsBar() { beginMiliseconds = normalizeX(beginMiliseconds); endMiliseconds = normalizeX(endMiliseconds); - AT_BREAKPOINT = false; + window.AT_BREAKPOINT = false; for (let miliSeconds = beginMiliseconds; miliSeconds < endMiliseconds; miliSeconds = miliSeconds + timePeriod) { @@ -202,7 +202,7 @@ function newBreakpointsBar() { } else { browserCanvasContext.fillStyle = 'rgba(255, 233, 31, ' + opacity + ')'; /* highlight the current record */ - AT_BREAKPOINT = true; // We are hitting a breakpoint! + window.AT_BREAKPOINT = true; // We are hitting a breakpoint! } browserCanvasContext.arc(point.x, point.y, radius, 0, Math.PI * 2, true); diff --git a/CloudVM/CloudRequire.js b/CloudVM/CloudRequire.js index 566965ff7..28df7ad61 100644 --- a/CloudVM/CloudRequire.js +++ b/CloudVM/CloudRequire.js @@ -19,13 +19,13 @@ return thisObject; - function downloadBot(pCloudStorage, pProcessConfig, callBackFunction) { + function downloadBot(pCloudStorage, pFilePath, callBackFunction) { try { if (INFO_LOG === true) { logger.write("[INFO] downloadBot -> Entering function."); } - let filePath = "Bots" + "/" + bot.devTeam + "/" + bot.repo + "/" + pProcessConfig.name + "/" + "User.Bot.js"; + let filePath = "Bots" + "/" + pFilePath + "/" + "User.Bot.js"; downloadModule(filePath, onDownloaded) @@ -42,13 +42,13 @@ } } - function downloadCommons(pCloudStorage, callBackFunction) { + function downloadCommons(pCloudStorage, pFilePath, callBackFunction) { try { if (INFO_LOG === true) { logger.write("[INFO] downloadCommons -> Entering function."); } - let filePath = "Bots" + "/" + bot.devTeam + "/" + bot.repo + "/" + "Commons.js"; + let filePath = "Bots" + "/" + pFilePath + "/" + "Commons.js"; downloadModule(filePath, onDownloaded) diff --git a/CloudVM/CloudSupport.js b/CloudVM/CloudSupport.js index 691074e3b..e28f6563a 100644 --- a/CloudVM/CloudSupport.js +++ b/CloudVM/CloudSupport.js @@ -7,10 +7,6 @@ When running at the clode on NodeJS some things work diffently that at the brows */ - - - - function webRequire(pModulePath) { switch (pModulePath) { @@ -166,6 +162,3 @@ function callServer(pContentToSend, pPath, callBackFunction) { } } - - - diff --git a/CloudVM/CloudVM.js b/CloudVM/CloudVM.js index cd6f0d07a..8cb9fd3bc 100644 --- a/CloudVM/CloudVM.js +++ b/CloudVM/CloudVM.js @@ -82,7 +82,13 @@ function newCloudVM() { console.log("Bot code changes detected."); let path = "AABrowserAPI" + "/" - + "saveBotCode" + + "saveBotCode" + "/" + + window.DEV_TEAM + "/" + + "members" + "/" + + window.USER_LOGGED_IN + "/" + + window.CURRENT_BOT + "/" + + window.CURRENT_PROCESS + "/" + + "User.Bot.js" ; callServer(currentBotCode, path, onServerResponse); diff --git a/Dashboard.js b/Dashboard.js index 68692edc3..e637ab66e 100644 --- a/Dashboard.js +++ b/Dashboard.js @@ -123,7 +123,7 @@ function convertTimePeriodToName(pTimePeriod) { } let cloudVM = newCloudVM(); -let AT_BREAKPOINT = false; +window.AT_BREAKPOINT = false; const DEBUG_START_UP_DELAY = 0 //3000; // This is a waiting time in case there is a need to debug the very first steps of initialization, to be able to hit F12 on time. const INITIAL_DEFAULT_MARKET = 2; // This is the market that will be shown when loading the site for the first time. diff --git a/Server/BotsScripts.js b/Server/BotsScripts.js index 19d1225ce..517024d1d 100644 --- a/Server/BotsScripts.js +++ b/Server/BotsScripts.js @@ -1,168 +1,68 @@ exports.newBotScripts = function newBotScripts() { let thisObject = { - loadBotScripts: loadBotScripts, + getScript: getScript, initialize: initialize } - const GITHUB = require('../Server/Github'); - let github = GITHUB.newGithub(); - const STORAGE = require('../Server/Storage'); let storage = STORAGE.newStorage(); let serverConfig; let storageData; - let ecosystem; - let ecosystemObject; return thisObject; - function initialize(pEcosystem, pEcosystemObject, pServerConfig, pStorageData, callBackFunction) { + function initialize(pServerConfig, callBackFunction) { - serverConfig = pServerConfig; - storageData = pStorageData; - ecosystem = pEcosystem; - ecosystemObject = pEcosystemObject; + let storageData = new Map(); // For backwards compatibility only. Not used. - storage.initialize(storageData, serverConfig); + storage.initialize(storageData, pServerConfig); callBackFunction(); } - function loadBotScripts(callBackfunction) { + function getScript(pDevTeam, pSource, pRepo, pPath, callBackFunction) { - try { + storage.getStorageData(pDevTeam + "/" + pSource, pRepo, pPath, onDataArrived); - if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Entering function."); } + function onDataArrived(pData) { - /* - - Here we go through the webModules Array and bring them all. - - */ + try { - let modulesToBeRetrieved = 0; - let modulesRetrieved = 0; + if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); } - for (let i = 0; i < ecosystemObject.devTeams.length; i++) { + let data; - let devTeam = ecosystemObject.devTeams[i]; + if (pData !== undefined) { - for (let j = 0; j < devTeam.bots.length; j++) { + data = pData.toString(); + data = data.trim(); // remove first byte with some encoding. - let bot = devTeam.bots[j]; + data = browserify(data); - getCommons(devTeam.codeName, bot.repo, onCommons); - - function onCommons() { - - for (let k = 0; k < bot.processes.length; k++) { - - let process = bot.processes[k]; - - modulesToBeRetrieved++; - getUserBot(devTeam.codeName, bot.repo, process.name, onUserBot); - - function onUserBot() { - - modulesRetrieved++; - - if (modulesRetrieved === modulesToBeRetrieved) { - - callBackfunction(); - } - } - } - } } + + callBackFunction(data); + } - - function getCommons(pDevTeam, pRepo, callBack) { - - let moduleName = "Commons.js"; - let key = pDevTeam + "." + pRepo; - let path = pDevTeam + "/" + pRepo; - - storage.getStorageData(pDevTeam + "/" + "bots", pRepo, moduleName, onDataArrived); - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); } - - if (pData !== undefined) { - - let data = pData.toString(); - data = data.trim(); // remove first byte with some encoding. - - data = browserify(data); - - storageData.set(key, data); - - } - - callBack(); - - } - catch (err) { - console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err); - } - } + catch (err) { + console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err); } - - function getUserBot(pDevTeam, pRepo, pProcess, callBack) { - - let moduleName = "User.Bot.js"; - let key = pDevTeam + "." + pRepo + "." + pProcess + "/" + moduleName; - let path = pProcess + "/" + moduleName; - - storage.getStorageData(pDevTeam + "/" + "bots", pRepo, path, onDataArrived); - - function onDataArrived(pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); } - - if (pData !== undefined) { - - let data = pData.toString(); - data = data.trim(); // remove first byte with some encoding. - - data = browserify(data); - - storageData.set(key, data); - - } - - callBack(); - - } - catch (err) { - console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err); - } - } - } - - function browserify(pData) { - - let result = pData.substring(pData.indexOf('=') + 2); - - result = replaceAll(result, "require", "webRequire"); - result = replaceAll(result, "global", "window"); - - return result; - - function replaceAll(text, search, replacement) { - return text.replace(new RegExp(search, 'g'), replacement); - } - } - } - catch (err) { - console.log("[ERROR] BotScripts -> retrieveScripts -> Error = " + err); + } + + function browserify(pData) { + + let result = pData.substring(pData.indexOf('=') + 2); + + result = replaceAll(result, "require", "webRequire"); + result = replaceAll(result, "global", "window"); + + return result; + + function replaceAll(text, search, replacement) { + return text.replace(new RegExp(search, 'g'), replacement); } } } \ No newline at end of file diff --git a/server.js b/server.js index 2a47a0e9c..5adf0caf0 100644 --- a/server.js +++ b/server.js @@ -35,6 +35,7 @@ global.CUSTOM_FAIL_RESPONSE = { let storageData = new Map; let HTMLCloudScripts; // This are the html script tags needed to download the cloud web scripts. +let botScripts; // This module is the one which grabs user bots scrips from the storage, and browserifys them. let ecosystem; let ecosystemObject; @@ -95,19 +96,15 @@ function initialize() { function onInitialized() { const BOT_SCRIPTS = require('./Server/BotsScripts'); - let botScripts = BOT_SCRIPTS.newBotScripts(); + botScripts = BOT_SCRIPTS.newBotScripts(); - botScripts.initialize(ecosystem, ecosystemObject, serverConfig, storageData, onInitialized); + botScripts.initialize(serverConfig, onInitialized); function onInitialized() { - botScripts.loadBotScripts(onScriptsLoaded); + HTMLCloudScripts = pHTMLCloudScripts; + startHtttpServer(); - function onScriptsLoaded(){ - - HTMLCloudScripts = pHTMLCloudScripts; - startHtttpServer(); - } } } } @@ -429,7 +426,18 @@ function onBrowserRequest(request, response) { switch (requestParameters[2]) { case "saveBotCode": { - api.saveBotCode(pData, onResponse); + + let devTeam = requestParameters[3]; + let source = requestParameters[4] + "/" + requestParameters[5]; + let repo = requestParameters[6]; + let path; + + if (requestParameters[8] !== undefined) { + path = requestParameters[7] + "/" + requestParameters[8]; + } else { + path = requestParameters[7]; + } + api.saveBotCode(devTeam, source, repo, path, pData, onResponse); break; } @@ -643,33 +651,53 @@ function onBrowserRequest(request, response) { case "Bots": // This means the cloud folder. { + switch (requestParameters[3]) { - let map = storageData; + case 'bots': { - let key; + let devTeam = requestParameters[2]; + let source = requestParameters[3]; + let repo = requestParameters[4]; + let path; - if (requestParameters[5] !== undefined) { + if (requestParameters[6] !== undefined) { + path = requestParameters[5] + "/" + requestParameters[6]; + } else { + path = requestParameters[5]; + } - key = requestParameters[2] + "." + requestParameters[3] + "." + requestParameters[4] + "/" + requestParameters[5]; + botScripts.getScript(devTeam, source, repo, path, onScriptReady) - } else { + break; + } - key = requestParameters[2] + "." + requestParameters[3] + "." + requestParameters[4]; + case 'members': { + let devTeam = requestParameters[2]; + let source = requestParameters[3] + "/" + requestParameters[4]; + let repo = requestParameters[5]; + let path; + + if (requestParameters[7] !== undefined) { + path = requestParameters[6] + "/" + requestParameters[7]; + } else { + path = requestParameters[6]; + } + + botScripts.getScript(devTeam, source, repo, path, onScriptReady) + + break; + } + + default : { + if (CONSOLE_LOG === true) { console.log("[ERROR] server -> onBrowserRequest -> Bots -> Invalid Request. "); } + } } - let script = map.get(key); + function onScriptReady(pScript) { - if (script !== undefined && script !== null) { + respondWithContent(pScript, response); - respondWithContent(script, response); - - } else { - - if (CONSOLE_LOG === true) { console.log("[WARN] server -> onBrowserRequest -> readEcosystemConfig -> Script Not Found."); } - if (CONSOLE_LOG === true) { console.log("[WARN] server -> onBrowserRequest -> readEcosystemConfig -> key = " + key); } - - respondWithContent("", response); } } break;