2022-02-07 19:43:36 -05:00
|
|
|
|
const eventTypes = {
|
|
|
|
|
1: 'start', // a server started being monitored
|
|
|
|
|
6: 'disconnect', // a client detected a leaving the game
|
|
|
|
|
9: 'preconnect', // client detected as joining via log or status
|
|
|
|
|
101: 'warn' // client was warned
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const servers = {};
|
|
|
|
|
const inDvar = 'sv_iw4madmin_in';
|
|
|
|
|
const outDvar = 'sv_iw4madmin_out';
|
2022-02-09 15:45:28 -05:00
|
|
|
|
const pollRate = 750;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
let logger = {};
|
|
|
|
|
|
|
|
|
|
let plugin = {
|
|
|
|
|
author: 'RaidMax',
|
|
|
|
|
version: 1.0,
|
|
|
|
|
name: 'Game Interface',
|
|
|
|
|
|
|
|
|
|
onEventAsync: (gameEvent, server) => {
|
2022-02-07 23:02:50 -05:00
|
|
|
|
if (servers[server.EndPoint] != null && !servers[server.EndPoint].enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 22:54:21 -05:00
|
|
|
|
const eventType = eventTypes[gameEvent.Type];
|
|
|
|
|
|
|
|
|
|
if (eventType === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
switch (eventType) {
|
|
|
|
|
case 'start':
|
2022-02-07 23:02:50 -05:00
|
|
|
|
const enabled = initialize(server);
|
2022-02-08 13:03:55 -05:00
|
|
|
|
|
2022-02-07 23:02:50 -05:00
|
|
|
|
if (!enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
break;
|
|
|
|
|
case 'preconnect':
|
|
|
|
|
// when the plugin is reloaded after the servers are started
|
2022-02-13 22:38:40 -05:00
|
|
|
|
if (servers[server.EndPoint] === undefined || servers[server.EndPoint] == null) {
|
2022-02-07 23:02:50 -05:00
|
|
|
|
const enabled = initialize(server);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-02-07 23:02:50 -05:00
|
|
|
|
if (!enabled) {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const timer = servers[server.EndPoint].timer;
|
|
|
|
|
if (!timer.IsRunning) {
|
|
|
|
|
timer.Start(0, pollRate);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'warn':
|
|
|
|
|
const warningTitle = _localization.LocalizationIndex['GLOBAL_WARNING'];
|
|
|
|
|
sendScriptCommand(server, 'Alert', gameEvent.Target, {
|
|
|
|
|
alertType: warningTitle + '!',
|
|
|
|
|
message: gameEvent.Data
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLoadAsync: manager => {
|
|
|
|
|
logger = _serviceResolver.ResolveService('ILogger');
|
|
|
|
|
logger.WriteInfo('Game Interface Startup');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onUnloadAsync: () => {
|
|
|
|
|
for (let i = 0; i < servers.length; i++) {
|
2022-02-09 15:45:28 -05:00
|
|
|
|
if (servers[i].enabled) {
|
|
|
|
|
servers[i].timer.Stop();
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onTickAsync: server => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let commands = [{
|
|
|
|
|
// required
|
|
|
|
|
name: 'giveweapon',
|
|
|
|
|
// required
|
|
|
|
|
description: 'gives specified weapon',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'gw',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'weapon name',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'GiveWeapon', gameEvent.Origin, {weaponName: gameEvent.Data});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// required
|
|
|
|
|
name: 'takeweapons',
|
|
|
|
|
// required
|
|
|
|
|
description: 'take all weapons from specifies player',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'tw',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [],
|
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'TakeWeapons', gameEvent.Target, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// required
|
|
|
|
|
name: 'switchteam',
|
|
|
|
|
// required
|
|
|
|
|
description: 'switches specified player to the opposite team',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'st',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'Administrator',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'SwitchTeams', gameEvent.Target, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// required
|
|
|
|
|
name: 'hide',
|
|
|
|
|
// required
|
|
|
|
|
description: 'hide yourself',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'hi',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [],
|
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Hide', gameEvent.Origin, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// required
|
|
|
|
|
name: 'unhide',
|
|
|
|
|
// required
|
|
|
|
|
description: 'unhide yourself',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'unh',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [],
|
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Unhide', gameEvent.Origin, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// required
|
|
|
|
|
name: 'alert',
|
|
|
|
|
// required
|
|
|
|
|
description: 'alert a player',
|
|
|
|
|
// required
|
|
|
|
|
alias: 'alr',
|
|
|
|
|
// required
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
// optional (defaults to false)
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
// optional
|
|
|
|
|
arguments: [{
|
2022-02-08 13:03:55 -05:00
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
},
|
2022-02-10 17:50:45 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'message',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-02-07 19:43:36 -05:00
|
|
|
|
supportedGames: ['IW4'],
|
|
|
|
|
// required
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Alert', gameEvent.Target, {
|
|
|
|
|
alertType: 'Alert',
|
|
|
|
|
message: gameEvent.Data
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
const sendScriptCommand = (server, command, target, data) => {
|
2022-02-08 13:03:55 -05:00
|
|
|
|
const state = servers[server.EndPoint];
|
|
|
|
|
if (state === undefined || !state.enabled) {
|
|
|
|
|
return;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
2022-02-08 13:03:55 -05:00
|
|
|
|
sendEvent(server, false, 'ExecuteCommandRequested', command, target, data);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendEvent = (server, responseExpected, event, subtype, client, data) => {
|
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
|
|
|
|
|
|
|
|
|
let pendingOut = true;
|
|
|
|
|
let pendingCheckCount = 0;
|
2022-02-09 15:45:28 -05:00
|
|
|
|
const start = new Date();
|
2022-02-10 17:50:45 -05:00
|
|
|
|
|
2022-02-13 22:38:40 -05:00
|
|
|
|
while (pendingOut && pendingCheckCount <= 10) {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
try {
|
2022-02-09 15:45:28 -05:00
|
|
|
|
const out = server.GetServerDvar(outDvar);
|
2022-02-10 17:50:45 -05:00
|
|
|
|
pendingOut = !(out == null || out === '' || out === 'null');
|
2022-02-07 19:43:36 -05:00
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could not check server output dvar for IO status ${error}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pendingOut) {
|
|
|
|
|
logger.WriteDebug('Waiting for event bus to be cleared');
|
|
|
|
|
System.Threading.Tasks.Task.Delay(1000).Wait();
|
|
|
|
|
}
|
|
|
|
|
pendingCheckCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 15:45:28 -05:00
|
|
|
|
if (pendingOut) {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
logger.WriteWarning(`Reached maximum attempts waiting for output to be available for ${server.EndPoint}`)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 15:45:28 -05:00
|
|
|
|
const output = `${responseExpected ? '1' : '0'};${event};${subtype};${client.ClientNumber};${buildDataString(data)}`;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
logger.WriteDebug(`Sending output to server ${output}`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
server.SetServerDvar(outDvar, output);
|
2022-02-09 15:45:28 -05:00
|
|
|
|
logger.WriteDebug(`SendEvent took ${(new Date() - start) / 1000}ms`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could not set server output dvar ${error}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const parseEvent = (input) => {
|
|
|
|
|
if (input === undefined) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const eventInfo = input.split(';');
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
eventType: eventInfo[1],
|
|
|
|
|
subType: eventInfo[2],
|
|
|
|
|
clientNumber: eventInfo[3],
|
2022-02-10 17:50:45 -05:00
|
|
|
|
data: eventInfo.length > 4 ? parseDataString(eventInfo[4]) : undefined
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const initialize = (server) => {
|
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
2022-02-12 22:54:21 -05:00
|
|
|
|
|
|
|
|
|
servers[server.EndPoint] = {
|
|
|
|
|
enabled: false
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
let enabled = false;
|
|
|
|
|
try {
|
|
|
|
|
enabled = server.GetServerDvar('sv_iw4madmin_integration_enabled') === '1';
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could not get integration status of ${server.EndPoint} - ${error}`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 13:03:55 -05:00
|
|
|
|
logger.WriteInfo(`GSC Integration enabled = ${enabled}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.WriteDebug(`Setting up bus timer for ${server.EndPoint}`);
|
|
|
|
|
|
2022-02-09 15:45:28 -05:00
|
|
|
|
let timer = _serviceResolver.ResolveService('IScriptPluginTimerHelper');
|
|
|
|
|
timer.OnTick(() => pollForEvents(server), `GameEventPoller ${server.ToString()}`);
|
|
|
|
|
// necessary to prevent multi-threaded access to the JS context
|
|
|
|
|
timer.SetDependency(_lock);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-02-12 22:54:21 -05:00
|
|
|
|
servers[server.EndPoint].timer = timer;
|
|
|
|
|
servers[server.EndPoint].enabled = true;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
server.SetServerDvar(inDvar, '');
|
|
|
|
|
server.SetServerDvar(outDvar, '');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could set default values bus dvars for ${server.EndPoint} - ${error}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const pollForEvents = server => {
|
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
|
|
|
|
|
|
|
|
|
let input;
|
|
|
|
|
try {
|
|
|
|
|
input = server.GetServerDvar(inDvar);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could not get input bus value for ${server.EndPoint} - ${error}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input === undefined || input === null || input === 'null') {
|
|
|
|
|
input = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.length > 0) {
|
|
|
|
|
const event = parseEvent(input)
|
|
|
|
|
|
2022-02-08 13:03:55 -05:00
|
|
|
|
logger.WriteDebug(`Processing input... ${event.eventType} ${event.subType} ${event.data} ${event.clientNumber}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-02-10 17:50:45 -05:00
|
|
|
|
// todo: refactor to mapping if possible
|
2022-02-07 19:43:36 -05:00
|
|
|
|
if (event.eventType === 'ClientDataRequested') {
|
|
|
|
|
const client = server.GetClientByNumber(event.clientNumber);
|
|
|
|
|
|
|
|
|
|
if (client != null) {
|
2022-02-08 13:03:55 -05:00
|
|
|
|
logger.WriteDebug(`Found client ${client.Name}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
|
|
|
|
let data = [];
|
|
|
|
|
|
|
|
|
|
if (event.subType === 'Meta') {
|
|
|
|
|
const metaService = _serviceResolver.ResolveService('IMetaService');
|
2022-02-11 16:33:05 -05:00
|
|
|
|
const meta = metaService.GetPersistentMeta(event.data, client).GetAwaiter().GetResult();
|
2022-02-07 19:43:36 -05:00
|
|
|
|
data[event.data] = meta === null ? '' : meta.Value;
|
|
|
|
|
} else {
|
|
|
|
|
data = {
|
|
|
|
|
level: client.Level,
|
2022-02-10 17:50:45 -05:00
|
|
|
|
clientId: client.ClientId,
|
2022-02-07 19:43:36 -05:00
|
|
|
|
lastConnection: client.LastConnection
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendEvent(server, false, 'ClientDataReceived', event.subType, client, data);
|
2022-02-08 13:03:55 -05:00
|
|
|
|
} else {
|
|
|
|
|
logger.WriteWarning(`Could not find client slot ${event.clientNumber} when processing ${event.eventType}`);
|
2022-02-10 17:50:45 -05:00
|
|
|
|
sendEvent(server, false, 'ClientDataReceived', 'Fail', {ClientNumber: event.clientNumber}, undefined);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.eventType === 'SetClientDataRequested') {
|
|
|
|
|
let client = server.GetClientByNumber(event.clientNumber);
|
|
|
|
|
let clientId;
|
|
|
|
|
|
|
|
|
|
if (client != null){
|
|
|
|
|
clientId = client.ClientId;
|
|
|
|
|
} else {
|
|
|
|
|
clientId = parseInt(event.data.clientId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.WriteDebug(`ClientId=${clientId}`);
|
|
|
|
|
|
|
|
|
|
if (clientId == null) {
|
|
|
|
|
logger.WriteWarning(`Could not find client slot ${event.clientNumber} when processing ${event.eventType}`);
|
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, {status: 'Fail'});
|
|
|
|
|
} else {
|
|
|
|
|
if (event.subType === 'Meta') {
|
|
|
|
|
const metaService = _serviceResolver.ResolveService('IMetaService');
|
|
|
|
|
try {
|
|
|
|
|
logger.WriteDebug(`Key=${event.data['key']}, Value=${event.data['value']}`);
|
|
|
|
|
if (event.data['direction'] != null) {
|
|
|
|
|
event.data['direction'] = 'up'
|
|
|
|
|
? metaService.IncrementPersistentMeta(event.data['key'], event.data['value'], clientId).GetAwaiter().GetResult()
|
|
|
|
|
: metaService.DecrementPersistentMeta(event.data['key'], event.data['value'], clientId).GetAwaiter().GetResult();
|
|
|
|
|
} else {
|
|
|
|
|
metaService.SetPersistentMeta(event.data['key'], event.data['value'], clientId).GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, {status: 'Complete'});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, {status: 'Fail'});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
server.SetServerDvar(inDvar, '');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.WriteError(`Could not reset in bus value for ${server.EndPoint} - ${error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 17:50:45 -05:00
|
|
|
|
else if (server.ClientNum === 0) {
|
|
|
|
|
servers[server.EndPoint].timer.Stop();
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buildDataString = data => {
|
|
|
|
|
if (data === undefined) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let formattedData = '';
|
|
|
|
|
|
|
|
|
|
for (const prop in data) {
|
|
|
|
|
formattedData += `${prop}=${data[prop]}|`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formattedData.substring(0, Math.max(0, formattedData.length - 1));
|
|
|
|
|
}
|
2022-02-10 17:50:45 -05:00
|
|
|
|
|
|
|
|
|
const parseDataString = data => {
|
|
|
|
|
if (data === undefined) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dict = {}
|
|
|
|
|
|
|
|
|
|
for (const segment of data.split('|')) {
|
|
|
|
|
const keyValue = segment.split('=');
|
|
|
|
|
if (keyValue.length !== 2) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
dict[keyValue[0]] = keyValue[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict.length === 0 ? data : dict;
|
|
|
|
|
}
|