Author: sveinung
Date: Fri Jul 15 13:21:29 2016
New Revision: 33248

URL: http://svn.gna.org/viewcvs/freeciv?rev=33248&view=rev
Log:
JSON: support worklist transfer.

See patch #7469

Modified:
    trunk/common/networking/dataio_json.c
    trunk/common/networking/dataio_json.h

Modified: trunk/common/networking/dataio_json.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/networking/dataio_json.c?rev=33248&r1=33247&r2=33248&view=diff
==============================================================================
--- trunk/common/networking/dataio_json.c       (original)
+++ trunk/common/networking/dataio_json.c       Fri Jul 15 13:21:29 2016
@@ -222,10 +222,31 @@
   Insert worklist information.
 **************************************************************************/
 void dio_put_worklist_json(struct json_data_out *dout,
-                           const struct plocation *location,
+                           struct plocation *location,
                            const struct worklist *pwl)
 {
-  /* TODO: implement */
+  int i;
+  const int size = worklist_length(pwl);
+
+  /* Must create the array before instertion. */
+  dio_put_farray_json(dout, location, size);
+
+  location->sub_location = plocation_elem_new(0);
+
+  for (i = 0; i < size; i++) {
+    const struct universal *pcp = &(pwl->entries[i]);
+    json_t *universal = json_object();
+
+    location->sub_location->number = i;
+
+    json_object_set(universal, "kind", json_integer(pcp->kind));
+    json_object_set(universal, "value",
+                    json_integer(universal_number(pcp)));
+
+    plocation_write_data(dout->json, location, universal);
+  }
+
+  FC_FREE(location->sub_location);
 }
 
 /**************************************************************************
@@ -329,10 +350,62 @@
   Receive worklist information.
 **************************************************************************/
 bool dio_get_worklist_json(json_t *json_packet,
-                           const struct plocation *location,
+                           struct plocation *location,
                            struct worklist *pwl)
 {
-  /* TODO: implement */
+  int i, length;
+
+  const json_t *wlist = plocation_read_data(json_packet, location);
+
+  worklist_init(pwl);
+
+  if (!json_is_array(wlist)) {
+    log_packet("Not a worklist");
+    return FALSE;
+  }
+
+  /* Safe. Checked that it was an array above. */
+  length = json_array_size(wlist);
+
+  /* A worklist is an array... */
+  location->sub_location = plocation_elem_new(0);
+
+  /* ... of universal objects. */
+  location->sub_location->sub_location = plocation_field_new("kind");
+
+  for (i = 0; i < length; i++) {
+    int value;
+    int kind;
+    struct universal univ;
+
+    location->sub_location->number = i;
+
+    location->sub_location->sub_location->name = "kind";
+    if (!dio_get_uint8_json(json_packet, location, &kind)) {
+      log_packet("Corrupt worklist element kind");
+      FC_FREE(location->sub_location->sub_location);
+      FC_FREE(location->sub_location);
+      return FALSE;
+    }
+
+    location->sub_location->sub_location->name = "value";
+    if (!dio_get_uint8_json(json_packet, location, &value)) {
+      log_packet("Corrupt worklist element value");
+      FC_FREE(location->sub_location->sub_location);
+      FC_FREE(location->sub_location);
+      return FALSE;
+    }
+
+    /*
+     * FIXME: the value returned by universal_by_number() should be checked!
+     */
+    univ = universal_by_number(kind, value);
+    worklist_append(pwl, &univ);
+  }
+
+  FC_FREE(location->sub_location->sub_location);
+  FC_FREE(location->sub_location);
+
   return TRUE;
 }
 

Modified: trunk/common/networking/dataio_json.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/networking/dataio_json.h?rev=33248&r1=33247&r2=33248&view=diff
==============================================================================
--- trunk/common/networking/dataio_json.h       (original)
+++ trunk/common/networking/dataio_json.h       Fri Jul 15 13:21:29 2016
@@ -75,7 +75,7 @@
                                 const struct plocation *location,
                                 int *dest);
 bool dio_get_worklist_json(json_t *json_packet,
-                           const struct plocation *location,
+                           struct plocation *location,
                            struct worklist *pwl);
 bool dio_get_requirement_json(json_t *json_packet,
                               const struct plocation *location,
@@ -148,7 +148,7 @@
                                 const struct plocation *location,
                                 const int *value);
 void dio_put_worklist_json(struct json_data_out *dout,
-                           const struct plocation *location,
+                           struct plocation *location,
                            const struct worklist *pwl);
 void dio_put_requirement_json(struct json_data_out *dout,
                               const struct plocation *location,


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to