-1

Right now, I coded a function to go like this

async function checkPlayerScam(ign) {
    const UUID = await getUUID(ign);
    if(MATCHING){
        playerIsScammer = true
    }
    else {
        playerIsScammer = false
    }
}

The MATCHING is just a placeholder at the moment. I want to check their UUID, and make sure it isn't in this list: https://raw.githubusercontent.com/skyblockz/pricecheckbot/master/scammer.json

Any idea how? It needs to be relatively fast

EDIT: It'd also be cool if I could get the reason from the list, but that's not as necessary

pilchard
  • 12,414
  • 5
  • 11
  • 23
  • You want to fetch that list each time? – see sharper Mar 18 '21 at 00:15
  • @seesharper is there a better way of doing it? I want the list to remain updated. It's a discord bot, so I feel as if it needs to be reasonably quick – SashimiDude Mar 18 '21 at 00:16
  • Does this answer your question? [How do I test if a string kind of equals another javascript](https://stackoverflow.com/questions/42539877/how-do-i-test-if-a-string-kind-of-equals-another-javascript) – Loveen Dyall Mar 18 '21 at 00:31
  • @LoveenDyall no, I want to know how to scan through the github for a matching UUID – SashimiDude Mar 18 '21 at 00:36

3 Answers3

1

https://lodash.com/docs/#find

Use lodash _.find to

const uuid = '000c97aaf948417a9a74d6858c01aaae'; // uuid you want to find
const scammer = _.find(scammersList, o => o.uuid === uuid);
if (scammer) { // if scammer found
    console.log(scammer);
    console.log(scammer.reason)
}

enter image description here

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

This function will fetch the data, then check if the uuid 1d0c0ef4295047b39f0fa899c485bd00 exists. Assuming that you already fetched the data somewhere else and stored it, all you need to do is check if a given uuid exists by adding the following line where you please:

!!data[uuidToCheck]

uuidToCheck should be the uuid string that you are looking for. This line will return true if the uuid exists and false otherwise.

In terms of the spacetime complexity, this function runs in constant time [O(1)] and O(N) space. This is the fastest time you can get it to run.

data[uuidToCheck].reason will return the reason.

async function playerIsScammer(uuidToCheck) {
    uuidToCheck = '1d0c0ef4295047b39f0fa899c485bd00';
    const response = await fetch('https://raw.githubusercontent.com/skyblockz/pricecheckbot/master/scammer.json');
    if (response.ok){
        let data = await response.json();
        if(!!data[uuidToCheck])
            return data[uuidToCheck].reason;
        return false
    }
}
Dmitry S.
  • 1,544
  • 2
  • 13
  • 22
Andrew M
  • 1
  • 1
  • This answer is wrong because key comparisons in JS are still O(N) N being number of keys, since JS doesn't stipulate the key is hashed to a memory address. Secondly, it is JSON from a 3rd party source so you can't assume the key field will always match the UUID field in the member. – Loveen Dyall Mar 18 '21 at 01:30
  • That's interesting, I've always been taught that hash maps have constant time for accessing. – Andrew M Mar 18 '21 at 01:53
  • JSON objects are not hash maps. They are key value pairs with keys being primitive values. Therefore, each key has to be assessed for equality check to the reference key. Hash maps are hash functions which hash the key to a memory address in O(1) complexity, with the hash member stored at the memory address. – Loveen Dyall Mar 18 '21 at 20:48
  • The .json() method converts the JSON into a JavaScript object. As far as I know JS object literals are commonly considered to be hash tables, which have O(1) look up times. – Andrew M Mar 19 '21 at 20:54
  • they can't be hash tables because there isn't a browser that defines a hash function for keys used in `get` accessors. – Loveen Dyall Mar 19 '21 at 23:27
0

For anyone wondering, this is how I solved it:

async function checkPlayerScam(ign) {
    const UUID = await getUUID(ign);
    const response = await fetch(`https://raw.githubusercontent.com/skyblockz/pricecheckbot/master/scammer.json`);
    const result = await responsejson();
    if (result[UUID] = null) {
        playerIsScammer == False
        } 
    else{
        playerIsScammer == True
        }
}