-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermagachi.c
294 lines (248 loc) · 6.34 KB
/
termagachi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#define _GNU_SOURCE /* for asprintf */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define TERMAGACHI_LOCAL_FILE ".dontmindme"
typedef enum
{
REGULAR = 0,
HAPPY,
HUNGRY,
SLEEPY,
PLAY,
LOSE,
WIN
} _MOOD;
void print_termagachi(_MOOD mood)
{
char *msg = NULL;
printf("\t __\n");
printf("\t (");
switch(mood) {
case HAPPY:
printf("^^");
msg = "Woo! Loving life!";
break;
case HUNGRY:
printf("**");
msg = "So...hungry...feed me more git commits....NOMNOMNOM";
break;
case SLEEPY:
printf("><");
msg = "Sshhhh! Nap time!";
break;
case PLAY:
printf("^^");
msg = "Yay a game! You're going DOWN.";
break;
case WIN:
printf("^^");
msg = "YAY! Welp, better luck next time, huh? Way to lose, suckaaahh.";
break;
case LOSE:
printf("~~");
msg = "Oh awesome..you win! Back to coding then for you...";
break;
default:
printf("--");
msg = "Sup, homie?";
break;
}
printf(")\n");
printf("\t \\||/\n");
printf("\t /\\\n");
printf("\n");
printf("___%s___\n", msg);
printf("\n");
}
void print_help()
{
printf("\n");
printf("Welcome to Termagachi! Your very own terminal companion.\nTo feed her, she needs looottss of git commits. A Termagachi without food gets angry. She also likes to play and say hi.\n");
printf("\n");
printf("Usage options: termagachi [command]\n");
printf("Commands:\n");
printf("\tstatus\t->See how your Termagachi is doing\n");
printf("\tsayhi\t->Say hi to your Termagachi!\n");
printf("\tplay\t->Play with Termagachi!\n");
printf("\n");
}
void write_to_file(char *hist_path, int num_commits)
{
FILE *fp = NULL;
if( (fp = fopen(TERMAGACHI_LOCAL_FILE, "w")) == NULL)
{
printf("ERROR: %d: fp is null\n", __LINE__);
exit(1);
}
// Save path to history file
fprintf(fp, "hist_path=%s\n", hist_path);
// Save initial number of commits
num_commits = check_commits(hist_path);
fprintf(fp, "commits_total=%d\n", num_commits);
fclose(fp);
}
void setup()
{
int num_commits;
char input_path[80];
printf("Need path to your history file: ");
if( EOF != scanf("%s", input_path))
write_to_file(input_path, num_commits);
}
char *get_path_to_hist()
{
int i;
char line[1024];
char *hist_path = NULL;
FILE *fp = NULL;
if((fp = fopen(TERMAGACHI_LOCAL_FILE, "r")) == NULL )
{
printf("ERROR: %d: fp is null\n", __LINE__);
exit(1);
}
while( fgets(line, sizeof(line), fp) != NULL )
{
if(strstr(line, "hist_path") != NULL )
{
asprintf(&hist_path, "%s", &line[10]);
for(i = 0; hist_path != NULL; i++)
{
if(hist_path[i] == '\n')
{
hist_path[i] = '\0';
break;
}
}
}
}
return hist_path;
}
int get_saved_num_commits()
{
int i, num_commits;
char line[80];
char *commits_str = NULL;
FILE *fp = NULL;
if((fp = fopen(TERMAGACHI_LOCAL_FILE, "r")) == NULL )
{
printf("ERROR: %d: fp is null\n", __LINE__);
exit(1);
}
while( fgets(line, sizeof(line), fp) != NULL )
{
if(strstr(line, "commits_total") != NULL )
{
asprintf(&commits_str, "%s", &line[14]);
for(i = 0; commits_str != NULL; i++)
{
if(commits_str[i] == '\n')
{
commits_str[i] = '\0';
break;
}
}
num_commits = atoi(commits_str);
}
}
return num_commits;
}
int check_commits(const char *hist_path)
{
int count;
char buf[4096];
FILE *fp = NULL;
if((fp = fopen(hist_path, "r")) == NULL )
{
printf("ERROR: %d: fp is null\n", __LINE__);
exit(1);
}
while( fgets(buf, sizeof(buf), fp) != NULL )
{
if(strstr(buf, "commit") != NULL )
count++;
}
fclose(fp);
return count;
}
_MOOD play_game()
{
char input[4];
int correct_num, guess;
int guessed_it = 0, num_tries = 0;
_MOOD mood = LOSE;
correct_num = (rand() % 100) + 1;
printf("\nLet's play \"Guess my number.\" You have 10 tries to guess the number I'm thinking of. Give me a guess and press enter!\n");
for(; num_tries < 10; num_tries++)
{
printf("Guess #%d: ", num_tries + 1);
if( fgets(input, sizeof(input), stdin) != NULL )
{
guess = atoi(input);
if(guess < correct_num)
{
printf("Nope! Higher!\n");
}
else if(guess > correct_num)
{
printf("Wrong! Loweeerrrr...\n");
}
else
{
printf("Lucky guess, punk.\n");
guessed_it = 1;
break;
}
}
}
if(num_tries == 10 && guessed_it == 0)
mood = WIN;
else
mood = LOSE;
return mood;
}
int main(int argc, char **argv)
{
int num_commits, saved_num_commits;
char *hist_path = NULL;
FILE *fp = NULL;
// If not already set up, get set up.
if( (fp = fopen(TERMAGACHI_LOCAL_FILE, "r")) == NULL )
setup();
// Print the help menu
if(argc < 2)
{
print_help();
exit(1);
}
// Seed the randomizer
srand(time(NULL));
_MOOD mood = REGULAR;
if(strcmp("status", argv[1]) == 0)
{
hist_path = get_path_to_hist();
num_commits = check_commits(hist_path);
saved_num_commits = get_saved_num_commits();
if(num_commits <= saved_num_commits)
mood = HUNGRY;
else
mood = HAPPY;
write_to_file(hist_path, num_commits);
free(hist_path);
}
else if(strcmp("sayhi", argv[1]) == 0)
{
mood = REGULAR;
}
else if(strcmp("play", argv[1]) == 0)
{
mood = PLAY;
print_termagachi(mood);
mood = play_game();
}
print_termagachi(mood);
return 0;
}