Class: Civility

Inherits:
Thor
  • Object
show all
Defined in:
lib/civility.rb

Constant Summary collapse

VERSION =
'1'
SAVE_DIRECTORY =
"/Documents/Aspyr/Sid\ Meier\'s\ Civilization\ 5/Saves/hotseat/"
FILE_PREFIX =
'civility'
FILE_EXT =
'Civ5Save'
API =
'http://multiplayerrobot.com/api/Diplomacy/'
CONFIG_FILE =
'.civility.yml'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Civility

Returns a new instance of Civility.



15
16
17
18
# File 'lib/civility.rb', line 15

def initialize(*args)
  @config = load_config
  super(*args)
end

Instance Method Details

#auth(key = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/civility.rb', line 21

def auth(key = nil)
  if key.nil?
    url = 'http://multiplayerrobot.com/download'
    puts "Grab your Authentication Key from #{url}"
    system('open', url)
  else
    @config[:version] = VERSION
    @config[:auth] = key
    @config[:user] = user
    self.config = @config
    puts "Hello, #{user['PersonaName']}, your auth is all configured!"
  end
end

#complete(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/civility.rb', line 55

def complete(name)
  return missing_auth_error unless auth_key
  game = game_by_name(name)
  return missing_game_error unless game
  path = game_path(game)
  response = upload_file('SubmitTurn', {authKey: auth_key, turnId: game['CurrentTurn']['TurnId']}, path)
  response = JSON.parse(response)
  case response['ResultType']
  when 0
    puts "UnexpectedError: #{response}"
  when 1
    puts "You earned #{response['PointsEarned']} points completing #{game['Name']} from #{path}"
  when 2
    puts "It's not your turn"
  when 3
    puts 'You already submitted your turn'
  else
    puts 'UnexpectedError'
  end
end

#gamesObject



36
37
38
39
40
41
42
# File 'lib/civility.rb', line 36

def games
  return missing_auth_error unless auth_key
  response = get('GetGamesAndPlayers', {authKey: auth_key, playerIDText: user_id})
  response = JSON.parse(response)
  output_games(response['Games'])
  self.config = @config.merge(games: response['Games'], updated_at: Time.now.to_i)
end

#play(name) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/civility.rb', line 45

def play(name)
  return missing_auth_error unless auth_key
  game = game_by_name(name)
  return missing_game_error unless game
  path = game_path(game)
  file('GetLatestSaveFileBytes', {authKey: auth_key, gameID: game['GameId']}, path)
  puts "Saved #{game['Name']} to #{path}"
end