Compare commits

...

2 Commits

3 changed files with 131 additions and 27 deletions

View File

@ -147,8 +147,6 @@ applicable, or by default is matched with the last set of metadata declared by
the time of the run directive the time of the run directive
Run Run
Category
Any%
Route Route
Magic Swordless Magic Swordless
Start Start

View File

@ -74,8 +74,22 @@ int main(int argc, char *argv[]) {
commandcode = 12; commandcode = 12;
} else if (!strcmp(argv[1], "save")) { } else if (!strcmp(argv[1], "save")) {
commandcode = 13; commandcode = 13;
} else if (!strcmp(argv[1], "count")) { } else if (!strcmp(argv[1], "runs")) {
commandcode = 14; commandcode = 14;
} else if (!strcmp(argv[1], "segments")) {
commandcode = 15;
} else if (!strcmp(argv[1], "start-split-stop")) {
commandcode = 16;
} else if (!strcmp(argv[1], "pause-resume")) {
commandcode = 17;
} else if (!strcmp(argv[1], "start-stop")) {
commandcode = 18;
} else if (!strcmp(argv[1], "start-split")) {
commandcode = 19;
} else if (!strcmp(argv[1], "split-stop")) {
commandcode = 20;
} else if (!strcmp(argv[1], "undo-redo")) {
commandcode = 21;
} else { } else {
perror("No valid command given"); perror("No valid command given");
exit(1); exit(1);
@ -93,7 +107,7 @@ int main(int argc, char *argv[]) {
//bzero(buffer,256); //bzero(buffer,256);
//read an int response //read an int response
if (commandcode < 11 || commandcode == 14) { if (commandcode < 11 || commandcode == 14 || commandcode == 15) {
int x = -1; int x = -1;
n = read(sockfd, &x, sizeof(int)); n = read(sockfd, &x, sizeof(int));

View File

@ -16,6 +16,7 @@ int pausedTime = 0;
bool timerActive = false; bool timerActive = false;
bool paused = false; bool paused = false;
bool alive = true; bool alive = true;
bool hasUndoneAtLeastOnce = false;
bool runUnsaved = false; bool runUnsaved = false;
int timerOffset = 0; int timerOffset = 0;
enum event_type { enum event_type {
@ -35,9 +36,12 @@ struct segment {
char *longname; char *longname;
char *description; char *description;
}; };
struct route {
char *name;
struct segment *segments;
int segment_count;
};
char* current_category = NULL;
char* current_route = NULL;
struct run_event *run; struct run_event *run;
//Enough to hold a sm64 16 star, can realloc later //Enough to hold a sm64 16 star, can realloc later
int runMaxLength = 12; int runMaxLength = 12;
@ -53,6 +57,9 @@ char **names, **values;
int valuecount; int valuecount;
struct segment *segments; struct segment *segments;
int segment_count = 0; int segment_count = 0;
struct route *routes;
int route_count = 0;
struct route current_route;
//functions //functions
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td); void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td);
@ -60,16 +67,6 @@ void offset_timespec(int milliseconds, struct timespec* t);
int timespecToMS(struct timespec t); int timespecToMS(struct timespec t);
void extend_run(); void extend_run();
void add_event(enum event_type t); void add_event(enum event_type t);
void start();
void stop();
void split();
void skip();
void addPauseTime();
void subtractPauseTime();
void undo();
void redo();
void pause_timer();
void resume();
void appendRunToFile(); void appendRunToFile();
void timespecToRFC3339(struct timespec t, char buf[]); void timespecToRFC3339(struct timespec t, char buf[]);
void loadFiles(); void loadFiles();
@ -79,6 +76,26 @@ void sendTime(int sock);
void sendValue(int sock, char* name); void sendValue(int sock, char* name);
void sendInt(int sock, int value); void sendInt(int sock, int value);
void doprocessing (int sock); void doprocessing (int sock);
void addPauseTime();
void subtractPauseTime();
//basic timer commands
void start();
void split();
void stop();
void skip();
void undo();
void redo();
void pause_timer();
void resume();
//convenient combination commands
void start_split_stop();
void start_split();
void split_stop();
void start_stop();
void pause_resume();
void undo_redo();
void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td) void sub_timespec(struct timespec t1, struct timespec t2, struct timespec* td)
@ -133,19 +150,27 @@ void add_event(enum event_type t)
runMarker2 = runMarker; runMarker2 = runMarker;
} }
void reset_timer()
{
runMarker = 0;
runMarker2 = 0;
}
void start() void start()
{ {
//TODO: Save the old run to the file before the new one starts, if (timerActive) return;
//Save the old run to the file before the new one starts,
//the reason to do this here is it gives the runner a chance to undo //the reason to do this here is it gives the runner a chance to undo
//if they accidentally hit the stop button //if they accidentally hit the stop button
appendRunToFile(); appendRunToFile();
//TODO: Clear the run data first reset_timer();
timerActive = true; timerActive = true;
add_event(START); add_event(START);
} }
void stop() void stop()
{ {
if (!timerActive) return;
timerActive = false; timerActive = false;
add_event(STOP); add_event(STOP);
//this makes sure the time clients recieve from time //this makes sure the time clients recieve from time
@ -154,13 +179,46 @@ void stop()
runUnsaved = true; runUnsaved = true;
} }
void start_split_stop()
{
if (!timerActive) {
start();
} else {
if (runMarker < current_route.segment_count) {
split();
} else {
stop();
}
}
}
void start_split()
{
if (!timerActive) start();
else split();
}
void split_stop()
{
if (runMarker < current_route.segment_count) split();
else stop();
}
void start_stop()
{
if (!timerActive) start();
else stop();
}
void split() void split()
{ {
if (!timerActive) return;
add_event(SPLIT); add_event(SPLIT);
} }
void skip() void skip()
{ {
if (!timerActive) return;
add_event(SKIP); add_event(SKIP);
} }
@ -192,6 +250,7 @@ void subtractPauseTime()
void undo() void undo()
{ {
if (!timerActive) return;
if (runMarker > 0) { if (runMarker > 0) {
runMarker--; runMarker--;
if (run[runMarker].type == STOP) if (run[runMarker].type == STOP)
@ -204,11 +263,13 @@ void undo()
paused = true; paused = true;
subtractPauseTime(); subtractPauseTime();
} }
hasUndoneAtLeastOnce = true;
} }
} }
void redo() void redo()
{ {
if (!timerActive) return;
if (runMarker < runMarker2) { if (runMarker < runMarker2) {
runMarker++; runMarker++;
if (run[runMarker - 1].type == STOP) if (run[runMarker - 1].type == STOP)
@ -221,12 +282,21 @@ void redo()
paused = false; paused = false;
addPauseTime(); addPauseTime();
} }
} else {
hasUndoneAtLeastOnce = false;
} }
} }
void undo_redo()
{
if (hasUndoneAtLeastOnce) redo();
else undo();
}
//this isnt just called pause() because that would overlap with <unistd.h> //this isnt just called pause() because that would overlap with <unistd.h>
void pause_timer() void pause_timer()
{ {
if (!timerActive) return;
if (!paused) { if (!paused) {
add_event(PAUSE); add_event(PAUSE);
paused = true; paused = true;
@ -235,6 +305,7 @@ void pause_timer()
void resume() void resume()
{ {
if (!timerActive) return;
if (paused) { if (paused) {
add_event(RESUME); add_event(RESUME);
paused = false; paused = false;
@ -242,6 +313,12 @@ void resume()
} }
} }
void pause_resume()
{
if (paused) resume();
else pause_timer();
}
void appendRunToFile() void appendRunToFile()
{ {
if (!runUnsaved) if (!runUnsaved)
@ -255,13 +332,9 @@ void appendRunToFile()
fp = fopen(save_path, "a+"); fp = fopen(save_path, "a+");
fprintf(fp, "%s\n", "Run"); fprintf(fp, "%s\n", "Run");
if (current_category != NULL) { if (current_route.name != NULL) {
fprintf(fp, "\t%s\n", "Category");
fprintf(fp, "\t\t%s\n", current_category);
}
if (current_route != NULL) {
fprintf(fp, "\t%s\n", "Route"); fprintf(fp, "\t%s\n", "Route");
fprintf(fp, "\t\t%s\n", current_route); fprintf(fp, "\t\t%s\n", current_route.name);
} }
int i = 0; int i = 0;
@ -320,13 +393,12 @@ void timespecToRFC3339(struct timespec t, char buf[])
void loadFiles() void loadFiles()
{ {
FILE* fp; FILE* fp;
//TODO: for now we're just looking for the metadata values
char buff[255]; char buff[255];
char buff2[255]; char buff2[255];
for (int i = 0; i < files; i++) { for (int i = 0; i < files; i++) {
printf("loading file: \"%s\"\n", filePaths[i]); printf("loading file: \"%s\"\n", filePaths[i]);
fp = fopen(filePaths[i], "r"); fp = fopen(filePaths[i], "r+");
while(1) { while(1) {
if (!fgets(buff, 255, fp)) if (!fgets(buff, 255, fp))
break; break;
@ -336,7 +408,6 @@ void loadFiles()
char *s = NULL; char *s = NULL;
char *l = NULL; char *l = NULL;
char *d = NULL; char *d = NULL;
//"\t\tStory 12\n\0"
for (int x = 0; x < 3; x++) { for (int x = 0; x < 3; x++) {
if (!fgets(buff2, 255, fp)) if (!fgets(buff2, 255, fp))
break; break;
@ -520,6 +591,27 @@ void doprocessing (int sock)
} else if (commandcode == 14) { } else if (commandcode == 14) {
printf("Recieved request for run count\n"); printf("Recieved request for run count\n");
sendInt(sock, run_count); sendInt(sock, run_count);
} else if (commandcode == 15) {
printf("Recieved request for segment count\n");
sendInt(sock, segment_count);
} else if (commandcode == 16) {
printf("Recieved start_split_stop command\n");
start_split_stop();
} else if (commandcode == 17) {
printf("Recieved pause_resume command\n");
pause_resume();
} else if (commandcode == 18) {
printf("Recieved start-stop command\n");
start_stop();
} else if (commandcode == 19) {
printf("Recieved start-split command\n");
start_split();
} else if (commandcode == 20) {
printf("Recieved split-stop command\n");
split_stop();
} else if (commandcode == 21) {
printf("Recieved undo-redo command\n");
undo_redo();
} else { } else {
printf("Recieved invalid command code, ignoring...\n"); printf("Recieved invalid command code, ignoring...\n");
} }