Author: sveinung
Date: Wed Apr  1 15:38:04 2015
New Revision: 28684

URL: http://svn.gna.org/viewcvs/freeciv?rev=28684&view=rev
Log:
JSON protocol: support sending and receiving a requirement.

See patch #5970

Modified:
    trunk/common/dataio_json.c

Modified: trunk/common/dataio_json.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/dataio_json.c?rev=28684&r1=28683&r2=28684&view=diff
==============================================================================
--- trunk/common/dataio_json.c  (original)
+++ trunk/common/dataio_json.c  Wed Apr  1 15:38:04 2015
@@ -366,12 +366,57 @@
 }
 
 /**************************************************************************
-  ..
+  Read a single requirement.
 **************************************************************************/
 bool dio_get_requirement_json(json_t *json_packet, char *key,
                               struct requirement *preq)
 {
-  /* TODO: implement */
+  int kind, range, value;
+  bool survives, present;
+
+  json_t *sub_field;
+
+  /* Find the requirement object. */
+  json_t *requirement = json_object_get(json_packet, key);
+  if (!requirement) {
+    log_error("ERROR: Unable to get requirement with key: %s", key);
+    return FALSE;
+  }
+
+  /* Find the requirement object fields and translate their values. */
+  if (!dio_get_uint8_json(requirement, "kind", &kind)) {
+    log_error("ERROR: Unable to get part of requirement with key: %s",
+              key);
+    return FALSE;
+  }
+
+  if (!dio_get_sint32_json(requirement, "value", &value)) {
+    log_error("ERROR: Unable to get part of requirement with key: %s",
+              key);
+    return FALSE;
+  }
+
+  if (!dio_get_uint8_json(requirement, "range", &range)) {
+    log_error("ERROR: Unable to get part of requirement with key: %s",
+              key);
+    return FALSE;
+  }
+
+  if (!dio_get_bool8_json(requirement, "survives", &survives)) {
+    log_error("ERROR: Unable to get part of requirement with key: %s",
+              key);
+    return FALSE;
+  }
+
+  if (!dio_get_bool8_json(requirement, "present", &present)) {
+    log_error("ERROR: Unable to get part of requirement with key: %s",
+              key);
+    return FALSE;
+  }
+
+  /* Create a requirement with the values sent over the network. */
+  *preq = req_from_values(kind, range, survives, present, value);
+
   return TRUE;
 }
 
@@ -486,12 +531,32 @@
 }
 
 /**************************************************************************
-  ..
+  Insert a single requirement.
 **************************************************************************/
 void dio_put_requirement_json(struct json_data_out *dout, char *key,
                               const struct requirement *preq)
 {
-  /* TODO: implement */
+  int kind, range, value;
+  bool survives, present;
+
+  /* Create the requirement object. */
+  json_t *requirement = json_object();
+
+  /* Read the requirment values. */
+  req_get_values(preq, &kind, &range, &survives, &present, &value);
+
+  /* Write the requirement values to the fields of the requirment
+   * object. */
+  json_object_set(requirement, "kind", json_integer(kind));
+  json_object_set(requirement, "value", json_integer(value));
+
+  json_object_set(requirement, "range", json_integer(range));
+
+  json_object_set(requirement, "survives", json_boolean(survives));
+  json_object_set(requirement, "present", json_boolean(present));
+
+  /* Put the requirement object in the packet. */
+  json_object_set_new(dout->json, key, requirement);
 }
 
 /**************************************************************************


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

Reply via email to