changeset 26:ea7372f5d314

rogue4: add logging all games
author edwarj4
date Thu, 26 Nov 2009 03:02:13 +0000
parents 00e90f1bffd6
children c68598659da6
files rogue4/Makefile rogue4/extern.c rogue4/extern.h rogue4/mach_dep.c rogue4/main.c rogue4/rip.c
diffstat 6 files changed, 75 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/rogue4/Makefile	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/Makefile	Thu Nov 26 03:02:13 2009 +0000
@@ -26,7 +26,8 @@
 MISC=	Makefile LICENSE.TXT rogue.6 rogue.me
 
 CC    = gcc
-CFLAGS= -O3 -DSAVEDIR=\"/usr/local/games/roguelike/rogue4save/\"
+CFLAGS= -O3 -DSAVEDIR=\"/usr/local/games/roguelike/rogue4save/\" \
+        -DLOGFILE=\"/usr/local/games/roguelike/rogue4.log\"
 CRLIB = -lcurses
 RM    = rm -f
 TAR   = tar
--- a/rogue4/extern.c	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/extern.c	Thu Nov 26 03:02:13 2009 +0000
@@ -101,6 +101,7 @@
 int group = 2;				/* Current group number */
 int hungry_state = 0;			/* How hungry is he */
 int fd;					/* File descriptor for score file */
+int lfd;				/* File descriptor for log file */
 int a_chances[MAXARMORS] = {		/* Chance for each armor type */
     20,
     35,
--- a/rogue4/extern.h	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/extern.h	Thu Nov 26 03:02:13 2009 +0000
@@ -41,7 +41,7 @@
 
 extern int	a_chances[], a_class[], count, dnum, food_left,
 		fung_hit, fd, group, hungry_state, inpack, lastscore,
-		level, max_level, mpos, no_command, no_food, no_move,
+		level, lfd, max_level, mpos, no_command, no_food, no_move,
 		ntraps, purse, quiet, total;
 
 extern long	seed;
--- a/rogue4/mach_dep.c	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/mach_dep.c	Thu Nov 26 03:02:13 2009 +0000
@@ -82,6 +82,16 @@
     return;
 }
 
+void open_log(void)
+{
+#ifdef LOGFILE
+    lfd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666);
+#else
+    lfd = -1;
+#endif
+    return;
+}
+
 /*
  * setup:
  *	Get starting setup for all games
--- a/rogue4/main.c	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/main.c	Thu Nov 26 03:02:13 2009 +0000
@@ -108,6 +108,7 @@
     /*
      * check for print-score option
      */
+    open_log(); /* do first, open_score might drop needed permissions */
     open_score();
     if (argc == 2 && strcmp(argv[1], "-s") == 0)
     {
@@ -349,6 +350,7 @@
 	mvprintw(LINES - 2, 0, "You quit with %d gold pieces", purse);
 	move(LINES - 1, 0);
 	refresh();
+	writelog(purse, 1, 0);
 	score(purse, 1);
 	exit(0);
     }
--- a/rogue4/rip.c	Tue Nov 24 13:04:43 2009 +0000
+++ b/rogue4/rip.c	Thu Nov 26 03:02:13 2009 +0000
@@ -236,6 +236,63 @@
     fclose(outf);
 }
 
+void writelog(int amount, int flags, char monst)
+{
+    FILE *logfi;
+    char logmessage[160], ltemp[80];
+    char *killer;
+    if (waswizard)
+        return;
+#ifdef LOGFILE
+    if (lfd >= 0)
+        logfi = fdopen(lfd, "a");
+    else
+        return;
+    if (logfi == NULL)
+        return;
+    /* otherwise writing should work */
+    sprintf(logmessage, "%d %d %.20s %d ", time(NULL), amount, whoami, 
+            pstats.s_lvl);
+    if (flags == 0) /* died */
+    {
+        strcat(logmessage, "killed by ");
+        killer = killname(monst, TRUE);
+        strcat(logmessage, killer);
+        if (amulet)
+            sprintf(ltemp, " on level %d [max %d] with the Amulet\n",
+                    level, max_level);
+        else
+            sprintf(ltemp, " on level %d\n", level);
+        strcat(logmessage, ltemp);
+    }
+    else if (flags == 1) /* quit */
+    {
+        if (amulet)
+            sprintf(ltemp, "quit on level %d [max %d] with the Amulet\n",
+                    level, max_level);
+        else
+            sprintf(ltemp, "quit on level %d\n", level);
+        strcat(logmessage, ltemp);
+    }
+    else if (flags == 2) /* won */
+    {
+        sprintf(ltemp, "escaped with the Amulet [deepest level :%d]\n",
+                max_level);
+        strcat(logmessage, ltemp);
+    }
+    else
+    {
+        fclose(logfi);
+        return;
+    }
+    
+    /* then write */
+    fprintf(logfi, "%s", logmessage);
+    fclose(logfi);    
+#endif
+    return;
+}
+
 /*
  * death:
  *	Do something really fun when he dies
@@ -271,6 +328,7 @@
     mvaddstr(18, 26, prbuf);
     move(LINES-1, 0);
     refresh();
+    writelog(purse, 0, monst);
     score(purse, 0, monst);
     /* Make sure the output gets through */
     printf("[Press return to exit]\n");
@@ -390,6 +448,7 @@
     }
     mvprintw(c - 'a' + 1, 0,"   %5d  Gold Pieces          ", oldpurse);
     refresh();
+    writelog(purse, 2, 0);
     score(purse, 2, 0);
     exit(0);
 }