Created
June 13, 2020 14:20
-
-
Save rijvirajib/51e975e83d82f36042d1145bcbac48ec to your computer and use it in GitHub Desktop.
Discord message parser for Ruthgar's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sessionStats from './data/session-stats.json' | |
import moment from 'moment' | |
function searchCharacter(player: string, name: string) { | |
const relevantMessages = sessionStats.messages.filter((m, i) => { | |
const playerRegex = new RegExp(player, 'i') | |
const regex = new RegExp(name, 'i') | |
return playerRegex.test(m.content) && regex.test(m.content) | |
// return regex.test(m.content) | |
}) | |
const totalXP = totalXPForCharacter(relevantMessages) | |
const totalGP = totalGPForCharacter(relevantMessages) | |
console.log('For Character:', name) | |
console.log('Total XP:', totalXP) | |
console.log('Level3', totalXP + 900) | |
console.log('Level4', totalXP + 2700) | |
console.log('Total GP:', totalGP) | |
const totalGames = gamesPlayed(relevantMessages) | |
console.log('Total Games:', totalGames) | |
} | |
function gamesPlayed(relevantMessages: Array<any>) { | |
let totalGamesPlayed = 0 | |
const weekCounter: any = {} | |
relevantMessages.forEach((v, i) => { | |
const timestamp = v.timestamp | |
const time = moment(timestamp) | |
const week = time.isoWeek() | |
const year = time.year() | |
totalGamesPlayed++ | |
const key = `${year} ${week}` | |
if (key in weekCounter) { | |
weekCounter[key]++ | |
} else { | |
weekCounter[key] = 0; | |
} | |
// console.log('timestamp', timestamp) | |
console.log("\n", v.content) | |
}) | |
let hasViolation = false | |
Object.keys(weekCounter).forEach(k => { | |
if (weekCounter[k] > 3) { | |
hasViolation = true | |
} | |
}) | |
if (hasViolation) { | |
// Spit out the relevant content | |
// console.log(relevantMessages) | |
return totalGamesPlayed + ' (HAS VIOLATIONS)' | |
} | |
return totalGamesPlayed + ' (No Violations)' | |
} | |
function totalXPForCharacter(relevantMessages: Array<any>) { | |
const regex = new RegExp('^\s*(xp|exp)', 'i') | |
const total = getTotalFromLines(relevantMessages, regex) | |
return total | |
} | |
function totalGPForCharacter(relevantMessages: Array<any>) { | |
const regex = new RegExp('^\s*(gp|gold)', 'i') | |
const total = getTotalFromLines(relevantMessages, regex) | |
return total | |
} | |
function getTotalFromLines(relevantMessages: Array<any>, regex: RegExp) { | |
let total = 0 | |
relevantMessages.forEach((v, i) => { | |
const content = v.content | |
const lines = content.split("\n") | |
lines.forEach((line: string) => { | |
if (regex.test(line)) { | |
total += extractNumber(line) | |
} | |
}) | |
}) | |
return total | |
} | |
function extractNumber(line: string) { | |
const regex = new RegExp(/\d+/g) | |
const normalizedLine = line.replace(/,\s*/,'').replace(/k/i, '000') | |
const match = normalizedLine.match(regex) | |
if (match) { | |
return parseInt(match[0]) | |
} | |
return 0 | |
} | |
searchCharacter('', '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment