1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Generate an ID (or integer) to identify a faction

Discussion in 'Development' started by Legoboy0215, Oct 28, 2017.

  1. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    I am currently thinking about how I would generate an ID or integer (unique) for every faction I create. I am trying to avoid the autoincrement field in SQLite, but can't seem to think of a better way. Any ideas?
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    increment, or a random and very long string to avoid collisions

    you can have meta.json {nextcount:10}
    and increment that slowly, if you want to increment without autoincrement
     
    SOFe likes this.
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You could alternatively just use a hash of a combination of - say - faction name + faction owner or something.
     
    jasonwynn10 and Legoboy0215 like this.
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    say that owner passed it's ownership
    name+time could do tho...
     
  5. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    microtime is always a good way to generate unqiue ids.
     
    jasonwynn10 likes this.
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    hope no one tried an timing attack to generate two or more guilds on the same time to break the system
    i dont think using only time is good enough
     
  7. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    yea, only using microtime is probably bad. but there is a soloution: sleep 1ms!
    (or more simply: only process one creation per tick)
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    guild creation queue
    seems overkill if you could have add something easier
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If only microtime is uesd, it is very unreliable, especially if the server becomes very popular and you have multiple servers accepting faction creation simultaneously (where not even CPU performance assumption is reliable). In addition, microtime is not an integer, and if you multiply it with 1e+9, you have to truncate the top part of the timestamp, which makes it just a random integer (but less random than random_int())
    Generally, a random 64-bit integer is reliable enough.
    [​IMG]
    So, you will get struck by a lightning before you get 20 million factions created.
    How do you increment it "slowly"? This sounds worse than using auto_increment.
    Auto-increment is not really that bad. If you don't want to populate the whole row before getting the ID, you may consider using two separate tables (one table with auto_increment, another table with factionId as the foreign key to the auto_increment row).
    Adding a current timestamp salt is more reliable if you don't want a duplicate ID when the owner recreates the faction after deletion.
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    or worse, (or not worse)..
    PHP:
    while(faction with $id random_int() exists);
    new 
    Faction($id);
     
    SOFe likes this.
  11. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    tho using auto increment can be problematic if you want to support non sql save formats
     
  12. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    How do you generate a random 64-bit integer in PHP? I googled and saw some people combining a few calls to mt_rand.
     
  13. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    NVM I think using openssl_random_pseudo_bytes(8) is good enough :p
     
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Use random_bytes(8), added in PHP 7.
    Or simply use random_int(), since PocketMine supposedly only runs in 64-bit environments.
     
    jasonwynn10 likes this.
  15. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    PHP:
    random_int(PHP_INT_MINPHP_INT_MAX);
    Is probably good enough right
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.