-
Notifications
You must be signed in to change notification settings - Fork 5
/
global.js
209 lines (174 loc) · 7.68 KB
/
global.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// this script is not meant to be directly called using /ts
// this script is run every time the server starts up or script pool is updated
if(typeof ts === 'undefined') ts = { eventsRegistered: false, global: { } }; // for scripts that need stuff to persist accross runs
eval = function(js) {
Vars.mods.getScripts().runConsole('try{evalOut = ' + js + '}catch(e){evalOut = e}');
if (evalOut instanceof Error) {
throw evalOut
}
return evalOut
}
// parses arguments into an array
parseArguments = function(arg) {
function parse(val) { // eval with extra steps, i should probably get rid of this
if (val.startsWith('"') && val.endsWith('"')) return val.slice(1, val.length - 1);
if (val.startsWith("'") && val.endsWith("'")) return val.slice(1, val.length - 1);
if (Strings.canParseFloat(val)) return parseFloat(val);
if (Strings.canParseInt(val)) return parseInt(val);
if (val === "Infinity") return Infinity;
if (val === "NaN") return NaN;
if (val === "undefined") return undefined;
if (val === "null") return undefined;
if (typeof this[val] !== 'undefined') return this[val];
try {
return eval(val);
} catch(e) {
return val;
}
}
let inDoubleQuote = false;
let args = [];
let str = '';
for(let i = 0; i < arg.length; i++) {
let lastChar = i === 0 ? '' : arg[i - 1];
let nextChar = i === arg.length - 1 ? '' : arg[i + 1];
let char = arg[i];
if (char == '"' && lastChar !== '\\') inDoubleQuote = !inDoubleQuote; // check if we managed to get inside a string
if (lastChar === '"' && char !== ' ' && !inDoubleQuote) { // a string just ended and no space afterwards
args.push(parse(str))
str = '';
}
if (char === ' ' && !inDoubleQuote) { // reached a space and is not inside a string
args.push(parse(str))
str = '';
continue
}
if (nextChar === '') { // end of string
args.push(parse(str + char))
str = '';
continue;
}
str += char;
}
return args;
}
// tries to find a player, returns null if not found
// should work with a part of a name or regex
resolvePlayer = function(player) {
return Groups.player.find(boolf(p => Strings.stripColors(p.name).includes(Strings.stripColors(player)) || Strings.stripColors(p.name).match(Strings.stripColors(player))))
}
// syncs rules for every player
syncRules = function() {
Call.setRules(Vars.state.rules)
}
// force syncs a player
sync = function(player) {
function sendWorldData(p) {
if (typeof ByteArrayOutputStream == 'undefined') importPackage(java.io);
if (typeof FastDeflaterOutputStream == 'undefined') importPackage(Packages.arc.util.io)
if (typeof NetworkIO == 'undefined') importPackage(Packages.mindustry.net)
var stream = new ByteArrayOutputStream()
var def = new FastDeflaterOutputStream(stream)
NetworkIO.writeWorld(p, def);
var data = new Packets.WorldStream();
data.stream = new ByteArrayInputStream(stream.toByteArray());
p.con.sendStream(data);
}
Call.worldDataBegin(player.con);
sendWorldData(player);
}
// :groundzero:
groundzero = ""
+ "[white][#fac11b80][#fac11b][][]\n"
+ "[white][#fac11b][#fac11b80][][][]\n"
+ "[white][#0][][#fac11b][][#0][][][#fac11b][#fac11b80][][]\n"
+ "[#0][white][#fac11b][][][#fac11b][][]\n"
+ "[#0][white][#fac11b][][][#fac11b][][][#fac11b80][]\n"
+ "[#fac11b][#0][white][][white][][white][][]\n"
+ "[#fac11b80][][#0][][white][]\n"
+ "[#0][]\n"
+ "\n"
+ "\n"
+ "\n"
+ "[#fac11b80][][#fac11b80][][]\n"
+ "[#fac11b80][#fac11b][][]\n"
+ "[#fac11b80][#fac11b][][]\n"
kickpirated = function(p) {
if (["VALVE", "IGGAMES", "Igruha.org", "tuttop", "CODEX", "FreeTP.Org"].includes(Strings.stripColors(p.name).trim())) {
Call.kick(p.con, ""
+ "Mindustry is free on [royal]https://anuke.itch.io/mindustry[]\n"
+ "\n"
+ "Mindustry можно скачать бесплатно на [royal]https://anuke.itch.io/mindustry[]\n"
+ "\n"
+ groundzero
)
}
}
teamKeeper = function(player, leaving) {
if (Vars.state.rules.mode() !== Gamemode.pvp) {
ts.global.teams = { }
return
}
var current = ts.global.teams || { }
if (current[player.uuid()] && !leaving) {
// different map
if (current[player.uuid()].map !== Vars.state.map.name()) current[player.uuid()].team = player.team()
// stored team is dead
if (!current[player.uuid()].team.active()) current[player.uuid()].team = player.team()
// stored team has all the players
var teamPlayers = 0
Groups.player.each(boolf(p => p.team() === current[player.uuid()].team), cons(() => teamPlayers++))
if (teamPlayers === Groups.player.size() - 1) current[player.uuid()].team = player.team()
// update team if its not the same
if (current[player.uuid()].team !== player.team()) {
player.team(current[player.uuid()].team)
player.unit().kill() // respawn at new core
}
}
current[player.uuid()] = {
team: player.team(),
map: Vars.state.map.name()
}
ts.global.teams = current
}
waveLimit = function() {
if (Vars.state.wave > 500 && Groups.player.size() === 0 && Groups.unit.size() > 3000) {
Events.fire(new GameOverEvent(Team.sharded))
}
}
if (!ts.eventsRegistered) {
Events.on(EventType.PlayerConnect, cons(e => kickpirated(e.player)))
Events.on(EventType.PlayerJoin, cons(e => teamKeeper(e.player, false)))
Events.on(EventType.PlayerLeave, cons(e => teamKeeper(e.player, true)))
Events.on(EventType.PlayerJoin, cons(e => { // Prevent duped uuids
if (Groups.player.contains(p => p != e.player && (p.uuid() == e.player.uuid() || p.usid() == e.player.usid()))) e.player.kick(Packets.KickReason.idInUse)
}))
Events.on(EventType.WaveEvent, () => waveLimit())
Timer.schedule(() => {
if (Groups.unit.size() > 5000) {
const counts = {}
Groups.unit.each(u => {
if (counts[u.team] == null) counts[u.team] = 0
counts[u.team]++
})
let highest = 0
let highestTeam = ''
for (let i = 0; i < Object.keys(counts).length; i++) {
const team = Object.keys(counts)[i]
if (counts[team] > highest) {
highestTeam = team
highest = counts[team]
}
}
let killCount = 0
Groups.unit.each(u => u.team == highestTeam, u => {
if (killCount < 100) {
u.kill()
killCount++
}
})
}
}, 0, 5)
ts.eventsRegistered = true
}
"[scarlet]This script is not meant to be run directly."