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

Commit

Permalink
Merged similar functions in to one(4)
Browse files Browse the repository at this point in the history
  • Loading branch information
penevl committed Apr 22, 2023
1 parent d5d1eff commit 33d8765
Showing 1 changed file with 34 additions and 52 deletions.
86 changes: 34 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,38 @@ function getSquadChartData(serverLogs, squadMembers) {
return toReturn
}

function getSquadIndividualAttendance(dbState, squad, squadName) {

var toReturn = []

squad.forEach(player => {
var temp = {
playerName: String,
attendance: Number
}
temp.playerName = player

logger.trace('Calculating attendance for player ' + player, `webserver/${squadName}`)
var attended = 0

dbState.forEach(element => {
logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, `webserver/${squadName}`)
if(element.players.toString().replaceAll(/\s*\[.*?]/g, '').includes(player)){
logger.trace('Bumping attendance counter for player ' + player, `webserver/${squadName}`)
attended++
}
})

logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, `webserver/${squadName}`)
var att = ((attended / dbState.length) * 100).toFixed(2)
logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', `webserver/${squadName}`)
temp.attendance = att
toReturn.unshift(temp)
})

return toReturn
}

app.get('/', async (req, res) => {

const dbState = await ServerState.find()
Expand Down Expand Up @@ -273,32 +305,7 @@ app.get('/echo', async (req, res) => {
var chartData = getSquadChartData(serverLogs, echotSquad)
logger.trace('chartData: ' + JSON.stringify(chartData), 'webserver/echo')

var individualAttendance = []

echotSquad.forEach(player => {
var temp = {
playerName: String,
attendance: Number
}
temp.playerName = player

logger.trace('Calculating attendance for player ' + player, 'webserver/echo')
var attended = 0

dbState.forEach(element => {
logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, 'webserver/echo')
if(element.players.toString().replaceAll(/\s*\[.*?]/g, '').includes(player)){
logger.trace('Bumping attendance counter for player ' + player, 'webserver/echo')
attended++
}
})

logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, 'webserver/echo')
var att = ((attended / dbState.length) * 100).toFixed(2)
logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', 'webserver/echo')
temp.attendance = att
individualAttendance.unshift(temp)
})
var individualAttendance = getSquadIndividualAttendance(dbState, echotSquad, 'echo')
logger.trace('individualAttendance: ' + JSON.stringify(individualAttendance), 'webserver/echo')

res.render('echo', {
Expand All @@ -322,32 +329,7 @@ app.get('/foxtrot', async (req, res) => {
var chartData = getSquadChartData(serverLogs, foxtrotSquad)
logger.trace('chartData' + JSON.stringify(chartData), 'webserver/foxtrot')

var individualAttendance = []

foxtrotSquad.forEach(player => {
var temp = {
playerName: String,
attendance: Number
}
temp.playerName = player

logger.trace('Calculating attendance for player ' + player, 'webserver/foxtrot')
var attended = 0

dbState.forEach(element => {
logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, 'webserver/foxtrot')
if(element.players.toString().replaceAll(/\s*\[.*?]/g, '').includes(player)){
logger.trace('Bumping attendance counter for player ' + player, 'webserver/foxtrot')
attended++
}
})

logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, 'webserver/foxtrot')
var att = ((attended / dbState.length) * 100).toFixed(2)
logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', 'webserver/foxtrot')
temp.attendance = att
individualAttendance.unshift(temp)
})
var individualAttendance = getSquadIndividualAttendance(dbState, foxtrotSquad, 'foxtrot')
logger.trace('individualAttendance: ' + JSON.stringify(individualAttendance), 'webserver/foxtrot')

res.render('foxtrot', {
Expand Down

0 comments on commit 33d8765

Please sign in to comment.