Class: Gamefic::Plot::Darkroom

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/plot/darkroom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plot) ⇒ Darkroom

Returns a new instance of Darkroom.



9
10
11
# File 'lib/gamefic/plot/darkroom.rb', line 9

def initialize plot
  @plot = plot
end

Instance Attribute Details

#plotGamefic::Plot (readonly)

Returns:



7
8
9
# File 'lib/gamefic/plot/darkroom.rb', line 7

def plot
  @plot
end

Instance Method Details

#restore(snapshot) ⇒ Object

Restore a snapshot.

Parameters:



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
# File 'lib/gamefic/plot/darkroom.rb', line 37

def restore snapshot
  # @todo Use `program` for verification


  plot.subplots.each(&:conclude)
  plot.subplots.clear

  index = plot.static + plot.players
  snapshot['index'].each_with_index do |obj, idx|
    next if index[idx]
    elematch = obj['class'].match(/^#<ELE_([\d]+)>$/)
    if elematch
      klass = index[elematch[1].to_i]
    else
      klass = Gamefic::Serialize.string_to_constant(obj['class'])
    end
    index.push klass.allocate
  end

  snapshot['index'].each_with_index do |obj, idx|
    if index[idx].class.to_s != obj['class']
      STDERR.puts "MISMATCH: #{index[idx].class} is not #{obj['class']}"
      STDERR.puts obj.inspect
    end
    obj['ivars'].each_pair do |k, v|
      next if k == '@subplots'
      uns = v.from_serial(index)
      next if uns == "#<UNKNOWN>"
      index[idx].instance_variable_set(k, uns)
    end
    if index[idx].is_a?(Gamefic::Subplot)
      index[idx].extend Gamefic::Scriptable
      index[idx].instance_variable_set(:@theater, nil)
      index[idx].send(:run_scripts)
      index[idx].players.each do |pl|
        pl.playbooks.push index[idx].playbook unless pl.playbooks.include?(index[idx].playbook)
      end
      index[idx].instance_variable_set(:@static, [index[idx]] + index[idx].scene_classes + index[idx].entities)
      plot.subplots.push index[idx]
    end
  end
end

#saveHash

Create a snapshot of the plot.

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gamefic/plot/darkroom.rb', line 16

def save
  index = plot.static + plot.players
  plot.to_serial(index)
  {
    'program' => {}, # @todo Metadata for version control, etc.

    'index' => index.map do |i|
      if i.is_a?(Gamefic::Serialize)
        {
          'class' => i.class.to_s,
          'ivars' => i.serialize_instance_variables(index)
        }
      else
        i.to_serial(index)
      end
    end
  }
end