Reorganized file structure.

- Reimplemented /LOOK.
- Commands are now accepted in both upper and lower case.
- Move now accepts a number for easier movement.
This commit is contained in:
Barry Kane 2022-05-20 22:28:07 +01:00
parent 151f3002b8
commit 8673bb1ad5
17 changed files with 98 additions and 31 deletions

View File

@ -1,9 +1,9 @@
CC = gcc CC = gcc
clientsrc = $(wildcard src/misc/*.c) \ clientsrc = $(wildcard src/*.c) \
src/SilverMUDClient.c src/client/SilverMUDClient.c
clientobj = $(clientsrc:.c=.o) clientobj = $(clientsrc:.c=.o)
serversrc = $(wildcard src/misc/*.c) \ serversrc = $(wildcard src/*.c) \
src/SilverMUDServer.c src/server/SilverMUDServer.c
serverobj = $(serversrc:.c=.o) serverobj = $(serversrc:.c=.o)
CLIENTLDFLAGS= -lpthread -lncurses -lgnutls CLIENTLDFLAGS= -lpthread -lncurses -lgnutls
SERVERLDFLAGS= -lpthread -lncurses -lgnutls SERVERLDFLAGS= -lpthread -lncurses -lgnutls

View File

@ -13,10 +13,10 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include "misc/constants.h" #include "../../include/constants.h"
#include "misc/playerdata.h" #include "../../include/playerdata.h"
#include "misc/texteffects.h" #include "../../include/texteffects.h"
#include "misc/inputoutput.h" #include "../../include/inputoutput.h"
// A struct for passing arguments to our threads containing a file descriptor and a window pointer: // A struct for passing arguments to our threads containing a file descriptor and a window pointer:
typedef struct threadparameters typedef struct threadparameters
@ -85,7 +85,8 @@ void * messageReceiver(void * parameters)
shouldExit = true; shouldExit = true;
pthread_exit(NULL); pthread_exit(NULL);
} }
slowPrintNcurses("\n --====<>====-- \n", 8000, threadParameters->window, true); slowPrintNcurses("\n --====<", 8000, threadParameters->window, true);
slowPrintNcurses(">====-- \n", 8000, threadParameters->window, true);
slowPrintNcurses(receiveBuffer.messageContent, 8000, threadParameters->window, false); slowPrintNcurses(receiveBuffer.messageContent, 8000, threadParameters->window, false);
slowPrintNcurses("\n --====<>====-- \n", 8000, threadParameters->window, true); slowPrintNcurses("\n --====<>====-- \n", 8000, threadParameters->window, true);
} }

View File

@ -1,10 +1,11 @@
// gamelogic.c: Contains function definitons for dealing with the game's logic. // gamelogic.c: Contains function definitons for dealing with the game's logic.
// Barry Kane, 2022. // Barry Kane, 2022.
#include <stdio.h>
#include <string.h> #include <string.h>
#include "constants.h" #include "../include/constants.h"
#include "gamelogic.h" #include "../include/gamelogic.h"
#include "playerdata.h" #include "../include/playerdata.h"
#include "inputoutput.h" #include "../include/inputoutput.h"
// ======================= // =======================
// -=[ Main Game Loop ]=-: // -=[ Main Game Loop ]=-:
@ -13,11 +14,13 @@
// Thread function which runs the main game loop, given the needed parameters: // Thread function which runs the main game loop, given the needed parameters:
void * gameLogicLoop(void * parameters) void * gameLogicLoop(void * parameters)
{ {
char formattedString[64];
gameLogicParameters * threadParameters = parameters; gameLogicParameters * threadParameters = parameters;
inputMessage * currentInput = NULL; inputMessage * currentInput = NULL;
bool keepRunning = true; bool keepRunning = true;
while(keepRunning) while(keepRunning)
{ {
// Check for new messages and pop them off the queue:
if(threadParameters->inputQueue->currentLength != 0) if(threadParameters->inputQueue->currentLength != 0)
{ {
while(threadParameters->inputQueue->lock == true) while(threadParameters->inputQueue->lock == true)
@ -26,11 +29,15 @@ void * gameLogicLoop(void * parameters)
} }
currentInput = peekInputMessage(threadParameters->inputQueue); currentInput = peekInputMessage(threadParameters->inputQueue);
userInputSanatize(currentInput->content->messageContent, MAX); userInputSanatize(currentInput->content->messageContent, MAX);
// A slash as the first character means the message is a user command:
if(currentInput->content->messageContent[0] == '/') if(currentInput->content->messageContent[0] == '/')
{ {
// TODO: Implement Command Queue. // TODO: Implement Command Queue.
// For now, basic intepretation will do. // For now, basic intepretation will do.
if(strncmp(&currentInput->content->messageContent[1], "EXIT", 4) == 0)
// Exit command: Sends an "empty" exit message to disconnect a client:
if(strncmp(&currentInput->content->messageContent[1], "EXIT", 4) == 0 ||
strncmp(&currentInput->content->messageContent[1], "exit", 4) == 0)
{ {
userMessage * exitMessage = malloc(sizeof(userMessage)); userMessage * exitMessage = malloc(sizeof(userMessage));
exitMessage->senderName[0] = '\0'; exitMessage->senderName[0] = '\0';
@ -38,9 +45,13 @@ void * gameLogicLoop(void * parameters)
queueTargetedOutputMessage(threadParameters->outputQueue, exitMessage, &currentInput->sender, 1); queueTargetedOutputMessage(threadParameters->outputQueue, exitMessage, &currentInput->sender, 1);
free(exitMessage); free(exitMessage);
} }
if(strncmp(&currentInput->content->messageContent[1], "MOVE", 4) == 0)
// Move command: Moves the current player down a path in their current area, given a pathname or number:
if(strncmp(&currentInput->content->messageContent[1], "MOVE", 4) == 0 ||
strncmp(&currentInput->content->messageContent[1], "move", 4) == 0)
{ {
userMessage * moveMessage = malloc(sizeof(userMessage)); userMessage * moveMessage = malloc(sizeof(userMessage));
bzero(moveMessage, sizeof(userMessage));
char requestedPath[32]; char requestedPath[32];
strncpy(requestedPath, &currentInput->content->messageContent[6], 32); strncpy(requestedPath, &currentInput->content->messageContent[6], 32);
userInputSanatize(requestedPath, 32); userInputSanatize(requestedPath, 32);
@ -55,12 +66,51 @@ void * gameLogicLoop(void * parameters)
requestedPath[31] = '\0'; requestedPath[31] = '\0';
if(movePlayerToArea(currentInput->sender, requestedPath) == 0) if(movePlayerToArea(currentInput->sender, requestedPath) == 0)
{ {
strcpy(moveMessage->senderName, "\0"); moveMessage->senderName[0] = '\0';
strcpy(moveMessage->messageContent, currentInput->sender->currentArea->areaDescription); strncat(moveMessage->messageContent, currentInput->sender->currentArea->areaName, 33);
strncat(moveMessage->messageContent, "\n", 2);
strncat(moveMessage->messageContent, currentInput->sender->currentArea->areaDescription, 256);
strncat(moveMessage->messageContent, "\nYou can go:", 13);
for(int index = 0; index < 16; index++)
{
if(currentInput->sender->currentArea->areaExits[index] != NULL)
{
snprintf(formattedString, 64, "\n\t%d. %s", index + 1, currentInput->sender->currentArea->areaExits[index]->pathName);
strncat(moveMessage->messageContent, formattedString, 64);
}
}
queueTargetedOutputMessage(threadParameters->outputQueue, moveMessage, &currentInput->sender, 1); queueTargetedOutputMessage(threadParameters->outputQueue, moveMessage, &currentInput->sender, 1);
} }
free(moveMessage); free(moveMessage);
} }
// Look command: Sends the current area's name, description, and
if(strncmp(&currentInput->content->messageContent[1], "LOOK", 4) == 0 ||
strncmp(&currentInput->content->messageContent[1], "look", 4) == 0)
{
userMessage * lookMessage = malloc(sizeof(userMessage));
strncat(lookMessage->messageContent, currentInput->sender->currentArea->areaName, 33);
strncat(lookMessage->messageContent, "\n", 2);
strncat(lookMessage->messageContent, currentInput->sender->currentArea->areaDescription, 256);
strncat(lookMessage->messageContent, "\nYou can go:", 13);
for(int index = 0; index < 16; index++)
{
if(currentInput->sender->currentArea->areaExits[index] != NULL)
{
snprintf(formattedString, 64, "\n\t%d. %s", index + 1, currentInput->sender->currentArea->areaExits[index]->pathName);
strncat(lookMessage->messageContent, formattedString, 64);
}
}
queueTargetedOutputMessage(threadParameters->outputQueue, lookMessage, &currentInput->sender, 1);
free(lookMessage);
}
// Name command: Checks if the name is isn't used and is valid, then changes the player's name:
if(strncmp(&currentInput->content->messageContent[1], "NAME", 4) == 0 ||
strncmp(&currentInput->content->messageContent[1], "name", 4) == 0)
{
}
} }
else else
{ {

View File

@ -5,10 +5,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "constants.h"
#include "playerdata.h"
#include "inputoutput.h"
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include "../include/constants.h"
#include "../include/playerdata.h"
#include "../include/inputoutput.h"
// Sends a message to a given TLS session, wraps the calls to gnutls_write: // Sends a message to a given TLS session, wraps the calls to gnutls_write:
int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend) int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend)

View File

@ -1,7 +1,7 @@
// Implementation of lists library for SilverMUD. // Implementation of lists library for SilverMUD.
// Barry Kane, 2021 // Barry Kane, 2021
#include "lists.h" #include "../include/lists.h"
#include "playerdata.h" #include "../include/playerdata.h"
areaNode * createAreaList(playerArea * initialArea) areaNode * createAreaList(playerArea * initialArea)
{ {

View File

@ -3,11 +3,27 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "playerdata.h" #include "../include/playerdata.h"
// Move a player to a different area given a path in the area: // Move a player to a different area given a path in the area:
int movePlayerToArea(playerInfo * player, char * requestedPath) int movePlayerToArea(playerInfo * player, char * requestedPath)
{ {
// Check if a number was given first:
int selected = atoi(requestedPath);
if(selected != 0)
{
if(player->currentArea->areaExits[selected -1]->areaToJoin != NULL)
{
player->currentArea = player->currentArea->areaExits[selected - 1]->areaToJoin;
return 0;
}
else
{
return 1;
}
}
// Otherwise search for the description:
for (int index = 0; index < 16; index++) for (int index = 0; index < 16; index++)
{ {
if(player->currentArea->areaExits[index] != NULL) if(player->currentArea->areaExits[index] != NULL)

View File

@ -14,12 +14,12 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include "misc/lists.h" #include "../../include/lists.h"
#include "misc/gamelogic.h" #include "../../include/gamelogic.h"
#include "misc/constants.h" #include "../../include/constants.h"
#include "misc/playerdata.h" #include "../../include/playerdata.h"
#include "misc/texteffects.h" #include "../../include/texteffects.h"
#include "misc/inputoutput.h" #include "../../include/inputoutput.h"
typedef struct sockaddr sockaddr; typedef struct sockaddr sockaddr;