-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathz_executor.rb
106 lines (95 loc) · 2.14 KB
/
z_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
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
require 'redis'
require 'json'
require 'dorothy'
require 'uri'
require 'open-uri'
def setup_game(file='games/LostPig.z8')
@game = Z::Machine.new(file)
@game.run
@bot_name = ["@adventure","@a"]
@game.output.join
end
def wait_for_messages
@from_redis ||= Redis.new
loop do
msg = @from_redis.brpop("to_game").last
js = JSON.parse(msg)
unless js["user_name"] && @bot_name.include?(js["user_name"])
query = extract_message(js)
command_response = check_command(query)
if command_response
puts "special command #{query} run"
send_reply(response: command_response, original: js)
else
puts "sending '#{query}' to game"
send_reply(response: execute_message(query), original: js)
end
end
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 check_command(message)
case message
when /^\/reset/
setup_game
@game.output.join
when /^\/load .+/
load_game(message.scan(/^\/load (.+)/).first.first)
when /^\/help/
"special commands are '/reset', and '/load <file>'"
when /^\/games/
list_games
else
nil
end
end
def url_to_filename(url)
url.split("/").last
end
def load_game(name)
if name[0] == "<"
name = name[1..-2]
end
if name[URI::regexp]
load_remote name
setup_game("games/#{url_to_filename(name)}")
else
if File.exist?("games/#{name}")
setup_game("games/#{name}")
else
"cannot find file games/#{name}"
end
end
end
def list_games
"games: " + (Dir.foreach('games').to_a - [".", ".."]).join("\n")
end
def load_remote(url)
open("games/" + url_to_filename(url),'w'){|f|
puts url
f.write open(url).read
}
end
def execute_message(message)
begin
@game.output.clear
@game.keyboard << message + "\n"
@game.run
@game.output.join
rescue
"sorry, there was an error running '#{message}': " + $!.message.to_s
end
end
if __FILE__ == $0
setup_game
wait_for_messages
end