gamefic 0.0.1

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.
@@ -0,0 +1,117 @@
1
+ module Gamefic
2
+
3
+ class Syntax
4
+ attr_reader :template, :command, :arguments
5
+ @@defaults = Array.new
6
+ def initialize(story, *arguments)
7
+ if arguments.length < 2
8
+ raise "Syntax.new requires at least two arguments (template and command)"
9
+ end
10
+ @template = arguments.shift
11
+ @command = arguments.shift
12
+ @arguments = arguments
13
+ if story == nil
14
+ @@defaults.push self
15
+ else
16
+ story.send :add_syntax, self
17
+ @story = story
18
+ end
19
+ end
20
+ def self.defaults
21
+ @@defaults.clone
22
+ end
23
+ def self.match(input, syntaxes)
24
+ # Given the input, return all the syntaxes that potentially match it.
25
+ matches = Array.new
26
+ words = input.split_words
27
+ syntaxes.each { |syntax|
28
+ input_words = words.clone
29
+ tokens = Hash.new
30
+ syntax_words = syntax.template.split_words
31
+ while syntax_words.length > 0
32
+ if input_words.length == 0
33
+ # No more input. Break with an imbalance.
34
+ break
35
+ end
36
+ symbol = syntax_words.shift
37
+ if symbol[0, 1] == ":"
38
+ if syntax_words.length == 0
39
+ # Last syntax word.
40
+ tokens[symbol[1..-1].to_sym] = input_words.join(' ')
41
+ input_words.clear
42
+ break
43
+ elsif input_words.length == 0
44
+ # Last input word.
45
+ break
46
+ else
47
+ non_vars = syntax_words.clone.delete_if { |w|
48
+ w[0, 1] == ':'
49
+ }
50
+ if non_vars.length == 0
51
+ # All remaining words in the syntax are variables. Dump everything now.
52
+ tokens[symbol[1..-1].to_sym] = input_words.join(' ')
53
+ syntax_words.clear
54
+ input_words.clear
55
+ else
56
+ next_syntax_word = syntax_words.shift
57
+ token = ''
58
+ if syntax_words.length == 0
59
+ last_input_word = input_words.pop
60
+ if last_input_word == next_syntax_word
61
+ tokens[symbol[1..-1].to_sym] = input_words.join(' ')
62
+ input_words.clear
63
+ end
64
+ break
65
+ end
66
+ next_input_word = input_words.shift
67
+ while next_input_word != next_syntax_word and input_words.length > 0
68
+ token = token + " #{next_input_word}"
69
+ next_input_word = input_words.shift
70
+ end
71
+ if input_words.length == 0 and syntax_words.length > 0
72
+ break
73
+ else
74
+ tokens[symbol[1..-1].to_sym] = token.strip
75
+ end
76
+ end
77
+ end
78
+ else
79
+ if input_words[0] == symbol
80
+ input_words.shift
81
+ else
82
+ break
83
+ end
84
+ end
85
+ end
86
+ if input_words.length == 0 and syntax_words.length == 0
87
+ arguments = Array.new
88
+ syntax.arguments.each { |a|
89
+ if a.kind_of?(Symbol)
90
+ if tokens[a] != nil and tokens[a] != ''
91
+ arguments.push(tokens[a])
92
+ end
93
+ else
94
+ if a != nil and a != ''
95
+ arguments.push(a)
96
+ end
97
+ end
98
+ }
99
+ if syntax.arguments.length == arguments.length
100
+ matches.push CommandHandler.new(syntax.command, arguments)
101
+ end
102
+ end
103
+ }
104
+ matches.uniq! # TODO: Is this necessary?
105
+ return matches
106
+ end
107
+ end
108
+
109
+ class CommandHandler
110
+ attr_reader :command, :arguments
111
+ def initialize command, arguments
112
+ @command = command
113
+ @arguments = arguments
114
+ end
115
+ end
116
+
117
+ end
@@ -0,0 +1,64 @@
1
+ module Gamefic
2
+
3
+ class User
4
+ attr_reader :state, :character, :story
5
+ def initialize(story)
6
+ @story = story
7
+ @stream = UserStream.new
8
+ @state = UserState.new self
9
+ end
10
+ def stream
11
+ @stream
12
+ end
13
+ def state=(state_class)
14
+ @state = state_class.new self
15
+ end
16
+ def character=(entity)
17
+ @character = entity
18
+ @character.connect self
19
+ end
20
+ def refresh
21
+ # Nothing to do
22
+ end
23
+ def story
24
+ @story
25
+ end
26
+ def quit
27
+ exit
28
+ end
29
+ end
30
+
31
+ class UserStream
32
+ def initialize
33
+ @queue = Array.new
34
+ end
35
+ def send(data)
36
+ print data
37
+ end
38
+ def select
39
+ print "> "
40
+ @queue.push STDIN.gets
41
+ end
42
+ def recv
43
+ @queue.shift
44
+ end
45
+ end
46
+
47
+ class UserState
48
+ attr_reader :user
49
+ def initialize(user)
50
+ @user = user
51
+ post_initialize
52
+ end
53
+ def post_initialize
54
+ @user.character = Character.new user.story, :name => 'Player'
55
+ end
56
+ def update
57
+ line = @user.stream.recv
58
+ if line != nil
59
+ @user.character.perform line
60
+ end
61
+ end
62
+ end
63
+
64
+ end
data/lib/gamefic.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "gamefic/core_ext/array"
2
+ require "gamefic/core_ext/string"
3
+ require "gamefic/keywords"
4
+ require "gamefic/entity"
5
+ require "gamefic/character"
6
+ require "gamefic/action"
7
+ require "gamefic/syntax"
8
+ require "gamefic/query"
9
+ require "gamefic/director"
10
+ require "gamefic/plot"
11
+ require "gamefic/engine"
12
+ require "gamefic/user"
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gamefic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fred Snyder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An adventure game engine
14
+ email: fsnyder@gamefic.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/gamefic.rb
20
+ - lib/gamefic/entity.rb
21
+ - lib/gamefic/character.rb
22
+ - lib/gamefic/base.rb
23
+ - lib/gamefic/query.rb
24
+ - lib/gamefic/action.rb
25
+ - lib/gamefic/node.rb
26
+ - lib/gamefic/director.rb
27
+ - lib/gamefic/engine.rb
28
+ - lib/gamefic/user.rb
29
+ - lib/gamefic/plot.rb
30
+ - lib/gamefic/keywords.rb
31
+ - lib/gamefic/syntax.rb
32
+ - lib/gamefic/describable.rb
33
+ - lib/gamefic/action_ext/look.rb
34
+ - lib/gamefic/action_ext/inventory.rb
35
+ - lib/gamefic/action_ext/container.rb
36
+ - lib/gamefic/action_ext/meta.rb
37
+ - lib/gamefic/action_ext/traversal.rb
38
+ - lib/gamefic/core_ext/string.rb
39
+ - lib/gamefic/core_ext/array.rb
40
+ - lib/gamefic/entity_ext/fixture.rb
41
+ - lib/gamefic/entity_ext/item.rb
42
+ - lib/gamefic/entity_ext/scenery.rb
43
+ - lib/gamefic/entity_ext/portable.rb
44
+ - lib/gamefic/entity_ext/itemized.rb
45
+ - lib/gamefic/entity_ext/room.rb
46
+ - lib/gamefic/entity_ext/portal.rb
47
+ - lib/gamefic/entity_ext/zone.rb
48
+ - lib/gamefic/entity_ext/container.rb
49
+ homepage: http://gamefic.com
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.7
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Gamefic
73
+ test_files: []