-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
162 lines (127 loc) · 5.69 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
var requirejs = require('requirejs');
// RequireJS config
requirejs.config({
nodeRequire: require,
paths: {
// Load Texty and connection modules
Texty: 'texty/texty',
TCPConnection: 'texty/lib/connections/tcp',
OrchestrateStore: 'texty/lib/auth/redis',
// Load the object action files (And the world and templates, but need to figure out a way to make it work)
CampfireActions: 'stroll/campfire',
StickActions: 'stroll/stick'
}
});
requirejs(['Texty', 'TCPConnection', 'OrchestrateStore', 'fs', 'CampfireActions', 'StickActions'],
function (Texty, TCPConnection, OrchestrateStore, fs, campfireActions, stickActions) {
// Load the game module and template (This should come in via RequireJS)
var world = JSON.parse(fs.readFileSync(__dirname + '/stroll/world.json')),
template = JSON.parse(fs.readFileSync(__dirname + '/texty/templates/ansiTelnet.json'));
// Initialize the Texty module with the game
var game = new Texty({
world: world,
actions: {
'campfire': campfireActions,
'stick': stickActions
}
});
// Initialize the TCP connection
var tcp = new TCPConnection(game, {
port: process.env.PORT || 10070
});
// Initialize the connection to Orchestrate
var db = new OrchestrateStore({
token: 'f57a7ecb-b0a6-4ae6-913f-84b9b1c25905'
});
// Add listeners
tcp.on('connect', function (session, callback) {
callback('Please enter your username:\r\n');
return;
});
// Session store
var sessionsByUser = {},
sessionsById = {};
tcp.on('command', function (session, command, callback) {
session.auth = session.auth || {};
// Authentication logic
if (!session.auth.authenticated) {
if (!session.auth.username || session.auth.username == '') {
// Check if the entered username was blank
if (command != '') {
session.auth.username = command;
callback('\r\nPlease enter your password:\r\n');
} else {
callback('Invalid username. Please enter your username:\r\n');
}
} else if (!session.auth.authenticated) {
// Authenticate against the db (I should really check for success/failure)
db.auth(session.auth.username, command, function (err, res) {
if (!err && res) {
session.auth.authenticated = true;
session.auth.userId = res;
// Now that we're authenticated, attempt to load the game state from Redis
db.load(session.auth.userId, function (err, res) {
if (!err) {
// Convert the loaded user state object into a compatible game state
session.gameState = game.initializeState(session.auth.userId, res, template);
callback(game.displayWelcome(session.gameState));
} else {
// Create a new user game state object
session.gameState = game.createNewState(session.auth.userId, template);
callback(game.displayWelcome(session.gameState));
}
});
} else {
// Username and password not found
delete session.auth.username;
callback('\r\nAuthentication failed. Please enter your username:\r\n');
}
});
}
} else {
if (!session.auth.started) {
// Begin the game and show the players last state
session.auth.started = true;
game.start(session.gameState, function (data) {
sessionsByUser[session.auth.username] = session;
sessionsById[session.sessionId] = session;
callback(data);
});
return;
}
// From now on, defer all commands to the command parser so that the game can take over
// NOTE: Maybe unless the command starts with "texty" or something? Might need a bit of rearchitecting
// NOTE 2: Actually, the parseCommand method shouldn't be responsible for the prefixing. We should check it outside, and only send it to Texty if it's supposed to go there.
game.parseCommand(session.gameState, command, function (data) {
callback(data);
return;
});
}
});
// Handle a user disconnect
tcp.on('disconnect', function (session) {
// If this isn't set, they were never properly in the game
if (sessionsById[session.sessionId]) {
game.quit(sessionsById[session.sessionId].auth.username);
}
});
// Handle game triggered events where a user needs notified
game.on('gameEvent', function (gameState, data) {
if (sessionsByUser[gameState.player]) {
tcp.sendData(sessionsByUser[gameState.player].sessionId, data);
return true;
} else {
return false;
}
});
// Handle a user quitting the game
game.on('quit', function (gameState) {
// Force tcp disconnect
if (gameState) {
tcp.endConnection(sessionsByUser[gameState.player].sessionId);
delete sessionsById[sessionsByUser[gameState.player].sessionId];
delete sessionsByUser[gameState.player];
}
});
});