forked from nutki/Goko-Live-Log-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 9
/
autokick.js
186 lines (168 loc) · 7.69 KB
/
autokick.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*jslint browser: true, devel: true, indent: 4, vars: true, nomen: true, regexp: true, forin: true */
/*global $, _, Audio, GS, FS, mtgRoom */
(function () {
"use strict";
var KickCriterion, RangeKickCriterion, NameKickCriterion, BlacklistKickCriterion;
var mod = GS.modules.autokick = new GS.Module('autokick');
mod.dependencies = [
'FS.ZoneClassicHelper',
'mtgRoom.conn'
];
mod.load = function () {
var kick, kickOrNotify, self = this;
this.myProRating = null;
this.kickedOpps = [];
GS.alsoDo(FS.ZoneClassicHelper, 'onPlayerJoinTable', null, function (table, join) {
if (this.isLocalOwner(table) && !join.get('player').get('isBot')) {
var tableName = JSON.parse(table.get('settings')).name;
var opp = join.get('player');
var myId = mtgRoom.localPlayer.getId();
console.info('On Join', tableName, opp);
// A Goko race condition can cause opponent's "playerId" field
// to not be populated. Slice it out of the "playerAddress".
var oppId = opp.get('playerAddress').slice(30, 54);
// Ratings:
var proCache = mtgRoom.helpers.RatingHelper._rankingsCached;
var oppPro = proCache[oppId].ratingPro;
var myPro = proCache[myId].ratingPro;
var myIso = null, oppIso = null;
if (GS.isoLevelCache !== 'undefined') {
myIso = GS.isoLevelCache[myId];
oppIso = GS.isoLevelCache[oppId];
}
console.info('Ratings', myPro, oppPro, myIso, oppIso);
// Isotropish kick criteria
var isoCrit = new RangeKickCriterion('isotropish.com rating level',
GS.parseIsoRange(tableName), myIso, oppIso);
isoCrit.apply = GS.get_option('autokick_by_level')
&& typeof oppIso !== 'undefined'
&& oppIso !== null;
// Goko Pro kick criteria
var proCrit = new RangeKickCriterion('Pro rating',
GS.parseProRange(tableName), myPro, oppPro);
proCrit.apply = GS.get_option('autokick_by_rating');
// Player name kick criteria
var forCrit = new NameKickCriterion(tableName, opp.get('playerName'));
forCrit.apply = GS.get_option('autokick_by_forname');
// Blacklist no-play kick criteria
var blCrit = new BlacklistKickCriterion(GS.getCombinedBlacklist(),
opp.get('playerName'));
// Don't kick or notify when in Adventure/Play Bots modes
// or when the user adds a bot to his own game
var room = mtgRoom.roomList.findByRoomId(mtgRoom.currentRoomId);
var doKickNotify = !opp.get('isBot')
&& typeof room !== 'undefined'
&& room.get('name').indexOf('Private') !== 0;
// Either notify the user or kick the joiner and optionally
// explain why
if (doKickNotify) {
console.info(proCrit, isoCrit, forCrit, blCrit);
if (proCrit.shouldKick()) {
kick(table, opp, proCrit.whyKick());
} else if (isoCrit.shouldKick()) {
kick(table, opp, isoCrit.whyKick());
} else if (forCrit.shouldKick()) {
kick(table, opp, forCrit.whyKick());
} else if (blCrit.shouldKick()) {
kick(table, opp, blCrit.whyKick());
} else {
var msg = opp.get('playerName') + ' joined '
+ ' [Pro ' + proCache[oppId].ratingPro
+ ', Iso ' + oppIso + ']';
GS.notifyUser(msg, new Audio('sounds/startTurn.ogg'));
}
}
}
});
kick = function (table, joiner, whyKick) {
// Kick joiner
mtgRoom.conn.bootTable({
table: table.get('number'),
playerAddress: joiner.get('playerAddress')
});
// Explain kick
console.info('kicking', self.kickedOpps, joiner);
var oppId = joiner.get('playerAddress').slice(30, 54);
if (GS.get_option('explain_kicks')
&& !_.contains(self.kickedOpps, oppId)
&& whyKick !== null) {
var text = joiner.get('playerName') + ', ' + whyKick;
mtgRoom.conn.chat({text: text});
self.kickedOpps.push(oppId);
}
};
};
KickCriterion = function () {
this.apply = true;
};
KickCriterion.prototype.evaluate = function () {
// To be overridden by subclasses
//return { wouldKick: false, whyKick: null };
throw 'Not Implemented';
};
KickCriterion.prototype.shouldKick = function () {
return this.apply && this.evaluate().wouldKick;
};
KickCriterion.prototype.whyKick = function () {
return this.evaluate().whyKick;
};
RangeKickCriterion = function (ratingSystemName, range, myRating, oppRating) {
if (range.hasOwnProperty('difference')) {
this.min = myRating - range.difference;
this.max = myRating + range.difference;
} else {
this.min = range.min;
this.max = range.max;
}
this.oppRating = oppRating;
this.ratingSystemName = ratingSystemName;
};
RangeKickCriterion.prototype = Object.create(KickCriterion.prototype);
RangeKickCriterion.prototype.evaluate = function () {
var out = {};
if (this.oppRating === null) {
out.wouldKick = true;
if (this.min !== null) {
out.whyKick = 'my minimum ' + this.ratingSystemName + ' is ' + this.min;
}
} else if (this.min > this.oppRating) {
out.wouldKick = true;
out.whyKick = 'my minimum ' + this.ratingSystemName + ' is ' + this.min;
} else if (this.max < this.oppRating) {
out.wouldKick = true;
out.whyKick = 'my maximum ' + this.ratingSystemName + ' is ' + this.max;
}
return out;
};
NameKickCriterion = function (tableName, oppName) {
var m = tableName.toLowerCase().match(/for (.*)/);
this.forName = (m === null ? null : m[1]);
this.oppName = oppName;
};
NameKickCriterion.prototype = Object.create(KickCriterion.prototype);
NameKickCriterion.prototype.evaluate = function () {
return {
wouldKick: this.forName !== null &&
this.forName.indexOf(this.oppName.toLowerCase()) !== 0,
whyKick: "I'm waiting for " + this.forName
};
};
BlacklistKickCriterion = function (blacklist, oppName) {
this.prototype = KickCriterion.prototype;
this.noplayNames = [];
var self = this;
_.each(GS.getCombinedBlacklist(), function (blEntry, blName) {
if (blEntry.noplay) {
self.noplayNames.push(blName.toLowerCase());
}
});
this.oppName = oppName;
};
BlacklistKickCriterion.prototype = Object.create(KickCriterion.prototype);
BlacklistKickCriterion.prototype.evaluate = function () {
return {
wouldKick: _.contains(this.noplayNames, this.oppName.toLowerCase()),
whyKick: null // No explanations for blacklisted players
};
};
}());