forked from wstrinz/adventure-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.rb
42 lines (36 loc) · 975 Bytes
/
executor.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
require 'redis'
require 'json'
require_relative 'text-adventure/lib/bootstrap'
require_relative 'text-adventure/lib/game'
def setup_game
b = Bootstrap.new 'text-adventure/data/epic_adventure/locations.yml', 'text-adventure/data/epic_adventure/messages.yml'
@game = Game.new(b)
@bot_name = ["@adventure","@a"]
end
def wait_for_messages
@from_redis ||= Redis.new
loop do
msg = @from_redis.brpop("to_game").last
js = JSON.parse(msg)
query = extract_message(js)
puts "sending '#{query}' to game"
send_reply(response: execute_message(query), original: js)
end
end
def extract_message(json)
@bot_name.each do |name|
json["text"] = json["text"].gsub(/^#{name}/,"").strip
end
json["text"]
end
def send_reply(message)
puts "sending #{message.to_json}"
(@to_redis ||= Redis.new).lpush("to_user",message.to_json)
end
def execute_message(message)
@game.engine.eval_msg(message)
end
if __FILE__ == $0
setup_game
wait_for_messages
end