class Encounter::Game

Class for game announce information

@!attribute [r] domain

@return [String] Domain name.

@!attribute [r] gid

@return [Integer] Game id.

@!attribute [r] name

@return [String] Game title.

@!attribute [r] authors

@return [Array<Encounter::Player>] List of game authors.

@!attribute [r] start_time

@return [String] Game start time.

@!attribute [r] end_time

@return [String] Game end time.

@!attribute [r] money

@return [String] Game price.

@!attribute [r] type

@return [String] Game engine type.
  Possible values are _Real_, _Points_, _Virtual_, _Quiz_, _PhotoHunt_,
  _PhotoExtreme_, _Caching_, _WetWars_, _Competition_

@!attribute [r] limit

@return [Integer] Maximal allowed players in team. 0 if unlimited.

@!attribute [r] description

@return [String] Game description.

@!attribute [r] play_by

@return [String] Possible values are _Team_, _Single_.

@!attribute [r] teams_accepted

@return [Array<Encouter::Player>, Array<Encounter::Team>] List of accepted
  teams or players.

@!attribute [r] teams_waiting

@return [Array<Encouter::Player>, Array<Encounter::Team>] List of teams or
  players waiting to be accepted.

Constants

GAME_TYPES

Attributes

conn[R]
domain[R]
gid[R]

Public Class Methods

new(conn, params) click to toggle source

@param [Encounter::Connection] conn @param [Hash] params You can pass values in this parameters to predefine

attributes. Any class attribute can be set.

@option params [String] :domain Domain name. Required option @option params [Integer] :gid Game ID. Required option

@todo Parse :play_by value

@return [Encounter::Game] New object @raise [ArgumentError] Raised if connection is not given @raise [ArgumentError] Raised if :domain or :gid option is not defined

Calls superclass method Encounter::Base::new
# File lib/encounter/game.rb, line 67
def initialize(conn, params)
  raise ArgumentError, ':domain is needed' unless params.key? :domain
  raise ArgumentError, ':gid is needed' unless params.key? :gid

  super(conn, params)
end

Private Instance Methods

load_data() click to toggle source
# File lib/encounter/game.rb, line 138
def load_data
  dom_page = load_page("http://#{domain}/GameDetails.aspx", gid: gid)

  raise 'No such game' unless dom_page.css('#boxCenterTopUsers').empty?
  assign_values parse_all(dom_page.css('table.gameInfo').first)
end
parse_authors(table) click to toggle source
# File lib/encounter/game.rb, line 83
def parse_authors(table)
  as = table.css('a').select { |a| a['id'] && a['id'].match(/lnkAuthor$/) }
  { authors: as.map { |a| parse_url_object(a) } }
end
parse_description(table) click to toggle source
# File lib/encounter/game.rb, line 118
def parse_description(table)
  loop do
    table = table.next_element
    break if table.name == 'table'
  end
  { description: table.css('tr:eq(2)').inner_html }
end
parse_game_type(table) click to toggle source
# File lib/encounter/game.rb, line 88
def parse_game_type(table)
  type_id = table.css('img#ImgGameType').first['src']
                 .match(/type\.(\d*)\.gif/).captures.first.to_i
  { type: GAME_TYPES[type_id] }
end
parse_limit(table) click to toggle source
# File lib/encounter/game.rb, line 98
def parse_limit(table)
  item = table.css('span#spanMaxTeamPlayers').first
  { limit: item.nil? ? 0 : item.text.match(/(\d+)/).captures.first.to_i }
end
parse_money(table) click to toggle source
# File lib/encounter/game.rb, line 112
def parse_money(table)
  base = table.css('span#GameDetail_lblFeeType').first
  return { money: 0 } if base.nil?
  { money: "#{base.previous_element.text} #{base.text}" }
end
parse_name(table) click to toggle source
# File lib/encounter/game.rb, line 94
def parse_name(table)
  { name: table.css('a#lnkGameTitle').first.text }
end
parse_player_list(div) click to toggle source
# File lib/encounter/game.rb, line 134
def parse_player_list(div)
  div.css('a').map { |a| parse_url_object(a) }
end
parse_players(table) click to toggle source
# File lib/encounter/game.rb, line 126
def parse_players(table)
  base = table.css('div.hr').last.parent.parent.previous_element.css('div')
  accepted = parse_player_list base.last
  waiting = parse_player_list base.first if base.size > 1

  { teams_accepted: accepted, teams_waiting: waiting || [] }
end
parse_time(table) click to toggle source
# File lib/encounter/game.rb, line 103
def parse_time(table)
  base = table.css('#GameDetail_YourTimeArea').first
  date = /(\d{2}\.\d{2}\.\d{4}\s\d{1,2}:\d{2}:\d{2})/
  {
    start_time: base.previous_element.text.match(date).captures.first,
    end_time: base.next_element.text.match(date).captures.first
  }
end