-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventDispatcher.js
86 lines (77 loc) · 2 KB
/
EventDispatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict'
/**
* Dependencies
*/
const firebase = require('firebase')
const cwd = process.cwd()
const path = require('path')
const Logger = require('./Logger')
const handlers = {
kill: require(path.join(cwd, 'handlers', 'KillHandler')),
revive: require(path.join(cwd, 'handlers', 'ReviveHandler')),
redeem: require(path.join(cwd, 'handlers', 'RedeemHandler')),
ticket: require(path.join(cwd, 'handlers', 'TicketHandler')),
mark_hvt: require(path.join(cwd, 'handlers', 'MarkHVTHandler')),
distress: require(path.join(cwd, 'handlers', 'DistressHandler')),
horde: require(path.join(cwd, 'handlers', 'HordeHandler')),
announce: require(path.join(cwd, 'handlers', 'AnnounceHandler')),
register: require(path.join(cwd, 'handlers', 'RegisterHandler')),
}
/**
* Scope
*/
const scope = 'EventDispatcher'
/**
* EventDispatcher
*/
class EventDispatcher {
static getRef (event) {
return firebase
.database()
.ref()
.child('events')
.child(event)
}
static init () {
let dispatcher = new EventDispatcher()
firebase.database().ref().child('events').orderByChild('processed').endAt(null)
.on('child_added', function (snapshot) {
let event = snapshot.val()
if (!event.processed) {
dispatcher.dispatch(event)
}
})
return dispatcher
}
dispatch (event) {
if (handlers[event.type]) {
handlers[event.type].handle(event)
} else {
this.unrecognised({event})
}
}
error (err) {
let {scope, message, generic} = err
let generic_string = generic ? '[GENERIC]' : ''
console.error(`[${scope}]${generic_string} ${message}`)
}
unrecognised (err) {
let {event} = err
if (event && event.id && event.type) {
return error({
scope,
message: `Event ${event.type} (${event.id}) unrecognised`
})
} else {
return error({
scope,
generic: true,
message: 'Event unrecognised'
})
}
}
}
/**
* Exports
*/
module.exports = EventDispatcher