-
Notifications
You must be signed in to change notification settings - Fork 1
/
userManager.js
254 lines (227 loc) · 6.24 KB
/
userManager.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const {
allocateUser,
syncUser,
encodeKey,
rewardGameToken,
rewardPoints,
setUserOwnership,
} = require('./nearConnector');
const db = require('./dataManager');
const log = require('./log');
const users = {};
const userIDs = {};
const userGodotPeerIDs = {};
const USER_STATE = {
CONNECTING: 0,
CONNECTED: 1,
INLINE: 2,
BUSY: 3,
WAITING_FOR_CONNECTION: 4,
READY_TO_PLAY: 5,
PLAYING: 6,
OUTLINE: 7,
DISCONNECTED: 8,
};
const init = () => {
log.info(`[UM] User Manager - initialization`);
}
const _createUser = (_sessionID) => {
let sessionID = _sessionID; // temporal
let userID = _sessionID; // permanent
let godotPeerID; // temporal
let nearAccount; // permanent
let state = USER_STATE.CONNECTING;
let encodedKey; // permanent
let turn=0;
let secretKey; // temporal
let adn;
let bugName;
let deathCause;
let introWords;
let lastWords;
let score = 0;
let achievements = [];
const ackConnection = (key) => {
if (state === USER_STATE.CONNECTING && key === sessionID){
state = USER_STATE.CONNECTED;
return true;
}
return false;
}
const allocateOnBlockchain = async (secretWord) => {
if (userID !== sessionID) return;
if (state === USER_STATE.BUSY) return;
const previousState = state;
state = USER_STATE.BUSY;
const reply = await allocateUser(sessionID, secretWord);
delete userIDs[userID];
userID = reply.userID;
userIDs[userID] = sessionID;
encodedKey = reply.encodedKey;
log.info(`[UM] userID allocated to ${ userID }`);
log.info(`[UM] protected by ${ encodedKey }`);
await db.saveUser(userID, encodedKey);
state = previousState;
return userID;
};
const recoverSession = async (_userID, password) => {
const _encodedKey = encodeKey(password);
const _user = await db.getUser(_userID, _encodedKey);
if (!_user) {
return false;
}
delete userIDs[userID];
userID = _user.id;
encodedKey = _user.password;
nearAccount = _user.address;
adn = _user.char.adn;
bugName = _user.char.name;
userIDs[userID] = sessionID;
await syncUser(_self);
return true;
};
const assignTurn = async (_turn) => {
turn = _turn;
state = USER_STATE.INLINE;
};
const setGodotPeer = (_godotPeerID) => {
godotPeerID = _godotPeerID;
if (godotPeerID) {
userGodotPeerIDs[godotPeerID] = sessionID;
} else {
delete userGodotPeerIDs[godotPeerID];
}
};
const setAdn = (_adn) => {
// must validate this user can have this adn
adn = _adn;
};
const scorePoints = async (points) => {
await rewardPoints(userID, points);
score += points;
};
const awardGameToken = async (rewardId, ipnft) => {
await rewardGameToken(userID, ipnft);
achievements.push(rewardId);
};
const gameOver = async (_deathCause) => {
state = USER_STATE.OUTLINE;
deathCause = _deathCause
};
const setBug = async (_adn, _name) => {
adn = _adn;
bugName = _name;
await db.saveCharacter(userID, adn, bugName);
};
const setNearAccount = async (accountId, secret) => {
try {
await setUserOwnership(userID, accountId, secret);
nearAccount = accountId;
await db.updateUser(userID, accountId);
return true;
} catch (e) {
console.error(e);
return false;
}
};
const _self = {
ackConnection,
allocateOnBlockchain,
recoverSession,
assignTurn,
setGodotPeer,
gameOver,
getSessionID: _ => sessionID,
getUserID: _ => userID,
getGodotPeerID: _ => godotPeerID,
getNearAccount: _ => nearAccount,
getState: _ => state,
getTurn: _ => turn,
getAdn: _ => adn,
getBugName: _ => bugName,
getDeathCause: _ => deathCause,
getIntroWords: _ => introWords,
getLastWords: _ => lastWords,
getScore: _ => score,
getAchievements: _ => achievements,
getSecretKey: _ => secretKey,
setState: _state => state = _state,
setBug,
setIntroWords: _introWords => introWords = _introWords,
setLastWords: _lastWords => lastWords = _lastWords,
setSecretKey: _secretKey => secretKey = _secretKey,
setTurn: _turn => turn = _turn,
setNearAccount,
scorePoints,
awardGameToken,
asObject: _ => {
return {
sessionID,
userID,
godotPeerID,
nearAccount,
state,
turn,
adn,
bugName,
deathCause,
introWords,
lastWords,
score,
achievements
}
},
};
return _self;
}
const deleteUser = (sessionID) => {
const user = users[sessionID];
const userID = user.getUserID();
const godotPeerID = user.getGodotPeerID();
delete users[sessionID];
delete userIDs[userID];
delete userGodotPeerIDs[godotPeerID];
};
const getUser = (sessionID) => users[sessionID];
const getUserByUserID = (userID) => getUser(userIDs[userID]);
const getUserByGodotPeerID = (godotPeerID) => getUser(userGodotPeerIDs[godotPeerID]);
const registerUser = (sessionID) => {
if (users[sessionID]) return; // already registered
const user = _createUser(sessionID);
users[sessionID] = user;
userIDs[user.getUserID()] = sessionID;
return user;
};
const isPlayerConnected = (sessionID) => {
const user = users[sessionID];
return user && user.getState() !== USER_STATE.CONNECTING;
}
// User
// near-account
// userID
// peerID
// status = outline, playing, inline
// active player -> user in play mode
// save a session user
// only two-three players required to be connected,
// allow calls only to be accepted by the specified ip, this case 127.0.0.1 or localhost
// how should work the communication between godot-server and node-server?
// add to line doesnt require game to be connected
// only active player can receive rewards -> token and points
// allocate a user is the first step in player coordination/synchronization?
// optional - users can link their near account
// it require to check if there is an existing userId for this user
// if yes,
// join to the line
// godot client flow require user allocation to connect to the server
// near account require user allocation to connect it
module.exports = {
init,
getUser,
getUserByUserID,
getUserByGodotPeerID,
deleteUser,
registerUser,
isPlayerConnected,
USER_STATE
};