-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (55 loc) · 1.41 KB
/
index.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
const express = require("express")
const cors = require("cors")
const app = express()
app.use(cors())
app.use(express.json())
const players= []
class Player {
constructor(id){
this.id = id;
}
asignPet(pet){
this.pet = pet;
}
updatePosition(x, y){
this.x = x;
this.y = y;
}
}
class Pet{
constructor (name){
this.name = name;
}
}
app.get("/join", (req, res) => {
const id = `${Math.random()}`
const player = new Player(id)
players.push(player)
res.setHeader("Access-Control-Allow-Origin","*")
res.send(id)
})
app.post("/pet/:playerId", (req, res) => {
const playerId = req.params.playerId || "No llegó"
const petName = req.body.pet || "Vacíoooooo"
const pet = new Pet(petName)
const playerIndex = players.findIndex((player) => playerId === player.id)
if (playerIndex >= 0) {
players[playerIndex].asignPet(pet)
}
console.log(players)
console.log(playerId)
res.end()
})
app.post("/pet/:playerId/position", (req, res) => {
const playerId = req.params.playerId || "No llegó"
const x = req.body.x || 0
const y = req.body.y || 0
const playerIndex = players.findIndex((player) => playerId === player.id)
if (playerIndex >= 0) {
players[playerIndex].updatePosition(x,y)
}
res.end()
})
app.listen(8080, () => {
console.log("Running Baby")
})