Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Added map generation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylankjy committed Sep 16, 2021
1 parent 4aa792a commit f9fbd35
Show file tree
Hide file tree
Showing 12 changed files with 502 additions and 11 deletions.
12 changes: 12 additions & 0 deletions app/helpers/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ module.exports = {
parseISOTimeOnly: (value) => {
return dateFormat(value, 'HH:MM')
},
ifIncludes(list, value, options) {
// Convert to array if not already
if (!Array.isArray(list)) {
list = list.split(',')
}

if (list.includes(value)) {
return options.fn(this)
} else {
return options.inverse(this)
}
},
}
42 changes: 42 additions & 0 deletions app/models/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Map extends Model {}

Map.init(
{
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
},
name: {
allowNull: false,
type: DataTypes.STRING,
},
lines_to_show: {
allowNull: false,
type: DataTypes.STRING,
defaultValue: '',
},
network_id: {
allowNull: false,
type: DataTypes.UUID,
},
},

{
sequelize,
tableName: 'Map',
modelName: 'Map',
},
)

// Map.associate = (models) => {
// Map.belongsTo(models.Network, {
// onDelete: 'cascade',
// foreignKey: 'map_id',
// })
// }

return Map
}
123 changes: 123 additions & 0 deletions app/routes/maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const express = require('express')
const router = express.Router()

const uuid = require('uuid')

const { Network, Line, Station, Connection, Map } = require('../models')
const { NotificationCookie, FormRestoreCookie } = require('../helpers/cookie_options')
const { Op } = require('sequelize')

router.get('/', async (req, res) => {
const networks = await Network.findAll({
attributes: ['id', 'name'],
raw: true,
})

const maps = await Map.findAll({
attributes: ['id', 'name'],
raw: true,
})

const data = {
data: {
networks,
maps,
},
}
return res.render('maps/index', data)
})

router.post('/', async (req, res) => {
const { name, networkToMap } = req.body

await Map.create({
id: uuid.v4(),
name: name,
network_id: networkToMap,
})

res.cookie('notification', `${name} has been created.`, NotificationCookie)
return res.redirect('back')
})

router.get('/edit/:mapID', async (req, res) => {
const { mapID } = req.params

const currentMap = await Map.findOne({
where: { id: mapID },
raw: true,
})

const mapNetwork = await Network.findOne({
where: {
id: currentMap.network_id,
},
include: [
{
model: Line,
},
],
})

// Data for rendering the map
const Lines = await Line.findAll({
where: {
id: {
[Op.in]: currentMap.lines_to_show.split(',') || [currentMap.lines_to_show],
},
network_id: currentMap.network_id,
},
include: [
{
model: Station,
include: [
{
model: Connection,
},
],
},
],
})

const data = {
data: {
currentMap: currentMap,
mapNetwork: mapNetwork,
selectedLines: Lines,
},
layout: 'map_editor',
}
return res.render('maps/editor', data)
})

router.post('/edit/:mapID', async (req, res) => {
const { mapID } = req.params
const { linesToDisplay } = req.body

let linesToDisplayArray

// Check whether linesToDisplay is valid
if (!linesToDisplay) {
linesToDisplayArray = []
} else {
// Check whether linesToDisplay is array
if (!Array.isArray(linesToDisplay)) {
// Convert linesToDisplay to array
linesToDisplayArray = linesToDisplay.split(',')
} else {
linesToDisplayArray = linesToDisplay
}
}

await Map.update({
lines_to_show: linesToDisplayArray.toString(),
}, {
where: {
id: mapID,
},
})

return res.redirect('back')
})

module.exports = router
1 change: 1 addition & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
module.exports = (app) => {
app.use('/', require('./index'))
app.use('/manage', require('./manage'))
app.use('/maps', require('./maps'))
}
59 changes: 51 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metronami",
"version": "0.0.2-beta",
"version": "0.0.3-beta",
"description": "An open source Public Transport network manager for roleplay and fantasy worlds",
"main": "app.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"express-handlebars": "^5.3.3",
"get-github-tag": "^1.1.0",
"moment": "^2.29.1",
"node-notifier": "^10.0.0",
"open": "^8.2.1",
"semver": "^7.3.5",
"sequelize": "^6.6.5",
Expand Down
33 changes: 33 additions & 0 deletions public/css/map.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.node {
height: 1.5rem;
width: 1.5rem;
border-radius: 50%;
background-color: #fff;
border: 4px #000 solid;
margin-top: -1.25rem;
}

.line {
height: 1rem;
width: 5.25rem;
margin-right: -.8rem;
/* z-index: 1 */
border-radius: 6rem;
}

.line-section {
display: inline-block;
min-height: min-content;
height: 12rem;
}

.station-name {
font-weight: 600;
/* rotate: y 45deg; */
transform: rotate(45deg) translate(1rem, -1rem);
transform-origin: 0 0;
width: 0;
height: auto;
white-space: nowrap;
line-height: 1rem;
}
Loading

0 comments on commit f9fbd35

Please sign in to comment.