From 6202fba484a9e996a750ca5245f61a1aabf0292d Mon Sep 17 00:00:00 2001 From: Tigri Date: Tue, 8 May 2018 16:45:28 +0200 Subject: [PATCH] :lipstick: Add the turn number at the top of the page --- index.js | 5 +++++ src/back/Crawler.js | 18 ++++++++++++++++++ src/front/front.js | 31 ++++++++++++++++++++----------- src/front/html/index.html | 3 +++ src/front/init.js | 5 +++++ 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index fc3fec2..a8bb328 100644 --- a/index.js +++ b/index.js @@ -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, _ => { diff --git a/src/back/Crawler.js b/src/back/Crawler.js index 49c16b6..e05dcaa 100644 --- a/src/back/Crawler.js +++ b/src/back/Crawler.js @@ -67,6 +67,7 @@ const InhabitantSchema = new Schema({ }) const StatusMongoose = mongoose.model('Status', { date: Date, + Turn: String, Survivors: [InhabitantSchema], Craft: String, Magic: String @@ -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], @@ -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'], { @@ -397,6 +414,7 @@ function getDetailsAboutStat(stat) { } module.exports = { + getTurnInfo, getDetailsAboutStat, getCounts, getInfoByCoord, diff --git a/src/front/front.js b/src/front/front.js index 378e3ea..d658f6e 100644 --- a/src/front/front.js +++ b/src/front/front.js @@ -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') @@ -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()) @@ -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 +}) diff --git a/src/front/html/index.html b/src/front/html/index.html index abe61a9..3791f25 100644 --- a/src/front/html/index.html +++ b/src/front/html/index.html @@ -7,6 +7,9 @@
+
+

+
diff --git a/src/front/init.js b/src/front/init.js index 3b61887..4ce25a0 100644 --- a/src/front/init.js +++ b/src/front/init.js @@ -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')