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
|
#include <string.h>
#define _GNU_SOURCE
#include "data.h"
#include "login.h"
#include "server.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
void handle_player(int conn_fd) {
printf("Let's make a new player object\n");
playerc_t plrc = {0};
plrc.state = LoggedOut;
plrc.conn = conn_fd;
player_t plr = {0};
plrc.plr = plr;
printf("No sense in having a player if they can't log in!\n");
int err = step_login(&plrc, conn_fd);
if (err) {
err = send_to_fd(conn_fd, "Error in login process, exiting");
if (err) {
printf("Could not send error message");
}
close(conn_fd);
return;
}
close(conn_fd);
}
int step_login(playerc_t *player, int conn_fd) {
int err = 0;
for (;;) {
switch (player->state) {
case LoggedOut: /* the player has yet to log in, this is a fresh connection!
*/
printf("The player is logged out\n");
send_to_fd(player->conn,
"Welcome! Please enter your **username** below\n");
char *buf = (char *)malloc(1 << 10);
printf("Waiting to receive...\n");
recv(player->conn, buf, sizeof(buf), 0);
printf("Received!\n");
err = try_load_plr(buf, player);
printf("Finished trying to load the player\n");
if (err) {
// File doesn't exist, let's create a player!
strcpy(player->plr.name, buf);
send_to_fd(player->conn, "Howdy! Want to introduce yourself? [y|n]\n");
recv(player->conn, buf, sizeof(buf), 0);
if (buf[0] == 'y') {
player->state = WantMakeAccount;
} else if (buf[0] == 'n') {
send_to_fd(player->conn,
"Awww :(. Oh well, hope to see you again soon!\n");
return 4;
}
continue;
} else {
player->state = EnterPassword;
continue;
}
case WantMakeAccount:
asprintf(&buf,
"Welcome aboard %s! Why don't you start us off by giving me a "
"password.\n",
*player->plr.name);
send_to_fd(player->conn, buf);
}
}
return err;
}
|