Galactic Bloodshed
enroll.cc
Go to the documentation of this file.
1 // Copyright 2014 The Galactic Bloodshed Authors. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the COPYING file.
4 
5 // enroll - racegen interface for Galactic Bloodshed race enrollment program.
6 
7 #include "gb/enroll.h"
8 
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12 
13 #include "gb/game_info.h"
14 #include "gb/racegen.h"
15 
16 #define DEFAULT_ENROLLMENT_FILENAME "enroll.saves"
17 #define DEFAULT_ENROLLMENT_FAILURE_FILENAME "failures.saves"
18 
19 extern int enroll_valid_race();
20 
21 static int enroll_player_race(char *failure_filename);
22 
23 /*
24  * Returns: 0 if the race was successfully enrolled, or 1 if not.
25  */
26 static int enroll_player_race(char *failure_filename) {
27  char c[128];
28  FILE *g;
29  int n;
30  static int recursing = 0;
31  static int successful_enroll_in_fix_mode = 0;
32 
33  while ((n = critique_to_file(nullptr, 1, 1))) {
34  printf("Race (%s) unacceptable, for the following reason%c:\n",
35  race_info.name, (n > 1) ? 's' : '\0');
36  critique_to_file(stdout, 1, 1);
37  if (recursing) {
38  printf("\"Quit\" to break out of fix mode.\n");
39  return 1;
40  }
41  if (race_info.status == STATUS_ENROLLED) return 0;
42  n = Dialogue("Abort, enroll anyway, fix, mail rejection?", "abort",
43  "enroll", "fix", "mail", 0);
44  if (n == 1) /* enroll anyway */
45  break;
46  if (n == 2) { /* fix */
47  printf("Recursive racegen. \"Enroll\" or \"Quit\" to exit.\n");
48  recursing = 1;
50  please_quit = recursing = 0;
51  if (successful_enroll_in_fix_mode) {
52  successful_enroll_in_fix_mode = 0;
53  return 0;
54  }
55  continue;
56  }
57  if (failure_filename != nullptr) {
58  if (nullptr == fopen(failure_filename, "w+")) {
59  printf("Warning: unable to open failures file \"%s\".\n",
60  failure_filename);
61  printf("Race not saved to failures file.\n");
62  } else {
63  // print_to_file(f, 0) ; // TODO(jeffbailey): What was this supposed to
64  // do?
65  printf("Race appended to failures file \"%s\".\n", failure_filename);
66  // fclose(f) ;
67  }
68  }
69  if (n == 0) /* Abort */
70  return 1;
71 
72  g = fopen(TMP, "w");
73  if (g == nullptr) {
74  printf("Unable to open file \"%s\".\n", TMP);
75  return 1;
76  }
77  fprintf(g, "To: %s\n", race_info.address);
78  fprintf(g, "Subject: %s Race Rejection\n", GAME);
79  fprintf(g, "\n");
80  fprintf(g,
81  "The race you submitted (%s) was not accepted, for the "
82  "following reason%c:\n",
83  race_info.name, (n > 1) ? 's' : '\0');
85  fprintf(g, "\n");
86  fprintf(g, "Please re-submit a race if you want to play in %s.\n", GAME);
87  fprintf(g, "(Check to make sure you are using racegen %s)\n", VERSION);
88  fprintf(g, "\n");
89  fprintf(g, "For verification, here is my understanding of your race:\n");
91  fclose(g);
92 
93  printf("Sending critique to %s via %s...", race_info.address, MAILER);
94  fflush(stdout);
95  sprintf(c, "cat %s | %s %s", TMP, MAILER, race_info.address);
96  system(c);
97  printf("done.\n");
98 
99  return 1;
100  }
101 
102  if (enroll_valid_race()) return enroll_player_race(failure_filename);
103 
104  if (recursing) {
105  successful_enroll_in_fix_mode = 1;
106  please_quit = 1;
107  }
108 
109  g = fopen(TMP, "w");
110  if (g == nullptr) {
111  printf("Unable to open file \"%s\".\n", TMP);
112  return 0;
113  }
114  fprintf(g, "To: %s\n", race_info.address);
115  fprintf(g, "Subject: %s Race Accepted\n", GAME);
116  fprintf(g, "\n");
117  fprintf(g, "The race you submitted (%s) was accepted.\n", race_info.name);
118 #if 0
119  if (race.modified_by_diety) {
120  fprintf(g, "The race was altered in order to be acceptable.\n") ;
121  fprintf(g, "Your race now looks like this:\n") ;
122  fprintf(g, "\n") ;
123  print_to_file(g, verbose, 0) ;
124  fprintf(g, "\n") ;
125  }
126 #endif
127  fclose(g);
128 
129  printf("Sending acceptance to %s via %s...", race_info.address, MAILER);
130  fflush(stdout);
131  sprintf(c, "cat %s | %s %s", TMP, MAILER, race_info.address);
132  system(c);
133  printf("done.\n");
134 
135  return 0;
136 }
137 
138 int enroll(int argc, char *argv[]) {
139  int ret;
140  FILE *g;
141 
142  if (argc < 2) argv[1] = DEFAULT_ENROLLMENT_FAILURE_FILENAME;
143  g = fopen(argv[1], "w+");
144  if (g == nullptr) printf("Unable to open failures file \"%s\".\n", argv[1]);
145  fclose(g);
146  bcopy(&race_info, &last, sizeof(struct x));
147 
148  /*
149  * race.address will be unequal to TO in the instance that this is a
150  * race submission mailed from somebody other than the moderator. */
151  if (strcmp(race_info.address, TO))
152  ret = enroll_player_race(argv[1]);
153  else if ((ret = critique_to_file(nullptr, 1, 0))) {
154  printf("Race (%s) unacceptable, for the following reason%c:\n",
155  race_info.name, (ret > 1) ? 's' : '\0');
156  critique_to_file(stdout, 1, 0);
157  } else if ((ret = enroll_valid_race()))
158  critique_to_file(stdout, 1, 0);
159 
160  if (ret) printf("Enroll failed.\n");
161  return ret;
162 }
163 
164 /**************
165  * Iteratively loads races from a file, and enrolls them.
166  */
167 void process(int argc, char *argv[]) {
168  FILE *f;
169  FILE *g;
170  int n;
171  int nenrolled;
172 
173  if (argc < 2) argv[1] = DEFAULT_ENROLLMENT_FILENAME;
174  f = fopen(argv[1], "r");
175  if (f == nullptr) {
176  printf("Unable to open races file \"%s\".\n", argv[1]);
177  return;
178  }
179 
180  if (argc < 3) argv[2] = DEFAULT_ENROLLMENT_FAILURE_FILENAME;
181  g = fopen(argv[2], "w");
182  if (g == nullptr) printf("Unable to open failures file \"%s\".\n", argv[2]);
183  fclose(g);
184 
185  n = 0;
186  nenrolled = 0;
187  while (!feof(f)) {
188  if (!load_from_file(f)) continue;
189  n++;
190  printf("%s, from %s\n", race_info.name, race_info.address);
191  /* We need the side effects: */
194  if (!enroll_player_race(argv[2])) nenrolled += 1;
195  }
196  fclose(f);
197 
198  printf("Enrolled %d race%c; %d failure%c saved in file %s.\n", nenrolled,
199  (nenrolled != 1) ? 's' : '\0', n - nenrolled,
200  (n - nenrolled != 1) ? 's' : '\0', argv[2]);
201 }
#define MAILER
Definition: racegen.h:29
int critique_to_file(FILE *f, int rigorous_checking, int is_player_race)
Definition: racegen.cc:440
int please_quit
Definition: racegen.cc:325
#define STARTING_POINTS
Definition: game_info.h:33
static int enroll_player_race(char *failure_filename)
Definition: enroll.cc:26
int cost_of_race()
Definition: racegen.cc:332
int load_from_file(FILE *g)
Definition: racegen.cc:843
int enroll(int argc, char *argv[])
Definition: enroll.cc:138
char status
Definition: racegen.h:181
char address[64]
Definition: racegen.h:176
void process(int argc, char *argv[])
Definition: enroll.cc:167
void print_to_file(FILE *f, int verbose)
Definition: racegen.cc:1061
int last_npoints
Definition: racegen.cc:322
#define TMP
Definition: racegen.h:31
int Dialogue(const char *,...)
Definition: racegen.cc:1213
#define STATUS_ENROLLED
Definition: racegen.h:167
#define GAME
Definition: game_info.h:14
#define VERSION
Definition: racegen.h:14
int npoints
Definition: racegen.cc:321
void modify_print_loop(int level)
Definition: racegen.cc:1329
#define DEFAULT_ENROLLMENT_FAILURE_FILENAME
Definition: enroll.cc:17
char name[64]
Definition: racegen.h:178
#define DEFAULT_ENROLLMENT_FILENAME
Definition: enroll.cc:16
int enroll_valid_race()
Definition: GB_racegen.cc:48
#define TO
Definition: game_info.h:21