diff --git a/cypress/fixtures/default.js b/cypress/fixtures/default.js index 6652f06..50eba96 100644 --- a/cypress/fixtures/default.js +++ b/cypress/fixtures/default.js @@ -37,7 +37,10 @@ export const mockState = { location: { id: 'd23cd552-b79b-4752-86d8-9abb22d04c27', isHomePlanet: true, - location: 99, + location: { + x: 0.22, + y: 0.03 + }, name: 'Kennajeannet', items: [ { @@ -83,7 +86,10 @@ export const mockState = { location: { id: '83a7a2b7-3e94-4968-ab35-edb37c30f6ea', isHomePlanet: false, - location: 51, + location: { + x: -0.3, + y: -0.14 + }, name: 'Gaffey', items: [ { @@ -131,7 +137,10 @@ export const mockState = { location: { id: 'd23cd552-b79b-4752-86d8-9abb22d04c27', isHomePlanet: true, - location: 99, + location: { + x: 0.22, + y: 0.03 + }, name: 'Kennajeannet', items: [ { @@ -178,7 +187,10 @@ export const mockState = { location: { id: '83a7a2b7-3e94-4968-ab35-edb37c30f6ea', isHomePlanet: false, - location: 51, + location: { + x: -0.3, + y: -0.14 + }, name: 'Gaffey', items: [ { @@ -219,7 +231,10 @@ export const mockState = { { id: 'd23cd552-b79b-4752-86d8-9abb22d04c27', isHomePlanet: true, - location: 99, + location: { + x: 0.22, + y: 0.03 + }, name: 'Kennajeannet', items: [ { @@ -237,7 +252,10 @@ export const mockState = { { id: '650f6b64-a7ff-4b72-b75c-a376bf1e4446', isHomePlanet: false, - location: 96, + location: { + x: -0.29, + y: -0.32 + }, name: 'Richardspalding', items: [ { @@ -255,7 +273,10 @@ export const mockState = { { id: '83a7a2b7-3e94-4968-ab35-edb37c30f6ea', isHomePlanet: false, - location: 51, + location: { + x: -0.3, + y: -0.14 + }, name: 'Gaffey', items: [ { diff --git a/cypress/fixtures/state.json b/cypress/fixtures/state.json deleted file mode 100644 index 11c46ce..0000000 --- a/cypress/fixtures/state.json +++ /dev/null @@ -1,260 +0,0 @@ -{ - "market": { - "buyers": [ - { - "color": { - "50": "#e0f2f1", - "100": "#b2dfdb", - "200": "#80cbc4", - "300": "#4db6ac", - "400": "#26a69a", - "500": "#009688", - "600": "#00897b", - "700": "#00796b", - "800": "#00695c", - "900": "#004d40", - "A100": "#a7ffeb", - "A200": "#64ffda", - "A400": "#1de9b6", - "A700": "#00bfa5" - }, - "id": "fa9cd06d-f2c6-4b64-9d18-2fc13f518a32", - "name": "Samara Zinoboppian", - "price": 10, - "location": { - "id": "d23cd552-b79b-4752-86d8-9abb22d04c27", - "isHomePlanet": true, - "location": 99, - "name": "Kennajeannet", - "items": [ - { - "description": "Midas' favorite.", - "name": "Gold", - "value": 5, - "volume": 0.5, - "id": "b9a6b27e-e778-4acd-8c48-fa676b6c4393", - "destination": { "name": "Gaffey", "value": 51 }, - "price": 6, - "quantity": 1 - } - ] - }, - "jumps": 6, - "item": { - "description": "Still used to fight the common cold!", - "name": "Antibiotics", - "value": 2, - "volume": 0.1 - } - }, - { - "color": { - "50": "#e8eaf6", - "100": "#c5cae9", - "200": "#9fa8da", - "300": "#7986cb", - "400": "#5c6bc0", - "500": "#3f51b5", - "600": "#3949ab", - "700": "#303f9f", - "800": "#283593", - "900": "#1a237e", - "A100": "#8c9eff", - "A200": "#536dfe", - "A400": "#3d5afe", - "A700": "#304ffe" - }, - "id": "e7621b0e-767b-46c2-ad2c-cb5014c946c7", - "name": "Kreia Protheans", - "price": 7, - "location": { - "id": "83a7a2b7-3e94-4968-ab35-edb37c30f6ea", - "isHomePlanet": false, - "location": 51, - "name": "Gaffey", - "items": [ - { - "description": "Bullets used for shootin'.", - "name": "Ammunition", - "value": 3, - "volume": 0.8, - "id": "3838ae02-28f0-4591-bd46-d9461f2c4967", - "destination": { "name": "Kennajeannet", "value": 99 }, - "price": 3, - "quantity": 6 - } - ] - }, - "jumps": 9, - "item": { - "description": "Your garden variety ore. From a rock. A space rock.", - "name": "Ore", - "value": 2, - "volume": 1 - } - } - ], - "sellers": [ - { - "color": { - "50": "#fffde7", - "100": "#fff9c4", - "200": "#fff59d", - "300": "#fff176", - "400": "#ffee58", - "500": "#ffeb3b", - "600": "#fdd835", - "700": "#fbc02d", - "800": "#f9a825", - "900": "#f57f17", - "A100": "#ffff8d", - "A200": "#ffff00", - "A400": "#ffea00", - "A700": "#ffd600" - }, - "id": "bec6030d-e4b0-4ed1-903c-f1fb73201884", - "name": "Talyn Vulpimancer", - "price": 2, - "location": { - "id": "d23cd552-b79b-4752-86d8-9abb22d04c27", - "isHomePlanet": true, - "location": 99, - "name": "Kennajeannet", - "items": [ - { - "description": "Midas' favorite.", - "name": "Gold", - "value": 5, - "volume": 0.5, - "id": "b9a6b27e-e778-4acd-8c48-fa676b6c4393", - "destination": { "name": "Gaffey", "value": 51 }, - "price": 6, - "quantity": 1 - } - ] - }, - "jumps": 3, - "item": { - "description": "Junk! Space junk, to be exact. Prevalent throughout the galaxy.", - "name": "Space Junk", - "value": 1, - "volume": 0.1 - } - }, - { - "color": { - "50": "#f9fbe7", - "100": "#f0f4c3", - "200": "#e6ee9c", - "300": "#dce775", - "400": "#d4e157", - "500": "#cddc39", - "600": "#c0ca33", - "700": "#afb42b", - "800": "#9e9d24", - "900": "#827717", - "A100": "#f4ff81", - "A200": "#eeff41", - "A400": "#c6ff00", - "A700": "#aeea00" - }, - "id": "7620a0fd-fdd8-4af2-8924-06183243d842", - "name": "Stark Minbari", - "price": 1, - "location": { - "id": "83a7a2b7-3e94-4968-ab35-edb37c30f6ea", - "isHomePlanet": false, - "location": 51, - "name": "Gaffey", - "items": [ - { - "description": "Bullets used for shootin'.", - "name": "Ammunition", - "value": 3, - "volume": 0.8, - "id": "3838ae02-28f0-4591-bd46-d9461f2c4967", - "destination": { "name": "Kennajeannet", "value": 99 }, - "price": 3, - "quantity": 6 - } - ] - }, - "jumps": 6, - "item": { - "description": "Smoke it or chew it. Farmed on distant planets by astronauts in overalls.", - "name": "Tobacco", - "value": 3, - "volume": 0.2 - } - } - ] - }, - "ship": { - "cargo": { "items": [], "volumeRemaining": 10 }, - "destination": null, - "isShipTraveling": false, - "location": { "name": "Kennajeannet", "value": 99 }, - "tripDuration": null - }, - "ui": { "view": "Ship" }, - "user": { "cash": 100 }, - "world": { - "isTimerRunning": false, - "planets": [ - { - "id": "d23cd552-b79b-4752-86d8-9abb22d04c27", - "isHomePlanet": true, - "location": 99, - "name": "Kennajeannet", - "items": [ - { - "description": "Midas' favorite.", - "name": "Gold", - "value": 5, - "volume": 0.5, - "id": "b9a6b27e-e778-4acd-8c48-fa676b6c4393", - "destination": { "name": "Gaffey", "value": 51 }, - "price": 6, - "quantity": 1 - } - ] - }, - { - "id": "650f6b64-a7ff-4b72-b75c-a376bf1e4446", - "isHomePlanet": false, - "location": 96, - "name": "Richardspalding", - "items": [ - { - "description": "Still used to fight the common cold!", - "name": "Antibiotics", - "value": 2, - "volume": 0.1, - "id": "4fbad0f5-152f-4070-af8b-e0128b845b9a", - "destination": { "name": "Kennajeannet", "value": 99 }, - "price": 4, - "quantity": 6 - } - ] - }, - { - "id": "83a7a2b7-3e94-4968-ab35-edb37c30f6ea", - "isHomePlanet": false, - "location": 51, - "name": "Gaffey", - "items": [ - { - "description": "Bullets used for shootin'.", - "name": "Ammunition", - "value": 3, - "volume": 0.8, - "id": "3838ae02-28f0-4591-bd46-d9461f2c4967", - "destination": { "name": "Kennajeannet", "value": 99 }, - "price": 3, - "quantity": 6 - } - ] - } - ] - } -} diff --git a/src/components/Map.js b/src/components/Map.js index 282af99..a3e8399 100644 --- a/src/components/Map.js +++ b/src/components/Map.js @@ -30,19 +30,21 @@ const Map = ({ currentShipLocation, planets, ship }) => { const svg = createSvg('#map-root', height, width) // TODO - Incorporate this into generatePlanets() - const planet1Location = { x: -0.25, y: 0 } - const planet2Location = { x: 0, y: 0 } - const planet3Location = { x: 0.25, y: 0 } - const mockPlanetLocations = [ - planet1Location, - planet2Location, - planet3Location - ] - - const nodes_data = planets.map((planet, i) => ({ - ...planet, - location: mockPlanetLocations[i] - })) + // const planet1Location = { x: 0.1, y: 0.4 } + // const planet2Location = { x: -0.01, y: -0.03 } + // const planet3Location = { x: 0.03, y: 0.36 } + // const mockPlanetLocations = [ + // planet1Location, + // planet2Location, + // planet3Location + // ] + + // const nodes_data = planets.map((planet, i) => ({ + // ...planet, + // location: mockPlanetLocations[i] + // })) + + const nodes_data = planets const links_data = [ { source: nodes_data[0].name, target: nodes_data[1].name }, diff --git a/src/util/main.js b/src/util/main.js index 562b567..9d59dd8 100644 --- a/src/util/main.js +++ b/src/util/main.js @@ -44,6 +44,34 @@ export const generateItems = possibleDestinations => { return items } +const randomSign = () => (Math.random() >= 0.5 ? 1 : -1) + +const randomCoordinate = () => + Math.round(100 * (randomSign() * (Math.random() * 0.4))) / 100 + +const generateCoordinate = planets => { + let coordinate = randomCoordinate() + while ( + planets.some( + // ? Should I be concerned about this? + // eslint-disable-next-line + ({ location }) => + location.x - coordinate < 0.1 && location.y - coordinate < 0.1 + ) + ) { + coordinate = randomCoordinate() + } + return coordinate +} + +const generateLocation = planets => { + console.log(planets) + return { + x: generateCoordinate(planets), + y: generateCoordinate(planets) + } +} + /** * Generataes an array of planet objects randomly. * @returns {array} @@ -54,7 +82,7 @@ export const generatePlanets = () => { for (let i = 0; i < 3; i++) { const id = uuidv4() const isHomePlanet = i === 0 - const location = Math.floor(Math.random() * 100 + 1) + const location = generateLocation(planets) const name = getPlanetName() planets.push({ id, isHomePlanet, location, name }) diff --git a/src/util/map.js b/src/util/map.js index 9aa48b1..530f851 100644 --- a/src/util/map.js +++ b/src/util/map.js @@ -10,6 +10,7 @@ export const createSvg = (selector, height, width) => .append('svg') .attr('height', height) .attr('width', width) + // .style('padding', '25px') export const createSimulation = data => d3 @@ -66,7 +67,7 @@ export const createLinks = (svg, data, height, width) => .attr( 'y1', ({ source }) => - height / 2 + svg.select(`#${source}`).data()[0].location.y * height + height / 2 - svg.select(`#${source}`).data()[0].location.y * height ) .attr( 'x2', @@ -78,7 +79,7 @@ export const createLinks = (svg, data, height, width) => .attr( 'y2', ({ target }) => - height / 2 + svg.select(`#${target}`).data()[0].location.y * height + height / 2 - svg.select(`#${target}`).data()[0].location.y * height ) const getLabelWidth = (svg, selector) => @@ -94,7 +95,7 @@ export const createLabels = (svg, height, width) => { ({ location, name }) => width / 2 + location.x * width - getLabelWidth(svg, `#${name}`) / 2 ) - .attr('y', ({ location }) => height / 2 + location.y * height + radius + 20) + .attr('y', ({ location }) => height / 2 - location.y * height + radius + 20) console.log({ selection }) return selection @@ -110,7 +111,7 @@ export const createHomePlanetInd = (svg, planets, height, width) => { .attr('id', 'home-planet-ind') .style('font-size', '10px') .attr('x', ({ location }) => width / 2 + location.x * width - 58.61 / 2) - .attr('y', ({ location }) => height / 2 + location.y * height + radius + 40) + .attr('y', ({ location }) => height / 2 - location.y * height + radius + 40) return selection } @@ -123,7 +124,7 @@ export const createShipInd = (svg, ship, height, width) => .attr('id', 'ship-ind') .style('font-size', '10px') .attr('x', ({ location }) => width / 2 + location.x * width - 61.67 / 2) - .attr('y', ({ location }) => height / 2 + location.y * height - radius - 10) + .attr('y', ({ location }) => height / 2 - location.y * height - radius - 10) export const addEventsToNodes = ( svg, @@ -190,7 +191,7 @@ export const updateShipLocation = ( .attr( 'y', () => - height / 2 + destinationNode.data()[0].location.y * height - radius - 10 + height / 2 - destinationNode.data()[0].location.y * height - radius - 10 ) // * Remove event listeners from nodes diff --git a/src/views/ship.js b/src/views/ship.js index 012975d..1366911 100644 --- a/src/views/ship.js +++ b/src/views/ship.js @@ -39,7 +39,7 @@ const ShipView = ({ items, handleRemoveCargo, location, volumeRemaining }) => { Value: - {location.value} + {/* {location.value} */} Name: