This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hearthstoned.rb
303 lines (271 loc) · 9.05 KB
/
hearthstoned.rb
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
require 'json'
require 'singleton'
require 'socket'
def Error(msg, die=false)
puts "#{die ? "FATAL " : ""}ERROR: #{msg}"
puts "##{State.instance.currentLineNo}: #{State.instance.currentLine}"
abort if die
end
class State
include Singleton
attr_accessor :inGame, :entities, :lastEntity, :currentLine, :currentLineNo, :lastGame
def initialize
@entities = nil
reset
end
def reset(inGame=false)
@inGame = inGame
@lastGame = @entities
@entities = {}
@lastEntity = nil
@currentLine = ""
@currentLineNo = 0
end
def hasEntity?(id)
@entities.key? id or !@entities[id].nil?
end
def addEntity(id)
@lastEntity = id
return if hasEntity? id
@entities[id] = {}
end
def players
@entities.select { |k, v| v.key? "PlayerID" }
end
def setEntityTag(id, key, value)
@entities[id][key] = value
end
def dump
@entities.each do |k, v|
puts "#{k}:"
v.each do |kk, vv|
puts "\t #{kk}=#{vv}"
end
end
end
end
def ParseGameState(type, tags)
case type
when "DebugPrintPower"
case tags
when "CREATE_GAME"
when /^GameEntity EntityID=(\d+)$/
State.instance.addEntity $1
when /^Player EntityID=(\d+) PlayerID=(\d+) GameAccountId=\[hi=(\d+) lo=(\d+)\]$/
State.instance.addEntity $1
State.instance.setEntityTag $1, "PlayerID", $2
State.instance.setEntityTag $1, "GameAccountHi", $3
State.instance.setEntityTag $1, "GameAccountLo", $4
when /^FULL_ENTITY - Creating ID=(\d+) CardID=(\S*)$/
State.instance.addEntity $1
State.instance.setEntityTag $1, "CardID", $2 unless $2.empty? or $2.nil?
when /^tag=(\S+) value=(\S+)$/
State.instance.setEntityTag State.instance.lastEntity, $1, $2
when /^\s*TAG_CHANGE Entity=(.*) tag=(\S+) value=(\S+)( DEF CHANGE)?$/
entity = $1
key = $2
value = $3
if entity == "GameEntity" and key == "STATE" and value == "COMPLETE"
State.instance.setEntityTag "1", key, value
State.instance.reset
return
end
case entity
when /^\d+$/
State.instance.addEntity entity
State.instance.setEntityTag entity, key, value
when /^\[/
if entity =~ /^\[entityName=(.*) id=(\d+) zone=(\S+) zonePos=(\d+) cardId=(\S+)? player=(\d+)\]$/
entityName = $1
id = $2
zone = $3
zonePos = $4
cardId = $5
player = $6
State.instance.addEntity id
State.instance.setEntityTag id, key, value
else
Error "Unhandled TAG_CHANGE format"
end
when /GameEntity/
State.instance.setEntityTag "1", key, value
else
State.instance.players.each do |k, v|
if v.key? "PlayerName" and v["PlayerName"] == entity
v[key] = value
break
end
end
end
when /^(SHOW_ENTITY) - Updating Entity=(.*) (\S+)=(\S+)$/,
/^(HIDE_ENTITY) - Entity=(.*) tag=(\S+) value=(\S+)$/,
/^(CACHED_TAG_FOR_DORMANT_CHANGE) Entity=(.*) tag=(\S+) value=(\S+)$/,
/^(CHANGE_ENTITY) - Updating Entity=(\S+) (\S+)=(\S+)$/,
/^(FULL_ENTITY) - Updating (.*) (\S+)=(\S*)$/
subtype = $1
entity = $2
tag = $3
value = $4
case $2
when /^(\d+)$/
State.instance.addEntity entity
State.instance.setEntityTag entity, tag, value unless value.nil?
when /^\[entityName=(.+) id=(\d+) zone=(\S+) zonePos=(\S+) cardId=(\S+)? player=(\d+)\]$/
entityName = $1
id = $2
zone = $3
zonePos = $4
cardId = $5
player = $6
State.instance.addEntity id
State.instance.setEntityTag id, tag, value unless value.nil?
else
Error "Unhandled #{subtype.upcase} format"
end
when /^BLOCK_START/
when "BLOCK_END"
when /^META_DATA - Meta=(\S+) Data=(\d+) InfoCount=(\d+)$/
when /^Info\[(\d+)\] = (.*)$/
when /^Source = (.*)$/
when /^Targets\[(\d+)\] = (.*)$/
when /^SHUFFLE_DECK PlayerID=(\d+)$/
when /^SUB_SPELL_START/
when "SUB_SPELL_END"
else
Error "Unhandled: GameState.DebugPrintPower"
end
when "OnEntityChoices", "OnEntitiesChosen"
when "DebugPrintEntityChoices", "DebugPrintEntitiesChosen"
when "DebugPrintPowerList"
when "DebugPrintGame"
case tags
when /^(\S+)=(\S+)$/
State.instance.setEntityTag "1", $1, $2
when /^PlayerID=(\d+), PlayerName=(.*)$/
State.instance.players.each do |k, v|
if v.key? "PlayerID" and v["PlayerID"] == $1
v["PlayerName"] = $2
end
end
else
Error "Unhandled: GameState.DebugPrintGame"
end
when "SendChoices"
when "SendOption"
when "DebugPrintOptions"
when "DebugDump"
when "DebugPrintPower"
else
Error "Unhandled GameState"
end
end
def ParsePowerTaskList(type, tags)
case type
when "DebugDump"
when "DebugPrintPower"
when "PrepareHistoryForCurrentTaskList"
when "EndCurrentTaskList"
when "DoTaskListForCard"
else
Error "Unhandled PowerTaskList: #{type}"
end
end
def ParseChoiceCardMgr(type, tags)
case type
when "WaitThenShowChoices"
when "DoesLocalChoiceMatchPacket"
when "WaitThenHideChoicesFromPacket"
else
Error "Unhandled ChoiceCardMgr: #{type}"
end
end
def Parse(line)
if line =~ /^D \d{2}:\d{2}:\d{2}\.\d{7} (\S+)\.(\S+)\(\)\s-\s+(.*)$/
case $1
when "GameState", "PowerTaskList"
ParseGameState $2, $3
when "PowerProcessor"
ParsePowerTaskList $2, $3
when "ChoiceCardMgr"
ParseChoiceCardMgr $2, $3
else
Error "Unhadled log category: #{$1}"
end
else
Error "Unhandled log format"
end
end
def Watch(f)
f.readlines.each do |line|
yield line
end
loop do
line = f.read
if line.nil? or line.empty?
sleep 0.1
next
end
for l in line.split "\n"
yield l
end
end
f.close
end
# $PROGRAM_NAME = "hearthstoned"
# exit if fork
# Process.setsid
# exit if fork
# STDIN.reopen "/dev/null"
# STDOUT.reopen "/dev/null", "a"
# STDERR.reopen "/dev/null", 'a'
# Dir.chdir "/"
Thread.abort_on_exception = true
Thread.new do
Watch File.open("/Applications/Hearthstone/Logs/Power.log", "r") do |line|
# $<.each_line do |line|
State.instance.currentLine = line
State.instance.currentLineNo += 1
if State.instance.inGame
Parse line.strip
else
State.instance.reset true if line =~ /GameState\.DebugPrintPower\(\) - CREATE_GAME$/
end
end
end
def FormResponse data, code=200, force=false
data = "{\"error\":\"Not in-game yet\"}" if not State.instance.inGame
if data.nil? or data.empty? or data == "{}"
data = "{\"error\": \"Nothing found\"}"
code = 404
end
"HTTP/1.1 #{code}\r\nContent-Type: application/json\r\nContent-Length: #{data.length}\r\n\r\n#{data}"
end
socket = TCPServer.new 8080
loop do
client = socket.accept
header = client.gets
method, path, version = header.split
client.puts case method
when "GET"
case path.downcase
when /^\/$/, /^\/entities\/?$/
FormResponse State.instance.entities.to_json
when /^\/entity\/(\d+)?\/?$/
FormResponse State.instance.entities.select { |k, v| k == $1 }.to_json
when /^\/players\/?$/
FormResponse State.instance.players.to_json
when /^\/player\/(\d+)\/?$/
FormResponse State.instance.players.select { |k, v| v["PlayerID"] == $1 }.to_json
when /^\/state\/?$/
FormResponse State.instance.entities["1"].to_json
when /^\/previous\/?$/
FormResponse State.instance.lastGame.to_json, code=200, force=true
else
FormResponse "{\"error\":\"Invalid API request\"}", code=404
end
else
FormResponse "{\"error\":\"Invalid API request\"}", code=404
end
client.close
end
socket.close