-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectUserList.js
More file actions
167 lines (116 loc) · 4.24 KB
/
Copy pathObjectUserList.js
File metadata and controls
167 lines (116 loc) · 4.24 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Created with JetBrains WebStorm
* User: brandonxavier
* Date: 7/14/13
*
Copyright 2013 Brandon Xavier (brandonxavier421@gmail.com)
This file is part of UserList.
UserList is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UserList is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UserList. If not, see <http://www.gnu.org/licenses/>.
*/
/**
This defines an extensible object that can be used
to maintain a list of users and some interesting
info about them - nothing too cloak-n-dagger - just
stuff like login time, recent tipping history, etc.
*/
function ObjectUserList() {
this.list = [];
this.add = add;
this.remove = remove;
this.get = get;
this.sortBy = sortBy;
this.tipped = tipped;
this.message = message;
// From http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects
this.sort_by = function(field, reverse, primer){
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];
}
};
function add (userToAdd, delay) {
if (delay == null){
delay = 0;
}
var t = this.get(userToAdd['user']);
if ( t == null ) {
this.list.push(new ObjectUser(userToAdd));
} else { // User exists, so update some stuff
var d = new Date();
if ( this.away && (d.getTime() - t.firstSeen.getTime())/1000 >= delay ){
this.list.splice(this.list.indexOf(t),1);
this.list.push(new ObjectUser(userToAdd)); // yeah, yeah, this could be done in the splice
} else {
t.away = false; // User must not be away
t.in_fanclub = userToAdd['in_fanclub'];
t.has_tokens = userToAdd['has_tokens'];
t.is_mod = userToAdd['is_mod'];
t.tipped_recently = userToAdd['tipped_recently'];
}
}
}
function remove(userToDelete){
var t = this.get(userToDelete['user']);
if (t != null ) {
t.away = true;
}
}
function get(searchFor) {
var i = 0;
while ( i < this.list.length && this.list[i].name != searchFor ){
i++;
}
return ( i < this.list.length ? this.list[i] : null );
}
function sortBy(sortKey, ascending, primer) {
this.list.sort(this.sort_by(sortKey, ascending, primer));
}
function tipped(tip) {
var t = this.get(tip['from_user']);
t.away = false;
t.in_fanclub = tip['from_user_in_fanclub'];
t.has_tokens = tip['from_user_has_tokens'];
t.is_mod = tip['from_user_is_mod'];
t.tipped_recently = tip['from_user_tipped_recently'];
t.tipCount++;
t.tipTotal += tip['amount'];
t.tipAvg = t.tipTotal / t.tipCount;
}
function message(msg) {
var t = this.get(msg['user']);
t.away = false; // User must not be away
t.in_fanclub = msg['in_fanclub'];
t.has_tokens = msg['has_tokens'];
t.is_mod = msg['is_mod'];
t.tipped_recently = msg['tipped_recently'];
t.chatCount++;
t.lastChat = msg['m'];
}
} // End of ObjectUserList
function ObjectUser (user) {
// Standard vars passed from CB
this.name = user['user'];
this.in_fanclub = user['in_fanclub'];
this.has_tokens = user['has_tokens'];
this.is_mod = user['is_mod'];
this.tipped_recently = user['tipped_recently'];
this.gender = user['gender'];
// Stuff that IMHO could be handy
this.firstSeen = new Date();
this.away = false;
this.tipCount = 0;
this.tipTotal = 0;
this.tipAvg = 0;
this.chatCount = 0;
this.lastChat = "";
} // End of ObjectUser