Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created test code for parsing JSON files into JSON objects and then into Game objects. #599

Merged
merged 21 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
650240f
adding a test text file to add sandbox directory
May 9, 2020
7afc2f5
Created a test json file as 'connected_rooms.jwdl' based on 'connecte…
May 9, 2020
131b6af
Added a README file
May 10, 2020
0235844
Created a Makefile and the first parser test function.
May 10, 2020
269c967
Removed text.txt
May 10, 2020
20a6689
Updated README.md
noahklowden May 10, 2020
413d350
Fixed Makefile to compile parser properly
May 10, 2020
66395b0
Merge branch 'wdl/json_parse_test' of https://github.com/uchicago-cs/…
May 10, 2020
7fc6ba1
Added first test to ensure information could be printed. Added assert…
May 11, 2020
1fb1633
Updated header file to be used to specify functions. Rearranged parse…
May 11, 2020
8648ff2
Proper swap to .jwdl instead of .json
May 11, 2020
fc06e83
This time actually pushing with connected_rooms.jwdl
May 11, 2020
dfccaef
Rewrote parser so that it would return game_t. Added in a parser that…
May 12, 2020
05d30e4
Fixed some typos. Continued trying to get the Makefile to connect to …
May 12, 2020
c849b6d
Merge branch 'dev' into wdl/json_parse_test
dtbukowski May 22, 2020
44bb8ab
Did the small changes that were requested besides doing tests to see …
May 22, 2020
0157a70
Resolving makefile issues
MaxineK36 May 23, 2020
67b2c5b
Added print statements for no arguments, empty file and changed Debug…
May 23, 2020
b9870f9
Made all changes requested by Maxine
May 23, 2020
9deddaa
Added code to main function that prints out game information that was…
May 23, 2020
a85574e
Merge branch 'dev' into wdl/json_parse_test
MaxineK36 May 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/wdl/sandbox/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Makefile for loading parser

BIN = parse
SRCS = parser.c ../../game-state/src/game.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

RM = rm -f

MaxineK36 marked this conversation as resolved.
Show resolved Hide resolved
$(BIN): parser.o ../../game-state/src/game.c
$(CC) $(DYN_LDFAGS) $^ -o $@ $(LDLIBS)

$(OBJS): %.o:%.c
$(CC) $(CFLAGS) -o $@ $(patsubst %.o, %.c, $@)

.PHONY: clean

clean:
$(RM) ${OBJS} $(BIN)
5 changes: 5 additions & 0 deletions src/wdl/sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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.
112 changes: 112 additions & 0 deletions src/wdl/sandbox/connected_rooms.jwdl
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
114 changes: 114 additions & 0 deletions src/wdl/sandbox/parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "parser.h"


/* DEBUG is 0 normally, 1 to print debugging statements */
#define DEBUG 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to change to 0 before this merges


/* Helper function: to print debugging statements only when debugging */
int my_print(char *string) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete these excess spaces (lines 14 and 18)

if (DEBUG) {
printf("%s\n",string);
}

return 0;

}


int add_rooms_to_game(json_object *rooms, game_t *g) {

//initializes json_objects for use in making room_t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments referring to multiple lines of code should be block comments
(/* Some comment here */)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed all of the code that should be block comments

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");
MaxineK36 marked this conversation as resolved.
Show resolved Hide resolved
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 = (char *) json_object_get_string(id_obj);

json_object_object_get_ex(room_obj, "short_desc", &short_desc_obj);
char *short_desc = (char *) json_object_get_string(short_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);

add_room_to_game(g, room);

}

return SUCCESS;

}

game_t *parse_wdl(char* filename) {
FILE *fp;
char buffer[4096];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare a macro for 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the file is empty?

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);

assert(game_obj);

//gets the intro text for the game
json_object_object_get_ex(game_obj, "intro", &intro_obj);
assert(intro_obj);

char *intro = (char*) json_object_get_string(intro_obj);

game_t *game = game_new(intro);

add_rooms_to_game(rooms_obj,game);
MaxineK36 marked this conversation as resolved.
Show resolved Hide resolved

return game;

}

int main(int argc, char **argv) {

if(argc > 1) {

parse_wdl(argv[1]);

}

MaxineK36 marked this conversation as resolved.
Show resolved Hide resolved
return 0;

}
66 changes: 66 additions & 0 deletions src/wdl/sandbox/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* The header file for the parser code
*
*/

#include "game-state/game.h"
MaxineK36 marked this conversation as resolved.
Show resolved Hide resolved
#include "game-state/room.h"

/* necessary includes to use JSON-C */
#include <json-c/json.h>

/*
* 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
*
* Returns:
* - a zero for success
*/
game_t *parse_wdl(char* filename);

/*
* main()
*
*/
int main(int argc, char **argv);