Class: KillerQueenSceneScoring::Tournament

Inherits:
Base
  • Object
show all
Defined in:
lib/killer_queen_scene_scoring/tournament.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#api_key, #logger

Instance Method Summary collapse

Methods inherited from Base

#hash_of_arrays, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(id:, api_key:, logger: nil) ⇒ Tournament

‘id` can be the slug or the challonge ID of the first bracket in the tournament. If you pass a slug, and the bracket is owned by an organization, it must be of the form “<org name>-<slug>”. `api_key` is your Challonge API key. `logger` can be a `Logger` object, or `true` to log to standard output.



13
14
15
16
17
18
19
20
21
# File 'lib/killer_queen_scene_scoring/tournament.rb', line 13

def initialize(id:, api_key:, logger: nil)
    super(api_key, logger)

    @brackets = []
    @scene_scores = []
    @loaded = false
    @complete = false
    @id = id
end

Instance Attribute Details

#completeObject (readonly)

Returns the value of attribute complete.



6
7
8
# File 'lib/killer_queen_scene_scoring/tournament.rb', line 6

def complete
  @complete
end

#scene_scoresObject (readonly)

Returns the value of attribute scene_scores.



6
7
8
# File 'lib/killer_queen_scene_scoring/tournament.rb', line 6

def scene_scores
  @scene_scores
end

Instance Method Details

#calculate_pointsObject

Calculates the score for each scene in the tournament, and sets ‘@scene_scores` to an array of `Scene` objects. The caller must call `load`, and `load` must succeed, before calling this function.



95
96
97
98
99
100
# File 'lib/killer_queen_scene_scoring/tournament.rb', line 95

def calculate_points
    raise "The tournament was not loaded" unless @loaded

    @brackets.each(&:calculate_points)
    calculate_scene_points
end

#loadObject

Reads the Challonge bracket with the ID that was passed to the constructor, and fills in all the data structures that represent that bracket and any later brackets in the tournament. ‘@complete` is also set to indicate whether the entire tournament is complete. Returns true if at least one bracket was loaded, and false otherwise.



28
29
30
31
32
33
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/killer_queen_scene_scoring/tournament.rb', line 28

def load
    @loaded = false
    tournament_id = @id
    all_brackets_loaded = true

    while tournament_id
        log_debug "Reading the bracket \"#{tournament_id}\""

        # Load the next bracket in the chain.  Bail out if we can't load it.
        bracket = Bracket.new(id: tournament_id, api_key: api_key, logger: logger)

        if !bracket.load
            all_brackets_loaded = false
            break
        end

        # Store that bracket.
        @brackets << bracket

        # For debugging purposes, log the players in each scene ->
        scenes = hash_of_arrays

        bracket.players.each_value do |team|
            team.each do |player|
                scenes[player.scene] << player
            end
        end

        scene_list = scenes.map do |scene, players|
            "#{scene} has #{players.size} players: " +
              players.map(&:name).join(", ")
        end

        log_info scene_list.join("\n")
        # <- end debug logging

        # Go to the next bracket in the chain.
        tournament_id = bracket.config.next_bracket
    end

    return false if @brackets.empty?

    # Check that all the config files have the same `max_players_to_count`.
    values = @brackets.map { |b| b.config.max_players_to_count }

    if values.count(values[0]) != values.size
        msg = "ERROR: All brackets must have the same \"max_players_to_count\"."
        log_error msg
        raise msg
    end

    # If we loaded all the brackets in the list of brackets, set our
    # `complete` member based on the completed state of the last bracket.
    # We only check the last bracket because previous brackets in the
    # sequence are not guaranteed to be marked as complete on Challonge.
    # For an example, see "bb3wc".  The BB3 wild card bracket was not
    # marked as complete because play stopped once it got down to 4 teams.
    @complete = all_brackets_loaded && @brackets.last.complete?

    @loaded = true
    true
end