-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty.js
119 lines (98 loc) · 3.94 KB
/
party.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
module.exports = (http=>{
const io = require('socket.io')(http);
const Config = {
Salas : [],
Users : [],
UserSala : {},
Sala_position:{},
ProcurarSala: (sala_,Lista_De_salas)=>{
for(let i of Lista_De_salas) {
if(i.id == sala_){
return i;
}
}
return undefined;
}
}
io.on("connection",async(client)=>{
// ao entrar uma sala é criada
client.on("CriarSala",async(msg)=>{
let sala = {
id : Math.floor(Math.random()*999999),
Users : [client],
Models : [],
}
console.log("Nova sala",sala.id)
Config.Sala_position[sala.id] = Config.Salas.length
Config.Salas.push(sala)
Config.Users.push(client)
Config.UserSala[client.id] = sala.id
client.emit("sala_atual",{id:sala.id})
})
// retira o client da sala anterior e o coloca na nova sala
client.on("EntrarSala",async(sala_)=>{
if(Config.Salas[Config.Sala_position[sala_.id]]) {
//removendo o usuario da sala atual
let user_room = Config.UserSala[client.id]
let sala = Config.Salas[Config.Sala_position[user_room]]
if(sala) {
let newUsers = []
for(let user of sala.Users) {
if(user.id!=client.id)
newUsers.push(user)
}
//atualizando a sala
Config.Salas[Config.Sala_position[sala.id]].Users = newUsers
}
//adicionando o usuario a sala nova
Config.UserSala[client.id] = sala_.id
Config.Salas[Config.Sala_position[sala_.id]].Users.push(client)
client.emit("Atualizar",Config.Salas[Config.Sala_position[sala_.id]].Models)
client.emit("sala_atual",{id:sala_.id})
} else {
client.emit("err",{err_name:"sala não encontrada"})
}
})
client.on("Editar_Model",async(data)=>{
let user_room = Config.UserSala[client.id]
let sala = Config.Salas[Config.Sala_position[user_room]]
let newModels = []
for(let model of sala.Models) {
if(model.name != data.name)
newModels.push(model)
else
newModels.push(data)
}
Config.Salas[Config.Sala_position[user_room]].Models = newModels
for( let user of Config.Salas[Config.Sala_position[user_room]].Users) {
if(user.id != client.id) {
user.emit("Atuzlizar_Model",data);
}
}
})
client.on("NewModel",async(model_config)=>{
let user_room = Config.UserSala[client.id]
Config.Salas[Config.Sala_position[user_room]].Models.push(model_config)
for( let user of Config.Salas[Config.Sala_position[user_room]].Users) {
if(user.id != client.id) {
user.emit("addModel",model_config);
}
}
})
client.on("RemoverModel",async(data)=>{
let user_room = Config.UserSala[client.id]
let sala = Config.Salas[Config.Sala_position[user_room]]
let newModels = []
for(let model of sala.Models) {
if(model.name != data.name)
newModels.push(model)
}
Config.Salas[Config.Sala_position[user_room]].Models = newModels
for( let user of Config.Salas[Config.Sala_position[user_room]].Users) {
if(user.id != client.id) {
user.emit("remover_model",data);
}
}
})
})
})