1 /* player lists */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include "world.h"
8 #include "externs.h"
9 
10 extern int bpipe;
11 
12 static struct list *rpmlist = NULL;
13 
14 struct list *addlist(struct list **head, int dsize);
15 struct list *remlist(struct list **head, struct list *dlp);
16 
17 /* Send server information to other servers */
send_sinfo(struct client * ccl,struct client * priv)18 void send_sinfo(struct client *ccl, struct client *priv) {
19 	struct wpacket spk;
20 	spk.type = WP_SINFO;
21 	spk.serverid = 0;
22 	strncpy(spk.d.sinfo.name, slist[ccl->authed-1].name, 30);
23 	spk.d.sinfo.sid = ccl->authed;
24 	if (priv)
25 		reply(&spk, priv);
26 	else
27 		relay(&spk, ccl);
28 }
29 
30 /*
31  * This function is called when a new server starts
32  * and needs to be given the current state of the
33  * world.
34  */
send_rplay(struct client * ccl)35 void send_rplay(struct client *ccl) {
36 	struct list *clp, *rlp;
37 	struct rplist *c_pl;
38 	struct client *c_cl;
39 	struct wpacket spk;
40 
41 	/* Send online server info */
42 	for (clp = clist; clp; clp = clp->next) {
43 		c_cl = (struct client*)clp->data;
44 		if (c_cl == ccl) continue;
45 		if (c_cl->authed <= 0) continue;
46 		send_sinfo(c_cl, ccl);
47 	}
48 
49 	/* Send online player login packets */
50 	rlp = rpmlist;
51 	spk.type = WP_NPLAYER;
52 	spk.serverid = 0;
53 	spk.d.play.silent = 1;
54 	while (rlp) {
55 		c_pl = (struct rplist*)rlp->data;
56 		spk.d.play.id = c_pl->id;
57 		spk.d.play.server = c_pl->server;
58 		strncpy(spk.d.play.name, c_pl->name, 30);
59 		reply(&spk, ccl);
60 		/* Temporary stderr output */
61 		if (bpipe) {
62 			fprintf(stderr, "SIGPIPE from send_rplay (fd: %d)\n", ccl->fd);
63 			bpipe = 0;
64 		}
65 		rlp = rlp->next;
66 	}
67 }
68 
69 /* Remove all players from a server */
rem_players(int16_t id)70 void rem_players(int16_t id) {
71 	struct rplist *c_pl;
72 	struct list *lp;
73 
74 	lp = rpmlist;
75 	while (lp) {
76 		c_pl = (struct rplist*)lp->data;
77 		if (c_pl->server == id)
78 			lp = remlist(&rpmlist, lp);
79 		else lp = lp->next;
80 	}
81 }
82 
add_rplayer(struct wpacket * wpk)83 void add_rplayer(struct wpacket *wpk) {
84 	struct list *lp;
85 	struct rplist *n_pl, *c_pl;
86 	unsigned char found = 0;
87 
88 	if (wpk->type == WP_NPLAYER && !wpk->d.play.server) return;
89 	lp = rpmlist;
90 	while (lp) {
91 		c_pl = (struct rplist*)lp->data;
92 //		if (/* c_pl->id == wpk->d.play.id && */ !(strcmp(c_pl->name, wpk->d.play.name))) {
93 		if (c_pl->server == wpk->d.play.server && !(strcmp(c_pl->name, wpk->d.play.name))) {
94 			found = 1;
95 			break;
96 		}
97 		lp = lp->next;
98 	}
99 
100 	if (wpk->type == WP_NPLAYER && !found) {
101 		lp = addlist(&rpmlist, sizeof(struct rplist));
102 		if (lp) {
103 			n_pl = (struct rplist*)lp->data;
104 			n_pl->id = wpk->d.play.id;
105 			n_pl->server = wpk->d.play.server;
106 			strncpy(n_pl->name, wpk->d.play.name, 30);
107 		}
108 	}
109 	else if(wpk->type == WP_QPLAYER && found)
110 		remlist(&rpmlist, lp);
111 }
112