summaryrefslogtreecommitdiff
path: root/ircobsbridge.rb
blob: 326b8d455a5d6eec3b8a1d47eaddcae1cf944d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env ruby

# IRC/OBS-websocket scene management bridge

require 'json'
require 'socket'
require 'openssl'
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\\"
ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/'

# returning 'true' on each line to conceal results when sourcing in irb

( $ACCESS_TOKEN = IO.read( confdir + 'access_token' ).chomp ) && true
( $CHANNEL      = IO.read( confdir + 'channel'      ).chomp ) && true
( $CLIENT_ID    = IO.read( confdir + 'client_id'    ).chomp ) && true
( $UNIX_SOCKET_PATH =      confdir + 'chat_socket'  )         && true
( $OBS_PASSWORD = IO.read( confdir + 'obs_password' ).chomp ) && true
( $TRN_API_KEY  = IO.read( confdir + '../trn-api-key' ).chomp ) && true

def ircsocketwrite( text )
  unix_socket = UNIXSocket.new( $UNIX_SOCKET_PATH )
  unix_socket.print( text )
rescue
ensure
  unix_socket.close
end

def websocketwrite( json )
  $tcp_socket.write( WebSocket::Frame::Outgoing::Client.new( data: json, type: :text ).to_s )
rescue IOError, EOFError, Errno::ECONNRESET
  $tcp_socket.close
  websocket_create()
  retry
end

# the obsreader thread specially interprets unusual message-id-keyed responses; '1' will be a safe default.

def SetSourceRender( source, render = 'true', id = '1' )
  websocketwrite( '{ "request-type": "SetSourceRender", "message-id" : "' + id.to_s + '", "source" : "' + source + '", "render" : ' + render.to_s + ' }' )
end

def GetCurrentScene( id = '1' )
  websocketwrite( '{ "request-type": "GetCurrentScene", "message-id" : "' + id.to_s + '" }' )
end

def GetSceneList( id = '1' )
  websocketwrite( '{ "request-type": "GetSceneList", "message-id" : "' + id.to_s + '" }' )
end

def SetCurrentScene( scene, id = '1' )
  websocketwrite( '{ "request-type": "SetCurrentScene", "message-id" : "' + id.to_s + '", "scene-name": "' + scene + '" }' )
end

def GetStreamingStatus( id = '1' )
  websocketwrite( '{ "request-type": "GetStreamingStatus", "message-id" : "' + id.to_s + '" }' )
end

def pubgstats( name )
  tries = 2
  http = Net::HTTP.new( 'pubgtracker.com', 443 )
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.read_timeout = 500

  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
    sleep 2
    retry unless( tries-=1 ).zero?
  end

  if response['error'] then
    ircsocketwrite( "| pubgstats: #{response['message']} (maybe bad name?)\n" )
    return
  end

  totalwins = 0
  totallosses = 0
  totalkills = 0
  totaldeaths = 0
  totalrounds = 0
  bestrank = 9999999
  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

