Skip to content

Instantly share code, notes, and snippets.

@darenc
Forked from rasher/latest_rideon.ps1
Last active December 1, 2022 17:54
Show Gist options
  • Save darenc/fcf99e04be828582f3aa5f4fa88cfe4b to your computer and use it in GitHub Desktop.
Save darenc/fcf99e04be828582f3aa5f4fa88cfe4b to your computer and use it in GitHub Desktop.
Monitor your Zwift log file and outputs the latest Ride On
# Copyright 2020 Jonas Häggqvist <rasher@rasher.dk>
#
# Usage of the works is permitted provided that this instrument is retained with the
# works, so that any entity that uses the works is notified of this instrument.
#
# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
# This script monitors your Zwift log file and outputs the latest Ride On to a given
# output file. The output file is updated at most every N ms (6000), to try and follow
# the Zwift GUI.
#
# Possible bug: If the log file is truncated or becomes smaller in any way, Powershell
# appears to panic and start outputting the entire file. Not sure there's any way to
# prevent that. May be fixed in new PowerShell versions? Written/tested on 5.1. It
# should not happen anyway unless Zwift does something really weird. This of course
# means it will.
#
# Additional comments from Daren:
#
# I know no Powershell but I hacked this about to add some additional files as output
#
# - a total number of Ride Ons received in the session
# - the number of Ride Ons given in the session
$docs = [environment]::GetFolderPath("MyDocuments")
$zwift = "$docs\Zwift" # Zwift data directory
$log = "$zwift\Logs\Log.txt" # Path to Zwift log file
#$output = "$zwift\Logs\LastRideon.txt" # File to write output to
$output = "LastRideon.txt" # File to write output to
$outputCount = "TotalRideOns.txt" # File to write output to
$outputGivenCount = "TotalRideOnsGiven.txt" # File to write output to
$delay = 1000 # Delay in ms between updates of the file
$prefix = "" # Added in the logfile in front of the username
$postfix = " says Ride On!" # Added after the username
$count = 0 # count of number of ride ons
Function Write-SlowOutput {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,
HelpMessage="How long to pause for (ms)")]
[int32]$waitFor,
[Parameter(Mandatory=$false,
HelpMessage="File to output to")]
[string]$outputFile,
[string]$outputFileCount,
[Parameter(ValueFromPipeline)]
[Object]$input
)
BEGIN {}
PROCESS {
if ($input -match "^\d+$") {
Write-Host $input Ride Ons given
if (!(Test-Path $outputGivenCount)) {
New-Item -name $outputGivenCount -type "file" -value "$input Ride Ons given"
} else {
Out-File -Encoding UTF8 -FilePath $outputGivenCount -InputObject "$input Ride Ons given"
}
} else {
$count++;
Write-Host $count $input
if ($outputFile) {
Out-File -Append -Encoding UTF8 -FilePath $outputFile -InputObject $input
}
if ($outputFileCount) {
if (!(Test-Path $outputFileCount)) {
New-Item -name $outputFileCount -type "file" -value "$count Ride Ons"
} else {
Out-File -Encoding UTF8 -FilePath $outputFileCount -InputObject "$count Ride Ons"
}
}
}
Start-Sleep -Milliseconds $waitFor
}
END {}
}
if (Test-Path $outputCount) {
Out-File -Encoding UTF8 -FilePath $outputCount -InputObject "0 Ride Ons"
}
if (Test-Path $outputGivenCount) {
Out-File -Encoding UTF8 -FilePath $outputGivenCount -InputObject "0 Ride Ons given"
}
Get-Content -Tail 0 -Wait -Encoding "UTF8" $log |
Select-String "HUD_Notify: (.*) says Ride On!","Total Ride Ons Given: (.*)" |
% {$prefix + $_.matches.groups[1].value} |
Write-SlowOutput -outputFile $output -outputFileCount $outputCount -waitFor $delay
@lionslair
Copy link

Cool

@cadpri
Copy link

cadpri commented Nov 6, 2020

excuse my dumb question:
but how do i get this running to show rideons on my twitch stream ?

@darenc
Copy link
Author

darenc commented Nov 6, 2020

excuse my dumb question:
but how do i get this running to show rideons on my twitch stream ?

You need to run Windows Powershell, then inside that run the script.

image

Then you just add a new Text input overlay in OBS/SLOBS or whatever you use, pointing at the relevant text files.

@cadpri
Copy link

cadpri commented Nov 7, 2020

ok with your help it works now 50%.
only ride ons i give is put in the totalrideonsgiven.txt.
thelastrideons.txt is not generated nor does it appear in the powersehll script. only given rideons is shown in the powersehll

UPDATE:
Nevermind, i got it fixed. The German Log Files are not compatible with the script. my bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment