Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

the start of the Universe

  • Loading branch information...
commit fc6be76c5bea17633c6e30591595aea29c0452b6 1 parent 0cf7586
Fernando Guillen authored May 12, 2011
2  .bundle/config
... ...
@@ -0,0 +1,2 @@
  1
+--- 
  2
+BUNDLE_DISABLE_SHARED_GEMS: "1"
1  .rvmrc
... ...
@@ -0,0 +1 @@
  1
+rvm 1.9.2@s2c
10  Gemfile
... ...
@@ -0,0 +1,10 @@
  1
+source 'http://rubygems.org'
  2
+
  3
+group :development do
  4
+  gem 'rack'
  5
+end
  6
+
  7
+group :test do
  8
+  gem 'mocha'
  9
+  gem 'ruby-debug19'
  10
+end
27  Gemfile.lock
... ...
@@ -0,0 +1,27 @@
  1
+GEM
  2
+  remote: http://rubygems.org/
  3
+  specs:
  4
+    archive-tar-minitar (0.5.2)
  5
+    columnize (0.3.2)
  6
+    linecache19 (0.5.12)
  7
+      ruby_core_source (>= 0.1.4)
  8
+    mocha (0.9.12)
  9
+    rack (1.2.2)
  10
+    ruby-debug-base19 (0.11.25)
  11
+      columnize (>= 0.3.1)
  12
+      linecache19 (>= 0.5.11)
  13
+      ruby_core_source (>= 0.1.4)
  14
+    ruby-debug19 (0.11.6)
  15
+      columnize (>= 0.3.1)
  16
+      linecache19 (>= 0.5.11)
  17
+      ruby-debug-base19 (>= 0.11.19)
  18
+    ruby_core_source (0.1.5)
  19
+      archive-tar-minitar (>= 0.5.2)
  20
+
  21
+PLATFORMS
  22
+  ruby
  23
+
  24
+DEPENDENCIES
  25
+  mocha
  26
+  rack
  27
+  ruby-debug19
46  QUESTIONS.md
Source Rendered
... ...
@@ -0,0 +1,46 @@
  1
+# Questions
  2
+
  3
+## symbolize keys in YAML load
  4
+
  5
+In my `S2C::CONFIG` hash all the keys are strings and I want them symbolized.
  6
+
  7
+The `Hash.symbolized_keys` on Rails hasn't solved my problem due only symbolize keys in the first level.
  8
+
  9
+## Modify an instance variable from out side
  10
+
  11
+Having this:
  12
+
  13
+    class Wadus
  14
+      attr_reader :variable
  15
+    end
  16
+    
  17
+    wadus = Wadus.new
  18
+    
  19
+How to modify the `wadus.variable` from outside?
  20
+
  21
+_I want this for testing pruposes._
  22
+
  23
+### Solution
  24
+
  25
+    wadus.instance_variable_set( :@variable, 'new value' )
  26
+
  27
+Actually I would like to know all the **introspection** methods available in Ruby.
  28
+    
  29
+## How to grow up the attributes
  30
+
  31
+I have stolen a _formula_ from an _OGame_ formula.. but I would like to build my own.
  32
+
  33
+## Improve the Config class
  34
+
  35
+I don't really like the way it is now.
  36
+
  37
+
  38
+## The possible statuses of the Construction
  39
+
  40
+The class `S2C::Models::Construction` has a list of the status that each descendent class can have, but I want every descendent class to **add its own statuses**.
  41
+
  42
+And also, I want to use this statuses list in the code, but if it is an array I don't know how to use them, and if is a hash, I don't understand the meaning.
  43
+
  44
+## How to reuse test_helper setup
  45
+
  46
+If I define a `Test::Unit::TestCase.setup` I can use it on any test class but if my test class **wants to add some new stuff to the setup** how can I deal with this?.
10  Rakefile
... ...
@@ -0,0 +1,10 @@
  1
+require 'rake'
  2
+require 'rake/testtask'
  3
+
  4
+task :default => :test
  5
+
  6
+Rake::TestTask.new do |t|
  7
+  t.libs << '.'
  8
+  t.test_files = FileList['test/*_test.rb']
  9
+  t.verbose = true
  10
+end
6  bin/space_suckers_chronicles.rb
... ...
@@ -0,0 +1,6 @@
  1
+require_relative '../lib/s2c'
  2
+
  3
+universe = S2C::Universe.new
  4
+planet = universe.create_planet( 'X700' )
  5
+planet.build_mine
  6
+universe.run
23  config/config.yml
... ...
@@ -0,0 +1,23 @@
  1
+mine:
  2
+  attack: 10
  3
+  defense: 10
  4
+  life: 10
  5
+  upgrade_timing: 10
  6
+  upgrade_black_stuff: 10
  7
+  power: 10
  8
+  
  9
+garage:
  10
+  attack: 10
  11
+  defense: 10
  12
+  life: 10
  13
+  upgrade_timing: 10
  14
+  upgrade_black_stuff: 10
  15
+  power: 10
  16
+    
  17
+mosquito:
  18
+  attack: 10
  19
+  defense: 10
  20
+  life: 10
  21
+  upgrade_timing: 10
  22
+  upgrade_black_stuff: 10
  23
+  power: 10
5  lib/s2c.rb
... ...
@@ -0,0 +1,5 @@
  1
+require_relative 's2c/config'
  2
+require_relative 's2c/models/planet'
  3
+require_relative 's2c/models/construction'
  4
+require_relative 's2c/models/mine'
  5
+require_relative 's2c/universe'
15  lib/s2c/config.rb
... ...
@@ -0,0 +1,15 @@
  1
+require 'yaml'
  2
+
  3
+module S2C
  4
+  class Config
  5
+    CONFIG_PATH = "#{File.dirname(__FILE__)}/../../config/config.yml"
  6
+    
  7
+    def self.config_path
  8
+      S2C::Config::CONFIG_PATH
  9
+    end
  10
+    
  11
+    def self.config
  12
+      YAML.load( File.read( S2C::Config.config_path ) )
  13
+    end
  14
+  end
  15
+end
104  lib/s2c/models/construction.rb
... ...
@@ -0,0 +1,104 @@
  1
+module S2C
  2
+  module Models
  3
+    class Construction
  4
+      attr_reader :id, :planet, :level, :type, :status, :process_tics
  5
+    
  6
+      # @@statuses = [
  7
+      #   :under_construction,
  8
+      #   :standby,
  9
+      #   :upgrading
  10
+      # ]
  11
+    
  12
+      def initialize( planet, type )
  13
+        @id = Time.now.to_i
  14
+        S2C::Universe.log( self, "Starting contruction Construction" )
  15
+        @planet = planet
  16
+        @level = 0
  17
+        @type = type
  18
+        @status = :under_construction
  19
+        @process_tics = self.upgrade_timing
  20
+        S2C::Universe.log( self, self.to_s )
  21
+      end
  22
+    
  23
+      # I know there are cleaner ways to define this methods 
  24
+      # but for the moment is ok.
  25
+      def attack
  26
+        self.property_value( 'attack' )
  27
+      end
  28
+    
  29
+      def defense
  30
+        self.property_value( 'defense' )
  31
+      end
  32
+    
  33
+      def power
  34
+        self.property_value( 'power' )
  35
+      end
  36
+    
  37
+      def upgrade_timing
  38
+        self.property_value( 'upgrade_timing' )
  39
+      end
  40
+    
  41
+      def upgrade_black_stuff
  42
+        self.property_value( 'upgrade_black_stuff' )
  43
+      end
  44
+    
  45
+      def upgrade_timing
  46
+        self.property_value( 'upgrade_timing' )
  47
+      end
  48
+    
  49
+      def property_value( property )
  50
+        init_value = S2C::Config.config[self.type][property]
  51
+        actual_value = init_value + ( init_value * ( @level * ( 1.1 ** @level ) ) ).round
  52
+      
  53
+        return actual_value
  54
+      end
  55
+    
  56
+      def upgrade
  57
+        S2C::Universe.log( self, "Upgrading" )
  58
+      
  59
+        if( self.status != :standby )
  60
+          raise Exception, "can't upgrade a Construction in status: '#{self.status}'"
  61
+        end
  62
+      
  63
+        if( self.planet.black_stuff < self.upgrade_black_stuff )
  64
+          raise Exception, 'not enough black stuff'
  65
+        end
  66
+      
  67
+        self.planet.remove_black_stuff( self.upgrade_black_stuff )
  68
+        @status = :upgrading
  69
+        @process_tics = self.upgrade_timing
  70
+      end
  71
+    
  72
+      def work_under_construction
  73
+        S2C::Universe.log( self, "In contruction" )
  74
+      
  75
+        @process_tics -= 1
  76
+        if( @process_tics == 0 )
  77
+          S2C::Universe.log( self, "Built" )
  78
+          @level = 1
  79
+          @status = :standby
  80
+        end
  81
+      end
  82
+    
  83
+      def work_upgrading
  84
+        S2C::Universe.log( self, "Upgrading" )
  85
+      
  86
+        @process_tics -= 1
  87
+        if( @process_tics == 0 )
  88
+          @level += 1
  89
+          @status = :standby
  90
+          S2C::Universe.log( self, "Upgraded to level #{self.level}" )
  91
+        end
  92
+      end
  93
+    
  94
+      def work
  95
+        S2C::Universe.log( self, "Working" )
  96
+        self.send( "work_#{self.status}" )
  97
+      end
  98
+    
  99
+      def to_s
  100
+        "type:#{self.type} level:#{self.level} status:#{self.status} tics:#{self.process_tics}"
  101
+      end
  102
+    end
  103
+  end
  104
+end
21  lib/s2c/models/mine.rb
... ...
@@ -0,0 +1,21 @@
  1
+module S2C
  2
+  module Models
  3
+    class Mine < S2C::Models::Construction
  4
+    
  5
+      # @@statuses = 
  6
+      #   super.statuses + [
  7
+      #     :extracting
  8
+      #   ]
  9
+      
  10
+      def initialize( planet )
  11
+        S2C::Universe.log( self, "Starting construction mine" )
  12
+        super( planet, 'mine' )
  13
+      end
  14
+
  15
+      def work_standby
  16
+        S2C::Universe.log( self, "Mine extracting" )
  17
+        self.planet.add_black_stuff( self.power )
  18
+      end
  19
+    end
  20
+  end
  21
+end
38  lib/s2c/models/planet.rb
... ...
@@ -0,0 +1,38 @@
  1
+module S2C
  2
+  module Models
  3
+    class Planet
  4
+      attr_reader :universe, :black_stuff, :name, :constructions
  5
+    
  6
+      def initialize( name )
  7
+        S2C::Universe.log( self, "Creating planet" )
  8
+        @black_stuff = 20
  9
+        @name = name
  10
+        @constructions = []
  11
+      end
  12
+    
  13
+      def add_black_stuff( amount )
  14
+        S2C::Universe.log( self, "Adding #{amount} black stuff to planet #{self.name}" )
  15
+        @black_stuff += amount
  16
+      end
  17
+    
  18
+      def remove_black_stuff( amount )
  19
+        S2C::Universe.log( self, "Removing #{amount} black stuff to planet #{self.name}" )
  20
+        @black_stuff -= black_stuff
  21
+      end
  22
+    
  23
+      def build_mine
  24
+        S2C::Universe.log( self, "Building a mine" )
  25
+        construction = S2C::Models::Mine.new( self )
  26
+        @constructions << construction
  27
+      end
  28
+      
  29
+      def id
  30
+        self.name
  31
+      end
  32
+      
  33
+      def to_s
  34
+        "constructions:#{self.constructions.size} black stuff:#{self.black_stuff}"
  35
+      end
  36
+    end
  37
+  end
  38
+end
66  lib/s2c/universe.rb
... ...
@@ -0,0 +1,66 @@
  1
+module S2C
  2
+  class Universe
  3
+    attr_accessor :planets, :log
  4
+    
  5
+    def initialize
  6
+      S2C::Universe.log( self, "Initializing Universe" )
  7
+      @planets = []
  8
+      @log = []
  9
+      S2C::Universe.log( self, "Universe intialized" )
  10
+    end
  11
+    
  12
+    def create_planet( name )
  13
+      planet = S2C::Models::Planet.new( name )
  14
+      
  15
+      @planets << planet
  16
+      
  17
+      return planet
  18
+    end
  19
+    
  20
+    def cycle
  21
+      S2C::Universe.log( self, "Start cycle" )
  22
+      @planets.each do |planet|
  23
+        planet.constructions.each do |construction|
  24
+          construction.work
  25
+        end
  26
+      end
  27
+      S2C::Universe.log( self, "End cycle" )
  28
+    end
  29
+    
  30
+    def run
  31
+      S2C::Universe.log( self, "Start run" )
  32
+      while( true )
  33
+        self.cycle
  34
+        self.stats
  35
+        sleep( 2 )
  36
+      end
  37
+      S2C::Universe.log( self, "End run" )
  38
+    end
  39
+    
  40
+    def id
  41
+      'Universe'
  42
+    end
  43
+    
  44
+    def self.log( element, message )
  45
+      puts "[#{element.id}] > #{message}"
  46
+    end
  47
+    
  48
+    def to_s
  49
+      "planets:#{self.planets.size}"
  50
+    end
  51
+    
  52
+    def stats
  53
+      puts "--STATS INI--"
  54
+      
  55
+      puts "[Universe] > #{self.to_s}"
  56
+      self.planets.each do |planet|
  57
+        puts "[#{planet.id}] > #{planet.to_s}"
  58
+        planet.constructions.each do |construction|
  59
+          puts "[#{construction.id}] > #{construction.to_s}"
  60
+        end
  61
+      end
  62
+      
  63
+      puts "--STATS END--"
  64
+    end
  65
+  end
  66
+end
62  test/construction_test.rb
... ...
@@ -0,0 +1,62 @@
  1
+require_relative 'test_helper'
  2
+
  3
+class ConstructionTest < Test::Unit::TestCase
  4
+  def setup 
  5
+    S2C::Config.stubs( :config_path ).returns( "#{FIXTURES_PATH}/config.yml" )
  6
+    S2C::Universe.stubs( :log )
  7
+  end
  8
+  
  9
+  def test_initialize
  10
+    construction = S2C::Models::Construction.new( 'jupiter', 'mine' )
  11
+    assert_equal( 0, construction.level )
  12
+    assert_equal( :under_construction, construction.status )
  13
+    assert_equal( 'jupiter', construction.planet )
  14
+    assert_equal( 'mine', construction.type )
  15
+  end
  16
+  
  17
+  def test_upgrade
  18
+    planet = S2C::Models::Planet.new( 'jupiter' )
  19
+    planet.expects( :black_stuff ).returns( 14 )
  20
+    planet.expects( :remove_black_stuff ).with( 14 )
  21
+    
  22
+    construction = S2C::Models::Construction.new( planet, 'mine' )
  23
+    construction.instance_variable_set( :@status, :standby )
  24
+    
  25
+    construction.upgrade
  26
+    assert_equal( 0, construction.level )
  27
+    assert_equal( :upgrading, construction.status )
  28
+  end
  29
+  
  30
+  def test_attack_calculation_for_mine
  31
+    construction = S2C::Models::Construction.new( 'jupiter', 'mine' )
  32
+    assert_equal( 10, construction.attack )
  33
+    
  34
+    construction.instance_variable_set( :@level, 1 )
  35
+    assert_equal( 21, construction.attack )
  36
+    
  37
+    construction.instance_variable_set( :@level, 2 )
  38
+    assert_equal( 34, construction.attack )
  39
+    
  40
+    construction.instance_variable_set( :@level, 3 )
  41
+    assert_equal( 50, construction.attack )
  42
+    
  43
+    construction.instance_variable_set( :@level, 4 )
  44
+    assert_equal( 69, construction.attack )
  45
