Class: MonkeyMusic::Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Player

Returns a new instance of Player.



7
8
9
10
11
12
13
14
15
# File 'lib/monkey_music/player.rb', line 7

def initialize(file)
  @file = file
  @monkey = Monkey.new
  @boost_cooldown = 0
  @penalty = 0
  @moves = []
  @queries = []
  @command_line_argument = ""
end

Instance Attribute Details

#boost_cooldownObject

Returns the value of attribute boost_cooldown.



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

def boost_cooldown
  @boost_cooldown
end

#command_line_argumentObject

Returns the value of attribute command_line_argument.



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

def command_line_argument
  @command_line_argument
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

#monkeyObject

Returns the value of attribute monkey.



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

def monkey
  @monkey
end

#remaining_timeObject

Returns the value of attribute remaining_time.



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

def remaining_time
  @remaining_time
end

Instance Method Details

#boost!Object



83
84
85
86
87
# File 'lib/monkey_music/player.rb', line 83

def boost!
  return unless @boost_cooldown == 0
  @boost_cooldown = @monkey.level.boost_cooldown
  [3, @tokens.length].min.times { parse_next_token! }
end

#init!Object



17
18
19
20
21
# File 'lib/monkey_music/player.rb', line 17

def init!
  IO.popen([@file, @command_line_argument], "r+") do |io| 
    io.puts initial_output
  end
end

#initial_outputObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/monkey_music/player.rb', line 38

def initial_output
  level = @monkey.level
  user = @monkey.level.user
  [ "INIT",
    "M#{@monkey.id}",
    level.width,
    level.height,
    level.turn_limit,
    user.toplists[:top_tracks].length,
    user.toplists[:top_tracks].map(&:serialize).join("\n"),
    user.toplists[:top_albums].length,
    user.toplists[:top_albums].map(&:serialize).join("\n"),
    user.toplists[:top_artists].length,
    user.toplists[:top_artists].map(&:serialize).join("\n"),
    user.toplists[:disliked_artists].length,
    user.toplists[:disliked_artists].map(&:serialize).join("\n"),
  ].join("\n")
end

#move!Object



111
112
113
114
# File 'lib/monkey_music/player.rb', line 111

def move!
  @moves.each {|move| @monkey.move! move }
  @moves = []
end

#parse!(s) ⇒ Object



69
70
71
72
73
# File 'lib/monkey_music/player.rb', line 69

def parse!(s)
  @queries = []
  @tokens = s.chomp.split(",")
  parse_next_token!
end

#parse_move(move) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/monkey_music/player.rb', line 102

def parse_move(move)
  case move
    when "N" then :north
    when "W" then :west
    when "E" then :east
    when "S" then :south
  end
end

#parse_next_token!Object



75
76
77
78
79
80
81
# File 'lib/monkey_music/player.rb', line 75

def parse_next_token!
  token = @tokens.shift
  if /^[NWES]$/.match(token) then @moves << parse_move(token)
  elsif /^spotify:track:/.match(token) then @queries << token
  elsif "B" == token then boost!
  end
end

#query!(turn) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/monkey_music/player.rb', line 23

def query!(turn)
  if @penalty > 0
    @penalty -= 1
    @remaining_time = @monkey.level.time_limit if @penalty < 1
  else
    IO.popen(@file, "r+") do |io|
      io.puts turn_output(turn)
      @remaining_time -= (Benchmark.realtime { @input = io.gets } * 1000).round
      parse!(@input) if @input
    end
    @penalty = 5 if @remaining_time < 0
    @boost_cooldown -= 1 if @boost_cooldown > 0
  end
end

#response_to(queries) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/monkey_music/player.rb', line 89

def response_to(queries)
  tracks = []
  queries.each do |uri|
    tracks << @monkey.level.tracks.find {|t| t.uri == uri }
  end
  lines = []
  lines << tracks.length
  tracks.each do |track|
    lines << "#{track.uri},#{track.metadata.serialize}"
  end
  lines.join("\n")
end

#to_sObject



116
117
118
# File 'lib/monkey_music/player.rb', line 116

def to_s
  @monkey.name
end

#turn_output(turn) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/monkey_music/player.rb', line 57

def turn_output(turn)
  [ "TURN",
    "M#{@monkey.id}",
    turn,
    @monkey.remaining_capacity,
    @remaining_time,
    @boost_cooldown,
    response_to(@queries),
    @monkey.level.serialize,
  ].join("\n")
end