Skip to content

Commit

Permalink
Change PoGo event data source (KartulUdus#907)
Browse files Browse the repository at this point in the history
* change pogo event datasource to ScrapedDuck

* fix lint issues

* fix event time provision, extend poracle-test
  • Loading branch information
hamster007Github authored Aug 17, 2024
1 parent e823ab9 commit e85712f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
3 changes: 0 additions & 3 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"latest": "npm run generate latest && npm run koji && node .",
"lint": "./node_modules/.bin/eslint ./src --fix",
"migrateV3": "node src/init/migrateV3.js",
"prepare": "husky install",
"prepare": "husky",
"start": "npm run generate && npm run koji && node .",
"test": "node test/test"
},
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/pokestop.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class Invasion extends Controller {
// Lineup 100% of encounter
let gruntLineupformNormalised = ''
const gruntLineupList = { confirmed: true, monsters: [] }
if (data.lineup && data.lineup != 'null') {
if (data.lineup && data.lineup !== 'null') {
data.lineup.forEach((lr) => {
const lineup = +lr.pokemon_id
const lineupForm = +lr.form
Expand Down
40 changes: 30 additions & 10 deletions src/lib/pogoEventParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const moment = require('moment-timezone')
class PogoEventParser {
constructor(log) {
this.log = log
this.spawnEvents = []
this.questEvents = []
}

/**
Expand All @@ -25,7 +27,7 @@ class PogoEventParser {
// Timeout Logic
}, timeoutMs)

const url = 'https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/events.json'
const url = 'https://raw.githubusercontent.com/bigfoott/ScrapedDuck/data/events.json'
const result = await axios({
method: 'get',
url,
Expand All @@ -42,7 +44,25 @@ class PogoEventParser {
* @param events
*/
loadEvents(events) {
this.events = events
try {
// create filtered pokemon spawn event list
// for now, only filter by eventType until spawn information is available
let filteredEvents = []
for (const event of events.filter((x) => x.eventType === 'community-day' || x.eventType === 'pokemon-spotlight-hour')) {
filteredEvents.push(event)
}
this.spawnEvents = filteredEvents

// create filtered quest event list
// for now, only filter by eventType until field research task information is available
filteredEvents = []
for (const event of events.filter((x) => x.eventType === 'community-day')) {
filteredEvents.push(event)
}
this.questEvents = filteredEvents
} catch (err) {
this.log.error('PogoEvents: Error creating filtered spawn and quest event lists', err)
}
}

/**
Expand All @@ -54,26 +74,26 @@ class PogoEventParser {
* @returns {{reason: string, name, time}}
*/
eventChangesSpawn(startTime, disappearTime, lat, lon) {
if (!this.events) return
if (!this.spawnEvents) return

try {
const tz = geoTz.find(lat, lon)[0].toString()

for (const event of this.events.filter((x) => (x.spawns && x.spawns.length) || x.type === 'community-day' || x.type === 'spotlight-hour')) {
for (const event of this.spawnEvents) {
const eventStart = moment.tz(event.start, tz).unix()
const eventEnd = moment.tz(event.end, tz).unix()
if (startTime < eventStart && eventStart < disappearTime) {
return {
reason: 'start',
name: event.name,
time: event.start.split(' ')[1],
time: moment(event.start).format('HH:mm'),
}
}
if (startTime < eventEnd && eventEnd < disappearTime) {
return {
reason: 'end',
name: event.name,
time: event.end.split(' ')[1],
time: moment(event.end).format('HH:mm'),
}
}
}
Expand All @@ -91,26 +111,26 @@ class PogoEventParser {
* @returns {{reason: string, name, time}}
*/
eventChangesQuest(startTime, disappearTime, lat, lon) {
if (!this.events) return
if (!this.questEvents) return

try {
const tz = geoTz.find(lat, lon)[0].toString()

for (const event of this.events.filter((x) => x.has_quests)) {
for (const event of this.questEvents) {
const eventStart = moment.tz(event.start, tz).unix()
const eventEnd = moment.tz(event.end, tz).unix()
if (startTime < eventStart && eventStart < disappearTime) {
return {
reason: 'start',
name: event.name,
time: event.start.split(' ')[1],
time: moment(event.start).format('HH:mm'),
}
}
if (startTime < eventEnd && eventEnd < disappearTime) {
return {
reason: 'end',
name: event.name,
time: event.end.split(' ')[1],
time: moment(event.end).format('HH:mm'),
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/lib/poracleMessage/commands/poracle-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const stripJsonComments = require('strip-json-comments')
const fs = require('fs')
const path = require('path')
const geoTz = require('geo-tz')
const moment = require('moment-timezone')

exports.run = async (client, msg, args, options) => {
try {
Expand Down Expand Up @@ -51,14 +53,23 @@ exports.run = async (client, msg, args, options) => {

return await msg.reply(message)
}

// Create extra command argument 'disapeardt' RegEx for 'poracle-test' command.
// Added here instead of regex.js, because there is no use in other commands.
// datetime format: "YYYY-MM-DDTHH:mm:ss" e.g. 2024-08-13T23:01:00
const disapeardtReStr = '^(disapeardt):?([2][0][0-9][0-9]-([0][0-9]|[1][0-2])-([0-2][0-9]|[3][0-1])t([0-1][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9])'
const disapeardtRe = RegExp(disapeardtReStr, 'i')
let disapearTimeOverwrite = null
// check and handle additional arguments used by 'poracle-test' command
for (let i = args.length - 1; i >= 0; i--) {
if (args[i].match(client.re.templateRe)) {
[, , template] = args[i].match(client.re.templateRe)
args.splice(i, 1)
} else if (args[i].match(client.re.languageRe)) {
[, , language] = args[i].match(client.re.languageRe)
args.splice(i, 1)
} else if (args[i].match(disapeardtRe)) {
[, , disapearTimeOverwrite] = args[i].match(disapeardtRe)
args.splice(i, 1)
}
}

Expand Down Expand Up @@ -88,7 +99,15 @@ exports.run = async (client, msg, args, options) => {
// Freshen test data
switch (hookType) {
case 'pokemon': {
hook.disappear_time = Date.now() / 1000 + 10 * 60
if (disapearTimeOverwrite !== null) {
// get timezone of pokemon location
const tz = geoTz.find(hook.latitude, hook.longitude)[0].toString()
// calculate timestamp based on provided time as localtime matching to pokemon location
// disapearTimeOverwrite string was converted to lowwercase so we need to change back to upper case ('t' -> 'T')
hook.disappear_time = moment.tz(disapearTimeOverwrite.toUpperCase(), tz).unix()
} else {
hook.disappear_time = Date.now() / 1000 + 10 * 60
}
break
}
case 'raid': {
Expand Down

0 comments on commit e85712f

Please sign in to comment.