Changes between Version 3 and Version 4 of Ruby Client Tutorial
- Timestamp:
- Aug 18, 2011 11:46:16 AM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ruby Client Tutorial
v3 v4 52 52 }}} 53 53 54 Sample output: 55 {{{ 56 whereis(q1) 57 => [-104.0, 96.0, -31.875] 58 }}} 59 60 54 61 The 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. 55 62 … … 61 68 {}, 62 69 lambda { |prev, data| 63 id, type, dist, yaw, pitch= data70 _, type, *location = data 64 71 if prev[type] == nil 65 return prev.merge({type => [data_to_doubles [dist, yaw, pitch]]})72 return prev.merge({type => [data_to_doubles location]}) 66 73 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])}) 68 75 }) 69 76 end 77 }}} 78 79 Sample output: 80 81 {{{ 82 irb(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]]} 70 91 }}} 71 92