-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-connection.js
118 lines (84 loc) · 2.92 KB
/
user-connection.js
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
/// Requirements: --------------------------------------------------------------
var connection = require( "./connection" );
var request = require( "request" );
/// Constants: -----------------------------------------------------------------
var AUTH_URL = "https://craft.michaelfogleman.com/api/1/identity";
/// Exports: -------------------------------------------------------------------
module.exports = {
open: openConnection,
wrap: wrapConnection,
getAuthToken: getAuthToken,
};
/// Functions: -----------------------------------------------------------------
function openConnection( host, port, username, identityToken ){
var conn = connection.open( host, port );
return wrapConnection( conn, username, identityToken );
}///
function wrapConnection( conn, username, identityToken ){
var userInfo = {
pid: 0,
name: "",
x: 0,
y: 0,
z: 0,
rx: 0,
ry: 0
};
conn.on( "connect", identify );
conn.on( "U", onUserInfo );
conn.on( "P", onPlayerPos );
conn.on( "N", onPlayerNick );
conn.on( "T", onText );
var api = Object.create( conn );
api.info = userInfo;
api.identify = identify;
return api;
function identify(){
getAuthToken( username, identityToken, onAuthToken );
}///
function onAuthToken( err, authToken ){
err || conn.send( "A", username, authToken );
}///
function onUserInfo( pid, x, y, z, rx, ry ){
userInfo.pid = pid;
conn.emit( "user/pid", username, pid );
updatePos.apply( this, arguments );
}///
function updatePos( pid, x, y, z, rx, ry ){
userInfo.x = x;
userInfo.y = y;
userInfo.z = z;
userInfo.rx = rx;
userInfo.ry = ry;
conn.emit( "user/position", userInfo.name, x, y, z, rx, ry );
}///
function onPlayerPos( pid, x, y, z, rx, ry ){
if ( pid === userInfo.pid ){
updatePos.apply( this, arguments );
}
}///
function onPlayerNick( pid, name ){
if ( pid === userInfo.pid ){
userInfo.name = name;
conn.emit( "user/name", username, name, pid );
}
}///
function onText( text ){
if ( userInfo.name ){
var privMsg = text.match( new RegExp( "^(\\S+)> @" + userInfo.name + "\\s+(.*)$" ));
if ( privMsg ){
conn.emit( "user/private-message", userInfo.name, privMsg[1], privMsg[2] );
}
}
}///
}///
function getAuthToken( username, identityToken, cb ){
request.post( AUTH_URL, { form: { username:username, identity_token:identityToken }}, onPost );
function onPost( err, response, body ){
if ( err || !body || response.statusCode !== 200 ){
cb( err, response );
} else {
cb( err, body );
}
}///
}///