From 650240f6f1ea3d8af992154b029202abd89b215c Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sat, 9 May 2020 18:09:46 -0500 Subject: [PATCH 01/18] adding a test text file to add sandbox directory --- src/wdl/sandbox/text.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/wdl/sandbox/text.txt diff --git a/src/wdl/sandbox/text.txt b/src/wdl/sandbox/text.txt new file mode 100644 index 0000000000..6de7b8c69d --- /dev/null +++ b/src/wdl/sandbox/text.txt @@ -0,0 +1 @@ +This is a test file. From 7afc2f505ba40b36a275d6b77d94114f703a85d4 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sat, 9 May 2020 18:15:47 -0500 Subject: [PATCH 02/18] Created a test json file as 'connected_rooms.jwdl' based on 'connected_rooms.wdl' in the tests directory --- src/wdl/sandbox/connected_rooms.jwdl | 114 +++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/wdl/sandbox/connected_rooms.jwdl diff --git a/src/wdl/sandbox/connected_rooms.jwdl b/src/wdl/sandbox/connected_rooms.jwdl new file mode 100644 index 0000000000..445f80e7f7 --- /dev/null +++ b/src/wdl/sandbox/connected_rooms.jwdl @@ -0,0 +1,114 @@ +{ + "ITEMS": [ + { + "state": "still", + "long_desc": "This is a chair long", + "value": "no", + "in": "room A", + "short_desc": "This is a chair", + "id": "CHAIR", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the chair", + "text_fail": "You cannot push this chair" + }, + { + "action": "PULL", + "text_success": "You pull the chair", + "text_fail": "You cannot pull this chair" + }, + { + "action": "TAKE", + "text_success": "You take the chair", + "text_fail": "You cannot take this chair" + } + ] + }, + { + "state": "solid", + "long_desc": "This is a table long", + "value": "no", + "in": "room B", + "short_desc": "This is a table", + "id": "TABLE", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the table", + "text_fail": "You cannot push this table" + }, + { + "action": "PULL", + "text_success": "You pull the table", + "text_fail": "You cannot pull this table" + }, + { + "action": "TAKE", + "text_success": "You take the table", + "text_fail": "You cannot take this table" + } + ] + }, + { + "state": "hot", + "long_desc": "This is a candle long", + "in": "room C", + "short_desc": "This is a candle", + "id": "CANDLE", + "actions": [ + { + "action": "TAKE", + "text_success": "You take the candle", + "text_fail": "You cannot take this candle" + } + ] + } + ], + "GAME": [ + { + "start": "room A", + "intro": "This is the intro", + "end": [ + { + "in_room": "room C" + } + ] + } + ], + "ROOMS": [ + { + "connections": [ + { + "to": "room B", + "direction": "NORTH" + } + ], + "long_desc": "This is room A long", + "short_desc": "This is room A", + "id": "room A" + }, + { + "connections": [ + { + "to": "room C", + "direction": "NORTH" + } + ], + "long_desc": "This is room B long", + "short_desc": "This is room B", + "id": "room B" + }, + { + "connections": [ + { + "to": "room A", + "direction": "NORTH" + } + ], + "long_desc": "This is room C long", + "short_desc": "This is room C", + "id": "room C" + } + ] +} From 131b6afab49db4518ba721b3d0f4e964f5bfcb69 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 01:10:23 -0500 Subject: [PATCH 03/18] Added a README file --- src/wdl/sandbox/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/wdl/sandbox/README.md diff --git a/src/wdl/sandbox/README.md b/src/wdl/sandbox/README.md new file mode 100644 index 0000000000..ea31080da5 --- /dev/null +++ b/src/wdl/sandbox/README.md @@ -0,0 +1,4 @@ +This sandbox is for testing the parsing of and loading to objects of JSON files for the purposes of Chiventure's WDL system. + + +To avoid confusion with the current WDL system (which uses YAML files saved as .wdl), all files in this branch will be instead be .jwdl files. From 02358447635d4f2bbd71648255c06845f80c439a Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 04:14:53 -0500 Subject: [PATCH 04/18] Created a Makefile and the first parser test function. --- src/wdl/sandbox/Makefile | 16 ++++ src/wdl/sandbox/connected_rooms.json | 114 +++++++++++++++++++++++++++ src/wdl/sandbox/parser.c | 27 +++++++ src/wdl/sandbox/parser.h | 3 + 4 files changed, 160 insertions(+) create mode 100644 src/wdl/sandbox/Makefile create mode 100644 src/wdl/sandbox/connected_rooms.json create mode 100644 src/wdl/sandbox/parser.c create mode 100644 src/wdl/sandbox/parser.h diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile new file mode 100644 index 0000000000..d2f0d0fee0 --- /dev/null +++ b/src/wdl/sandbox/Makefile @@ -0,0 +1,16 @@ +# Makefile for loading parser + +BIN = parse +SRCS = parser.c + +CC = gcc +CFLAGS = -Wall -Wextra -fPIC -O2 -g +LDLIBS = -ljson-c + +RM = rm -f + +all: + $(CC) $(CFLAGS) $(LDLIBS) -o $(BIN) $(SRCS) + +clean: + $(RM) $(BIN) diff --git a/src/wdl/sandbox/connected_rooms.json b/src/wdl/sandbox/connected_rooms.json new file mode 100644 index 0000000000..445f80e7f7 --- /dev/null +++ b/src/wdl/sandbox/connected_rooms.json @@ -0,0 +1,114 @@ +{ + "ITEMS": [ + { + "state": "still", + "long_desc": "This is a chair long", + "value": "no", + "in": "room A", + "short_desc": "This is a chair", + "id": "CHAIR", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the chair", + "text_fail": "You cannot push this chair" + }, + { + "action": "PULL", + "text_success": "You pull the chair", + "text_fail": "You cannot pull this chair" + }, + { + "action": "TAKE", + "text_success": "You take the chair", + "text_fail": "You cannot take this chair" + } + ] + }, + { + "state": "solid", + "long_desc": "This is a table long", + "value": "no", + "in": "room B", + "short_desc": "This is a table", + "id": "TABLE", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the table", + "text_fail": "You cannot push this table" + }, + { + "action": "PULL", + "text_success": "You pull the table", + "text_fail": "You cannot pull this table" + }, + { + "action": "TAKE", + "text_success": "You take the table", + "text_fail": "You cannot take this table" + } + ] + }, + { + "state": "hot", + "long_desc": "This is a candle long", + "in": "room C", + "short_desc": "This is a candle", + "id": "CANDLE", + "actions": [ + { + "action": "TAKE", + "text_success": "You take the candle", + "text_fail": "You cannot take this candle" + } + ] + } + ], + "GAME": [ + { + "start": "room A", + "intro": "This is the intro", + "end": [ + { + "in_room": "room C" + } + ] + } + ], + "ROOMS": [ + { + "connections": [ + { + "to": "room B", + "direction": "NORTH" + } + ], + "long_desc": "This is room A long", + "short_desc": "This is room A", + "id": "room A" + }, + { + "connections": [ + { + "to": "room C", + "direction": "NORTH" + } + ], + "long_desc": "This is room B long", + "short_desc": "This is room B", + "id": "room B" + }, + { + "connections": [ + { + "to": "room A", + "direction": "NORTH" + } + ], + "long_desc": "This is room C long", + "short_desc": "This is room C", + "id": "room C" + } + ] +} diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c new file mode 100644 index 0000000000..2c0735ddd9 --- /dev/null +++ b/src/wdl/sandbox/parser.c @@ -0,0 +1,27 @@ +#include +#include +#include + +/* necessary includes to use JSON-C */ +#include + +int main(int argc, char **argv) { + FILE *fp; + char buffer[2048]; + + struct json_object *game_document; + struct json_object *game; + struct json_object *rooms; + struct json_object *items; + + fp = fopen("connected_rooms.json", "r"); + fread(buffer, 2048, 1, fp); + fclose(fp); + + game_document = json_tokener_parse(buffer); + + json_object_object_get_ex(game_document, "GAME", &game); + json_object_object_get_ex(game_document, "ROOMS", &rooms); + json_object_object_get_ex(game_document, "ITEMS", &items); + +} diff --git a/src/wdl/sandbox/parser.h b/src/wdl/sandbox/parser.h new file mode 100644 index 0000000000..97c3c1b329 --- /dev/null +++ b/src/wdl/sandbox/parser.h @@ -0,0 +1,3 @@ +/* The header file for the parser code + * + */ From 269c9674775b6f85a56e58d14e6d95a962b33a89 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 04:22:33 -0500 Subject: [PATCH 05/18] Removed text.txt --- src/wdl/sandbox/text.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/wdl/sandbox/text.txt diff --git a/src/wdl/sandbox/text.txt b/src/wdl/sandbox/text.txt deleted file mode 100644 index 6de7b8c69d..0000000000 --- a/src/wdl/sandbox/text.txt +++ /dev/null @@ -1 +0,0 @@ -This is a test file. From 20a668958c1225987f0d7e049da2c2a1eb8165fb Mon Sep 17 00:00:00 2001 From: noahklowden <62965310+noahklowden@users.noreply.github.com> Date: Sun, 10 May 2020 02:38:13 -0700 Subject: [PATCH 06/18] Updated README.md --- src/wdl/sandbox/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wdl/sandbox/README.md b/src/wdl/sandbox/README.md index ea31080da5..791f0e56c9 100644 --- a/src/wdl/sandbox/README.md +++ b/src/wdl/sandbox/README.md @@ -1,4 +1,5 @@ -This sandbox is for testing the parsing of and loading to objects of JSON files for the purposes of Chiventure's WDL system. +Currently, the JSON-C library must be installed for the parser to work. +This sandbox is for testing the parsing of and loading to objects of JSON files for the purposes of Chiventure's WDL system. To avoid confusion with the current WDL system (which uses YAML files saved as .wdl), all files in this branch will be instead be .jwdl files. From 413d350fad0a630ad58c541cb3e892e84cb585e6 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 18:24:47 -0500 Subject: [PATCH 07/18] Fixed Makefile to compile parser properly --- src/wdl/sandbox/Makefile | 14 +++- src/wdl/sandbox/connected_rooms.jwdl | 114 --------------------------- 2 files changed, 11 insertions(+), 117 deletions(-) delete mode 100644 src/wdl/sandbox/connected_rooms.jwdl diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile index d2f0d0fee0..c7b1fca53c 100644 --- a/src/wdl/sandbox/Makefile +++ b/src/wdl/sandbox/Makefile @@ -2,15 +2,23 @@ BIN = parse SRCS = parser.c +OBJS = $(SRCS:.c=.o) CC = gcc CFLAGS = -Wall -Wextra -fPIC -O2 -g +DYN_LDFLAGS = -L /usr/lib/x86_64=linux-gnu | grep json -Wl,-rpath,/usr/lib/x86_64=linux-gnu | grep json #-L designates where to find the library. LDLIBS = -ljson-c +LD_LIBRARY_PATH = /usr/lib/x86_64=linux-gnu | grep json RM = rm -f -all: - $(CC) $(CFLAGS) $(LDLIBS) -o $(BIN) $(SRCS) +$(BIN): parser.o + $(CC) $(DYN_LDFAGS) $^ -o $@ $(LDLIBS) + +$(OBJS): %.o:%.c + $(CC) $(CFLAGS) -c -o $@ $(patsubst %.o, %.c, $@) + +.PHONY: clean clean: - $(RM) $(BIN) + $(RM) ${OBJS} $(BIN) diff --git a/src/wdl/sandbox/connected_rooms.jwdl b/src/wdl/sandbox/connected_rooms.jwdl deleted file mode 100644 index 445f80e7f7..0000000000 --- a/src/wdl/sandbox/connected_rooms.jwdl +++ /dev/null @@ -1,114 +0,0 @@ -{ - "ITEMS": [ - { - "state": "still", - "long_desc": "This is a chair long", - "value": "no", - "in": "room A", - "short_desc": "This is a chair", - "id": "CHAIR", - "actions": [ - { - "action": "PUSH", - "text_success": "You push the chair", - "text_fail": "You cannot push this chair" - }, - { - "action": "PULL", - "text_success": "You pull the chair", - "text_fail": "You cannot pull this chair" - }, - { - "action": "TAKE", - "text_success": "You take the chair", - "text_fail": "You cannot take this chair" - } - ] - }, - { - "state": "solid", - "long_desc": "This is a table long", - "value": "no", - "in": "room B", - "short_desc": "This is a table", - "id": "TABLE", - "actions": [ - { - "action": "PUSH", - "text_success": "You push the table", - "text_fail": "You cannot push this table" - }, - { - "action": "PULL", - "text_success": "You pull the table", - "text_fail": "You cannot pull this table" - }, - { - "action": "TAKE", - "text_success": "You take the table", - "text_fail": "You cannot take this table" - } - ] - }, - { - "state": "hot", - "long_desc": "This is a candle long", - "in": "room C", - "short_desc": "This is a candle", - "id": "CANDLE", - "actions": [ - { - "action": "TAKE", - "text_success": "You take the candle", - "text_fail": "You cannot take this candle" - } - ] - } - ], - "GAME": [ - { - "start": "room A", - "intro": "This is the intro", - "end": [ - { - "in_room": "room C" - } - ] - } - ], - "ROOMS": [ - { - "connections": [ - { - "to": "room B", - "direction": "NORTH" - } - ], - "long_desc": "This is room A long", - "short_desc": "This is room A", - "id": "room A" - }, - { - "connections": [ - { - "to": "room C", - "direction": "NORTH" - } - ], - "long_desc": "This is room B long", - "short_desc": "This is room B", - "id": "room B" - }, - { - "connections": [ - { - "to": "room A", - "direction": "NORTH" - } - ], - "long_desc": "This is room C long", - "short_desc": "This is room C", - "id": "room C" - } - ] -} From 7fc6ba17954f6938244e494352d36fda8fec3d86 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 21:38:26 -0500 Subject: [PATCH 08/18] Added first test to ensure information could be printed. Added asserts to code. Fixed warnings of unused inputs in code. --- src/wdl/sandbox/connected_rooms.json | 6 ++--- src/wdl/sandbox/parser.c | 38 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/wdl/sandbox/connected_rooms.json b/src/wdl/sandbox/connected_rooms.json index 445f80e7f7..8b645f05f4 100644 --- a/src/wdl/sandbox/connected_rooms.json +++ b/src/wdl/sandbox/connected_rooms.json @@ -65,8 +65,7 @@ ] } ], - "GAME": [ - { + "GAME": { "start": "room A", "intro": "This is the intro", "end": [ @@ -74,8 +73,7 @@ "in_room": "room C" } ] - } - ], + }, "ROOMS": [ { "connections": [ diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index 2c0735ddd9..a2e4dd4551 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -1,27 +1,59 @@ #include #include #include +#include /* necessary includes to use JSON-C */ #include -int main(int argc, char **argv) { +/* DEBUG is 0 normally, 1 to print debugging statements */ +#define DEBUG 1 + +/* Helper function: to print debugging statements only when debugging */ +int my_print(char *string) { + + if (DEBUG) { + printf("%s\n",string); + } + + return 0; + +} + +int main() { FILE *fp; - char buffer[2048]; + char buffer[4096]; + /* The main json_objects for storing top level information*/ struct json_object *game_document; struct json_object *game; struct json_object *rooms; struct json_object *items; + /* json_objects for storing game object information */ + struct json_object *intro; + + /* reads the input file */ fp = fopen("connected_rooms.json", "r"); - fread(buffer, 2048, 1, fp); + assert(fp); + fread(buffer, 4096, 1, fp); fclose(fp); game_document = json_tokener_parse(buffer); + assert(game_document); + json_object_object_get_ex(game_document, "GAME", &game); json_object_object_get_ex(game_document, "ROOMS", &rooms); json_object_object_get_ex(game_document, "ITEMS", &items); + assert(game); + + json_object_object_get_ex(game, "intro", &intro); + + assert(intro); + + my_print("Intro:"); + my_print((char*)json_object_get_string(intro)); + } From 1fb16334c535ca6e0104c28ced94a6f9a2e14199 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 23:48:14 -0500 Subject: [PATCH 09/18] Updated header file to be used to specify functions. Rearranged parser to have parsing occur in separate function from main. --- src/wdl/sandbox/parser.c | 21 +++++++++++++++++---- src/wdl/sandbox/parser.h | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index a2e4dd4551..be364eea0f 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -3,8 +3,7 @@ #include #include -/* necessary includes to use JSON-C */ -#include +#include "parser.h" /* DEBUG is 0 normally, 1 to print debugging statements */ #define DEBUG 1 @@ -20,7 +19,7 @@ int my_print(char *string) { } -int main() { +json_object *parse(char* filename) { FILE *fp; char buffer[4096]; @@ -34,7 +33,7 @@ int main() { struct json_object *intro; /* reads the input file */ - fp = fopen("connected_rooms.json", "r"); + fp = fopen(filename, "r"); assert(fp); fread(buffer, 4096, 1, fp); fclose(fp); @@ -56,4 +55,18 @@ int main() { my_print("Intro:"); my_print((char*)json_object_get_string(intro)); + return game_document; + +} + +int main(int argc, char **argv) { + + if(argc > 1) { + + parse(argv[1]); + + } + + return 0; + } diff --git a/src/wdl/sandbox/parser.h b/src/wdl/sandbox/parser.h index 97c3c1b329..118634f24a 100644 --- a/src/wdl/sandbox/parser.h +++ b/src/wdl/sandbox/parser.h @@ -1,3 +1,23 @@ /* The header file for the parser code * */ + +/* necessary includes to use JSON-C */ +#include + +/* + * parse() + * + * Parameters: + * - filename: the path to the file to be parsed + * + * Returns: + * - a zero for success + */ +json_object *parse(char* filename); + +/* + * main() + * + */ +int main(int argc, char **argv); From 8648ff2080b342a7b7e7727163cae4a48df4d115 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 23:58:30 -0500 Subject: [PATCH 10/18] Proper swap to .jwdl instead of .json --- src/wdl/sandbox/connected_rooms.json | 112 --------------------------- 1 file changed, 112 deletions(-) delete mode 100644 src/wdl/sandbox/connected_rooms.json diff --git a/src/wdl/sandbox/connected_rooms.json b/src/wdl/sandbox/connected_rooms.json deleted file mode 100644 index 8b645f05f4..0000000000 --- a/src/wdl/sandbox/connected_rooms.json +++ /dev/null @@ -1,112 +0,0 @@ -{ - "ITEMS": [ - { - "state": "still", - "long_desc": "This is a chair long", - "value": "no", - "in": "room A", - "short_desc": "This is a chair", - "id": "CHAIR", - "actions": [ - { - "action": "PUSH", - "text_success": "You push the chair", - "text_fail": "You cannot push this chair" - }, - { - "action": "PULL", - "text_success": "You pull the chair", - "text_fail": "You cannot pull this chair" - }, - { - "action": "TAKE", - "text_success": "You take the chair", - "text_fail": "You cannot take this chair" - } - ] - }, - { - "state": "solid", - "long_desc": "This is a table long", - "value": "no", - "in": "room B", - "short_desc": "This is a table", - "id": "TABLE", - "actions": [ - { - "action": "PUSH", - "text_success": "You push the table", - "text_fail": "You cannot push this table" - }, - { - "action": "PULL", - "text_success": "You pull the table", - "text_fail": "You cannot pull this table" - }, - { - "action": "TAKE", - "text_success": "You take the table", - "text_fail": "You cannot take this table" - } - ] - }, - { - "state": "hot", - "long_desc": "This is a candle long", - "in": "room C", - "short_desc": "This is a candle", - "id": "CANDLE", - "actions": [ - { - "action": "TAKE", - "text_success": "You take the candle", - "text_fail": "You cannot take this candle" - } - ] - } - ], - "GAME": { - "start": "room A", - "intro": "This is the intro", - "end": [ - { - "in_room": "room C" - } - ] - }, - "ROOMS": [ - { - "connections": [ - { - "to": "room B", - "direction": "NORTH" - } - ], - "long_desc": "This is room A long", - "short_desc": "This is room A", - "id": "room A" - }, - { - "connections": [ - { - "to": "room C", - "direction": "NORTH" - } - ], - "long_desc": "This is room B long", - "short_desc": "This is room B", - "id": "room B" - }, - { - "connections": [ - { - "to": "room A", - "direction": "NORTH" - } - ], - "long_desc": "This is room C long", - "short_desc": "This is room C", - "id": "room C" - } - ] -} From fc06e83b5a40a75604b27385afa9f6dcf82332ad Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sun, 10 May 2020 23:59:15 -0500 Subject: [PATCH 11/18] This time actually pushing with connected_rooms.jwdl --- src/wdl/sandbox/connected_rooms.jwdl | 112 +++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/wdl/sandbox/connected_rooms.jwdl diff --git a/src/wdl/sandbox/connected_rooms.jwdl b/src/wdl/sandbox/connected_rooms.jwdl new file mode 100644 index 0000000000..8b645f05f4 --- /dev/null +++ b/src/wdl/sandbox/connected_rooms.jwdl @@ -0,0 +1,112 @@ +{ + "ITEMS": [ + { + "state": "still", + "long_desc": "This is a chair long", + "value": "no", + "in": "room A", + "short_desc": "This is a chair", + "id": "CHAIR", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the chair", + "text_fail": "You cannot push this chair" + }, + { + "action": "PULL", + "text_success": "You pull the chair", + "text_fail": "You cannot pull this chair" + }, + { + "action": "TAKE", + "text_success": "You take the chair", + "text_fail": "You cannot take this chair" + } + ] + }, + { + "state": "solid", + "long_desc": "This is a table long", + "value": "no", + "in": "room B", + "short_desc": "This is a table", + "id": "TABLE", + "actions": [ + { + "action": "PUSH", + "text_success": "You push the table", + "text_fail": "You cannot push this table" + }, + { + "action": "PULL", + "text_success": "You pull the table", + "text_fail": "You cannot pull this table" + }, + { + "action": "TAKE", + "text_success": "You take the table", + "text_fail": "You cannot take this table" + } + ] + }, + { + "state": "hot", + "long_desc": "This is a candle long", + "in": "room C", + "short_desc": "This is a candle", + "id": "CANDLE", + "actions": [ + { + "action": "TAKE", + "text_success": "You take the candle", + "text_fail": "You cannot take this candle" + } + ] + } + ], + "GAME": { + "start": "room A", + "intro": "This is the intro", + "end": [ + { + "in_room": "room C" + } + ] + }, + "ROOMS": [ + { + "connections": [ + { + "to": "room B", + "direction": "NORTH" + } + ], + "long_desc": "This is room A long", + "short_desc": "This is room A", + "id": "room A" + }, + { + "connections": [ + { + "to": "room C", + "direction": "NORTH" + } + ], + "long_desc": "This is room B long", + "short_desc": "This is room B", + "id": "room B" + }, + { + "connections": [ + { + "to": "room A", + "direction": "NORTH" + } + ], + "long_desc": "This is room C long", + "short_desc": "This is room C", + "id": "room C" + } + ] +} From dfccaef8c5a05410ef40f1d2ec1ed425920e8c06 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Mon, 11 May 2020 23:12:49 -0500 Subject: [PATCH 12/18] Rewrote parser so that it would return game_t. Added in a parser that will add rooms to the game. --- src/wdl/sandbox/Makefile | 4 +- src/wdl/sandbox/parser.c | 102 +++++++++++++++++++++++++++------------ src/wdl/sandbox/parser.h | 46 +++++++++++++++++- 3 files changed, 118 insertions(+), 34 deletions(-) diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile index c7b1fca53c..453d3ce9d6 100644 --- a/src/wdl/sandbox/Makefile +++ b/src/wdl/sandbox/Makefile @@ -5,7 +5,7 @@ SRCS = parser.c OBJS = $(SRCS:.c=.o) CC = gcc -CFLAGS = -Wall -Wextra -fPIC -O2 -g +CFLAGS = -Wall -Wextra -fPIC -O2 -c -g -I.../include/ DYN_LDFLAGS = -L /usr/lib/x86_64=linux-gnu | grep json -Wl,-rpath,/usr/lib/x86_64=linux-gnu | grep json #-L designates where to find the library. LDLIBS = -ljson-c LD_LIBRARY_PATH = /usr/lib/x86_64=linux-gnu | grep json @@ -16,7 +16,7 @@ $(BIN): parser.o $(CC) $(DYN_LDFAGS) $^ -o $@ $(LDLIBS) $(OBJS): %.o:%.c - $(CC) $(CFLAGS) -c -o $@ $(patsubst %.o, %.c, $@) + $(CC) $(CFLAGS) -o $@ $(patsubst %.o, %.c, $@) .PHONY: clean diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index be364eea0f..cf52dbee34 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -5,6 +5,7 @@ #include "parser.h" + /* DEBUG is 0 normally, 1 to print debugging statements */ #define DEBUG 1 @@ -19,43 +20,84 @@ int my_print(char *string) { } -json_object *parse(char* filename) { - FILE *fp; - char buffer[4096]; - - /* The main json_objects for storing top level information*/ - struct json_object *game_document; - struct json_object *game; - struct json_object *rooms; - struct json_object *items; - - /* json_objects for storing game object information */ - struct json_object *intro; - /* reads the input file */ - fp = fopen(filename, "r"); - assert(fp); - fread(buffer, 4096, 1, fp); - fclose(fp); +int add_rooms_to_game(json_object *rooms, game_t *g) { + + //initializes json_objects for use in making room_t + int arr_len = json_object_array_length(rooms); + struct json_object room_obj; + struct json_object id_obj; + struct json_object long_desc_obj; + struct json_object short_desc_obj; + + //if room list is empty, return 1 + if(rooms != NULL) { + fprintf(stderr, "rooms list is empty\n"); + return FAILURE; + } + + //for loops list of rooms, creates a new game_struct room, add room to game + for (int i = 0; i < arr_len; i++) { + + room_obj = json_object_array_get_idx(rooms, i); + + json_object_object_get_ex(room_obj, "id", &id_obj); + char *id = json_object_get_string(id_obj); + + json_object_object_get_ex(room_obj, "short_desc", &short_desc_obj); + char *short_desc = json_object_get_string(short_desc_obj); + + json_object_object_get_ex(rooms_obj, "long_desc", &long_desc_obj); + char *long_desc = json_object_get_string(long_desc_obj); + + room_t *room = room_new(id, short_desc, long_desc); + + add_room_to_game(g, room); - game_document = json_tokener_parse(buffer); - - assert(game_document); + } + + return SUCCESS; + +} + +game_t *parse_wdl(char* filename) { + FILE *fp; + char buffer[4096]; + + /* The main json_objects for storing top level information*/ + struct json_object *game_document; + struct json_object *game_obj; + struct json_object *rooms_obj; + struct json_object *items_obj; + struct json_object *intro_obj; + + /* reads the input file */ + fp = fopen(filename, "r"); + assert(fp); + fread(buffer, 4096, 1, fp); + fclose(fp); + + game_document = json_tokener_parse(buffer); + + assert(game_document); + + json_object_object_get_ex(game_document, "GAME", &game_obj); + json_object_object_get_ex(game_document, "ROOMS", &rooms_obj); + json_object_object_get_ex(game_document, "ITEMS", &items_obj); - json_object_object_get_ex(game_document, "GAME", &game); - json_object_object_get_ex(game_document, "ROOMS", &rooms); - json_object_object_get_ex(game_document, "ITEMS", &items); + assert(game_obj); - assert(game); + //gets the intro text for the game + json_object_object_get_ex(game, "intro", &intro_obj); + assert(intro_text); - json_object_object_get_ex(game, "intro", &intro); + char *intro = json_object_get_string(intro_obj); - assert(intro); + game_t *game = game_new(intro); - my_print("Intro:"); - my_print((char*)json_object_get_string(intro)); + add_rooms_to_game(rooms_obj,game); - return game_document; + return game; } @@ -63,7 +105,7 @@ int main(int argc, char **argv) { if(argc > 1) { - parse(argv[1]); + parse_wdl(argv[1]); } diff --git a/src/wdl/sandbox/parser.h b/src/wdl/sandbox/parser.h index 118634f24a..97f9f42511 100644 --- a/src/wdl/sandbox/parser.h +++ b/src/wdl/sandbox/parser.h @@ -2,11 +2,53 @@ * */ +#include "game-state/game.h" + /* necessary includes to use JSON-C */ #include /* - * parse() + * add_connections_to_rooms() + * Adds the connections to the rooms + * + * Parameters: + * - doc: The json_object that it was stored in + * - g: the game object + * + * Returns: + * - SUCCESS if passes, FAILURE if fails + */ +int add_connections_to_rooms(json_object *doc, game_t *g); + +/* + * add_items_to_game() + * Adds the items to the game object + * + * Parameters: + * - doc: The json_object that it was stored in + * - g: the game object + * + * Returns: + * - SUCCESS if passes, FAILURE if fails + */ +int add_items_to_game(json_object *doc, game_t *g); + +/* + * add_rooms_to_game() + * Adds the rooms to the game object + * + * Parameters: + * - doc: The json_object that it was stored in + * - g: the game object + * + * Returns: + * - SUCCESS if passes, FAILURE if fails + */ +int add_rooms_to_game(json_object *doc, game_t *g); + +/* + * parse_wdl() + * parses the wdl document into a game_t * * Parameters: * - filename: the path to the file to be parsed @@ -14,7 +56,7 @@ * Returns: * - a zero for success */ -json_object *parse(char* filename); +game_t *parse_wdl(char* filename); /* * main() From 05d30e45e4f1a977c46475a6fa3a712c29ea9020 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Tue, 12 May 2020 02:44:57 -0500 Subject: [PATCH 13/18] Fixed some typos. Continued trying to get the Makefile to connect to game.c --- src/wdl/sandbox/Makefile | 6 +++--- src/wdl/sandbox/parser.c | 22 +++++++++++----------- src/wdl/sandbox/parser.h | 1 + 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile index 453d3ce9d6..0a112714aa 100644 --- a/src/wdl/sandbox/Makefile +++ b/src/wdl/sandbox/Makefile @@ -1,18 +1,18 @@ # Makefile for loading parser BIN = parse -SRCS = parser.c +SRCS = parser.c ../../game-state/src/game.c OBJS = $(SRCS:.c=.o) CC = gcc -CFLAGS = -Wall -Wextra -fPIC -O2 -c -g -I.../include/ +CFLAGS = -Wall -Wextra -fPIC -O2 -c -g -I../../../include/ -I.../include/ DYN_LDFLAGS = -L /usr/lib/x86_64=linux-gnu | grep json -Wl,-rpath,/usr/lib/x86_64=linux-gnu | grep json #-L designates where to find the library. LDLIBS = -ljson-c LD_LIBRARY_PATH = /usr/lib/x86_64=linux-gnu | grep json RM = rm -f -$(BIN): parser.o +$(BIN): parser.o ../../game-state/src/game.c $(CC) $(DYN_LDFAGS) $^ -o $@ $(LDLIBS) $(OBJS): %.o:%.c diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index cf52dbee34..f15c4d7ad9 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -25,10 +25,10 @@ int add_rooms_to_game(json_object *rooms, game_t *g) { //initializes json_objects for use in making room_t int arr_len = json_object_array_length(rooms); - struct json_object room_obj; - struct json_object id_obj; - struct json_object long_desc_obj; - struct json_object short_desc_obj; + struct json_object *room_obj; + struct json_object *id_obj; + struct json_object *long_desc_obj; + struct json_object *short_desc_obj; //if room list is empty, return 1 if(rooms != NULL) { @@ -42,13 +42,13 @@ int add_rooms_to_game(json_object *rooms, game_t *g) { room_obj = json_object_array_get_idx(rooms, i); json_object_object_get_ex(room_obj, "id", &id_obj); - char *id = json_object_get_string(id_obj); + char *id = (char *) json_object_get_string(id_obj); json_object_object_get_ex(room_obj, "short_desc", &short_desc_obj); - char *short_desc = json_object_get_string(short_desc_obj); + char *short_desc = (char *) json_object_get_string(short_desc_obj); - json_object_object_get_ex(rooms_obj, "long_desc", &long_desc_obj); - char *long_desc = json_object_get_string(long_desc_obj); + json_object_object_get_ex(room_obj, "long_desc", &long_desc_obj); + char *long_desc = (char *) json_object_get_string(long_desc_obj); room_t *room = room_new(id, short_desc, long_desc); @@ -88,10 +88,10 @@ game_t *parse_wdl(char* filename) { assert(game_obj); //gets the intro text for the game - json_object_object_get_ex(game, "intro", &intro_obj); - assert(intro_text); + json_object_object_get_ex(game_obj, "intro", &intro_obj); + assert(intro_obj); - char *intro = json_object_get_string(intro_obj); + char *intro = (char*) json_object_get_string(intro_obj); game_t *game = game_new(intro); diff --git a/src/wdl/sandbox/parser.h b/src/wdl/sandbox/parser.h index 97f9f42511..c8bd493c07 100644 --- a/src/wdl/sandbox/parser.h +++ b/src/wdl/sandbox/parser.h @@ -3,6 +3,7 @@ */ #include "game-state/game.h" +#include "game-state/room.h" /* necessary includes to use JSON-C */ #include From 44bb8ab15cd46def5195353a46c923c0b92e42da Mon Sep 17 00:00:00 2001 From: David Thomas Bukowski Date: Fri, 22 May 2020 18:47:09 -0500 Subject: [PATCH 14/18] Did the small changes that were requested besides doing tests to see if jsonobjs are libobjs, however makefile has problems --- src/wdl/sandbox/Makefile | 12 ++++++------ src/wdl/sandbox/parser.c | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile index 0a112714aa..ca0d7e19a0 100644 --- a/src/wdl/sandbox/Makefile +++ b/src/wdl/sandbox/Makefile @@ -1,19 +1,19 @@ # Makefile for loading parser BIN = parse -SRCS = parser.c ../../game-state/src/game.c +SRCS = parser.c OBJS = $(SRCS:.c=.o) CC = gcc CFLAGS = -Wall -Wextra -fPIC -O2 -c -g -I../../../include/ -I.../include/ -DYN_LDFLAGS = -L /usr/lib/x86_64=linux-gnu | grep json -Wl,-rpath,/usr/lib/x86_64=linux-gnu | grep json #-L designates where to find the library. -LDLIBS = -ljson-c -LD_LIBRARY_PATH = /usr/lib/x86_64=linux-gnu | grep json +DYN_LDFLAGS = "-L /usr/lib/x86_64=linux-gnu | grep json -L ../../../build/src/game-state/libgame-state.a" -Wl,-rpath,"/usr/lib/x86_64=linux-gnu ../../../build/src/game-state/libgame-state.a"#-L designates where to find the library. +LDLIBS = -ljson-c -l:$libgame-state.a +LD_LIBRARY_PATH = ../../../build/src/game-state/ /usr/lib/x86_64=linux-gnu | grep json RM = rm -f -$(BIN): parser.o ../../game-state/src/game.c - $(CC) $(DYN_LDFAGS) $^ -o $@ $(LDLIBS) +$(BIN): parser.o ../../../build/src/game-state/libgame-state.a + $(CC) $(DYN_LDFAGS) $< -o $@ $(LDLIBS) $(OBJS): %.o:%.c $(CC) $(CFLAGS) -o $@ $(patsubst %.o, %.c, $@) diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index f15c4d7ad9..49f4bbc51b 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -9,15 +9,15 @@ /* DEBUG is 0 normally, 1 to print debugging statements */ #define DEBUG 1 +/* Maximum number of bytes in a file parser can parse */ +#define MAX_BYTES 4096 + /* Helper function: to print debugging statements only when debugging */ int my_print(char *string) { - if (DEBUG) { printf("%s\n",string); } - return 0; - } @@ -62,7 +62,7 @@ int add_rooms_to_game(json_object *rooms, game_t *g) { game_t *parse_wdl(char* filename) { FILE *fp; - char buffer[4096]; + char buffer[MAX_BYTES]; /* The main json_objects for storing top level information*/ struct json_object *game_document; @@ -74,7 +74,7 @@ game_t *parse_wdl(char* filename) { /* reads the input file */ fp = fopen(filename, "r"); assert(fp); - fread(buffer, 4096, 1, fp); + fread(buffer, MAX_BYTES, 1, fp); fclose(fp); game_document = json_tokener_parse(buffer); From 0157a7043e65d3710f4be3438088e4818eaddbf8 Mon Sep 17 00:00:00 2001 From: Maxine King Date: Fri, 22 May 2020 21:59:36 -0500 Subject: [PATCH 15/18] Resolving makefile issues --- src/wdl/sandbox/Makefile | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/wdl/sandbox/Makefile b/src/wdl/sandbox/Makefile index ca0d7e19a0..857d2e2768 100644 --- a/src/wdl/sandbox/Makefile +++ b/src/wdl/sandbox/Makefile @@ -6,14 +6,19 @@ OBJS = $(SRCS:.c=.o) CC = gcc CFLAGS = -Wall -Wextra -fPIC -O2 -c -g -I../../../include/ -I.../include/ -DYN_LDFLAGS = "-L /usr/lib/x86_64=linux-gnu | grep json -L ../../../build/src/game-state/libgame-state.a" -Wl,-rpath,"/usr/lib/x86_64=linux-gnu ../../../build/src/game-state/libgame-state.a"#-L designates where to find the library. -LDLIBS = -ljson-c -l:$libgame-state.a -LD_LIBRARY_PATH = ../../../build/src/game-state/ /usr/lib/x86_64=linux-gnu | grep json + +DYN_LDFLAGS = -L /usr/lib/x86_64=linux-gnu | grep json -Wl,-rpath,/usr/lib/x86_64=linux-gnu | grep json #-L designates where to find the library. +LPATH = ../../../build/src/game-state/ +STC_LDFLAGS = -L../../../build/src/game-state/$(LPATH) +STC_LIB = libgame-state.a + +LDLIBS = -ljson-c +LD_LIBRARY_PATH = /usr/lib/x86_64=linux-gnu | grep json RM = rm -f -$(BIN): parser.o ../../../build/src/game-state/libgame-state.a - $(CC) $(DYN_LDFAGS) $< -o $@ $(LDLIBS) +$(BIN): parser.o $(LPATH)$(STC_LIB) + $(CC) $(DYN_LDFAGS) $(STC_LDFLAGS) $^ -o $@ -l:$(STC_LIB) $(LDLIBS) -lm $(OBJS): %.o:%.c $(CC) $(CFLAGS) -o $@ $(patsubst %.o, %.c, $@) From 67b2c5b7c172104609cd592f24ce806208076be3 Mon Sep 17 00:00:00 2001 From: David Thomas Bukowski Date: Fri, 22 May 2020 22:29:34 -0500 Subject: [PATCH 16/18] Added print statements for no arguments, empty file and changed Debug to 0 --- src/wdl/sandbox/parser.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index 49f4bbc51b..1bd3f052f4 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -7,7 +7,7 @@ /* DEBUG is 0 normally, 1 to print debugging statements */ -#define DEBUG 1 +#define DEBUG 0 /* Maximum number of bytes in a file parser can parse */ #define MAX_BYTES 4096 @@ -74,7 +74,12 @@ game_t *parse_wdl(char* filename) { /* reads the input file */ fp = fopen(filename, "r"); assert(fp); - fread(buffer, MAX_BYTES, 1, fp); + int bytesparsed = fread(buffer, MAX_BYTES, 1, fp); + + if (bytesparsed == 0){ + printf("File is empty"); + } + fclose(fp); game_document = json_tokener_parse(buffer); @@ -106,9 +111,12 @@ int main(int argc, char **argv) { if(argc > 1) { parse_wdl(argv[1]); - + return 0; + } else { + printf("Need arguments to parse\n"); + return 1; } - return 0; + } From b9870f9a35100f1fd6ef65011d34648d4f39e5df Mon Sep 17 00:00:00 2001 From: David Thomas Bukowski Date: Fri, 22 May 2020 22:33:03 -0500 Subject: [PATCH 17/18] Made all changes requested by Maxine --- src/wdl/sandbox/parser.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index 1bd3f052f4..bc324739d5 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -30,13 +30,13 @@ int add_rooms_to_game(json_object *rooms, game_t *g) { struct json_object *long_desc_obj; struct json_object *short_desc_obj; - //if room list is empty, return 1 - if(rooms != NULL) { + /*if room list is empty, return 1*/ + if(rooms == NULL) { fprintf(stderr, "rooms list is empty\n"); return FAILURE; } - //for loops list of rooms, creates a new game_struct room, add room to game + /*for loops list of rooms, creates a new game_struct room, add room to game*/ for (int i = 0; i < arr_len; i++) { room_obj = json_object_array_get_idx(rooms, i); @@ -79,7 +79,7 @@ game_t *parse_wdl(char* filename) { if (bytesparsed == 0){ printf("File is empty"); } - + fclose(fp); game_document = json_tokener_parse(buffer); @@ -92,7 +92,7 @@ game_t *parse_wdl(char* filename) { assert(game_obj); - //gets the intro text for the game + /*gets the intro text for the game*/ json_object_object_get_ex(game_obj, "intro", &intro_obj); assert(intro_obj); From 9deddaaed5ec9c7a7c5091859e9bfecb826fca78 Mon Sep 17 00:00:00 2001 From: Noah Elliot Chewbacca Klowden Date: Sat, 23 May 2020 02:19:03 -0500 Subject: [PATCH 18/18] Added code to main function that prints out game information that was parsed from the returned game object. Also fixed fread file so that empty file text only displays when file is actually empty. --- src/wdl/sandbox/parser.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/wdl/sandbox/parser.c b/src/wdl/sandbox/parser.c index bc324739d5..6e393bda7b 100644 --- a/src/wdl/sandbox/parser.c +++ b/src/wdl/sandbox/parser.c @@ -74,7 +74,7 @@ game_t *parse_wdl(char* filename) { /* reads the input file */ fp = fopen(filename, "r"); assert(fp); - int bytesparsed = fread(buffer, MAX_BYTES, 1, fp); + int bytesparsed = fread(buffer, 1, MAX_BYTES, fp); if (bytesparsed == 0){ printf("File is empty"); @@ -108,15 +108,29 @@ game_t *parse_wdl(char* filename) { int main(int argc, char **argv) { + game_t *game; + if(argc > 1) { - parse_wdl(argv[1]); - return 0; + game = parse_wdl(argv[1]); } else { printf("Need arguments to parse\n"); return 1; } - + printf("Start Description:\n%s\n\n",game->start_desc); + + room_list_t *rooms = get_all_rooms(game); + room_t *room; + + printf("List of rooms:\n"); + while(rooms->next) + { + room = rooms->room; + printf("%s:\n",room->room_id); + printf(" Short description: %s\n",room->short_desc); + printf(" Long description: %s\n\n",room->long_desc); + rooms = rooms->next; + } }