gamefic-sdk 1.7.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/bin/gamefic +16 -0
- data/lib/gamefic-sdk.rb +4 -24
- data/lib/gamefic-sdk/diagram.rb +205 -0
- data/lib/gamefic-sdk/scaffold.rb +54 -0
- data/lib/gamefic-sdk/server.rb +81 -65
- data/lib/gamefic-sdk/shell.rb +22 -129
- data/lib/gamefic-sdk/tasks.rb +40 -0
- data/lib/gamefic-sdk/tasks/common.rb +21 -0
- data/lib/gamefic-sdk/tasks/ruby.rb +99 -0
- data/lib/gamefic-sdk/tasks/web.rb +82 -0
- data/lib/gamefic-sdk/version.rb +5 -5
- metadata +123 -139
- data/bin/gfk +0 -6
- data/html/core/engine.js +0 -114
- data/html/core/jquery.js +0 -5
- data/html/core/jquery.modal.css +0 -44
- data/html/core/jquery.modal.js +0 -225
- data/html/core/js.cookie.js +0 -137
- data/html/sinatra/engine.js +0 -65
- data/html/skins/hypertext/compass.png +0 -0
- data/html/skins/hypertext/index.html +0 -149
- data/html/skins/hypertext/style.css +0 -74
- data/html/skins/minimal/index.html.erb +0 -22
- data/html/skins/minimal/index.rb +0 -4
- data/html/skins/minimal/play.js +0 -36
- data/html/skins/minimal/style.css +0 -8
- data/html/skins/multimedia/close.png +0 -0
- data/html/skins/multimedia/index.html +0 -179
- data/html/skins/multimedia/mobile.css +0 -10
- data/html/skins/multimedia/spinner.gif +0 -0
- data/html/skins/multimedia/style.css +0 -186
- data/html/skins/standard/index.html.erb +0 -22
- data/html/skins/standard/index.rb +0 -4
- data/html/skins/standard/play.js +0 -18
- data/html/skins/standard/style.css +0 -73
- data/lib/gamefic-sdk/build.rb +0 -30
- data/lib/gamefic-sdk/config.rb +0 -126
- data/lib/gamefic-sdk/debug.rb +0 -4
- data/lib/gamefic-sdk/debug/action.rb +0 -30
- data/lib/gamefic-sdk/debug/plot.rb +0 -23
- data/lib/gamefic-sdk/platform.rb +0 -22
- data/lib/gamefic-sdk/platform/base.rb +0 -68
- data/lib/gamefic-sdk/platform/gfic.rb +0 -27
- data/lib/gamefic-sdk/platform/sinatra.rb +0 -98
- data/lib/gamefic-sdk/platform/web.rb +0 -176
- data/lib/gamefic-sdk/platform/web/app_config.rb +0 -81
- data/lib/gamefic-sdk/platform/web/engine.rb +0 -44
- data/lib/gamefic-sdk/platform/web/user.rb +0 -19
- data/lib/gamefic-sdk/shell/init.rb +0 -83
- data/lib/gamefic-sdk/shell/test.rb +0 -32
- data/scripts/save-restore-undo.plot.rb +0 -25
- data/scripts/snapshots.plot.rb +0 -16
- data/scripts/standard.plot.rb +0 -10
- data/scripts/standard/actions.plot.rb +0 -13
- data/scripts/standard/actions/drop.plot.rb +0 -19
- data/scripts/standard/actions/enter.plot.rb +0 -19
- data/scripts/standard/actions/go.plot.rb +0 -48
- data/scripts/standard/actions/insert.plot.rb +0 -37
- data/scripts/standard/actions/inventory.plot.rb +0 -9
- data/scripts/standard/actions/leave.plot.rb +0 -30
- data/scripts/standard/actions/look.plot.rb +0 -108
- data/scripts/standard/actions/nil.plot.rb +0 -36
- data/scripts/standard/actions/place.plot.rb +0 -41
- data/scripts/standard/actions/quit.plot.rb +0 -11
- data/scripts/standard/actions/take.plot.rb +0 -31
- data/scripts/standard/actions/talk.plot.rb +0 -27
- data/scripts/standard/actions/wait.plot.rb +0 -3
- data/scripts/standard/clothing.plot.rb +0 -4
- data/scripts/standard/clothing/actions.plot.rb +0 -4
- data/scripts/standard/clothing/actions/doff.plot.rb +0 -12
- data/scripts/standard/clothing/actions/drop.plot.rb +0 -8
- data/scripts/standard/clothing/actions/inventory.plot.rb +0 -14
- data/scripts/standard/clothing/actions/wear.plot.rb +0 -20
- data/scripts/standard/clothing/entities.plot.rb +0 -7
- data/scripts/standard/clothing/entities/clothing.plot.rb +0 -5
- data/scripts/standard/clothing/entities/coat.plot.rb +0 -3
- data/scripts/standard/clothing/entities/gloves.plot.rb +0 -3
- data/scripts/standard/clothing/entities/hat.plot.rb +0 -3
- data/scripts/standard/clothing/entities/pants.plot.rb +0 -3
- data/scripts/standard/clothing/entities/shirt.plot.rb +0 -3
- data/scripts/standard/clothing/entities/shoes.plot.rb +0 -3
- data/scripts/standard/container.plot.rb +0 -28
- data/scripts/standard/container/actions.plot.rb +0 -0
- data/scripts/standard/container/entities.plot.rb +0 -5
- data/scripts/standard/direction.plot.rb +0 -55
- data/scripts/standard/edible.plot.rb +0 -21
- data/scripts/standard/entities.plot.rb +0 -12
- data/scripts/standard/entities/character.plot.rb +0 -10
- data/scripts/standard/entities/fixture.plot.rb +0 -3
- data/scripts/standard/entities/item.plot.rb +0 -3
- data/scripts/standard/entities/portal.plot.rb +0 -44
- data/scripts/standard/entities/receptacle.plot.rb +0 -3
- data/scripts/standard/entities/room.plot.rb +0 -57
- data/scripts/standard/entities/rubble.plot.rb +0 -11
- data/scripts/standard/entities/scenery.plot.rb +0 -3
- data/scripts/standard/entities/supporter.plot.rb +0 -7
- data/scripts/standard/entities/thing.plot.rb +0 -7
- data/scripts/standard/give.plot.rb +0 -27
- data/scripts/standard/lockable.plot.rb +0 -79
- data/scripts/standard/modules.plot.rb +0 -9
- data/scripts/standard/modules/attachable.plot.rb +0 -12
- data/scripts/standard/modules/auto_takes.plot.rb +0 -50
- data/scripts/standard/modules/enterable.plot.rb +0 -15
- data/scripts/standard/modules/explicit_exits.plot.rb +0 -15
- data/scripts/standard/modules/itemizable.plot.rb +0 -18
- data/scripts/standard/modules/locale_description.plot.rb +0 -20
- data/scripts/standard/modules/parent-room.plot.rb +0 -9
- data/scripts/standard/modules/portable.plot.rb +0 -11
- data/scripts/standard/modules/use.plot.rb +0 -51
- data/scripts/standard/openable.plot.rb +0 -43
- data/scripts/standard/pathfinder.plot.rb +0 -65
- data/scripts/standard/queries.plot.rb +0 -21
- data/scripts/standard/test.plot.rb +0 -10
- data/scripts/undo.plot.rb +0 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ce08abec9929267cbcd41b95da9a7bd313e3449ce3160277b26160aadd8437bf
|
|
4
|
+
data.tar.gz: 1e7c8e9827b24816addfec88ff81f1c9d9e8112f9e5422f045a2ef07471375b1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1dc60435ac2e35ed97f76de2d2ba8494c5dad6a54b6aeb943b4767159c866cdd799e21763f29ed1b9360e117a8b2912934ad607b6b343d986cac9c4769d004bd
|
|
7
|
+
data.tar.gz: 76c10bcf57491365f4b61b78f2f076d3bdd9bab43b0d46a6ed838b612c2a7c82026cb6d2ef5d94bc7f0fa817fe364ff3325b1550e8de7bfc49b5e95e47a4d910
|
data/bin/gamefic
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'gamefic-sdk'
|
|
4
|
+
require 'gamefic-sdk/shell'
|
|
5
|
+
|
|
6
|
+
Gamefic::Sdk::Shell.start(ARGV)
|
|
7
|
+
|
|
8
|
+
#!/usr/bin/env ruby
|
|
9
|
+
|
|
10
|
+
#require 'gamefic'
|
|
11
|
+
#require 'gamefic-tty'
|
|
12
|
+
|
|
13
|
+
# Make play the default command if the first argument is an existing file
|
|
14
|
+
#args = %w(play help).include?(ARGV[0]) || ARGV.count.zero? || !File.exist?(ARGV[0]) ? ARGV : ARGV.dup.unshift('play')
|
|
15
|
+
|
|
16
|
+
#Gamefic::Tty::Shell.start(args)
|
data/lib/gamefic-sdk.rb
CHANGED
|
@@ -1,30 +1,10 @@
|
|
|
1
1
|
require 'gamefic'
|
|
2
|
-
require 'gamefic-sdk/platform'
|
|
3
|
-
#require 'gamefic-sdk/plot_config'
|
|
4
|
-
require 'gamefic-sdk/debug'
|
|
5
2
|
require 'gamefic-sdk/version'
|
|
6
3
|
|
|
7
4
|
module Gamefic::Sdk
|
|
8
5
|
autoload :Server, 'gamefic-sdk/server'
|
|
9
|
-
autoload :
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
LIB_PATH = File.dirname(__FILE__)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
class Class
|
|
17
|
-
def descendants
|
|
18
|
-
result = []
|
|
19
|
-
ObjectSpace.each_object(::Class) {|klass| result << klass if klass < self }
|
|
20
|
-
result
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
class Gamefic::Entity
|
|
25
|
-
def self.names
|
|
26
|
-
result = []
|
|
27
|
-
Entity.descendants.each { |e| result << e.to_s.split('::').last }
|
|
28
|
-
result
|
|
29
|
-
end
|
|
6
|
+
autoload :Diagram, 'gamefic-sdk/diagram'
|
|
7
|
+
autoload :Scaffold, 'gamefic-sdk/scaffold'
|
|
8
|
+
autoload :Tasks, 'gamefic-sdk/tasks'
|
|
9
|
+
SCAFFOLDS_PATH = File.realpath(File.join(File.dirname(__FILE__), '..', 'scaffolds'))
|
|
30
10
|
end
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module Gamefic
|
|
4
|
+
module Sdk
|
|
5
|
+
class Diagram
|
|
6
|
+
class Position
|
|
7
|
+
attr_accessor :x, :y
|
|
8
|
+
|
|
9
|
+
def initialize x = 0, y = 0
|
|
10
|
+
@x = x
|
|
11
|
+
@y = y
|
|
12
|
+
freeze
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def move x, y
|
|
16
|
+
Position.new(self.x + x, self.y + y)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @return [Gamefic::Plot]
|
|
21
|
+
attr_reader :plot
|
|
22
|
+
|
|
23
|
+
def initialize plot
|
|
24
|
+
@plot = plot
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def get type
|
|
28
|
+
if valid?(type)
|
|
29
|
+
send(type)
|
|
30
|
+
else
|
|
31
|
+
raise ArgumentError, "Unknown diagram type '#{type}'"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def verbs
|
|
36
|
+
plot.verbs
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rooms
|
|
40
|
+
clear
|
|
41
|
+
position = Position.new
|
|
42
|
+
plot.entities.that_are(Room).each do |room|
|
|
43
|
+
proceed_from room, position
|
|
44
|
+
next_x = right
|
|
45
|
+
next_y = bottom
|
|
46
|
+
if next_x > next_y
|
|
47
|
+
position = Position.new(0, next_y + distance)
|
|
48
|
+
else
|
|
49
|
+
position = Position.new(next_x + distance, 0)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
elements.values
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def entities
|
|
56
|
+
plot.initial_state[:entities].map do |e|
|
|
57
|
+
e.transform_keys do |k|
|
|
58
|
+
str = k.to_s
|
|
59
|
+
if str.start_with?('@')
|
|
60
|
+
str[1..-1]
|
|
61
|
+
else
|
|
62
|
+
str
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def actions
|
|
69
|
+
plot.actions.map do |act|
|
|
70
|
+
{
|
|
71
|
+
verb: act.verb,
|
|
72
|
+
meta: act.meta?,
|
|
73
|
+
signature: act.signature
|
|
74
|
+
}
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def commands
|
|
79
|
+
{
|
|
80
|
+
actions: actions,
|
|
81
|
+
verbs: verbs,
|
|
82
|
+
syntaxes: syntaxes
|
|
83
|
+
}
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def verbs
|
|
87
|
+
plot.verbs
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def syntaxes
|
|
91
|
+
plot.syntaxes.map do |syn|
|
|
92
|
+
{
|
|
93
|
+
template: syn.template,
|
|
94
|
+
command: syn.command,
|
|
95
|
+
first_word: syn.first_word,
|
|
96
|
+
verb: syn.verb
|
|
97
|
+
}
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def valid? type
|
|
102
|
+
%w[rooms entities actions verbs syntaxes commands].include?(type)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def distance
|
|
108
|
+
150
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def elements
|
|
112
|
+
@elements ||= {}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def entity_uid entity
|
|
116
|
+
raise "Invalid entity" if plot.entities.index(entity).nil?
|
|
117
|
+
"EIN_#{plot.entities.index(entity)}"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def proceed_from room, position
|
|
121
|
+
return if elements.has_key?(room)
|
|
122
|
+
directions = ['north', 'south', 'west', 'east', 'northwest', 'northeast', 'southwest', 'southeast']
|
|
123
|
+
rel = { data: {} }
|
|
124
|
+
rel[:data][:id] = entity_uid(room)
|
|
125
|
+
rel[:data][:label] = room.definitely
|
|
126
|
+
rel[:position] = { x: position.x, y: position.y }
|
|
127
|
+
elements[room] = rel
|
|
128
|
+
room.children.that_are(Portal).each do |portal|
|
|
129
|
+
next if portal.destination.nil?
|
|
130
|
+
# For lack of a better way to lay them out, 'up' and 'down' are not
|
|
131
|
+
# treated as specific directions
|
|
132
|
+
if portal.direction.nil? or portal.direction.to_s == 'up' or portal.direction.to_s == 'down'
|
|
133
|
+
direction = directions.shift
|
|
134
|
+
new_pos = move_from(position, direction)
|
|
135
|
+
while position_taken?(new_pos)
|
|
136
|
+
direction = directions.shift
|
|
137
|
+
return if direction.nil?
|
|
138
|
+
new_pos = move_from(position, direction)
|
|
139
|
+
end
|
|
140
|
+
else
|
|
141
|
+
directions.delete portal.direction.to_s
|
|
142
|
+
new_pos = move_from(position, portal.direction.to_s)
|
|
143
|
+
end
|
|
144
|
+
proceed_from portal.destination, new_pos
|
|
145
|
+
pel = { data: {} }
|
|
146
|
+
pel[:data][:id] = entity_uid(portal)
|
|
147
|
+
pel[:data][:source] = entity_uid(room)
|
|
148
|
+
pel[:data][:target] = entity_uid(portal.destination)
|
|
149
|
+
elements[portal] = pel
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def clear
|
|
154
|
+
elements.clear
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def bottom
|
|
158
|
+
max_y = 0
|
|
159
|
+
elements.values.each do |el|
|
|
160
|
+
next if el[:position].nil?
|
|
161
|
+
max_y = [max_y, el[:position][:y]].max
|
|
162
|
+
end
|
|
163
|
+
max_y
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def right
|
|
167
|
+
max_x = 0
|
|
168
|
+
elements.values.each do |el|
|
|
169
|
+
next if el[:position].nil?
|
|
170
|
+
max_x = [max_x, el[:position][:x]].max
|
|
171
|
+
end
|
|
172
|
+
max_x
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def position_taken?(position)
|
|
176
|
+
elements.values.each do |el|
|
|
177
|
+
next if el[:position].nil?
|
|
178
|
+
return true if el[:position][:x] == position.x and el[:position][:y] == position.y
|
|
179
|
+
end
|
|
180
|
+
false
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def move_from position, direction
|
|
184
|
+
case direction
|
|
185
|
+
when 'north'
|
|
186
|
+
position.move 0, -distance
|
|
187
|
+
when 'south'
|
|
188
|
+
position.move 0, distance
|
|
189
|
+
when 'west'
|
|
190
|
+
position.move -distance, 0
|
|
191
|
+
when 'east'
|
|
192
|
+
position.move distance, 0
|
|
193
|
+
when 'northwest'
|
|
194
|
+
position.move -distance * 0.5, -distance * 0.5
|
|
195
|
+
when 'northeast'
|
|
196
|
+
position.move distance * 0.5, -distance * 0.5
|
|
197
|
+
when 'southwest'
|
|
198
|
+
position.move -distance * 0.5, distance * 0.5
|
|
199
|
+
when 'southeast'
|
|
200
|
+
position.move distance * 0.5, distance * 0.5
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'erb'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'shellwords'
|
|
6
|
+
|
|
7
|
+
module Gamefic
|
|
8
|
+
module Sdk
|
|
9
|
+
module Scaffold
|
|
10
|
+
class Binder < OpenStruct
|
|
11
|
+
def get_binding
|
|
12
|
+
binding()
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def render(file, data)
|
|
19
|
+
template = File.read(file)
|
|
20
|
+
erb = ERB.new(template)
|
|
21
|
+
erb.result(data.get_binding)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def custom_copy origin, destination, data
|
|
25
|
+
FileUtils.mkdir_p File.dirname(destination)
|
|
26
|
+
if destination.end_with?('.gf.erb')
|
|
27
|
+
File.write destination[0..-8], render(origin, data)
|
|
28
|
+
else
|
|
29
|
+
FileUtils.cp_r origin, destination
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def build name, destination
|
|
34
|
+
dir = File.join(SCAFFOLDS_PATH, name)
|
|
35
|
+
raise LoadError, "Scaffold `#{name}` does not exist" unless File.directory?(dir)
|
|
36
|
+
path = Pathname.new('.').join(destination).realdirpath
|
|
37
|
+
data = Binder.new(name: File.basename(path))
|
|
38
|
+
files = Dir.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).select { |entry| File.file?(entry) }
|
|
39
|
+
map = {}
|
|
40
|
+
files.each do |file|
|
|
41
|
+
rename = File.join(File.dirname(file), File.basename(file).gsub('__name__', data.name))
|
|
42
|
+
dst = File.join(destination, rename[dir.length..-1])
|
|
43
|
+
if File.file?(dst)
|
|
44
|
+
raise "Gamefic generation would overwrite existing file #{rename}"
|
|
45
|
+
else
|
|
46
|
+
map[file] = dst
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
map.each_pair { |src, dst| custom_copy src, dst, data }
|
|
50
|
+
system "bundle install --gemfile=#{Shellwords.escape(File.join(destination, 'Gemfile'))}"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/gamefic-sdk/server.rb
CHANGED
|
@@ -1,65 +1,81 @@
|
|
|
1
|
-
require 'sinatra/base'
|
|
2
|
-
require 'yaml'
|
|
3
|
-
|
|
4
|
-
module Gamefic
|
|
5
|
-
module Sdk
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
set :
|
|
9
|
-
|
|
10
|
-
get '/' do
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
@@plot.
|
|
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
|
-
|
|
1
|
+
require 'sinatra/base'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
|
|
4
|
+
module Gamefic
|
|
5
|
+
module Sdk
|
|
6
|
+
class Server < Sinatra::Base
|
|
7
|
+
set :port, 4342
|
|
8
|
+
set :server, :webrick
|
|
9
|
+
|
|
10
|
+
get '/' do
|
|
11
|
+
File.read File.join(settings.public_folder, 'index.html')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
post '/start' do
|
|
15
|
+
content_type :json
|
|
16
|
+
reset_features
|
|
17
|
+
load File.join(settings.source_dir, 'main.rb')
|
|
18
|
+
@@plot = Gamefic::Plot.new
|
|
19
|
+
@@character = @@plot.get_player_character
|
|
20
|
+
@@plot.introduce @@character
|
|
21
|
+
@@plot.ready
|
|
22
|
+
@@character.output.to_json
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
post '/receive' do
|
|
26
|
+
content_type :json
|
|
27
|
+
@@character.queue.push params['command']
|
|
28
|
+
{}.to_json
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
post '/update' do
|
|
32
|
+
content_type :json
|
|
33
|
+
@@plot.update
|
|
34
|
+
@@plot.ready
|
|
35
|
+
# @@character.state.merge(input: params['command'], continued: @@character.queue.any?).to_json
|
|
36
|
+
@@character.output.to_json
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
get '/snapshot' do
|
|
40
|
+
content_type :json
|
|
41
|
+
@@plot.save.to_json
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
post '/restore' do
|
|
45
|
+
content_type :json
|
|
46
|
+
# The snapshot needs to be received as a JSON string because of issues
|
|
47
|
+
# with IndifferentHash malforming arrays.
|
|
48
|
+
snapshot = JSON.parse(params['snapshot'], symbolize_names: true)
|
|
49
|
+
@@plot.restore snapshot
|
|
50
|
+
@@character.cue @@plot.default_scene
|
|
51
|
+
@@plot.update
|
|
52
|
+
@@plot.ready
|
|
53
|
+
@@character.output.to_json
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def reset_features
|
|
57
|
+
@@old_features ||= $LOADED_FEATURES.clone
|
|
58
|
+
@@old_constants ||= Object.constants(false)
|
|
59
|
+
$LOADED_FEATURES.keep_if { |e| @@old_features.include?(e) }
|
|
60
|
+
Object.constants(false).each do |const|
|
|
61
|
+
Object.send(:remove_const, const) unless @@old_constants.include?(const)
|
|
62
|
+
end
|
|
63
|
+
Gamefic::Plot.blocks.clear
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class << self
|
|
67
|
+
# def run!
|
|
68
|
+
# start_browser if settings.browser
|
|
69
|
+
# super
|
|
70
|
+
# end
|
|
71
|
+
|
|
72
|
+
# def start_browser
|
|
73
|
+
# Thread.new {
|
|
74
|
+
# sleep 1 until Server.running?
|
|
75
|
+
# `start http://localhost:#{settings.port}`
|
|
76
|
+
# }
|
|
77
|
+
# end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|