diff --git a/Globals.js b/Globals.js index 6c1c265f8..eebdfc073 100644 --- a/Globals.js +++ b/Globals.js @@ -1,6 +1,7 @@ /* User Interface Colors */ const STORAGE_PROVIDER = 'AAWeb'; +const CANVAS_APP_NAME = 'Canvas App' const SUPPORTED_EXCHANGES = ["Poloniex"]; const SUPPORTED_MARKETS = [ diff --git a/Spaces/CockpitSpace.js b/Spaces/CockpitSpace.js index 3767afe41..86fc1c821 100644 --- a/Spaces/CockpitSpace.js +++ b/Spaces/CockpitSpace.js @@ -47,20 +47,18 @@ thisObject.container.frame.position.x = 0 let INITIAL_POSITION - const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) - - if (accessToken !== null) { - INITIAL_POSITION = 55 + if (canvas.strategySpace.workspace.enabled === true) { + let localStorage = window.localStorage.getItem(MODULE_NAME) + if (localStorage !== null) { + storage = JSON.parse(localStorage) + thisObject.container.frame.position.y = storage.spacePosition + } else { + INITIAL_POSITION = 55 + thisObject.container.frame.position.y = browserCanvas.height * INITIAL_POSITION / 100 - COCKPIT_SPACE_HEIGHT + } } else { INITIAL_POSITION = 100 - } - - thisObject.container.frame.position.y = browserCanvas.height * INITIAL_POSITION / 100 - COCKPIT_SPACE_HEIGHT - - let localStorage = window.localStorage.getItem(MODULE_NAME) - if (localStorage !== null) { - storage = JSON.parse(localStorage) - thisObject.container.frame.position.y = storage.spacePosition + thisObject.container.frame.position.y = browserCanvas.height * INITIAL_POSITION / 100 - COCKPIT_SPACE_HEIGHT } canvasBrowserResizedEventSubscriptionId = window.canvasApp.eventHandler.listenToEvent('Browser Resized', resize) @@ -101,7 +99,9 @@ } function childrenPhysics () { - thisObject.restartSimulation.physics() + if (canvas.strategySpace.workspace.enabled === true) { + thisObject.restartSimulation.physics() + } thisObject.fullscreen.physics() } function thisObjectPhysics () { @@ -109,8 +109,7 @@ thisObject.status = 'MIDDLE' - let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) - if (user !== null) { // Only if user is logged in + if (canvas.strategySpace.workspace.enabled === true) { thisObject.container.isDraggeable = true } else { thisObject.container.isDraggeable = false @@ -143,8 +142,10 @@ function getContainer (point) { let container - container = thisObject.restartSimulation.getContainer(point) - if (container !== undefined) { return container } + if (canvas.strategySpace.workspace.enabled === true) { + container = thisObject.restartSimulation.getContainer(point) + if (container !== undefined) { return container } + } container = thisObject.fullscreen.getContainer(point) if (container !== undefined) { return container } @@ -162,7 +163,9 @@ drawBackground() thisObject.assetBalances.draw() - thisObject.restartSimulation.draw() + if (canvas.strategySpace.workspace.enabled === true) { + thisObject.restartSimulation.draw() + } thisObject.fullscreen.draw() } @@ -196,8 +199,7 @@ browserCanvasContext.closePath() browserCanvasContext.fill() - let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) - if (user !== null) { // Only if user is logged in + if (canvas.strategySpace.workspace.enabled === true) { arrow() } } diff --git a/Spaces/StrategySpace.js b/Spaces/StrategySpace.js index 10b73a164..0a030a844 100644 --- a/Spaces/StrategySpace.js +++ b/Spaces/StrategySpace.js @@ -32,7 +32,6 @@ function newStrategySpace () { buildIconByPartTypeMap() thisObject.strategizerGateway = newStrategizerGateway() - thisObject.strategizerGateway.initialize() thisObject.workspace = newWorkspace() thisObject.workspace.initialize() @@ -163,11 +162,6 @@ function newStrategySpace () { function getContainer (point) { let container - if (thisObject.strategizerGateway !== undefined) { - container = thisObject.strategizerGateway.getContainer(point) - if (container !== undefined) { return container } - } - if (thisObject.sidePanel !== undefined) { container = thisObject.sidePanel.getContainer(point) if (container !== undefined) { return container } diff --git a/StrategySpace/StrategizerGateway.js b/StrategySpace/StrategizerGateway.js index e8ce8ce3d..59cecb130 100644 --- a/StrategySpace/StrategizerGateway.js +++ b/StrategySpace/StrategizerGateway.js @@ -1,18 +1,15 @@ function newStrategizerGateway () { const MODULE_NAME = 'Strategizer Gateway' - const ERROR_LOG = true const logger = newWebDebugLog() logger.fileName = MODULE_NAME let thisObject = { + idAtStrategizer: undefined, strategizerData: undefined, container: undefined, loadFromStrategyzer: loadFromStrategyzer, - saveToStrategyzer: saveToStrategyzer, - draw: draw, - getContainer: getContainer, - initialize: initialize + saveToStrategyzer: saveToStrategyzer } let colletionItems = [] @@ -30,111 +27,28 @@ function newStrategizerGateway () { thisObject.container.isDraggeable = false thisObject.container.isClickeable = false + let graphQLServer + return thisObject - function initialize () { - - } - - async function saveToStrategyzer () { - try { - const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) - - let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) - if (user === null) { - if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> Can not save because user is not logged in. ') } - return - } - - user = JSON.parse(user) - let fbSlug = 'simulator' + '-' + 'bot' + '-' + user.alias.replace('.', '') - - /* See if we need to update or create a new record at the strategizer */ - - let tradingSystem = canvas.strategySpace.workspace.getProtocolTradingSystem() - let idAtStrategizer = window.localStorage.getItem(MODULE_NAME) - - if (idAtStrategizer === undefined) { - let response = await axios({ - url: window.canvasApp.graphQL.masterAppApiUrl, - method: 'post', - data: { - query: ` - mutation strategizer_CreateTradingSystem - ( - $fbSlug: String!, $data: strategizer_JSON! - ) - { - strategizer_CreateTradingSystem(fbSlug: $fbSlug, tradingSystem: { data: $data }) - { - id - } - } - `, - variables: { - fbSlug: fbSlug, - data: tradingSystem - } - }, - headers: { - authorization: 'Bearer ' + accessToken - } - }) - - if (response.data.errors) { - console.log('Error getting events: ' + JSON.stringify(response.data.errors)) - throw 'Error getting events: ' + JSON.stringify(response.data.errors) - } else { - window.localStorage.setItem(MODULE_NAME, response.data.data.strategizer_CreateTradingSystem.id) - } - } else { - const graphQLServer = await axios({ - url: window.canvasApp.graphQL.masterAppApiUrl, - method: 'post', - data: { - query: ` - mutation strategizer_EditTradingSystem - ( - $id: ID!, $data: strategizer_JSON! - ) - { - strategizer_EditTradingSystem(id: $id, tradingSystem: { data: $data }) - { - id - } - } - `, - variables: { - id: idAtStrategizer, - data: tradingSystem - } - }, - headers: { - authorization: 'Bearer ' + accessToken - } - }) - } - - return true - } catch (err) { - if (ERROR_LOG === true) { logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) } - } - } - async function loadFromStrategyzer () { try { const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) + if (accessToken === null) { + logger.write('[ERROR] loadFromStrategyzer -> Can load from strategizer because the accessToken is missing.') + return + } let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) if (user === null) { - // if there is no user that means that we are logged off, which means this object can not be used. + logger.write('[ERROR] saveToStrategyzer -> Can not load from strategizer because user is not logged in. ') return } user = JSON.parse(user) let fbSlug = 'simulator' + '-' + 'bot' + '-' + user.alias.replace('.', '') - let response = await axios({ + graphQLServer = await axios({ url: window.canvasApp.graphQL.masterAppApiUrl, method: 'post', data: { @@ -158,30 +72,83 @@ function newStrategizerGateway () { } }) - if (response.data.errors) { - if (response.data.errors[0] === 'Ressource not found : None of the tradingSystem are linked to that fbSlug') { - return false - } else { - logger.write('[ERROR] Error while getting Trading System from Strategizer: ' + JSON.stringify(response.data.errors)) - return false - } + if (graphQLServer.data.errors) { + logger.write('[ERROR] loadFromStrategyzer -> GraphQL Error: ' + JSON.stringify(graphQLServer.data.errors)) + return false } else { - thisObject.strategizerData = JSON.parse(JSON.stringify(response.data.data.strategizer_TradingSystemByFb.data)) - window.localStorage.setItem(MODULE_NAME, response.data.data.strategizer_TradingSystemByFb.id) + thisObject.strategizerData = JSON.parse(JSON.stringify(graphQLServer.data.data.strategizer_TradingSystemByFb.data)) + thisObject.idAtStrategizer = graphQLServer.data.data.strategizer_TradingSystemByFb.id + window.localStorage.setItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias, thisObject.idAtStrategizer) return true } } catch (err) { - if (ERROR_LOG === true) { logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) } + logger.write('[ERROR] loadFromStrategyzer -> err = ' + err.stack) } } - function getContainer (point) { - let container + async function saveToStrategyzer () { + try { + const accessToken = window.localStorage.getItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY) + if (accessToken === null) { + logger.write('[ERROR] saveToStrategyzer -> Can not save because the accessToken is missing.') + return + } - return undefined - } + let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) + if (user === null) { + logger.write('[ERROR] saveToStrategyzer -> Can not save because user is not logged in. ') + return + } - function draw () { + user = JSON.parse(user) + let fbSlug = 'simulator' + '-' + 'bot' + '-' + user.alias.replace('.', '') + /* See if we need to update or create a new record at the strategizer */ + + let tradingSystem = canvas.strategySpace.workspace.getProtocolTradingSystem() + if (tradingSystem === undefined || idAtStrategizer === null) { + logger.write('[ERROR] saveToStrategyzer -> Can not save when tradingSystem is null or undefined.') + return + } + + let idAtStrategizer = window.localStorage.getItem(CANVAS_APP_NAME + '.' + MODULE_NAME + '.' + user.alias) + if (idAtStrategizer === undefined || idAtStrategizer === null) { + logger.write('[ERROR] saveToStrategyzer -> Can not save when idAtStrategizer is null or undefined.') + return + } + graphQLServer = await axios({ + url: window.canvasApp.graphQL.masterAppApiUrl, + method: 'post', + data: { + query: ` + mutation strategizer_EditTradingSystem + ( + $id: ID!, $data: strategizer_JSON! + ) + { + strategizer_EditTradingSystem(id: $id, tradingSystem: { data: $data }) + { + id + } + } + `, + variables: { + id: idAtStrategizer, + data: tradingSystem + } + }, + headers: { + authorization: 'Bearer ' + accessToken + } + }) + + if (graphQLServer.data.errors) { + logger.write('[ERROR] saveToStrategyzer -> GraphsQL Error -> graphQLServer.data.errors = ' + JSON.stringify(graphQLServer.data.errors)) + } + + return true + } catch (err) { + logger.write('[ERROR] saveToStrategyzer -> err = ' + err.stack) + } } } diff --git a/StrategySpace/Workspace/Workspace.js b/StrategySpace/Workspace/Workspace.js index e0c81f559..a8b947318 100644 --- a/StrategySpace/Workspace/Workspace.js +++ b/StrategySpace/Workspace/Workspace.js @@ -5,13 +5,13 @@ function newWorkspace () { let thisObject = { tradingSystem: undefined, container: undefined, + enabled: false, onMenuItemClick: onMenuItemClick, getProtocolTradingSystem: getProtocolTradingSystem, physics: physics, spawn: spawn, detachNode: detachNode, attachNode: attachNode, - getContainer: getContainer, initialize: initialize, finalize: finalize } @@ -42,7 +42,6 @@ function newWorkspace () { functionLibraryProtocolNode = newProtocolNode() functionLibraryWorkspaceNodes = newStringifyNode() - let isInitialized = false return thisObject function finalize () { @@ -58,8 +57,10 @@ function newWorkspace () { } user = JSON.parse(user) - let savedWorkspace = window.localStorage.getItem(user.alias + '.' + 'workspace') - if (savedWorkspace === null) { + let idAtStrategizer = window.localStorage.getItem(CANVAS_APP_NAME + '.' + 'Strategizer Gateway' + '.' + user.alias) + let savedWorkspace = window.localStorage.getItem(CANVAS_APP_NAME + '.' + 'Workspace' + '.' + user.alias) + + if (savedWorkspace === null || idAtStrategizer === null) { workspaceNode.type = 'Workspace' workspaceNode.name = 'My Workspace' functionLibraryPartsFromNodes.createPartFromNode(workspaceNode, undefined, undefined) @@ -72,7 +73,7 @@ function newWorkspace () { let rootNode = workspaceNode.rootNodes[i] functionLibraryPartsFromNodes.createPartFromNode(rootNode, undefined, undefined) } - isInitialized = true + thisObject.enabled = true } } @@ -83,21 +84,8 @@ function newWorkspace () { workspaceNode.rootNodes.push(thisObject.tradingSystem) functionLibraryPartsFromNodes.createPartFromNode(thisObject.tradingSystem, undefined, undefined) thisObject.tradingSystem.payload.uiObject.setRunningStatus() - } else { - // First use of the Designer, lets help by creating the first Trading System - thisObject.tradingSystem = { - type: 'Trading System', - strategies: [] - } - workspaceNode.rootNodes.push(thisObject.tradingSystem) - functionLibraryPartsFromNodes.createPartFromNode(thisObject.tradingSystem, undefined, undefined) - thisObject.tradingSystem.payload.uiObject.setRunningStatus() + thisObject.enabled = true } - isInitialized = true - } - - function getContainer (point) { - } function getProtocolTradingSystem () { @@ -113,7 +101,7 @@ function newWorkspace () { } function physics () { - if (isInitialized !== true) { return } + if (thisObject.enabled !== true) { return } /* Here we will save all the workspace related objects into the local storage */ let user = window.localStorage.getItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY) if (user === null) { @@ -122,7 +110,7 @@ function newWorkspace () { user = JSON.parse(user) let textToSave = stringifyWorkspace() - window.localStorage.setItem(user.alias + '.' + 'workspace', textToSave) + window.localStorage.setItem(CANVAS_APP_NAME + '.' + 'Workspace' + '.' + user.alias, textToSave) } function stringifyWorkspace () {