-
Notifications
You must be signed in to change notification settings - Fork 25
/
verifyOnMap.ts
32 lines (26 loc) · 1.08 KB
/
verifyOnMap.ts
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
import { MARGIN_OF_ERROR, PeerResponse } from '../utils'
// validate that the player is active in a catalyst server, and in the indicated coordinates, or within a margin of error
export async function checkPlayer(playerId: string, server: string, parcel: number[]) {
// `https://peer.decentraland.org/comms/peers`
const url = server + '/comms/peers/'
try {
const response = await fetch(url)
const data: PeerResponse = await response.json()
if (data.ok) {
const player = data.peers.find((peer) => peer.address && peer.address.toLowerCase() === playerId.toLowerCase())
if (!player.parcel) {
return false
}
return player && checkCoords(player.parcel, parcel)
}
} catch (error) {
console.log(error)
return false
}
return false
}
// check coordinates against a single parcel - within a margin of error
export function checkCoords(coords: number[], parcel: number[]) {
const validMargin = (p: number, c: number) => Math.abs(p - c) <= MARGIN_OF_ERROR
return validMargin(coords[0], parcel[0]) && validMargin(coords[1], parcel[1])
}