Class: KillerQueenSceneScoring::Bracket

Inherits:
Base
  • Object
show all
Defined in:
lib/killer_queen_scene_scoring/bracket.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:) ⇒ Bracket

‘id` can be the slug or the challonge ID of the bracket. If you pass a

and the bracket is owned by an organization, it must be of the form

“<org name>-<slug>”. ‘api_key` is your Challonge API key.



11
12
13
14
15
16
17
# File 'lib/killer_queen_scene_scoring/bracket.rb', line 11

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

    @id = id
    @loaded = false
    @state = ""
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

Instance Method Details

#calculate_pointsObject

Calculates how many points each player has earned in this bracket. If the bracket is not yet complete, the point values are the mininum number of points that the player can earn based on their current position in the bracket. On exit, ‘@players` contains a hash. The keys are the Challonge IDs of the teams in the bracket. The values are arrays of `Player` objects representing the players on the team. The caller must call `load`, and `load` must succeed, before calling this function.



75
76
77
78
79
80
# File 'lib/killer_queen_scene_scoring/bracket.rb', line 75

def calculate_points
    raise_error "The bracket was not loaded" if !@loaded

    calculate_team_points
    calculate_player_points
end

#complete?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/killer_queen_scene_scoring/bracket.rb', line 19

def complete?
    @state == "complete"
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. Returns a boolean indicating whether the bracket was loaded.



26
27
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
# File 'lib/killer_queen_scene_scoring/bracket.rb', line 26

def load
    url = "https://api.challonge.com/v1/tournaments/#{@id}.json"
    params = { include_matches: 1, include_participants: 1 }

    begin
        response = send_get_request(url, params)
    rescue RestClient::NotFound
        # Bail out if we got a 404 error.  The bracket doesn't exist on
        # Challonge right now, but it might be created in the future.
        log_warn "The bracket does not exist."
        return false
    end

    # Set `@challonge_bracket` to the data structure that represents the
    # bracket, its teams, and its matches.  We also keep the bracket's
    # state separately, for convenience.
    @challonge_bracket = OpenStruct.new(response[:tournament])
    @state = @challonge_bracket.state

    # Bail out if the bracket hasn't started yet.  This lets the tournament
    # organizer set the `next_bracket` value to a bracket that has been
    # created on Challonge, but which will be started in the future.  For
    # example, the organizer can create a wild card bracket and a finals
    # bracket, and set `next_bracket` in the wild card bracket to the ID
    # of the finals bracket before the wild card bracket has finished.
    if @challonge_bracket.started_at.nil?
        log_warn "The bracket has not been started yet."
        return false
    end

    # Read all the things.
    read_config
    read_teams
    read_matches
    read_players

    @loaded = true
    true
end