Class: InternetHakai::ScenarioHandler

Inherits:
BaseHandler show all
Defined in:
lib/internethakai/scenario_handler.rb

Overview

シナリオを扱うクラス

Constant Summary collapse

UNIQUE_BY_THREAD =

require ‘resolv’

false

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHandler

clear, clearall, get_class, get_config, get_handler, get_instance, get_instance_thread, get_thread_id, #initialize, #on_create, set_config, set_thread_id, unique_by_thread?

Constructor Details

This class inherits a constructor from InternetHakai::BaseHandler

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/internethakai/scenario_handler.rb', line 6

def config
  @config
end

Instance Method Details

#closeObject



88
89
90
# File 'lib/internethakai/scenario_handler.rb', line 88

def close
    @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/internethakai/scenario_handler.rb', line 85

def closed?
    @closed
end

#complete?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/internethakai/scenario_handler.rb', line 91

def complete?
    @size <= 0
end

#create_scenario(process_id = '') ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/internethakai/scenario_handler.rb', line 17

def create_scenario process_id=''
    o = Scenario::new(@config)
    @size += 1
    o.scenario_id = (@size.to_s + process_id).to_i
    o.create_scenario
    #シナリオのサイズ
    @scenarios[o.object_id] = o.size
    @act_sum += o.size
    o
end

#init(config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/internethakai/scenario_handler.rb', line 7

def init config
    @config = config
    @size = 0
    @closed = false
    @max_request = @config['max_scenario']
    @max_request_counter = @max_request
    @act_sum = 0
    @scenarios = Hash::new
    @logger = BaseHandler::get_handler(@config["logger"])
end

#on_completeObject



35
36
37
# File 'lib/internethakai/scenario_handler.rb', line 35

def on_complete
    @on_complete_callback.call() if @on_complete_callback
end

#on_scenarios_createdObject

すべてのシナリオがつくられた



28
29
30
31
# File 'lib/internethakai/scenario_handler.rb', line 28

def on_scenarios_created
    @loop_counter, @mod_counter = @act_sum.divmod(@max_request)
    @queue = BaseHandler::get_handler(@config['queue'])
end

#on_turn_completeObject



41
42
43
44
# File 'lib/internethakai/scenario_handler.rb', line 41

def on_turn_complete
    @queue.add([@on_turn_complete_callback,[]]) if @on_turn_complete_callback
    @logger.run('o', 2)
end

#quit_action(scenario_id) ⇒ Object

ひとつのアクションが終了時に呼ばれる



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
78
79
80
81
82
83
84
# File 'lib/internethakai/scenario_handler.rb', line 51

def quit_action scenario_id
    #ひとつのアクションが終了
    @scenarios[scenario_id] -= 1
    @max_request_counter -= 1
    #puts "req_counter: #{@max_request_counter}"
    if @max_request_counter == 0
        #ひとつのアクション×リクエスト並列数が終了
        on_turn_complete
        @max_request_counter = @max_request
        @loop_counter -= 1
    end
    #シナリオ並列数と、リクエスト並列数がちがうので、あまりの処理が必要
    #puts "loop: #{@loop_counter}"
    #if @loop_counter == 0
    #    @mod_counter -= 1
    #    if @mod_counter==0
    #        on_turn_complete
    #        on_complete
    #    end
    #end
    if(@scenarios[scenario_id]==0)
        #ひとつのシナリオが終了
        @size -= 1
        #@logger.run("size:#{@size}\n", 2)
        if @config['infinity']
            @scenarios[scenario_id] += 100
            return
        end
        if @size==0
            @logger.run("O", 2)
            on_complete
        end
    end
end

#set_on_complete(&block) ⇒ Object



32
33
34
# File 'lib/internethakai/scenario_handler.rb', line 32

def set_on_complete &block
    @on_complete_callback = block
end

#set_on_turn_complete(&block) ⇒ Object



38
39
40
# File 'lib/internethakai/scenario_handler.rb', line 38

def set_on_turn_complete &block
    @on_turn_complete_callback = block
end

#silenceObject



45
46
47
48
49
# File 'lib/internethakai/scenario_handler.rb', line 45

def silence
    def on_turn_complete
        @queue.add([@on_turn_complete_callback,[]]) if @on_turn_complete_callback
    end
end