Skip to content

Commit

Permalink
V1.0 - Itworks
Browse files Browse the repository at this point in the history
it woks i promise
  • Loading branch information
djpiper28 committed Apr 16, 2021
1 parent 5c5f6ee commit 7ceb733
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
39 changes: 16 additions & 23 deletions src/apiserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,22 @@ static void serverCreateGameCommand(struct ServerConnection *s,

//Check is line has equals with non-null strings on each side
if (eqPtr < line.len - 1 && eqPtr > 1) {
//Read value into *tmp
//+1 for null terminator -1 to remove \n
size_t valueLen = line.len - eqPtr;

char *tmp = (char *) malloc(sizeof(char) * (valueLen + 1));

//Read value
int j = 0;
for (size_t i = eqPtr + 1; i < line.len; i++) {
tmp[j] = line.ptr[i];
j++;
}
tmp[valueLen] = 0;

//Read prop tag into prop
size_t valueLen = line.len - eqPtr - 1;
size_t propLen = eqPtr;

//Error case - no prop len
if (propLen == 0) {
free(tmp);
} else {
if (propLen != 0) {
//Read value of the tag
char *tmp = (char *) malloc(sizeof(char) * (valueLen + 1));
size_t j = 0;
for (size_t i = eqPtr + 1; j < valueLen; i++) {
tmp[j] = line.ptr[i];
j++;
}
tmp[valueLen] = 0;

//Read prop tag into prop
char *prop = (char *) malloc(sizeof(char) * (propLen + 1));

char *prop = (char *) malloc(sizeof(char) * (propLen + 1));
for (size_t i = 0; i < eqPtr; i++)
prop[i] = line.ptr[i];
prop[propLen] = 0;
Expand Down Expand Up @@ -214,8 +206,8 @@ static void serverCreateGameCommand(struct ServerConnection *s,
&& gameName != NULL && password != NULL
&& playerCount != -1 && spectatorsAllowed != -1
&& spectatorsNeedPassword != -1 && spectatorsCanChat != -1
&& spectatorsCanSeeHands != -1 && onlyRegistered != -1;
&& spectatorsCanSeeHands != -1 && onlyRegistered != -1;

if (valid) {
//Check authtoken
if (strncmp(authToken, s->api->config.authToken, BUFFER_LENGTH) == 0) {
Expand Down Expand Up @@ -317,7 +309,6 @@ static void eventHandler(struct mg_connection *c,
freeGameCreateCallbackWaitParam(paramdata);

s->isGameCreate = 0;
s->closing = 1;
}
}

Expand All @@ -336,13 +327,15 @@ static void eventHandler(struct mg_connection *c,
api = s->api;
if (s->isGameCreate) {
pthread_mutex_lock(&s->param->mutex);

int ID = s->param->gameID;
if (ID != -1) {
freeGameCreateCallbackWaitParam(s->param);
free(s);
} else {
s->param->callbackFn = &ErrorCallback;
}

pthread_mutex_unlock(&s->param->mutex);
}
c->fn_data = NULL;
Expand Down
12 changes: 8 additions & 4 deletions src/bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ static void executeCallback(struct triceBot *b,
if (cmd->message != NULL)
free(cmd->message);

if (cmd->isGame) {
freeGameCreateCallbackWaitParam((struct gameCreateCallbackWaitParam *) cmd->param);
}

free(cmd);
}

Expand Down Expand Up @@ -514,7 +518,7 @@ static void handleGameEvent(struct triceBot *b,

//Game events
MACRO_CALL_FUNCTION_PTR_FOR_GAME_EVENT(onGameEventJoin,
Event_Join)
Event_Join)
else MACRO_CALL_FUNCTION_PTR_FOR_GAME_EVENT(onGameEventLeave,
Event_Leave)
else MACRO_CALL_FUNCTION_PTR_FOR_GAME_EVENT(onGameEventGameClosed,
Expand Down Expand Up @@ -582,7 +586,6 @@ static void handleGameCreate(struct triceBot *b,
const Event_GameJoined listGames) {
struct pendingCommand *cmd = gameWithName(&b->callbackQueue,
listGames.game_info().description().c_str());

if (cmd != NULL) {
//Create and add game item to the list
addGame(&b->gameList, createGame(listGames.game_info().game_id()));
Expand All @@ -591,7 +594,7 @@ static void handleGameCreate(struct triceBot *b,
struct gameCreateCallbackWaitParam *game = (struct gameCreateCallbackWaitParam *)
cmd->param;
//Check for null pointer
if (game != NULL) {
if (game != NULL) {
//Set the game ID (used in callbackFn both cases)
pthread_mutex_lock(&game->mutex);
game->gameID = listGames.game_info().game_id();
Expand Down Expand Up @@ -682,8 +685,9 @@ sendCreateGameCommand(struct triceBot *b,
param->gameID = -1;
param->sendTime = time(NULL);
param->callbackFn = callbackFn;
param->mutex = PTHREAD_MUTEX_INITIALIZER;

cmd->param = (void *) &param;
cmd->param = (void *) param;
cmd->isGame = 1;
enq(cmd, &b->sendQueue);

Expand Down

0 comments on commit 7ceb733

Please sign in to comment.