2022-03-07 20:59:34 -05:00
|
|
|
|
const servers = {};
|
2022-02-07 19:43:36 -05:00
|
|
|
|
const inDvar = 'sv_iw4madmin_in';
|
|
|
|
|
const outDvar = 'sv_iw4madmin_out';
|
2022-03-07 20:59:34 -05:00
|
|
|
|
const pollRate = 900;
|
|
|
|
|
const enableCheckTimeout = 10000;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
let logger = {};
|
2022-03-07 20:59:34 -05:00
|
|
|
|
const maxQueuedMessages = 25;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
|
|
|
|
let plugin = {
|
|
|
|
|
author: 'RaidMax',
|
2022-03-07 20:59:34 -05:00
|
|
|
|
version: 1.1,
|
2022-02-07 19:43:36 -05:00
|
|
|
|
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-03-07 20:59:34 -05:00
|
|
|
|
const eventType = String(gameEvent.TypeName).toLowerCase();
|
2022-02-15 21:05:50 -05:00
|
|
|
|
|
2022-02-12 22:54:21 -05:00
|
|
|
|
if (eventType === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-15 21:05:50 -05:00
|
|
|
|
|
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'];
|
2022-07-09 15:23:08 -04:00
|
|
|
|
sendScriptCommand(server, 'Alert', gameEvent.Origin, gameEvent.Target, {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
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 = [{
|
|
|
|
|
name: 'giveweapon',
|
|
|
|
|
description: 'gives specified weapon',
|
|
|
|
|
alias: 'gw',
|
|
|
|
|
permission: 'SeniorAdmin',
|
2022-02-15 21:05:50 -05:00
|
|
|
|
targetRequired: true,
|
2022-02-07 19:43:36 -05:00
|
|
|
|
arguments: [{
|
2022-02-15 21:05:50 -05:00
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
},
|
2022-03-07 20:59:34 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'weapon name',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-02-07 19:43:36 -05:00
|
|
|
|
execute: (gameEvent) => {
|
2022-02-18 11:15:11 -05:00
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-15 21:05:50 -05:00
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'GiveWeapon', gameEvent.Origin, gameEvent.Target, {weaponName: gameEvent.Data});
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
},
|
2022-03-07 20:59:34 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'takeweapons',
|
|
|
|
|
description: 'take all weapons from specified player',
|
|
|
|
|
alias: 'tw',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'TakeWeapons', gameEvent.Origin, gameEvent.Target, undefined);
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'switchteam',
|
|
|
|
|
description: 'switches specified player to the opposite team',
|
|
|
|
|
alias: 'st',
|
|
|
|
|
permission: 'Administrator',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'SwitchTeams', gameEvent.Origin, gameEvent.Target, undefined);
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
2022-05-19 18:04:34 -04:00
|
|
|
|
{
|
|
|
|
|
name: 'lockcontrols',
|
|
|
|
|
description: 'locks target player\'s controls',
|
|
|
|
|
alias: 'lc',
|
|
|
|
|
permission: 'Administrator',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-05-19 18:04:34 -04:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'LockControls', gameEvent.Origin, gameEvent.Target, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'noclip',
|
|
|
|
|
description: 'enable noclip on yourself ingame',
|
|
|
|
|
alias: 'nc',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
arguments: [],
|
|
|
|
|
supportedGames: ['IW4', 'IW5'],
|
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'NoClip', gameEvent.Origin, gameEvent.Origin, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-03-07 20:59:34 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'hide',
|
|
|
|
|
description: 'hide yourself ingame',
|
|
|
|
|
alias: 'hi',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
arguments: [],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Hide', gameEvent.Origin, gameEvent.Origin, undefined);
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'alert',
|
|
|
|
|
description: 'alert a player',
|
|
|
|
|
alias: 'alr',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
2022-02-07 19:43:36 -05:00
|
|
|
|
required: true
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'message',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Alert', gameEvent.Origin, gameEvent.Target, {
|
|
|
|
|
alertType: 'Alert',
|
|
|
|
|
message: gameEvent.Data
|
|
|
|
|
});
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2022-03-07 20:59:34 -05:00
|
|
|
|
name: 'gotoplayer',
|
|
|
|
|
description: 'teleport to a player',
|
|
|
|
|
alias: 'g2p',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Goto', gameEvent.Origin, gameEvent.Target, undefined);
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
},
|
2022-05-19 18:04:34 -04:00
|
|
|
|
{
|
|
|
|
|
name: 'playertome',
|
|
|
|
|
description: 'teleport a player to you',
|
|
|
|
|
alias: 'p2m',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-05-19 18:04:34 -04:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'PlayerToMe', gameEvent.Origin, gameEvent.Target, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-02-07 19:43:36 -05:00
|
|
|
|
{
|
2022-03-07 20:59:34 -05:00
|
|
|
|
name: 'goto',
|
|
|
|
|
description: 'teleport to a position',
|
|
|
|
|
alias: 'g2',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: false,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'x',
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'y',
|
|
|
|
|
required: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'z',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const args = String(gameEvent.Data).split(' ');
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Goto', gameEvent.Origin, gameEvent.Target, {
|
|
|
|
|
x: args[0],
|
|
|
|
|
y: args[1],
|
|
|
|
|
z: args[2]
|
|
|
|
|
});
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'kill',
|
|
|
|
|
description: 'kill a player',
|
|
|
|
|
alias: 'kpl',
|
|
|
|
|
permission: 'SeniorAdmin',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'Kill', gameEvent.Origin, gameEvent.Target, undefined);
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'setspectator',
|
|
|
|
|
description: 'sets a player as spectator',
|
|
|
|
|
alias: 'spec',
|
|
|
|
|
permission: 'Administrator',
|
|
|
|
|
targetRequired: true,
|
|
|
|
|
arguments: [{
|
|
|
|
|
name: 'player',
|
|
|
|
|
required: true
|
|
|
|
|
}],
|
2022-10-05 10:49:00 -04:00
|
|
|
|
supportedGames: ['IW4', 'IW5', 'T5'],
|
2022-03-07 20:59:34 -05:00
|
|
|
|
execute: (gameEvent) => {
|
|
|
|
|
if (!validateEnabled(gameEvent.Owner, gameEvent.Origin)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendScriptCommand(gameEvent.Owner, 'SetSpectator', gameEvent.Origin, gameEvent.Target, undefined);
|
2022-03-01 13:45:39 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
}];
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
const sendScriptCommand = (server, command, origin, 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-15 21:05:50 -05:00
|
|
|
|
sendEvent(server, false, 'ExecuteCommandRequested', command, origin, target, data);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
const sendEvent = (server, responseExpected, event, subtype, origin, target, data) => {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
2022-03-07 20:59:34 -05:00
|
|
|
|
const state = servers[server.EndPoint];
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
if (state.queuedMessages.length >= maxQueuedMessages) {
|
|
|
|
|
logger.WriteWarning('Too many queued messages so we are skipping');
|
|
|
|
|
return;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
let targetClientNumber = -1;
|
|
|
|
|
if (target != null) {
|
|
|
|
|
targetClientNumber = target.ClientNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const output = `${responseExpected ? '1' : '0'};${event};${subtype};${origin.ClientNumber};${targetClientNumber};${buildDataString(data)}`;
|
2022-03-07 20:59:34 -05:00
|
|
|
|
logger.WriteDebug(`Queuing output for server ${output}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
state.queuedMessages.push(output);
|
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-15 21:05:50 -05:00
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
let enabled = false;
|
|
|
|
|
try {
|
2022-03-07 20:59:34 -05:00
|
|
|
|
enabled = server.GetServerDvar('sv_iw4madmin_integration_enabled', enableCheckTimeout) === '1';
|
2022-02-07 19:43:36 -05:00
|
|
|
|
} 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-03-07 20:59:34 -05:00
|
|
|
|
servers[server.EndPoint].waitingOnInput = false;
|
|
|
|
|
servers[server.EndPoint].waitingOnOutput = false;
|
|
|
|
|
servers[server.EndPoint].queuedMessages = [];
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
setDvar(server, inDvar, '', onSetDvar);
|
|
|
|
|
setDvar(server, outDvar, '', onSetDvar);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
|
|
|
|
return true;
|
2022-03-07 20:59:34 -05:00
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
function onReceivedDvar(server, dvarName, dvarValue, success) {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
2022-03-07 20:59:34 -05:00
|
|
|
|
logger.WriteDebug(`Received ${dvarName}=${dvarValue} success=${success}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
let input = dvarValue;
|
|
|
|
|
const state = servers[server.EndPoint];
|
|
|
|
|
|
|
|
|
|
if (state.waitingOnOutput && dvarName === outDvar && isEmpty(dvarValue)) {
|
|
|
|
|
logger.WriteDebug('Setting out bus to read to send');
|
|
|
|
|
// reset our flag letting use the out bus is open
|
|
|
|
|
state.waitingOnOutput = !success;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
if (state.waitingOnInput && dvarName === inDvar) {
|
|
|
|
|
logger.WriteDebug('Setting in bus to ready to receive');
|
|
|
|
|
// we've received the data so now we can mark it as ready for more
|
|
|
|
|
state.waitingOnInput = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isEmpty(input)) {
|
2022-02-07 19:43:36 -05:00
|
|
|
|
input = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.length > 0) {
|
|
|
|
|
const event = parseEvent(input)
|
|
|
|
|
|
2022-07-13 17:10:16 -04:00
|
|
|
|
logger.WriteDebug(`Processing input... ${event.eventType} ${event.subType} ${event.data.toString()} ${event.clientNumber}`);
|
|
|
|
|
|
|
|
|
|
const metaService = _serviceResolver.ResolveService('IMetaServiceV2');
|
|
|
|
|
const threading = importNamespace('System.Threading');
|
|
|
|
|
const token = new threading.CancellationTokenSource().Token;
|
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 = [];
|
|
|
|
|
|
2022-09-21 14:04:15 -04:00
|
|
|
|
const metaService = _serviceResolver.ResolveService('IMetaServiceV2');
|
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
if (event.subType === 'Meta') {
|
2022-09-11 12:46:13 -04:00
|
|
|
|
const meta = metaService.GetPersistentMeta(event.data, client.ClientId, token).GetAwaiter().GetResult();
|
2022-02-07 19:43:36 -05:00
|
|
|
|
data[event.data] = meta === null ? '' : meta.Value;
|
2022-09-11 12:46:13 -04:00
|
|
|
|
logger.WriteDebug(`event data is ${event.data}`);
|
2022-02-07 19:43:36 -05:00
|
|
|
|
} else {
|
2022-09-21 14:04:15 -04:00
|
|
|
|
const tagMeta = metaService.GetPersistentMetaByLookup('ClientTagV2', 'ClientTagNameV2', client.ClientId, token).GetAwaiter().GetResult();
|
2022-02-07 19:43:36 -05:00
|
|
|
|
data = {
|
|
|
|
|
level: client.Level,
|
2022-02-10 17:50:45 -05:00
|
|
|
|
clientId: client.ClientId,
|
2022-09-21 14:04:15 -04:00
|
|
|
|
lastConnection: client.LastConnection,
|
2022-09-24 11:06:07 -04:00
|
|
|
|
tag: tagMeta?.Value ?? ''
|
2022-02-07 19:43:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 21:05:50 -05:00
|
|
|
|
sendEvent(server, false, 'ClientDataReceived', event.subType, client, undefined, 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-15 21:05:50 -05:00
|
|
|
|
sendEvent(server, false, 'ClientDataReceived', 'Fail', event.clientNumber, undefined, {ClientNumber: event.clientNumber});
|
2022-02-10 17:50:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.eventType === 'SetClientDataRequested') {
|
|
|
|
|
let client = server.GetClientByNumber(event.clientNumber);
|
|
|
|
|
let clientId;
|
2022-02-15 21:05:50 -05:00
|
|
|
|
|
|
|
|
|
if (client != null) {
|
2022-02-10 17:50:45 -05:00
|
|
|
|
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}`);
|
2022-02-15 21:05:50 -05:00
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, undefined, {status: 'Fail'});
|
2022-02-10 17:50:45 -05:00
|
|
|
|
} else {
|
|
|
|
|
if (event.subType === 'Meta') {
|
|
|
|
|
try {
|
2022-07-13 17:10:16 -04:00
|
|
|
|
logger.WriteDebug(`Key=${event.data['key']}, Value=${event.data['value']}, Direction=${event.data['direction']} ${token}`);
|
2022-02-10 17:50:45 -05:00
|
|
|
|
if (event.data['direction'] != null) {
|
|
|
|
|
event.data['direction'] = 'up'
|
2022-07-16 18:47:07 -04:00
|
|
|
|
? metaService.IncrementPersistentMeta(event.data['key'], parseInt(event.data['value']), clientId, token).GetAwaiter().GetResult()
|
|
|
|
|
: metaService.DecrementPersistentMeta(event.data['key'], parseInt(event.data['value']), clientId, token).GetAwaiter().GetResult();
|
2022-02-10 17:50:45 -05:00
|
|
|
|
} else {
|
2022-07-13 17:10:16 -04:00
|
|
|
|
metaService.SetPersistentMeta(event.data['key'], event.data['value'], clientId, token).GetAwaiter().GetResult();
|
2022-02-10 17:50:45 -05:00
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, undefined, {status: 'Complete'});
|
2022-02-10 17:50:45 -05:00
|
|
|
|
} catch (error) {
|
2022-03-07 20:59:34 -05:00
|
|
|
|
sendEvent(server, false, 'SetClientDataCompleted', 'Meta', {ClientNumber: event.clientNumber}, undefined, {status: 'Fail'});
|
2022-07-13 17:10:16 -04:00
|
|
|
|
logger.WriteError('Could not persist client meta ' + error.toString());
|
2022-02-10 17:50:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
setDvar(server, inDvar, '', onSetDvar);
|
2022-02-15 21:05:50 -05:00
|
|
|
|
} else if (server.ClientNum === 0) {
|
2022-02-10 17:50:45 -05:00
|
|
|
|
servers[server.EndPoint].timer.Stop();
|
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
function onSetDvar(server, dvarName, dvarValue, success) {
|
|
|
|
|
const logger = _serviceResolver.ResolveService('ILogger');
|
|
|
|
|
logger.WriteDebug(`Completed set of dvar ${dvarName}=${dvarValue}, success=${success}`);
|
|
|
|
|
|
|
|
|
|
const state = servers[server.EndPoint];
|
|
|
|
|
|
|
|
|
|
if (dvarName === inDvar && success && isEmpty(dvarValue)) {
|
|
|
|
|
logger.WriteDebug('In bus is ready for new data');
|
|
|
|
|
// reset our flag letting use the in bus is ready for more data
|
|
|
|
|
state.waitingOnInput = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pollForEvents = server => {
|
|
|
|
|
const state = servers[server.EndPoint];
|
|
|
|
|
|
|
|
|
|
if (state === null || !state.enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (server.Throttled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!state.waitingOnInput) {
|
|
|
|
|
state.waitingOnInput = true;
|
|
|
|
|
getDvar(server, inDvar, onReceivedDvar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!state.waitingOnOutput) {
|
|
|
|
|
if (state.queuedMessages.length === 0) {
|
|
|
|
|
logger.WriteDebug('No messages in queue');
|
2022-04-25 16:39:30 -04:00
|
|
|
|
return;
|
2022-03-07 20:59:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.waitingOnOutput = true;
|
|
|
|
|
const nextMessage = state.queuedMessages.splice(0, 1);
|
|
|
|
|
setDvar(server, outDvar, nextMessage, onSetDvar);
|
|
|
|
|
}
|
2022-05-19 18:04:34 -04:00
|
|
|
|
|
2022-03-07 20:59:34 -05:00
|
|
|
|
if (state.waitingOnOutput) {
|
|
|
|
|
getDvar(server, outDvar, onReceivedDvar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parseEvent = (input) => {
|
|
|
|
|
if (input === undefined) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const eventInfo = input.split(';');
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
eventType: eventInfo[1],
|
|
|
|
|
subType: eventInfo[2],
|
|
|
|
|
clientNumber: eventInfo[3],
|
|
|
|
|
data: eventInfo.length > 4 ? parseDataString(eventInfo[4]) : undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-11 12:46:13 -04:00
|
|
|
|
return Object.keys(dict).length === 0 ? data : dict;
|
2022-02-10 17:50:45 -05:00
|
|
|
|
}
|
2022-02-18 11:15:11 -05:00
|
|
|
|
|
|
|
|
|
const validateEnabled = (server, origin) => {
|
|
|
|
|
const enabled = servers[server.EndPoint] != null && servers[server.EndPoint].enabled;
|
|
|
|
|
if (!enabled) {
|
2022-03-07 20:59:34 -05:00
|
|
|
|
origin.Tell('Game interface is not enabled on this server');
|
2022-02-18 11:15:11 -05:00
|
|
|
|
}
|
|
|
|
|
return enabled;
|
|
|
|
|
}
|
2022-03-07 20:59:34 -05:00
|
|
|
|
|
|
|
|
|
function isEmpty(value) {
|
|
|
|
|
return value == null || false || value === '' || value === 'null';
|
|
|
|
|
}
|