Skip to content

Commit

Permalink
api: improve host and port change with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
savalet committed Nov 25, 2024
1 parent d901d93 commit a76ef07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions include/hunter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#define AMMO_COUNT 3
#define FRAMERATE 144
#define MICRO_TO_SEC(time) time.microseconds / 1000000.0
#define API_HOST "http://localhost"
#define API_PORT 3000
#define API_TIMEOUT 2000000

typedef struct {
sfTexture *texture;
Expand Down
14 changes: 8 additions & 6 deletions src/score_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ int get_score(hunterinfo_t *hf)
sfHttp *http = sfHttp_create();
sfHttpRequest *req = sfHttpRequest_create();
sfHttpResponse *res;
sfTime timeout = (sfTime){ .microseconds = 0 };
sfTime timeout = (sfTime){ .microseconds = API_TIMEOUT };
char const *body;

sfHttpRequest_setMethod(req, sfHttpGet);
sfHttpRequest_setUri(req, "/score");
sfHttp_setHost(http, "http://localhost", 3000);
sfHttp_setHost(http, API_HOST, API_PORT);
res = sfHttp_sendRequest(http, req, timeout);
body = sfHttpResponse_getBody(res);
hf->high_score = my_getnbr(body);
my_printf("%d\n", hf->high_score);
my_printf("Get score from %s:%d\nAPI response: %d\n", API_HOST, API_PORT,
hf->high_score);
return EXIT_SUCCESS;
}

Expand All @@ -32,7 +33,7 @@ int post_score(hunterinfo_t *hf)
sfHttp *http = sfHttp_create();
sfHttpRequest *req = sfHttpRequest_create();
sfHttpResponse *res;
sfTime timeout = (sfTime){ .microseconds = 0 };
sfTime timeout = (sfTime){ .microseconds = API_TIMEOUT };
char score[10];
char *uri = "/score?score=";
char const *body;
Expand All @@ -41,9 +42,10 @@ int post_score(hunterinfo_t *hf)
my_numstr(score, hf->score);
sfHttpRequest_setUri(req, my_strcat(uri, score));
sfHttpRequest_setBody(req, score);
sfHttp_setHost(http, "http://localhost", 3000);
sfHttp_setHost(http, API_HOST, API_PORT);
res = sfHttp_sendRequest(http, req, timeout);
body = sfHttpResponse_getBody(res);
my_printf("%s\n", body);
my_printf("Post score to %s:%d\nAPI response: %s\n", API_HOST, API_PORT,
body);
return EXIT_SUCCESS;
}

0 comments on commit a76ef07

Please sign in to comment.