Skip to content

Commit

Permalink
Merge pull request #256 from ect0s/LogTickets
Browse files Browse the repository at this point in the history
Improve round end information, and add DiscordRoundEnded plugin
  • Loading branch information
Thomas-Smyth authored Jan 2, 2023
2 parents 10efb9c + fea5b0b commit 2f0cd85
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,29 @@ Grafana:
<pre><code>16761867</code></pre></li></ul>
</details>

<details>
<summary>DiscordRoundEnded</summary>
<h2>DiscordRoundEnded</h2>
<p>The <code>DiscordRoundEnded</code> plugin will send the round winner to a Discord channel.</p>
<h3>Options</h3>
<ul><li><h4>discordClient (Required)</h4>
<h6>Description</h6>
<p>Discord connector name.</p>
<h6>Default</h6>
<pre><code>discord</code></pre></li>
<li><h4>channelID (Required)</h4>
<h6>Description</h6>
<p>The ID of the channel to log round end events to.</p>
<h6>Default</h6>
<pre><code></code></pre></li><h6>Example</h6>
<pre><code>667741905228136459</code></pre>
<li><h4>color</h4>
<h6>Description</h6>
<p>The color of the embed.</p>
<h6>Default</h6>
<pre><code>16761867</code></pre></li></ul>
</details>

<details>
<summary>DiscordServerStatus</summary>
<h2>DiscordServerStatus</h2>
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@
"channelID": "",
"color": 16761867
},
{
"plugin": "DiscordRoundEnded",
"enabled": true,
"discordClient": "discord",
"channelID": "",
"color": 16761867
},
{
"plugin": "DiscordServerStatus",
"enabled": true,
Expand Down
4 changes: 4 additions & 0 deletions squad-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export default class SquadServer extends EventEmitter {
this.emit('PLAYER_UNPOSSESS', data);
});

this.logParser.on('ROUND_ENDED', async (data) => {
this.emit('ROUND_ENDED', data);
});

this.logParser.on('TICK_RATE', (data) => {
this.emit('TICK_RATE', data);
});
Expand Down
4 changes: 4 additions & 0 deletions squad-server/log-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import PlayerPossess from './player-possess.js';
import PlayerRevived from './player-revived.js';
import PlayerUnPossess from './player-un-possess.js';
import PlayerWounded from './player-wounded.js';
import RoundEnded from './round-ended.js';
import RoundTickets from './round-tickets.js';
import RoundWinner from './round-winner.js';
import ServerTickRate from './server-tick-rate.js';
import SteamIDConnected from './steamid-connected.js';
Expand All @@ -34,6 +36,8 @@ export default class SquadLogParser extends LogParser {
PlayerRevived,
PlayerUnPossess,
PlayerWounded,
RoundEnded,
RoundTickets,
RoundWinner,
ServerTickRate,
SteamIDConnected,
Expand Down
21 changes: 21 additions & 0 deletions squad-server/log-parser/round-ended.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Matches when Map state Changes to PostMatch (ScoreBoard)
*
* Emits winner and loser from eventstore
*
* winner and loser may be null if the match ends with a draw
*/
export default {
regex:
/^\[([0-9.:-]+)]\[([ 0-9]*)]LogGameState: Match State Changed from InProgress to WaitingPostMatch/,
onMatch: (args, logParser) => {
const data = {
winner: logParser.eventStore.ROUND_WINNER ? logParser.eventStore.ROUND_WINNER : null,
loser: logParser.eventStore.ROUND_LOSER ? logParser.eventStore.ROUND_LOSER : null,
time: args[1]
};
logParser.emit('ROUND_ENDED', data);
delete logParser.eventStore.ROUND_WINNER;
delete logParser.eventStore.ROUND_LOSER;
}
};
28 changes: 28 additions & 0 deletions squad-server/log-parser/round-tickets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Matches when tickets appear in the log
*
* Will not match on Draw or Map Changes before the game has started
*/
export default {
regex:
/^\[([0-9.:-]+)]\[([ 0-9]*)]LogSquadGameEvents: Display: Team ([0-9]), (.*) \( ?(.*?) ?\) has (won|lost) the match with ([0-9]+) Tickets on layer (.*) \(level (.*)\)!/,
onMatch: (args, logParser) => {
const data = {
raw: args[0],
time: args[1],
chainID: args[2],
team: args[3],
subfaction: args[4],
faction: args[5],
action: args[6],
tickets: args[7],
layer: args[8],
level: args[9]
};
if (data.action === 'won') {
logParser.eventStore.ROUND_WINNER = data;
} else {
logParser.eventStore.ROUND_LOSER = data;
}
}
};
79 changes: 79 additions & 0 deletions squad-server/plugins/discord-roundended.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import DiscordBasePlugin from './discord-base-plugin.js';

export default class DiscordRoundEnded extends DiscordBasePlugin {
static get description() {
return 'The <code>DiscordRoundEnded</code> plugin will send the round winner to a Discord channel.';
}

static get defaultEnabled() {
return true;
}

static get optionsSpecification() {
return {
...DiscordBasePlugin.optionsSpecification,
channelID: {
required: true,
description: 'The ID of the channel to log round end events to.',
default: '',
example: '667741905228136459'
},
color: {
required: false,
description: 'The color of the embed.',
default: 16761867
}
};
}

constructor(server, options, connectors) {
super(server, options, connectors);

this.onRoundEnd = this.onRoundEnd.bind(this);
}

async mount() {
this.server.on('ROUND_ENDED', this.onRoundEnd);
}

async unmount() {
this.server.removeEventListener('ROUND_ENDED', this.onRoundEnd);
}

async onRoundEnd(info) {
if (!info.winner || !info.loser) {
await this.sendDiscordMessage({
embed: {
title: 'Round Ended',
description: 'This match Ended in a Draw',
color: this.options.color,
timestamp: info.time.toISOString()
}
});
return;
}

await this.sendDiscordMessage({
embed: {
title: 'Round Ended',
description: `${info.winner.layer} - ${info.winner.level}`,
color: this.options.color,
fields: [
{
name: `Team ${info.winner.team} Won`,
value: `${info.winner.subfaction}\n ${info.winner.faction}\n won with ${info.winner.tickets} tickets.`
},
{
name: `Team ${info.loser.team} Lost`,
value: `${info.loser.subfaction}\n ${info.loser.faction}\n lost with ${info.loser.tickets} tickets.`
},
{
name: 'Ticket Difference',
value: `${info.winner.tickets - info.loser.tickets}.`
}
],
timestamp: info.time.toISOString()
}
});
}
}

0 comments on commit 2f0cd85

Please sign in to comment.