-
-
Save zack6849/dfbd1f550a19512d0837 to your computer and use it in GitHub Desktop.
Steam Users class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SteamUsers | |
require 'open-uri' | |
require 'steam-condenser' | |
attr_accessor :players | |
attr_accessor :ranks | |
attr_accessor :online_staff | |
def initialize | |
load_ranks | |
puts "Loaded @ranks!" | |
load_server | |
puts "Loaded server!" | |
load_online_staff | |
puts "Loaded online staff" | |
end | |
private | |
def load_server | |
Rails.cache.fetch("server", :expires_in => 1.seconds){ | |
@players = {} | |
server = SourceServer.new(Settings.rcon.host, Settings.rcon.port) | |
server.rcon_auth(Settings.rcon.password) | |
server.players.each do |player| | |
name = player[0] | |
if player[1].state | |
@players[player[1].name] = [] | |
@players[player[1].name].push(player[1]) | |
end | |
end | |
} | |
end | |
def load_online_staff | |
@online_staff = {} | |
@ranks.keys.each do |rank| | |
@ranks[rank].each do |member| | |
@players.each do |player| | |
if player.steam_id.to_s == member.to_s | |
if @online_staff[rank] == nil | |
@online_staff[rank] = [] | |
end | |
@online_staff[rank].push player | |
end | |
end | |
end | |
end | |
puts "@players: \r\n#{@players.pretty_print_inspect}" | |
puts "@ranks: \r\n#{@ranks.pretty_print_inspect}" | |
puts "Online Staff\r\n#{@online_staff.pretty_print_inspect}" | |
end | |
def load_ranks | |
Rails.cache.fetch("@ranksfile", :expires_in => 2.minutes) do | |
users_file = '/home/gmod/gmod/garrysmod/data/ulib/users.txt' | |
lines = [] | |
if ranks == nil | |
@ranks = {} | |
end | |
if not File.exist?(users_file) or not File.readable?(users_file) | |
open('https://zack6849.com/users.txt') { | |
|f| lines = f.read.to_s.split(/\n/) | |
} | |
else | |
f = File.open(users_file, 'r') | |
lines = f.read.to_s.split(/\n/) | |
end | |
found = false | |
inside = false | |
user = nil | |
group = nil | |
lines.each do |line| | |
umatch = /"(STEAM.+)"/.match(line) | |
if umatch | |
user = umatch.captures[0] | |
inside = true | |
end | |
gmatch = /.+group"\s"(.+)"/.match(line) | |
if inside and gmatch | |
group = gmatch.captures[0] | |
found = true | |
end | |
if line == '}' | |
if found | |
if @ranks[group] == nil | |
@ranks[group] = [] | |
end | |
@ranks[group].push user | |
end | |
#reset variables for next user tag. | |
inside = false | |
found = false | |
user = nil | |
group = nil | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment