lomic 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -15,7 +15,7 @@ lib/lomic.rb
15
15
  lib/lomic/Event.rb
16
16
  lib/lomic/EventEngine.rb
17
17
  lib/lomic/GameState.rb
18
- lib/lomic/Lomic.rb
18
+ lib/lomic/LomicBase.rb
19
19
  lib/lomic/LomicParser.rb
20
20
  lib/lomic/Rule.rb
21
21
  lomic.gemspec
data/Rakefile CHANGED
@@ -10,7 +10,9 @@ begin
10
10
  gem.email = "gilbertbgarza@gmail.com"
11
11
  gem.homepage = "http://github.com/mindeavor/lomic"
12
12
  gem.authors = ["Gilbert B Garza"]
13
+ gem.add_development_dependency "commander", ">= 4.0.3"
13
14
  gem.add_development_dependency "cucumber", ">= 0.8.0"
15
+ gem.add_development_dependency "json", ">= 1.4.3"
14
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
17
  end
16
18
  Jeweler::GemcutterTasks.new
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :build:
4
- :minor: 0
5
- :patch: 2
4
+ :minor: 1
5
+ :patch: 0
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ require 'commander/import'
3
+ require 'jeweler'
4
+ # $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
+ dir = File.join(File.dirname(__FILE__), '..')
6
+
7
+ program :version, Jeweler::VersionHelper.new(dir).to_s
8
+ program :description, 'Starts up an instance of lomic'
9
+
10
+ global_option '--verbose'
11
+
12
+ command :start do |c|
13
+ c.syntax = 'lomic start <address:port> <rule file>'
14
+ c.description = 'Starts an instance of lomic that listens to <address:port> and populates its rules with <rule file>.'
15
+ c.action do |args,options|
16
+ address,rfilename = args
17
+ ip,port = address.split(':')
18
+ # TODO: fancy error checking
19
+ require 'socket'
20
+ require 'lomic'
21
+ socket = TCPSocket::open(ip,port)
22
+ puts "\nLomic started, listening for first event on: #{args.inspect}"
23
+ Lomic.verbose = options.verbose
24
+ Lomic.new_game(socket,rfilename)
25
+ end
26
+ end
27
+
@@ -0,0 +1,15 @@
1
+
2
+ rule 101 do |g|
3
+ event "game:start" do
4
+ puts 'Game starting'
5
+ listen 'path:left', 'path:right'
6
+ end
7
+
8
+ event "path:left" do
9
+ puts 'Took the LEFT path!'
10
+ end
11
+
12
+ event "path:right" do
13
+ puts 'Took the RIGHT path!'
14
+ end
15
+ end
@@ -1,9 +1,9 @@
1
- class Player < Lomic
1
+ class Player < LomicBase
2
2
  var :number
3
3
  end
4
4
 
5
5
  # global data: all lomic functions search exclusively in this class for global variables
6
- class Globals < Lomic
6
+ class Globals < LomicBase
7
7
  var :currentRule # Rule is a built-in class type
8
8
  var :ruleChangeType => ""
9
9
 
@@ -1,4 +1,4 @@
1
- class Globals < Lomic
1
+ class Globals < LomicBase
2
2
  var :number => 100
3
3
  end
4
4
 
@@ -1,4 +1,4 @@
1
- class Globals < Lomic
1
+ class Globals < LomicBase
2
2
  var :didiwin => 'No...'
3
3
  end
4
4
 
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path(File.dirname(__FILE__) + '/lib')
2
+ require 'lomic/Event'
3
+ require 'lomic/EventEngine'
4
+ require 'lomic/GameState'
5
+ require 'lomic/LomicBase'
6
+ require 'lomic/Rule'
7
+ require 'lomic/LomicParser'
8
+
9
+ module Lomic
10
+ @@verbose = false
11
+ require 'json'
12
+
13
+ def self.new_game(socket,filename)
14
+ gstate = LomicParser.load_source(filename)
15
+ puts 'Waiting for response from server...'
16
+ first_event = socket.gets.strip!
17
+ puts "Received first event: #{first_event}" if verbose?
18
+ gstate.run(first_event,socket)
19
+ end
20
+
21
+ def self.verbose?
22
+ @@verbose
23
+ end
24
+
25
+ def self.verbose=(new_val)
26
+ @@verbose = new_val
27
+ end
28
+ end
@@ -7,10 +7,11 @@ class EventEngine
7
7
  @counter = 0 # the number of event code blocks executed
8
8
  end
9
9
 
10
- def run(event_name, rules)
10
+ def run(event_name, rules, socket)
11
+ @socket = socket
11
12
  add_sort_events rules
12
13
  @next_event = event_name
13
-
14
+
14
15
  begin
15
16
  event_name = @next_event
16
17
  @next_event = nil
@@ -21,6 +22,19 @@ class EventEngine
21
22
  end while @next_event.nil? == false
22
23
  end
23
24
 
25
+ def listen(*valid_events)
26
+ puts "Listening for valid events: #{valid_events.inspect}" if Lomic.verbose?
27
+ res = @socket.gets.strip!
28
+ if not valid_events.include? res
29
+ result = {:status => 'fail', :reason => 'invalid event'}.to_json
30
+ @socket.puts result
31
+ else
32
+ result = {:status => 'ok'}.to_json
33
+ @socket.puts result
34
+ end
35
+ set_next res
36
+ end
37
+
24
38
  def set_next(event_name)
25
39
  @next_event = event_name
26
40
  end
@@ -27,8 +27,8 @@ class GameState
27
27
  @rules.push(rule)
28
28
  end
29
29
 
30
- def emit(event_name)
31
- @em.run(event_name,@globals.rules)
30
+ def run(event_name,socket)
31
+ @em.run(event_name,@globals.rules,socket)
32
32
  end
33
33
  end
34
34
 
@@ -1,9 +1,10 @@
1
1
  module Lomic
2
2
 
3
3
  ### This class helps clean up in-game class definitions
4
- class Lomic
4
+ class LomicBase
5
5
  class << self
6
6
  public :define_method, :remove_method
7
+ public :class_variable_get
7
8
  end
8
9
 
9
10
  def self.var(symbols)
@@ -81,10 +82,12 @@ class Lomic
81
82
 
82
83
  def initialize
83
84
  @init_used = {}
84
- inits = self.class.class_eval "@@__#{className}_inits__"
85
- inits.each do |var,val|
86
- instance_variable_set "@#{var}", val
87
- @init_used[var] = true
85
+ if self.class.class_variable_defined? "@@__#{className}_inits__"
86
+ inits = self.class.class_variable_get "@@__#{className}_inits__"
87
+ inits.each do |var,val|
88
+ instance_variable_set "@#{var}", val
89
+ @init_used[var] = true
90
+ end
88
91
  end
89
92
  end
90
93
 
@@ -14,7 +14,14 @@ class LomicParser
14
14
  # TODO: ensure number is int
15
15
  @currentRule = Rule.new(number)
16
16
  if @first_rule
17
- @state.globals = instance_eval 'Globals.new'
17
+ begin
18
+ @state.globals = instance_eval 'Globals.new'
19
+ rescue
20
+ # No Globals class was declared. Create one
21
+ instance_eval "class Globals < LomicBase
22
+ end"
23
+ @state.globals = instance_eval 'Globals.new'
24
+ end
18
25
  @first_rule = false
19
26
  end
20
27
 
@@ -35,7 +42,6 @@ class LomicParser
35
42
  def self.load_source(filename)
36
43
  dsl = new
37
44
  dsl.instance_eval(File.read(filename),filename)
38
- # dsl.gamestate.globals = (dsl.instance_eval 'Globals.new')
39
45
  dsl.gamestate
40
46
  end
41
47
  end
@@ -1,6 +1,6 @@
1
1
  module Lomic
2
2
 
3
- class Rule < Lomic
3
+ class Rule < LomicBase
4
4
  def initialize(number)
5
5
  @number = number
6
6
  @event_bag = {} # "event_name" => [Event]
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gilbert B Garza
@@ -15,13 +15,29 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-12 00:00:00 -05:00
19
- default_executable:
18
+ date: 2010-06-15 00:00:00 -05:00
19
+ default_executable: lomic
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: cucumber
22
+ name: commander
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 57
30
+ segments:
31
+ - 4
32
+ - 0
33
+ - 3
34
+ version: 4.0.3
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: cucumber
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
25
41
  none: false
26
42
  requirements:
27
43
  - - ">="
@@ -33,11 +49,27 @@ dependencies:
33
49
  - 0
34
50
  version: 0.8.0
35
51
  type: :development
36
- version_requirements: *id001
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: json
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 1
62
+ segments:
63
+ - 1
64
+ - 4
65
+ - 3
66
+ version: 1.4.3
67
+ type: :development
68
+ version_requirements: *id003
37
69
  description: Lomic is a Domain Specific Language (DSL) intended to be used for Pomic, a programming version of the game Nomic.
38
70
  email: gilbertbgarza@gmail.com
39
- executables: []
40
-
71
+ executables:
72
+ - lomic
41
73
  extensions: []
42
74
 
43
75
  extra_rdoc_files:
@@ -51,6 +83,7 @@ files:
51
83
  - Rakefile
52
84
  - TODO
53
85
  - VERSION.yml
86
+ - examples/listen_path.rb
54
87
  - examples/nomic_initial_rules.rb
55
88
  - examples/priority.rb
56
89
  - examples/simple.rb
@@ -58,13 +91,14 @@ files:
58
91
  - features/step_definitions/steps.rb
59
92
  - features/support/env.rb
60
93
  - features/var_init.feature
61
- - lib/Lomic.rb
94
+ - lib/lomic.rb
62
95
  - lib/lomic/Event.rb
63
96
  - lib/lomic/EventEngine.rb
64
97
  - lib/lomic/GameState.rb
65
- - lib/lomic/Lomic.rb
98
+ - lib/lomic/LomicBase.rb
66
99
  - lib/lomic/LomicParser.rb
67
100
  - lib/lomic/Rule.rb
101
+ - bin/lomic
68
102
  has_rdoc: true
69
103
  homepage: http://github.com/mindeavor/lomic
70
104
  licenses: []
@@ -100,6 +134,7 @@ signing_key:
100
134
  specification_version: 3
101
135
  summary: A Ruby DSL for the game Nomic
102
136
  test_files:
137
+ - examples/listen_path.rb
103
138
  - examples/nomic_initial_rules.rb
104
139
  - examples/priority.rb
105
140
  - examples/simple.rb
@@ -1,16 +0,0 @@
1
- $:.push File.expand_path(File.dirname(__FILE__) + '/lib')
2
- require 'lomic/Event'
3
- require 'lomic/EventEngine'
4
- require 'lomic/GameState'
5
- require 'lomic/Lomic'
6
- require 'lomic/Rule'
7
- require 'lomic/LomicParser'
8
-
9
- module Lomic
10
- VERSION = '0.0.1'
11
-
12
- def self.parse(filename, start_event="game:start")
13
- gstate = LomicParser.load_source(filename)
14
- gstate.emit start_event
15
- end
16
- end