class MonkeyMusic::Player
Attributes
boost_cooldown[RW]
command_line_argument[RW]
level[RW]
monkey[RW]
remaining_time[RW]
Public Class Methods
new(file)
click to toggle source
# 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
Public Instance Methods
boost!()
click to toggle source
# 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!()
click to toggle source
# 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_output()
click to toggle source
# 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!()
click to toggle source
# File lib/monkey_music/player.rb, line 112 def move! @moves.each {|move| @monkey.move! move } @moves = [] end
parse!(s)
click to toggle source
# File lib/monkey_music/player.rb, line 69 def parse!(s) @queries = [] @tokens = s.chomp.split(",") parse_next_token! end
parse_move(move)
click to toggle source
# File lib/monkey_music/player.rb, line 103 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!()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/monkey_music/player.rb, line 89 def response_to(queries) tracks = [] queries.each do |uri| browse_result = @monkey.level.tracks.find {|t| t.uri == uri } tracks << browse_result if browse_result end lines = [] lines << tracks.length tracks.each do |track| lines << "#{track.uri},#{track.metadata.serialize}" end lines.join("\n") end
to_s()
click to toggle source
# File lib/monkey_music/player.rb, line 117 def to_s @monkey.name end
turn_output(turn)
click to toggle source
# 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