Skip to content

Commit

Permalink
💄 Add the turn number at the top of the page
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard-IMBERT committed May 8, 2018
1 parent 1b5c775 commit 6202fba
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ app.get('/stats', (req, res) => {
Crawler.getCounts().then(d => res.send(d))
})

app.get('/turn', (req, res) => {
res.append('Content-Type', 'application/json')
Crawler.getTurnInfo().then((d) => res.send(d))
})

const port = process.env.STARLIGHT_PORT || 3000

app.listen(port, _ => {
Expand Down
18 changes: 18 additions & 0 deletions src/back/Crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const InhabitantSchema = new Schema({
})
const StatusMongoose = mongoose.model('Status', {
date: Date,
Turn: String,
Survivors: [InhabitantSchema],
Craft: String,
Magic: String
Expand Down Expand Up @@ -294,6 +295,7 @@ function refresh() {

(new StatusMongoose({
date: new Date(),
Turn: result.Turns[result.Turns.length - 1].designation,
Survivors: [...result.Survivors,
...result.LooseItems,
...result.Structures],
Expand All @@ -303,6 +305,21 @@ function refresh() {
}).end()
}

function getTurnInfo() {
return new Promise((resolve, reject) => {
StatusMongoose.find({}, 'Turn', {
skip:0,
limit: 1,
sort: {
date: -1
},
}, (err, stat) => {
if(err) { reject(err) }
resolve(stat)
})
})
}

function getAllInfo() {
return new Promise((resolve, reject) => {
StatusMongoose.find({}, ['Survivors'], {
Expand Down Expand Up @@ -397,6 +414,7 @@ function getDetailsAboutStat(stat) {
}

module.exports = {
getTurnInfo,
getDetailsAboutStat,
getCounts,
getInfoByCoord,
Expand Down
31 changes: 20 additions & 11 deletions src/front/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const inputSearch = document.querySelector('input#search-survivor')
const markButton = document.querySelector("button#markButton")
const blankButton = document.querySelector("button#blankButton")

const turnTitle = document.querySelector("h1#turn")

const stats = document.querySelector("div#stats")

const table = document.querySelector('table#resources')
Expand Down Expand Up @@ -62,6 +64,10 @@ function fetchMetadata() {
return fetch(Requests.MetadataRequest()).then(d => d.json())
}

function fetchTurn() {
return fetch(Requests.TurnRequest()).then(d => d.json())
}

function getSurvivors(x, y) {
return (((x && y) ? fetch(Requests.InfoRequest(x,y)) : fetch(Requests.AllInfoRequest()))
.then(d => d.json())
Expand Down Expand Up @@ -291,15 +297,18 @@ fetch(Requests.StatsRequest()).then(d => d.json()).then(d => {

const surPos = row.insertCell()
surPos.appendChild(document.createTextNode(
`(${detail[1]},${detail[2]})`))
surPos.addEventListener('click',
e => getSurvivors(detail[1],detail[2]))
surPos.classList.add('location')

const surCount = row.insertCell()
surCount.appendChild(document.createTextNode(`${detail[3]}`))
}
})
`(${detail[1]},${detail[2]})`))
surPos.addEventListener('click',
e => getSurvivors(detail[1],detail[2]))
surPos.classList.add('location')

const surCount = row.insertCell()
surCount.appendChild(document.createTextNode(`${detail[3]}`))
}
})
}
})
})
}
})
fetchTurn().then(d => {
turnTitle.textContent = d[0].Turn
})
3 changes: 3 additions & 0 deletions src/front/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</head>
<body>
<div id="content">
<div class="row">
<h1 id="turn"></h1>
</div>
<canvas id="map">
</canvas>
<div class="row">
Expand Down
5 changes: 5 additions & 0 deletions src/front/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export default class Requests {
headers: (new Headers()).append('Accept','application/json')
})}

static TurnRequest() { return new Request('/turn', {
method: 'GET',
headers: (new Headers()).append('Accept','application/json')
})}

static InfoRequest(x,y) { return new Request(`/info?col=${x}&row=${y}`, {
method: 'GET',
headers: (new Headers()).append('Accept', 'application/json')
Expand Down

0 comments on commit 6202fba

Please sign in to comment.