#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
      totaldeaths += stats['value'].to_i / kdr
    end
    ( stats['field'] == 'BestRating'                          && stats['value'].to_i > bestrating )     && bestrating     = stats['value'].to_i
    ( stats['field'] == 'RoundMostKills'                      && stats['value'].to_i > bestroundkills ) && bestroundkills = stats['value'].to_i
    ( 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
rescue NoMethodError => e
  ircsocketwrite( e.message )
end

#bestrank
#bestrating
#bestroundkills
totalkills /= 2
totaldeaths /= 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, 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:
# highest 'Kills' 'KillDeathRatio' & percentile 'Wins'/'WinPoints'/'WinRatio'/percentile 'RoundMostKills' 'RoundsPlayed' 'BestRating' 'BestRank' 'LongestKill'
# realistically:
# BestRank BestRating RoundsPlayed Wins/Losses WinRatio Kills RoundMostKills LongestKill
	
end

def transientsource( source )
  Thread.new {
    SetSourceRender( 'media-' + source , false )
    sleep 0.1
    SetSourceRender( 'media-' + source , true )
    sleep 60
    SetSourceRender( 'media-' + source , false )
  }
end

#json["subscriptions"][0]["user"]["name"]

def websocket_create()
  begin
    $tcp_socket = TCPSocket.new( '127.0.0.1', 4444 )
    $tcp_socket.write( WebSocket::Handshake::Client.new( url: 'ws://localhost/' ).to_s )
    IO.select( [ $tcp_socket ] )
    print $tcp_socket.read_nonblock( 4096 )
    websocketwrite( '{ "request-type": "GetAuthRequired", "message-id" : "1" }' )
    # Let the obsreader thread do the actual authentication.
  rescue IOError, EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET
    $tcp_socket && $tcp_socket.close
    sleep 5
    retry
  end
end

$lastused = Hash.new
$globallastused = 0

def command_dispatch( mode, user, command, arg1 )
  # arg1 may be empty String
  safe = /^[-0-9a-zA-Z_]+$/
  if mode == '@' && command == 'scene'
    if arg1 =~ safe
      SetCurrentScene( arg1 )
      return
    else
      GetSceneList(  )
      return
    end
  elsif mode == '@' && command == 'source'
    if arg1 =~ safe
      # GetCurrentScene for current visibility state
      GetCurrentScene( arg1 )
      # SetSourceRender to toggle
      return
    else
      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( "| !songrequest !currentsong !next !volume !delsong !source !scene !pubg !metaminute !nopgl1 !nopgl2\n" )
    return
  end
  # ratelimited commands after +o commands
  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 )
      return
    end
  end
  if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 120 ) ) && $globallastused <= ( Time.now.to_i - 60 ) # || mode == '@'
    $globallastused = Time.now.to_i
    $lastused[ command ] = Time.now.to_i
    if command =~ /^(metaminute|nopgl1|nopgl2)$/
      transientsource( command )
      return
    elsif command == 'commands'
      ircsocketwrite( "| !songrequest !currentsong !pubg !metaminute !nopgl1 !nopgl2\n" )
      return
    end
  end
end

def frame_dispatch( frame )
  if frame["update-type"] == "StreamStatus"
    $status = frame
  elsif frame['current-scene'] && frame['scenes'] # GetSceneList
    text = '| Current scene: ' + frame['current-scene'] + ' | Available: ' + frame['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n"
    print text
    ircsocketwrite( text )
  elsif frame['name'] && frame['sources']         # GetCurrentScene
    pp frame
    text = '| Sources: ' + frame['sources'].map{|v| v['name'] }.sort.join(' ') + "\n"
    print text
    if frame['message-id'] == '1'
      ircsocketwrite( text )
    else # other message-ids are a source we want to toggle
      frame['sources'].each do |v|
        if v['name'] == frame['message-id']
          if    v['render'] == true
            SetSourceRender( v['name'], 'false', '2' )
            ircsocketwrite( '| Source ' + v['name'] + " is now unrendered\n" )
          elsif v['render'] == false
            SetSourceRender( v['name'], 'true',  '2' )
            ircsocketwrite( '| Source ' + v['name'] + " is now visible\n" )
          end
        end
      end
    end
  elsif frame['update-type'] == 'SwitchScenes'
    ircsocketwrite( '| Scene is now ' + frame['scene-name'] + "\n" )
  #elsif frame['update-type'] == 'SceneItemVisibilityChanged'
  #  ircsocketwrite( '| Source ' + frame['item-name'] + ' visibility is now ' + frame['item-visible'].to_s + "\n" )
  elsif frame['authRequired'] == true                    # GetAuthRequired
    websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest($OBS_PASSWORD + frame['salt'])+frame['challenge']) + '" }' )
  else
    print frame
  end
end

# irclog read loop event thread
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]+) *(.*)/) )
      command_dispatch( *match.to_a[1..4] )
    end
  end
rescue
ensure
  irclog.close
end
}

websocket_create()
frame = WebSocket::Frame::Incoming::Client.new
frame_decoded = String.new
$status = Hash.new

# websocket read loop event thread
obsreader = Thread.new {
  begin
    while true
      frame << $tcp_socket.read_nonblock( 4096 )
      while ( frame_decoded = frame.next.to_s ) != ""
        frame_parsed = JSON.parse(frame_decoded)
        frame_dispatch( frame_parsed )
      end
      #pp JSON.parse(frame_decoded)
    end
  rescue IO::WaitReadable, IO::EAGAINWaitReadable, JSON::ParserError
    IO.select([$tcp_socket])
    retry
  rescue IOError, EOFError, Errno::ECONNRESET
    $tcp_socket.close
    websocket_create()
    retry
  end
}