Class: InternetHakai::Scenario

Inherits:
Object
  • Object
show all
Defined in:
lib/internethakai/scenario.rb

Overview

シナリオ

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Scenario

Returns a new instance of Scenario.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/internethakai/scenario.rb', line 4

def initialize(opt)
    @config = opt
    @size = 0
    @actions_size
    @performance_id = nil
    @scenario_id = nil
    @loop = @config['loop']
    @loop_org = @loop
    digit = @loop.to_s.size
    @tmpl = "%0#{digit}d"
    @request_pool = BaseHandler::get_handler(@config['request_pool'])

    #@page_size = [10, @loop].min
    @page_size = 1
    @page = (@loop.quo(@page_size)).ceil
    @page_counter = @page
    @vars = {}
    @cookie = {}
end

Instance Attribute Details

#actions_sizeObject (readonly)

Returns the value of attribute actions_size.



23
24
25
# File 'lib/internethakai/scenario.rb', line 23

def actions_size
  @actions_size
end

Returns the value of attribute cookie.



24
25
26
# File 'lib/internethakai/scenario.rb', line 24

def cookie
  @cookie
end

#performance_idObject

Returns the value of attribute performance_id.



24
25
26
# File 'lib/internethakai/scenario.rb', line 24

def performance_id
  @performance_id
end

#scenario_idObject

Returns the value of attribute scenario_id.



24
25
26
# File 'lib/internethakai/scenario.rb', line 24

def scenario_id
  @scenario_id
end

#varsObject

Returns the value of attribute vars.



24
25
26
# File 'lib/internethakai/scenario.rb', line 24

def vars
  @vars
end

Instance Method Details

#create_act(idx) ⇒ Object



97
98
99
100
101
102
# File 'lib/internethakai/scenario.rb', line 97

def create_act idx
    actconf = @actconfigs[idx]
    obj = actconf['class_obj']::new(actconf)
    obj.set_scenario(self)
    obj
end

#create_scenarioObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/internethakai/scenario.rb', line 66

def create_scenario
    @commands = []
    @performance_id = (@scenario_id.to_s + sprintf(@tmpl, @loop)).to_i
    @actions_size = @config['actions'].inject(0){|sum, i|
        times = i['times']
        sum + times
    }
    baseconfig = @config.clone
    baseconfig.delete('actions')
    1.upto(@page_size){
        @config["actions"].each do |actconfig|
            actconfig = baseconfig.merge(actconfig)
            times = actconfig['times']
            command = actconfig['class_obj']
            1.upto(times) do
                actconfignew = actconfig.clone
                obj = command.new(actconfignew, self)
                obj.set_wait if(actconfignew['wait'])
                @commands << obj
            end
        end
    }
    @action_counter = @actions_size
    @commands_org = @commands.clone
    @size = @actions_size * @loop_org
end

#each(&block) ⇒ Object



61
62
63
64
65
# File 'lib/internethakai/scenario.rb', line 61

def each &block
    @commands_org.each do |cm|
        block.call(cm)
    end
end

#initObject



28
29
30
31
32
33
# File 'lib/internethakai/scenario.rb', line 28

def init
    @performance_id = (@scenario_id.to_s + sprintf(@tmpl, @loop)).to_i
    act = @commands.shift
    act.performance_id = @performance_id
    @request_pool.add(act)
end

#nextObject



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
# File 'lib/internethakai/scenario.rb', line 34

def next
    @action_counter -= 1
    #puts "action: #{@action_counter}"
    #puts "loop: #{@loop}"
    if @action_counter == 0
        @loop -= 1
        #これで最後
        if @loop == 0
            if @config['infinity']
                @loop += 100 # 無限ループ!
            else
                return 
            end
        end
        @action_counter = @actions_size
        @performance_id = (@scenario_id.to_s + sprintf(@tmpl, @loop)).to_i
    end
    if @commands.empty?
        #ループが残っていればまた最初から
        @commands = @commands_org.clone
        @cookie = {}
        @vars   = {}
    end
    act = @commands.shift
    act.performance_id = @performance_id
    @request_pool.add(act)
end


92
93
94
95
96
# File 'lib/internethakai/scenario.rb', line 92

def set_cookie cookie
    return unless cookie
    k, v = cookie[0...cookie.index(';')].split('=')
    @cookie[k] = v if k && v
end

#sizeObject



25
26
27
# File 'lib/internethakai/scenario.rb', line 25

def size
    @size
end