1 | 'use strict'
|
2 |
|
3 | const EventEmitter = require('events').EventEmitter
|
4 |
|
5 | const User = require('./user')
|
6 |
|
7 | class Brain extends EventEmitter {
|
8 |
|
9 |
|
10 |
|
11 | constructor (robot) {
|
12 | super()
|
13 | this.data = {
|
14 | users: {},
|
15 | _private: {}
|
16 | }
|
17 |
|
18 | this.autoSave = true
|
19 |
|
20 | robot.on('running', () => {
|
21 | this.resetSaveInterval(5)
|
22 | })
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | set (key, value) {
|
30 | let pair
|
31 | if (key === Object(key)) {
|
32 | pair = key
|
33 | } else {
|
34 | pair = {}
|
35 | pair[key] = value
|
36 | }
|
37 |
|
38 | Object.keys(pair).forEach((key) => {
|
39 | this.data._private[key] = pair[key]
|
40 | })
|
41 |
|
42 | this.emit('loaded', this.data)
|
43 |
|
44 | return this
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | get (key) {
|
52 | return this.data._private[key] != null ? this.data._private[key] : null
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | remove (key) {
|
60 | if (this.data._private[key] != null) {
|
61 | delete this.data._private[key]
|
62 | }
|
63 |
|
64 | return this
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | save () {
|
72 | this.emit('save', this.data)
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | close () {
|
79 | clearInterval(this.saveInterval)
|
80 | this.save()
|
81 | this.emit('close')
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | setAutoSave (enabled) {
|
90 | this.autoSave = enabled
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | resetSaveInterval (seconds) {
|
99 | if (this.saveInterval) {
|
100 | clearInterval(this.saveInterval)
|
101 | }
|
102 | this.saveInterval = setInterval(() => {
|
103 | if (this.autoSave) {
|
104 | this.save()
|
105 | }
|
106 | }, seconds * 1000)
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | mergeData (data) {
|
115 | for (let k in data || {}) {
|
116 | this.data[k] = data[k]
|
117 | }
|
118 |
|
119 | this.emit('loaded', this.data)
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | users () {
|
126 | return this.data.users
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | userForId (id, options) {
|
133 | let user = this.data.users[id]
|
134 |
|
135 | if (!user) {
|
136 | user = new User(id, options)
|
137 | this.data.users[id] = user
|
138 | }
|
139 |
|
140 | if (options && options.room && (!user.room || user.room !== options.room)) {
|
141 | user = new User(id, options)
|
142 | this.data.users[id] = user
|
143 | }
|
144 |
|
145 | return user
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | userForName (name) {
|
152 | let result = null
|
153 | const lowerName = name.toLowerCase()
|
154 |
|
155 | for (let k in this.data.users || {}) {
|
156 | const userName = this.data.users[k]['name']
|
157 | if (userName != null && userName.toString().toLowerCase() === lowerName) {
|
158 | result = this.data.users[k]
|
159 | }
|
160 | }
|
161 |
|
162 | return result
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | usersForRawFuzzyName (fuzzyName) {
|
171 | const lowerFuzzyName = fuzzyName.toLowerCase()
|
172 |
|
173 | const users = this.data.users || {}
|
174 |
|
175 | return Object.keys(users).reduce((result, key) => {
|
176 | const user = users[key]
|
177 | if (user.name.toLowerCase().lastIndexOf(lowerFuzzyName, 0) === 0) {
|
178 | result.push(user)
|
179 | }
|
180 | return result
|
181 | }, [])
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | usersForFuzzyName (fuzzyName) {
|
190 | const matchedUsers = this.usersForRawFuzzyName(fuzzyName)
|
191 | const lowerFuzzyName = fuzzyName.toLowerCase()
|
192 | const fuzzyMatchedUsers = matchedUsers.filter(user => user.name.toLowerCase() === lowerFuzzyName)
|
193 |
|
194 | return fuzzyMatchedUsers.length > 0 ? fuzzyMatchedUsers : matchedUsers
|
195 | }
|
196 | }
|
197 |
|
198 | module.exports = Brain
|