aboutsummaryrefslogtreecommitdiff
path: root/player.c
blob: b7e9a3ee8165ea6ba7a3ac359f328820af051625 (plain)
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
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <mp.h>
#include <libsec.h>
#include <thread.h>
#include <draw.h>
#include <geometry.h>
#include "dat.h"
#include "fns.h"

Player *
newplayer(char *name, NetConn *nc)
{
	Player *p;

	p = emalloc(sizeof(Player));
	p->name = name? strdup(name): nil;
	p->conn = nc;
	p->inputq = chancreate(sizeof(ulong), 20);
	p->oldkdown = p->kdown = 0;
	p->next = nil;

	return p;
}

void
delplayer(Player *p)
{
	free(p->name);
	chanclose(p->inputq);
	free(p);
}

/* Player queue */

static void
playerq_put(Playerq *pq, Player *p)
{
	if(pq->tail == nil)
		pq->head = pq->tail = p;
	else{
		pq->tail->next = p;
		pq->tail = p;
	}
	pq->len++;
}

static Player *
playerq_get(Playerq *pq)
{
	Player *p;

	if(pq->head == nil)
		return nil;

	p = pq->head;
	if(pq->head == pq->tail)
		pq->head = pq->tail = nil;
	else{
		pq->head = p->next;
		p->next = nil;
	}
	pq->len--;
	return p;
}

static void
playerq_del(Playerq *pq, Player *p)
{
	Player *np;

	if(pq->head == p){
		pq->get(pq);
		return;
	}

	for(np = pq->head; np != nil && np->next != nil; np = np->next)
		if(np->next == p){
			np->next = np->next->next;
			p->next = nil;
			pq->len--;
		}
}

void
initplayerq(Playerq *pq)
{
	pq->head = pq->tail = nil;
	pq->len = 0;
	pq->put = playerq_put;
	pq->get = playerq_get;
	pq->del = playerq_del;
}