+  end
  46
+  
  47
+  def test_defense_calculation_for_mine
  48
+    construction = S2C::Models::Construction.new( 'jupiter', 'mine' )
  49
+    assert_equal( 11, construction.defense )
  50
+    
  51
+    construction.instance_variable_set( :@level, 1 )
  52
+    assert_equal( 23, construction.defense )
  53
+  end
  54
+  
  55
+  def test_attack_calculation_for_garage
  56
+    construction = S2C::Models::Construction.new( 'jupiter', 'garage' )
  57
+    assert_equal( 20, construction.attack )
  58
+    
  59
+    construction.instance_variable_set( :@level, 1 )
  60
+    assert_equal( 42, construction.attack )
  61
+  end
  62
+end
23  test/fixtures/config.yml
... ...
@@ -0,0 +1,23 @@
  1
+mine:
  2
+  attack: 10
  3
+  defense: 11
  4
+  life: 12
  5
+  upgrade_timing: 13
  6
+  upgrade_black_stuff: 14
  7
+  power: 15
  8
+  
  9
+garage:
  10
+  attack: 20
  11
+  defense: 21
  12
+  life: 22
  13
+  upgrade_timing: 23
  14
+  upgrade_black_stuff: 24
  15
+  power: 25
  16
+    
  17
+mosquito:
  18
+  attack: 30
  19
+  defense: 31
  20
+  life: 32
  21
+  upgrade_timing: 33
  22
+  upgrade_black_stuff: 34
  23
+  power: 35
6  test/test_helper.rb
... ...
@@ -0,0 +1,6 @@
  1
+require_relative '../lib/s2c' 
  2
+require 'test/unit'
  3
+require 'mocha'
  4
+require 'ruby-debug'
  5
+
  6
+FIXTURES_PATH = File.expand_path( "#{File.dirname(__FILE__)}/fixtures" )
10  test/universe_test.rb
... ...
@@ -0,0 +1,10 @@
  1
+require_relative 'test_helper'
  2
+
  3
+class UniverseTest < Test::Unit::TestCase
  4
+  # def test_universe
  5
+  #   universe = S2C::Universe.new
  6
+  #   planet = universe.create_planet( 'X700' )
  7
+  #   planet.build_mine
  8
+  #   universe.run
  9
+  # end
  10
+end
6  wiki/roadmap.md
Source Rendered
@@ -58,6 +58,12 @@ Implement a system where we can define:
58 58
 
59 59
 This is a sort of _DelayedJob_ or something like this.
60 60
 
  61
+## Artificial Intelligence
  62
+
  63
+Try to add _non_users_ players to the game who can execute random _commands_.
  64
+
  65
+This could be great for experiments: every player is a _non_user_ and then accelerate the time to see what happen.
  66
+
61 67
 ## Implement API of the gem
62 68
 
63 69
 After its definition we implement the commands for the internal gem, these are Ruby commands.
18  wiki/the_processes.md
Source Rendered
... ...
@@ -0,0 +1,18 @@
  1
+# The Processes
  2
+
  3
+One of the most important things in this project are the **processes** I think is the most addictive ingredient: _waiting for a process to finish_ so you can move to the next _process_, or maybe see the results (is the process is an _attack_).
  4
+
  5
+Nothing happens immediately, every thing needs time to complete:
  6
+
  7
+* upgrading levels
  8
+* traveling to another planet
  9
+* build new construction 
  10
+
  11
+## How to resolve the processes
  12
+
  13
+I can think in two ways to resolve the process:
  14
+
  15
+* One independent entity manage the process, track the time needed, and the event that happens when process finishes.
  16
+* Each _element_ in the game (buildings, ships) keep track of its own status, and the _process_ it is involved on.
  17
+
  18
+For now I'm going to implement the second approach. Because I think there is not a _process_ that is not refereed to a _construction_. And because I think the _status_ and the _possibilities_ of every _construction_ is very related to the _process_ it is involved on.

0 notes on commit fc6be76

Please sign in to comment.
Something went wrong with that request. Please try again.