-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathusers.js
More file actions
92 lines (77 loc) · 2.17 KB
/
users.js
File metadata and controls
92 lines (77 loc) · 2.17 KB
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
import {v4 as uuidv4} from 'uuid';
import {colorPicker} from './scripts/ColorPicker.js'
// users is an associative array containing all users with their credential id being the key.
export const users = {};
const gameID = {
_current: 0,
next () { return uuidv4(); }
}
export function exportUsers(){
const allUsers = {};
for (const userKey in users) {
const id = users[userKey].gameID;
allUsers[id] = users[userKey];
}
return allUsers;
}
export function changeID(newID, oldID){
if(users.hasOwnProperty(oldID)){
Object.assign(users, {[newID]:users[oldID]})
delete users[oldID];
}
else console.log('User did not exist')
}
export function get(cid){
if (users.hasOwnProperty(cid))
return users[cid];
else return undefined;
}
export function positions(){
const result = {};
Object.keys(users)
.forEach(id => result[users[id].gameID] = users[id].position);
return result;
}
export function colors(){
const result = {};
Object.keys(users)
.forEach(id => result[users[id].gameID] = users[id].color);
return result;
}
// Takes a name and a color and attempts to create a new user. If the name or the color is invalid, it returns undefined.
export function create(name, color){
if (!(validateName(name) || !validateColor(color))) return undefined;
const cid = uuidv4();
const id = gameID.next();
users[cid] = {
gameID: id,
name: name,
color: colorPicker.getShade(color),
position: {
top: Math.random() * 300,
left: Math.random() * 300
},
rotation: 1
};
return cid;
}
export function remove(cid){
if (users.hasOwnProperty(cid)){
delete users[cid];
console.log('user ' + cid + ' removed');
}
else console.log('user ' + cid + ' did not exist!');
}
// If the name is available, return true, else return false.
function validateName(name){
for (const id of Object.keys(users)) {
const user = get(id);
if (user && user.name === name)
return false;
}
return true;
}
// Dummy function.
function validateColor(color){
return true;
}