aboutsummaryrefslogtreecommitdiffstats
path: root/src/Client.hx
blob: c7bc2568523106099b78ad6ec0190a04093783f5 (plain) (blame)
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
package;

#if nodejs
import js.node.http.IncomingMessage;
import js.npm.ws.WebSocket;
#end
import haxe.EnumFlags;

enum ClientGroup {
	User;
	Leader;
	Admin;
}

typedef ClientData = {
	name:String,
	group:Int
}

class Client {

	#if nodejs
	public final ws:WebSocket;
	public final id:Int;
	public final req:IncomingMessage;
	public var isAlive = true;
	#end
	public var name:String;
	public var group:EnumFlags<ClientGroup>;
	public var isUser(get, set):Bool;
	public var isLeader(get, set):Bool;
	public var isAdmin(get, set):Bool;

	#if nodejs
	public function new(?ws:WebSocket, ?req:IncomingMessage, ?id:Int, name:String, group:Int) {
	#else
	public function new(name:String, group:Int) {
	#end
		#if nodejs
		this.ws = ws;
		this.req = req;
		this.id = id;
		#end
		this.name = name;
		this.group = new EnumFlags(group);
	}

	inline function get_isUser():Bool {
		return group.has(User);
	}

	inline function set_isUser(flag:Bool):Bool {
		return setGroupFlag(User, flag);
	}

	inline function get_isLeader():Bool {
		return group.has(Leader);
	}

	inline function set_isLeader(flag:Bool):Bool {
		return setGroupFlag(Leader, flag);
	}

	inline function get_isAdmin():Bool {
		return group.has(Admin);
	}

	inline function set_isAdmin(flag:Bool):Bool {
		return setGroupFlag(Admin, flag);
	}

	function setGroupFlag(type:ClientGroup, flag:Bool):Bool {
		if (flag) group.set(type);
		else group.unset(type);
		return flag;
	}

	public function getData():ClientData {
		return {
			name: name,
			group: group.toInt()
		}
	}

	public static function fromData(data:ClientData):Client {
		return new Client(data.name, data.group);
	}

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage