Class: KAG::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/kag/player.rb

Overview

Defined Under Namespace

Classes: Avatar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nick) ⇒ Player

Public: Initialize a new player.

nick - The case-insensitive name of the player in King Arthur’s Gold game.



13
14
15
16
# File 'lib/kag/player.rb', line 13

def initialize(nick)
  @nick   = nick
  @avatar = Avatar.new(nick)
end

Instance Attribute Details

#avatarObject (readonly)

Public: Returns the KAG::Player::Avatar object.



8
9
10
# File 'lib/kag/player.rb', line 8

def avatar
  @avatar
end

#nickObject (readonly)

Public: Returns the String name of the player.



5
6
7
# File 'lib/kag/player.rb', line 5

def nick
  @nick
end

Instance Method Details

#active?Boolean

Public: Get the status of the player.

Examples

player.active?
# => false

Returns true or false.

Returns:

  • (Boolean)


47
48
49
# File 'lib/kag/player.rb', line 47

def active?
  info['active']
end

#ban_expirationObject

Public: Get the ban expiration date if user was banned. This field is appears only for banned users.

Examples

player.ban_expiration
# => #<DateTime: 2022-03-02T09:09:53+00:00 ((2459641j,32993s,0n),+0s,2299161j)>

Returns DateTime object, which has ban expiration date of the player or nil, if the player has no active bans.



142
143
144
145
# File 'lib/kag/player.rb', line 142

def ban_expiration
  ban_expiration_date = info['banExpiration']
  DateTime.parse ban_expiration_date if ban_expiration_date
end

#ban_reasonObject

Public: Get the ban reason if user was banned. This field is appears only for banned users.

Examples

player.ban_reason
# => 'Speedhacking'

Returns String object, which represents description of the ban if any bans are active.



157
158
159
# File 'lib/kag/player.rb', line 157

def ban_reason
  info['banReason']
end

#banned?Boolean

Public: Get the ban status of the player.

Examples

player.banned?
# => true

Returns true or false.

Returns:

  • (Boolean)


81
82
83
# File 'lib/kag/player.rb', line 81

def banned?
  info['banned']
end

#gold?Boolean

Public: Check for account type of the player. Owners of gold account bought the game.

Examples

player.gold?
# => false

Returns true if the player has gold account or false otherwise.

Returns:

  • (Boolean)


128
129
130
# File 'lib/kag/player.rb', line 128

def gold?
  info['gold']
end

#info(force = false) ⇒ Object

Public: Retrieve information about KAG player.

force - The flag which enables/disables caching of the info. Being equal

to true, disables caching and resend GET request to KAG API
endpoint (default: false).

Examples

player.info
player.info(true) # Disable cached info

Returns Hash with info about player or Hash with statusMessage, if the player doesn’t exist..



31
32
33
34
35
36
37
# File 'lib/kag/player.rb', line 31

def info(force = false)
  if force
    @info = get_info
  else
    @info ||= get_info
  end
end

#role(readable = false) ⇒ Object

Public: Get the role of the player.

readable - The Boolean flag, which describes how the return value should

be represented (either machine-readable format or
human-readable) (default: false).

Examples

# Machine-readable role.
player.role
# => 4

# Human-readable role.
player.role(true)
# => 'team member'

Returns Integer, representing role of the player or String, if readable flag was used.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kag/player.rb', line 103

def role(readable = false)
  role = info['role']

  if readable
    case role
    when 0 then 'normal'
    when 1 then 'developer'
    when 2 then 'guard'
    when 4 then 'team member'
    when 5 then 'tester'
    end
  else
    role
  end
end

#usernameObject

Public: Get the username of the player.

Examples

player.username
# => 'prostosuper'

# Please, note, that KAG::Player#username is not alias for
# KAG::Player#nick. KAG::Player#username is the name, which is given by
# KAG API and Player#nick is an attribute of your object. Feel the
# difference:
# player = KAG::Player.new('flieslikeabrick')
# player.username
# => 'FliesLikeABrick'
# player.nick
# => 'flieslikeabrick'

Returns String with containing name of the user in game.



69
70
71
# File 'lib/kag/player.rb', line 69

def username
  info['username']
end