forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.reputation.js
108 lines (102 loc) · 3.48 KB
/
task.reputation.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
const NPC = {
["Source Keeper"]: true,
["Invader"]: true,
};
const CONST = {
MY_SCORE: 1000,
WHITELIST_SCORE: 200,
ALLY: 100,
NEUTRAL: 1,
NPC_SCORE: -200,
};
const mod = {
name: 'reputation',
myName: () => _.chain(Game.rooms).map('controller').flatten().filter('my').map('owner.username').first().value(),
isNPC: username => NPC[username] === true,
npcOwner: creep => creep.owner && mod.isNPC(creep.owner.username),
isAlly: username => mod.score(username) >= CONST.ALLY,
notAlly: username => !mod.isAlly(username),
allyOwner: creep => creep.owner && mod.isAlly(creep.owner.username),
isHostile: username => mod.score(username) < CONST.NEUTRAL,
notHostile: username => !mod.isHostile(username),
hostileOwner: creep => creep.owner && mod.isHostile(creep.owner.username),
whitelist: () => mod.cache('whitelist'),
score: username => {
const reps = mod.cache('score');
if( username === undefined ) {
return reps;
}
const name = username && username.toLowerCase();
if( reps[name] ) {
return reps[name];
} else {
return reps[name] = 0;
}
},
setScore: (username, score) => {
const name = username && username.toLowerCase();
mod.score()[name] = score;
mod.playerMemory(name).score = score;
},
flush: () => {
mod._loadWhitelist();
mod._loadScore();
},
cache: table => Task.cache(mod.name, table),
killScoreCache: () => {
Task.clearCache(mod.name, 'score');
return mod.score();
},
killWhitelistCache: () => {
Task.clearCache(mod.name, 'score');
Task.clearCache(mod.name, 'whitelist');
return mod.whitelist();
},
memory: table => Task.memory(mod.name, table),
playerMemory: username => {
const playerMemory = mod.memory('players');
const name = username && username.toLowerCase();
if( playerMemory[name] ) {
return playerMemory[name];
} else {
return playerMemory[name] = {};
}
},
_loadScore: () => {
const etc = mod.cache('etc');
const playerMemory = mod.memory('players');
const whitelist = mod.whitelist();
let score = mod.score();
if( _.keys(playerMemory).length + _.keys(whitelist).length
!== _.keys(score).length + etc.whitelistRepUnion) {
score = mod.killScoreCache();
for( const n in NPC ) {
score[n] = CONST.NPC_SCORE;
}
_.keys(whitelist).forEach(function(player) {
score[player] = CONST.WHITELIST_SCORE;
});
etc.whitelistRepUnion = 0;
_.reduce(playerMemory, function(list, player, name) {
if( typeof player.score === "number" ) {
if( whitelist[name] ) {
etc.whitelistRepUnion++;
}
list[name] = player.score;
}
return list;
}, score);
mod.setScore(mod.myName(), CONST.MY_SCORE);
}
},
_loadWhitelist: () => {
let whitelist = mod.whitelist();
if( _.keys(whitelist).length !== PLAYER_WHITELIST.length ) {
whitelist = mod.killWhitelistCache();
_.forEach(PLAYER_WHITELIST, function(playerName) {
whitelist[playerName.toLowerCase()] = true;
});
}
},
};
module.exports = mod;