Changes between Version 3 and Version 4 of Ruby Client Tutorial


Ignore:
Timestamp:
Aug 18, 2011 11:46:16 AM (13 years ago)
Author:
kedwar10
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Ruby Client Tutorial

    v3 v4  
    5252}}}
    5353
     54Sample output:
     55{{{
     56whereis(q1)
     57=> [-104.0, 96.0, -31.875]
     58}}}
     59
     60
    5461The write method takes the arguments "now", "location", and "" and sends the string "n lc #" (where # is an integer) to the server.  It then creates a separate thread that reads replies from the socket.  When the reply is of type "rs", 'write' calls its last argument on the accumulator (initially the penultimate argument) and the data from the last response (an array of strings).  When the reply is of type "cp", 'write' terminates the thread and returns the accumulator. In the previous example, there was no need to specify an initial value since the server only sends data once.  The radar op, however, sends data for every object it finds.  The 'scan' function uses a hash map as the initial value and then builds a list of (relative) locations for each item type.
    5562
     
    6168                {},
    6269                lambda { |prev, data|
    63                   id, type, dist, yaw, pitch = data
     70                  _, type, *location = data
    6471                  if prev[type] == nil
    65                     return prev.merge({type => [data_to_doubles [dist, yaw, pitch]]})
     72                    return prev.merge({type => [data_to_doubles location]})
    6673                  end
    67                   return prev.merge({type => prev[type].concat([data_to_doubles [dist, yaw, pitch]])})
     74                  return prev.merge({type => prev[type].concat([data_to_doubles location])})
    6875                })
    6976end
     77}}}
     78
     79Sample output:
     80
     81{{{
     82irb(main):008:0> scan(q1, 8000)
     83=> {"info_player_deathmatch"=>[[32.000244, -90.0, 0.223811],
     84                               [0.125, 0.0, 90.0]],
     85    "player"=>[[313.243591, -13.452801, 0.0]],
     86    "quagent_item_gold"=>[[375.085327, 56.309933, 1.374918],
     87                          [1019.278626, 42.455196, 0.505915],
     88                          [905.141357, 8.130102, 0.569713]],
     89    "quagent_item_treasure"=>[[572.16864, 20.462269, 0.901278],
     90                              [697.711304, 63.434952, 0.739097]]}
    7091}}}
    7192