Compare commits

..

4 Commits

Author SHA1 Message Date
ea776fd93f stop stopwatch 2023-12-01 11:16:56 +00:00
0cc63a2b75 add messages example 2023-12-01 10:47:33 +00:00
396813114b upd regex.
values with colour codes now returned
2023-12-01 09:46:27 +00:00
22d036cbab upd function names, use approved verbs 2023-12-01 09:35:37 +00:00
2 changed files with 62 additions and 23 deletions

View File

@ -12,7 +12,7 @@ $CAB = New-Object System.Windows.Forms.Button
$Lbl = New-Object System.Windows.Forms.Label $Lbl = New-Object System.Windows.Forms.Label
$RLbl = New-Object System.Windows.Forms.Label $RLbl = New-Object System.Windows.Forms.Label
Function InitForm { Function New-Form {
$form = New-Object System.Windows.Forms.Form $form = New-Object System.Windows.Forms.Form
$form.Text = "Q3Rcon Client" $form.Text = "Q3Rcon Client"
$form.Size = New-Object System.Drawing.Size(275, 200) $form.Size = New-Object System.Drawing.Size(275, 200)
@ -20,17 +20,17 @@ Function InitForm {
return $form return $form
} }
Function AddOkButton { Function Add-OkButton {
param($form, $rcon) param($form, $rcon)
$OKB.Location = New-Object System.Drawing.Size(65, 100) $OKB.Location = New-Object System.Drawing.Size(65, 100)
$OKB.Size = New-Object System.Drawing.Size(65, 23) $OKB.Size = New-Object System.Drawing.Size(65, 23)
$OKB.Text = "Send" $OKB.Text = "Send"
$OKB.Add_Click({ SendRconCommand -rcon $rcon }) $OKB.Add_Click({ Send-RconCommand -rcon $rcon })
$form.Controls.Add($OKB) $form.Controls.Add($OKB)
} }
Function AddCloseButton($form) { Function Add-CloseButton($form) {
$CAB.Location = New-Object System.Drawing.Size(140, 100) $CAB.Location = New-Object System.Drawing.Size(140, 100)
$CAB.Size = New-Object System.Drawing.Size(65, 23) $CAB.Size = New-Object System.Drawing.Size(65, 23)
$CAB.Text = "Close" $CAB.Text = "Close"
@ -38,39 +38,40 @@ Function AddCloseButton($form) {
$form.Controls.Add($CAB) $form.Controls.Add($CAB)
} }
Function AddLabel($form) { Function Add-Label($form) {
$Lbl.Location = New-Object System.Drawing.Size(10, 20) $Lbl.Location = New-Object System.Drawing.Size(10, 20)
$Lbl.Size = New-Object System.Drawing.Size(260, 20) $Lbl.Size = New-Object System.Drawing.Size(260, 20)
$Lbl.Text = "Input Rcon Command:" $Lbl.Text = "Input Rcon Command:"
$form.Controls.Add($Lbl) $form.Controls.Add($Lbl)
} }
Function AddTextBox { Function Add-TextBox {
param($form, $rcon) param($form, $rcon)
$OTB.Location = New-Object System.Drawing.Size(10, 50) $OTB.Location = New-Object System.Drawing.Size(10, 50)
$OTB.Size = New-Object System.Drawing.Size(240, 20) $OTB.Size = New-Object System.Drawing.Size(240, 20)
$OTB.Add_KeyDown({ $OTB.Add_KeyDown({
if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Enter) { if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Enter) {
SendRconCommand -rcon $rcon Send-RconCommand -rcon $rcon
} }
}) })
$form.Controls.Add($OTB) $form.Controls.Add($OTB)
} }
Function AddResponseLabel($form) { Function Add-ResponseLabel($form) {
$RLbl.Location = New-Object System.Drawing.Size(10, 75) $RLbl.Location = New-Object System.Drawing.Size(10, 75)
$RLbl.Size = New-Object System.Drawing.Size(260, 20) $RLbl.Size = New-Object System.Drawing.Size(260, 20)
$RLbl.Text = "" $RLbl.Text = ""
$form.Controls.Add($RLbl) $form.Controls.Add($RLbl)
} }
Function FinalizeForm($form) { Function Start-Form($form) {
$form.Topmost = $true $form.Topmost = $true
$form.Add_Shown({ $form.Activate() }) $form.Add_Shown({ $form.Activate() })
[void] $form.ShowDialog()
} }
Function SendRconCommand() { Function Send-RconCommand() {
param($rcon) param($rcon)
$line = $OTB.Text $line = $OTB.Text
@ -89,7 +90,7 @@ Function SendRconCommand() {
$resp = $rcon.Send($line) $resp = $rcon.Send($line)
} }
if ($resp -match '^["](?<name>[a-z_]+)["]\sis[:]\s["](?<value>[A-Za-z_]+)\^7["]\s') { if ($resp -match '^["](?<name>[a-z_]+)["]\sis[:]\s["](?<value>.*?)\^7["]\s') {
$RLbl.Text = $Matches.name + ": " + $Matches.value $RLbl.Text = $Matches.name + ": " + $Matches.value
} }
$OTB.Text = "" $OTB.Text = ""
@ -102,22 +103,22 @@ Function Get-ConnFromPSD1 {
} }
Function Main { Function Main {
try { BEGIN {
$conn = Get-ConnFromPSD1 $conn = Get-ConnFromPSD1
$rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd $rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd
Write-Host $rcon.base.ToString() -ForegroundColor Green Write-Host $rcon.base.ToString() -ForegroundColor Green
$form = InitForm $form = New-Form
AddOkButton -form $form -rcon $rcon Add-OkButton -form $form -rcon $rcon
AddCloseButton($form) Add-CloseButton($form)
AddLabel($form) Add-Label($form)
AddResponseLabel($form) Add-ResponseLabel($form)
AddTextBox -form $form -rcon $rcon Add-TextBox -form $form -rcon $rcon
FinalizeForm($form)
[void] $form.ShowDialog()
} }
finally { PROCESS {
Start-Form($form)
}
END {
Disconnect-Rcon -rcon $rcon Disconnect-Rcon -rcon $rcon
} }
} }

38
examples/messages.ps1 Normal file
View File

@ -0,0 +1,38 @@
[cmdletbinding()]
param()
Import-Module ../lib/Q3Rcon.psm1
Function Get-ConnFromPSD1 {
$configpath = Join-Path $PSScriptRoot "config.psd1"
return Import-PowerShellDataFile -Path $configpath
}
Function Get-DadJoke {
Invoke-WebRequest -Uri "https://icanhazdadjoke.com" -Headers @{accept = "application/json" } | Select-Object -ExpandProperty Content | ConvertFrom-Json | Select-Object -ExpandProperty Joke
}
Function Send-Message {
param($rcon)
$msg = Get-DadJoke
$msg | Write-Debug
$rcon.Say($msg)
}
try {
$conn = Get-ConnFromPSD1
$rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd
$stopWatch = [system.diagnostics.stopwatch]::StartNew()
$timeSpan = New-TimeSpan -Seconds 30
do {
Send-Message -rcon $rcon
Start-Sleep -Seconds 10
} until ($stopWatch.Elapsed -ge $timeSpan)
$stopWatch.Stop()
}
finally {
Disconnect-Rcon -rcon $rcon
}