Changes between Version 39 and Version 40 of Clojure Client Tutorial


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

--

Legend:

Unmodified
Added
Removed
Modified
  • Clojure Client Tutorial

    v39 v40  
    237237}}}
    238238
    239 Notice that the room has a few items scattered around it, let's define a function "scan-area" that takes two arguments (a quagent key and a radius) and returns a hash map of the positions of the items.  The simplest implementation is to just use the basic radar op from protocol zero with a vector for an initial value and "conj" for the combination function. (Note the use of "pp" to pretty-print the previous result.)
     239Notice that the room has a few items scattered around it, let's define a function "scan-area" that takes two arguments (a quagent key and a radius) and returns a hash map of the positions of the items.  Before trying to write the full function it is a good idea just to print out what the server is returning.
     240
     241{{{
     242client.core=> (send-and-get :Bob :radar :now [8000] nil (fn [prev data] (println data)))
     243(0 player 768.875366 90.065201 0.000000)
     244(2 player 32.000000 -90.000000 0.000000)
     245(72 info_player_deathmatch 32.000244 -90.000000 0.223811)
     246(74 quagent_item_treasure 572.168640 20.462269 0.901278)
     247(75 quagent_item_gold 375.085327 56.309933 1.374918)
     248(76 quagent_item_gold 1019.278626 42.455196 0.505915)
     249(77 quagent_item_treasure 697.711304 63.434952 0.739097)
     250(78 quagent_item_gold 905.141357 8.130102 0.569713)
     251(79 info_player_deathmatch 0.125000 0.000000 90.000000)
     252nil
     253}}}
     254
     255The simplest implementation is to just use the basic radar op from protocol zero with a vector for an initial value and "conj" for the combination function. (Note the use of "pp" to pretty-print the previous result.)
    240256
    241257{{{
     
    255271}}}
    256272
     273Partitioning these into a map is going to be a little more difficult as multiple positions will need to be stored at each key.  However, we know already that the initial data structure to be a hash-map and the keys need to be the item type.
     274
     275{{{
     276client.core=> (send-and-get :Bob :radar :now [8000]
     277                            {}
     278                            (fn [prev [_ item-type & pos]]
     279                              (assoc prev item-type pos))
     280)
     281{"quagent_item_gold" ("905.141357" "8.130102" "0.569713")... (output truncated)
     282client.core=> (pp)
     283{"quagent_item_gold" ("905.141357" "8.130102" "0.569713"),
     284 "quagent_item_treasure" ("697.711304" "63.434952" "0.739097"),
     285 "info_player_deathmatch" ("0.125000" "0.000000" "90.000000"),
     286 "player" ("32.000000" "-90.000000" "0.000000")}
     287nil
     288}}}
     289
     290This is progress but the data from the new replies is overriding the previous results. To get the right behaviour, the "merge-with" function must be used to combine the maps.
     291
     292{{{
     293client.core=> (send-and-get :Bob :radar :now [8000] {} (fn [prev [_ item-type & pos]] (merge-with concat prev {item-type (list pos)})))
     294{"quagent_item_gold" (("375.085327" "56.309933" "1.374918") ... (output truncated)
     295client.core=> (pp)
     296{"quagent_item_gold"
     297 (("375.085327" "56.309933" "1.374918")
     298  ("1019.278626" "42.455196" "0.505915")
     299  ("905.141357" "8.130102" "0.569713")),
     300 "quagent_item_treasure"
     301 (("572.168640" "20.462269" "0.901278")
     302  ("697.711304" "63.434952" "0.739097")),
     303 "info_player_deathmatch"
     304 (("32.000244" "-90.000000" "0.223811")
     305  ("0.125000" "0.000000" "90.000000")),
     306 "player"
     307 (("768.875366" "90.065201" "0.000000")
     308  ("32.000000" "-90.000000" "0.000000"))}
     309nil
     310}}}
     311
     312
     313
    257314
    258315