summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Rayhawk <jrayhawk@omgwallhack.org>2017-12-23 03:19:19 -0800
committerJoe Rayhawk <jrayhawk@omgwallhack.org>2017-12-23 03:19:19 -0800
commit8b5c3bb7ce8d9caddf06fb3beeec467bb25801e1 (patch)
treefbadc707e6456e706f85bd42e456e290b3f30017
parentdb638057fb77eb81020803aab0cee9763be36425 (diff)
downloadtwitchtools-8b5c3bb7ce8d9caddf06fb3beeec467bb25801e1.tar.gz
twitchtools-8b5c3bb7ce8d9caddf06fb3beeec467bb25801e1.zip
ircobsbridge: normalize pubg wins, add basic music player daemon control
-rw-r--r--ircobsbridge.rb128
1 files changed, 113 insertions, 15 deletions
diff --git a/ircobsbridge.rb b/ircobsbridge.rb
index 103c7a2..326b8d4 100644
--- a/ircobsbridge.rb
+++ b/ircobsbridge.rb
@@ -9,6 +9,9 @@ require 'websocket'
require 'net/http'
require 'pp'
require 'uri'
+require 'mpd'
+$VERBOSE = nil # mpd.rb is a bit noisy about versions, though.
+$m = MPD.new
confdir = Dir.home + '/.config/twitch/'
ENV['APPDATA'] && confdir = ENV['APPDATA'] + "\\twitch\\"
@@ -70,7 +73,7 @@ def pubgstats( name )
req = Net::HTTP::Get.new( "/api/profile/pc/#{name}" )
( req["TRN-Api-Key"] = $TRN_API_KEY ) && true
-
+
begin
response = JSON.parse( http.request( req ).body )
rescue
@@ -92,18 +95,51 @@ def pubgstats( name )
bestrating = 0
bestroundkills = 0
bestrangekill = 0
-
+ solowins = 0
+ sololosses = 0
+ duowins = 0
+ duolosses = 0
+ squadwins = 0
+ squadlosses = 0
+
#response['Stats'].each { |matchstats| matchstats['Stats'].each { |stats| printf( "%9s %3s %5s %25s %10s\n", matchstats['Season'], matchstats['Region'], matchstats['Match'], stats['field'], stats['value'] ) } } && true# debug
#response['Stats'].each { |matchstats| matchstats['Stats'].each { |stats| printf( "%9s %3s %5s %23s %20s %12s %10s %10s %10s %10s\n", matchstats['Season'], matchstats['Region'], matchstats['Match'], stats['label'], stats['field'], stats['category'], stats['value'], stats['rank'], stats['percentile'], stats['displayValue'] ) } } && true # debug
-
+
+begin
response['Stats'].each do |matchstats|
kdr = 0.0
matchstats['Stats'].each do |stats|
if stats['field'] == 'KillDeathRatio' then
kdr = stats['value'].to_f
end
- stats['field'] == 'Wins' && totalwins += stats['value'].to_i
- stats['field'] == 'Losses' && totallosses += stats['value'].to_i
+
+#matchstats['Match']
+#solo
+#duo
+#squad
+#solo-fpp
+#duo-fpp
+#squad-fpp
+ if stats['field'] == 'Wins' then
+ case matchstats['Match']
+ when /^solo/
+ solowins += ( stats['value'].to_i )
+ when /^duo/
+ duowins += ( stats['value'].to_i )
+ when /^squad/
+ squadwins += ( stats['value'].to_i )
+ end
+ end
+ if stats['field'] == 'Losses' then
+ case matchstats['Match']
+ when /^solo/
+ sololosses += stats['value'].to_i
+ when /^duo/
+ duolosses += stats['value'].to_i
+ when /^squad/
+ squadlosses += stats['value'].to_i
+ end
+ end
stats['field'] == 'RoundsPlayed' && totalrounds += stats['value'].to_i
if stats['field'] == 'Kills' && kdr != 0 then
totalkills += stats['value'].to_i
@@ -114,22 +150,30 @@ response['Stats'].each do |matchstats|
( stats['field'] == 'LongestKill' && stats['value'].to_i > bestrangekill ) && bestrangekill = stats['value'].to_i
( stats['field'] == 'BestRank' && 0 < stats['value'].to_i && stats['value'].to_i < bestrank ) && bestrank = stats['value'].to_i
end
-
-end && true
+end && true
+rescue NoMethodError => e
+ ircsocketwrite( e.message )
+end
#bestrank
#bestrating
#bestroundkills
totalkills /= 2
totaldeaths /= 2
-totalwins /= 2
+#totalwins # we cancel out the aggregate earlier
totallosses /= 2
+sololosses /= 2
+duolosses /= 2
+squadlosses /= 2
#bestrangekill
winratio = totalwins.to_f/totallosses.to_f
+solowinratio = solowins.to_f/sololosses.to_f*100
+duowinratio = duowins.to_f/duolosses.to_f*50
+squadwinratio = squadwins.to_f/squadlosses.to_f*25
killratio = totalkills.to_f/totaldeaths.to_f
-ircsocketwrite( sprintf( "| %s BEST: rating: %i, rank: %i, roundkills: %i, rangekill: %i; TOTAL: kills/deaths: %i / %i = %02.03f, wins/losses: %i / %i = %01.03f ", response['PlayerName'], bestrating, bestrank, bestroundkills, bestrangekill, totalkills, totaldeaths, killratio, totalwins, totallosses, winratio ) )
+ircsocketwrite( sprintf( "| %s BEST: rating: %i, rank: %i, roundkills: %i, rangekill: %i; TOTAL: kills/deaths: %i / %i = %02.03f, normalized win likelihood: solo: %i/%i*100 = %01.03f, duo: %i/%i*50 = %01.03f, squad: %i/%i*25 = %01.03f", response['PlayerName'], bestrating, bestrank, bestroundkills, bestrangekill, totalkills, totaldeaths, killratio, solowins, sololosses, solowinratio, duowins, duolosses, duowinratio, squadwins, squadlosses, squadwinratio ) )
#ircsocketwrite( "| #{response['PlayerName']} PUBG Stats: BestRating: #{bestrating}, BestRank: #{bestrank}, HighestRoundKills: #{bestroundkills}, TotalKills: #{totalkills}, TotalWins/Losses #{totalwins}/#{totallosses} = #{winratio}, HighestRange: #{bestrangekill}\n" )
# optimally:
@@ -190,12 +234,66 @@ def command_dispatch( mode, user, command, arg1 )
GetCurrentScene( )
return
end
+ elsif mode == '@' && ( command == 'delete' || command == 'delsong' )
+ if arg1 =~ /^[1-9][0-9]*$/
+ begin
+ file = $m.playlistinfo( arg1.to_i - 1 )['file']
+ $m.delete( arg1.to_i - 1 )
+ ircsocketwrite( '| deleting song' + file + "\n" )
+ rescue RuntimeError
+ ircsocketwrite( '| problem deleting song ' + arg1 + "\n" )
+ ensure
+ return
+ end
+ end
+ elsif mode == '@' && ( command == 'volume' || command == 'vol' )
+ if arg1 =~ /^[0-9]+$/
+ ircsocketwrite( "| volume adjusted: " + $m.setvol( arg1 ) + "\n" )
+ return
+ else
+ ircsocketwrite( "| volume: " + $m.status['volume'] + "\n" )
+ return
+ end
+ elsif mode == '@' && ( command == 'nextsong' || command == 'next' )
+ ircsocketwrite( "| " + $m.next["file"] + "\n" )
+ return
+ elsif ( command == 'currentsong' ) || ( command == 'current' )
+ ircsocketwrite( "| " + $m.currentsong["file"] + "\n" )
+ return
+ elsif ( command == 'songlist' ) || ( command == 'list' )
+ ircsocketwrite( "| song list: http://action.omgwallhack.org/music\n" )
+ return
+ elsif( ( command == 'songrequest' ) || ( command == 'sr' ) ) && arg1.size > 1
+ if( system( 'sraddsong.sh', arg1 ) )
+ if( $m.playlistinfo[0].file == 'http://music/music.ogg' )
+ $m.next
+ end
+ $m.playlistinfo.each do | song |
+ if( song.file == 'http://music/music.ogg' )
+ $m.deleteid( song.dbid )
+ end
+ end
+ ircsocketwrite( '| ' + $m.playlistinfo[ $m.status['playlistlength'].to_i - 1 ]['file'] + ' added in position ' + $m.status['playlistlength'] + "\n" )
+ $m.add( 'http://music/music.ogg' )
+ return
+ else
+ ircsocketwrite( '| adding song ' + arg1 + " FAILED\n" )
+ return
+ end
+ # popen to get output someday?
elsif mode == '@' && command == 'commands'
- ircsocketwrite( "| !source !scene !pubg !metaminute !nopgl1 !nopgl2\n" )
+ ircsocketwrite( "| !songrequest !currentsong !next !volume !delsong !source !scene !pubg !metaminute !nopgl1 !nopgl2\n" )
return
- end
+ end
# ratelimited commands after +o commands
- if command =~ /^(pubg)$/
+ if command =~ /^(dexem)$/
+ if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 5 ) ) # || mode == '@'
+ $lastused[ command ] = Time.now.to_i
+ ircsocketwrite( "| You're doing great work, Dexem!\n" )
+ return
+ end
+ end
+ if command =~ /^(pubg)$/
if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 5 ) ) # || mode == '@'
$lastused[ command ] = Time.now.to_i
pubgstats( arg1 )
@@ -209,7 +307,7 @@ def command_dispatch( mode, user, command, arg1 )
transientsource( command )
return
elsif command == 'commands'
- ircsocketwrite( "| !pubg !metaminute !nopgl1 !nopgl2\n" )
+ ircsocketwrite( "| !songrequest !currentsong !pubg !metaminute !nopgl1 !nopgl2\n" )
return
end
end
@@ -257,7 +355,7 @@ ircreader = Thread.new {
begin
irclog = IO.popen('/usr/bin/tail -n0 -F ' + ENV['HOME'] + '/irclogs/twitch/#' + $CHANNEL + '.log' )
irclog.each_line do |line|
- if ( match = line.match(/^\d\d:\d\d <([@ +])(.+)?> *!([-0-9a-zA-Z]+) *([-0-9a-zA-Z_]*)/) )
+ if ( match = line.match(/^\d\d:\d\d <([@ +])(.+)?> *!([-0-9a-zA-Z]+) *(.*)/) )
command_dispatch( *match.to_a[1..4] )
end
end
@@ -291,4 +389,4 @@ obsreader = Thread.new {
websocket_create()
retry
end
-}
+}