Save online time in mysql

Discussion in 'Plugin Development' started by letsgo00, Apr 21, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    letsgo00

    I'm developing a plugin where I need to save players online time, and get it with a command. I need to get the times for periods e.g.: today or 2016-02-02 to 2016-02-05.
    I need to know how should I setup my database, and how should I select and insert to it.
     
  2. Create the columns uuid, duration

    When a player joins the server, store the current time in a hashmap.
    When he logs out, the time he was online is current time - join time.
    Finally add this to current value of the player of the database

    You may change it from milliseconds to seconds or minutes
     
    Last edited: Apr 21, 2016
  3. Offline

    letsgo00

    @FisheyLP I know that, but I need to get it at a specific time like yesterday, or this month
     
  4. Offline

    mcdorli

    Have you tried the Date object in java?
     
  5. Offline

    letsgo00

    @mcdorli yes, but I have to save multiple dates in one column
     
  6. Offline

    mcdorli

    Why?
     
  7. Offline

    letsgo00

    @mcdorli to get onloine time from spectific time or range like from 2016-02-02 to 2016-02-12 etc
     
  8. Offline

    mcdorli

    Why not store a long with the total amount of milliseconds?
    And if you really need the 2 dates, then why can't you store it in separate columns.
     
    timtower likes this.
  9. Offline

    letsgo00

    @mcdorli I need to get every single date to be gettable
    For example if a user tries to get someones online time with a command like: /onlinetime <date> <user>
     
  10. Online

    timtower Administrator Administrator Moderator

    @letsgo00 Then store a date, and the amount of milliseconds.
     
  11. Offline

    letsgo00

    @timtower but if I have multiple dates then I dont know how to store it in one column
    like the database would look like: username | uuid | onlinetime
     
  12. Online

    timtower Administrator Administrator Moderator

    @letsgo00 That is the beauty of a database, you can store it multiple times.
    Code:
    Timtower, don'tknowmyuuid,20-04-2016, 5 hours
    Timtower, stilldon'tknowit, 21-04-2016, 3 hours
    You wouldn't be able to save it per date with your way.
    It would be even better if you would have a user table with uuid and name and a table for the playtime.
     
  13. Offline

    mcdorli

    Why the hell do you want to store it in 1 column if you can have 2?

    @timtower
    302df51d-3264-4cb8-9de2-c09c3e572be1
     
    timtower likes this.
  14. Offline

    mythbusterma

    @letsgo00

    You need to learn about database normalization.

    You shouldn't store the username every time, and you don't need to store their UUID each time either.

    You should instead create a table of players that has their username, their UUID, and a numerical autoincrement value, then use this as a foreign key in the times table, which has that value, then a log in time and log out time.

    players:
    username (varchar(16)) | uuid (varchar(26)) | id (autoincrement)

    times:
    id (int) | in (timestamp) | out (timestamp)

    Then you can select the user from the players database by their name and left join this with times to find all of a user's times, or select where a time is in between in and out.

    It's amazing what you can do with a database when you actually take the time to learn how to use it.
     
    letsgo00, Zombie_Striker and timtower like this.
  15. Offline

    mcdorli

    Yeah, I forgot to ask, is there a specific reason to use a database in this case? The first thing you learn while studying MySQL is to avoid it unless it's certainly, 100% needed.
     
  16. Offline

    Konato_K

  17. Offline

    letsgo00

  18. Offline

    mcdorli

    For bungeecord or separate servers?
     
  19. Offline

    letsgo00

    @mcdorli both and I think I'll make a website for it too
     
  20. Online

    timtower Administrator Administrator Moderator

    Locked.
    Bungeecord requires offline mode, offline mode is not supported by Bukkit.
     
Thread Status:
Not open for further replies.

Share This Page