Class: SBPanel::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/sbpanel/game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Game

Returns a new instance of Game.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sbpanel/game.rb', line 31

def initialize(state)
  @address = "starbound.mispy.me"
  @port = 21025
  
  @status = state[:status] || :unknown
  @last_status_change = state[:last_status_change] || Time.now # Persist last observed status change
  @players = state[:players] || {} # Persist info for players we've seen
  @worlds = state[:worlds] || {} # Persist info for worlds we've seen
  @chat = state[:chat] || [] # Chat logs

  @version = 'unknown' # Server version
  @online_players = [] # Players we've seen connect
  @active_worlds = [] # Worlds we've seen activated
  @offline_players = @players.values.select { |pl| !@online_players.include?(pl) }

  @timing = false # We read the log initially without timing
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



8
9
10
# File 'lib/sbpanel/game.rb', line 8

def address
  @address
end

#log_pathObject

Returns the value of attribute log_path.



8
9
10
# File 'lib/sbpanel/game.rb', line 8

def log_path
  @log_path
end

#state_pathObject

Returns the value of attribute state_path.



8
9
10
# File 'lib/sbpanel/game.rb', line 8

def state_path
  @state_path
end

Class Method Details

.load(log_path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sbpanel/game.rb', line 10

def self.load(log_path)
  state_path = File.join(File.dirname(log_path), ".sbpanel")

  state = {}
  if File.exists?(state_path)
    begin
      state = Marshal.load(File.read(state_path))
      puts "Loaded sbpanel state from #{state_path}"
    rescue Exception
      puts "Error loading sbpanel state, making new panel"
    end
  end

  panel = Game.new(state)

  panel.log_path = log_path
  panel.state_path = state_path
  panel.update_status
  panel
end

Instance Method Details

#parse_line(line, time) ⇒ Object



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
# File 'lib/sbpanel/game.rb', line 97

def parse_line(line, time)
  events = {
    version: /^Info: Server version '(.+?)'/,
    login: /^Info: Client '(.+?)' <.> \(.+?\) connected$/,
    logout: /^Info: Client '(.+?)' <.> \(.+?\) disconnected$/,
    world: /^Info: Loading world db for world (\S+)/,
    unworld: /^Info: Shutting down world (\S+)/,
    chat: /^Info:  <(.+?)> (.+?)$/
  }

  events.each do |name, regex|
    event = regex.match(line)
    next unless event

    case name
    when :version
      puts "Server version: #{event[1]}"
      @version = event[1]
      reset!
    when :login
      name = event[1]

      player = @players[name] || {}
      player[:name] = name
      player[:last_connect] = time if @timing
      @players[name] = player

      @online_players.push(player) unless @online_players.find { |pl| pl[:name] == name }
      @offline_players.delete_if { |pl| pl[:name] == name }
      puts "#{name} connected at #{player[:last_connect]}"
    when :logout
      name = event[1]

      player = @players[name] || {}
      player[:name] = name
      player[:last_disconnect] = time if @timing
      @players[name] = player

      @online_players.delete_if { |pl| pl[:name] == name }
      @offline_players.push(player) unless @offline_players.find { |pl| pl[:name] == name }
      puts "#{name} disconnected at #{player[:last_disconnect]}"
    when :world
      coords = event[1]

      world = @worlds[coords] || {}
      world[:coords] = coords
      world[:last_load] = time if @timing
      @worlds[coords] ||= world

      @active_worlds.push(world) unless @active_worlds.find { |w| w[:coords] == coords }
      puts "Loaded world #{coords}"
    when :unworld
      coords = event[1]

      world = @worlds[coords] || {}
      world[:coords] = coords
      world[:last_unload] = time if @timing
      @worlds[coords] ||= world

      @active_worlds.delete_if { |w| w[:coords] == coords }
      puts "Unloaded world #{coords}"
    when :chat
      chat = {
        name: event[1],
        text: event[2]
      }

      # Hacky attempt to prevent chat desync
      if @timing || @last_chat == @chat[-1]
        @chat.push(chat)
        puts "#{chat[:name]}: #{chat[:text]}"
      end
      @last_chat = chat
    end

    if @timing
      # For post-initial-load events, check server
      # status and save state updates
      update_status; save
    end
  end
end

#read_logsObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/sbpanel/game.rb', line 180

def read_logs
  # Initial read without timing
  File.read(@log_path).each_line do |line|
    parse_line(line, nil)
  end

  @timing = true
  log = File.open(@log_path)
  log.extend(File::Tail)
  log.backward(0)
  log.tail do |line|
    parse_line(line, Time.now)
  end

  log.close
end

#reset!Object

Reset non-persistent state



90
91
92
93
94
95
# File 'lib/sbpanel/game.rb', line 90

def reset!
  @last_status_change = Time.now if @timing
  @online_players = []
  @active_worlds = []
  @offline_players = @players.values.select { |pl| !@online_players.include?(pl) }
end

#saveObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sbpanel/game.rb', line 49

def save
  File.open(@state_path, 'w') do |f|
    f.write(Marshal.dump({
      players: @players,
      worlds: @worlds,
      status: @status,
      last_status_change: @last_status_change,
      chat: @chat
    }))
  end
end

#update_statusObject

Detect server status Looks for processes which have log file open for writing



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
# File 'lib/sbpanel/game.rb', line 63

def update_status
  status = :offline
  fuser = `fuser -v #{@log_path} 2>&1`.split("\n")[2..-1]
  
  if fuser
    fuser.each do |line|
      if line.strip.split[2].include?('F')
        status = :online
      end
    end
  end

  if status != @status
    time = Time.now
    if status == :offline
      puts "Server is currently offline"
    else
      puts "Server is currently online"
    end

    @status = status
    @last_status_change = time
    reset!
  end
end