-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathroom-claim-assistant.user.js
More file actions
169 lines (149 loc) · 6.57 KB
/
room-claim-assistant.user.js
File metadata and controls
169 lines (149 loc) · 6.57 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
165
166
167
168
169
/*jshint multistr: true */
// ==UserScript==
// @name Screeps room claim assistant
// @namespace https://screeps.com/
// @version 0.1.6
// @author James Cook
// @include https://screeps.com/a/
// @run-at document-ready
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://github.com/Esryok/screeps-browser-ext/raw/master/screeps-browser-core.js
// @downloadUrl https://github.com/Esryok/screeps-browser-ext/raw/master/room-claim-assistant.user.js
// ==/UserScript==
let roomObjectCounts = {};
function getRoomObjectCounts(shardName, roomName, callback) {
let scope = angular.element(document.body).scope();
if (roomObjectCounts[roomName]) {
callback(roomObjectCounts[roomName]);
} else {
//console.log("Bind socket event", roomName)
let eventFunc = ScreepsAdapter.Socket.bindEventToScope(scope, `roomMap2:${shardName}/${roomName}`, function(objectCounts) {
roomObjectCounts[roomName] = objectCounts;
eventFunc.remove();
// console.log("Data loaded", roomName);
callback(objectCounts);
});
}
}
var interceptingApiPost = false;
function interceptClaim0StatsRequest() {
if (interceptingApiPost) return;
interceptingApiPost = true;
let api = ScreepsAdapter.Api;
let post = api.post;
api.post = (uri, body) => {
//console.log("interceptClaim0StatsRequest", uri, body);
if (uri === "game/map-stats" && body.statName === "claim0") {
body.statName = "minerals0";
}
return post(uri, body);
}
}
function recalculateClaimOverlay() {
// $(".room-prohibited").hide();
// console.log("recalculateClaimOverlay");
let user = angular.element(document.body).scope().Me();
let mapContainerElem = angular.element($('.map-container'));
let worldMap = mapContainerElem.scope().WorldMap;
let mapSectors = $('.map-sector');
for (let i = 0; i < mapSectors.length; i++) {
let sectorElem = angular.element(mapSectors[i]);
let scope = sectorElem.scope();
let sector = scope.$parent.sector;
let roomName = sector.name;
if (roomName) {
let roomStats = worldMap.roomStats[roomName];
if (!roomStats || roomStats.status === "out of borders") {
// can't get the room objects for this, don't bother rendering anything
continue;
}
getRoomObjectCounts(worldMap.shard, roomName, (counts) => {
if (!counts) return;
if (!counts.s) {
console.log("Bad object list for". roomName, counts)
return;
}
let userOwned = (roomStats.own && roomStats.own.user === user._id);
// show minerals if:
let showMinerals =
(userOwned && roomStats.own.level > 0) || // user has claimed it OR
counts.s.length > 1; // it has 2+ sources
let state = "not-recommended";
if (userOwned && roomStats.own.level > 0) {
state = "owned";
} else if (roomStats.own && !userOwned) {
state = "prohibited";
} else if (roomStats.sign && !userOwned && roomStats.sign.user !== user._id) {
state = "signed";
} else if (counts.c.length === 0) {
state = "unclaimable";
} else if (counts.s.length >= 2 &&
(!roomStats.own || (userOwned && roomStats.own.level === 0))) {
// recommend if it has two sources and a controller, nobody else owns it,
// and user hasn't already claimed
state = "recommended";
}
let claimAssistDiv = $(sectorElem).find('.claim-assist');
if (!claimAssistDiv.length) {
claimAssistDiv = $("<div></div>");
$(sectorElem).append(claimAssistDiv);
}
let claimRoom = $(claimAssistDiv).attr("room");
if (claimRoom !== roomName) {
if (showMinerals && roomStats.minerals0) {
claimAssistDiv.html(`
<div class='room-mineral-type room-mineral-type-${roomStats.minerals0.type} room-mineral-density-${roomStats.minerals0.density}'>
${roomStats.minerals0.type}
</div>`);
} else {
claimAssistDiv.html('');
}
claimAssistDiv.attr("class", `room-stats claim-assist ${state}`);
}
$(claimAssistDiv).attr("room", roomName);
});
}
}
}
var pendingClaimRedraws = 0;
function bindMapStatsMonitor() {
let mapContainerElem = angular.element(".map-container");
let scope = mapContainerElem.scope();
let worldMap = scope.WorldMap;
let deferRecalculation = function () {
$('.claim-assist').hide();
$('.claim-assist').remove();
if (worldMap.displayOptions.layer === "claim0") {
if (worldMap.zoom === 3) {
pendingClaimRedraws++;
setTimeout(() => {
pendingClaimRedraws--;
if (pendingClaimRedraws === 0) {
recalculateClaimOverlay();
$('.claim-assist').show();
}
}, 500);
}
}
}
scope.$on("mapSectorsRecalced", deferRecalculation);
scope.$on("mapStatsUpdated", deferRecalculation);
}
// Entry point
$(document).ready(() => {
DomHelper.addStyle(`
.claim-assist { pointer-events: none; }
.claim-assist.not-recommended { background: rgba(192, 192, 50, 0.3); }
.claim-assist.recommended { background: rgba(25, 255, 25, 0.2); }
.claim-assist.owned { background: rgba(50, 50, 255, 0.2); }
.claim-assist.signed { background: rgba(255, 128, 0, 0.35); }
.claim-assist.prohibited { background: rgba(255, 50, 50, 0.2); }
.room-prohibited { display: none; }
`);
ScreepsAdapter.onViewChange(function(view) {
if (view === "worldMapEntered") {
interceptClaim0StatsRequest();
ScreepsAdapter.$timeout(bindMapStatsMonitor);
}
});
});