From f73c07bea3dcca594008a14065aba2f4620e7267 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Mon, 28 Mar 2022 20:09:01 +0200 Subject: [PATCH] Birthday stuff in SQLPlayer and IOffPlayer --- .../lib/core/players/IOffPlayer.java | 25 +++++++++++ .../lib/core/players/PlayerFinder.java | 2 +- .../pandacube/lib/core/players/SQLPlayer.java | 41 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java index 3cc2528..abe9c0e 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java @@ -1,5 +1,6 @@ package fr.pandacube.lib.core.players; +import java.util.Calendar; import java.util.UUID; import fr.pandacube.lib.core.chat.ChatColorUtil; @@ -304,6 +305,30 @@ public interface IOffPlayer { + /* + * Birthday + */ + + public default void setBirthday(int day, int month, Integer year) { + try { + SQLPlayer dbPlayer = getDbPlayer(); + dbPlayer.setBirthday(day, month, year); + dbPlayer.save(); + } catch (DBException e) { + throw new RuntimeException(e); + } + } + + public default Calendar getBirthday() { + try { + return getDbPlayer().getBirthday(); + } catch (DBException e) { + throw new RuntimeException(e); + } + } + + + /* diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java index 386862a..76e1f18 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java @@ -145,7 +145,7 @@ public class PlayerFinder { return name.matches("[0-9a-zA-Z_.]{2,20}"); } - public static SQLPlayer getDBPlayer(UUID id) throws Exception { + public static SQLPlayer getDBPlayer(UUID id) throws DBException { if (id == null) return null; return SQLPlayer.getPlayerFromUUID(id); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayer.java index 62055c7..2412c8c 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayer.java @@ -1,6 +1,8 @@ package fr.pandacube.lib.core.players; import java.sql.Date; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.Set; import java.util.UUID; @@ -9,8 +11,15 @@ import fr.pandacube.lib.core.db.DBException; import fr.pandacube.lib.core.db.SQLElement; import fr.pandacube.lib.core.db.SQLElementList; import fr.pandacube.lib.core.db.SQLField; +import fr.pandacube.lib.core.util.Log; public class SQLPlayer extends SQLElement { + + /** If the player birth year is internally 1800, it is considered as a non disclosed age. + * All player with an age below 13 should have their age automatically hidden. + */ + public static final int UNDISCLOSED_AGE_YEAR = 1800; + public SQLPlayer() { super(); @@ -59,6 +68,38 @@ public class SQLPlayer extends SQLElement { + + + public Calendar getBirthday() { + + try { + Date birthday = get(SQLPlayer.birthday); + if (birthday == null) // le joueur n'a pas de date d'anniversaire + return null; + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(birthday); + return cal; + } catch (Exception e) { + Log.severe(e); + return null; + } + } + + public void setBirthday(int day, int month, Integer year) { + if (year == null) + year = UNDISCLOSED_AGE_YEAR; + + GregorianCalendar cal = new GregorianCalendar(); + cal.set(year, month, day, 0, 0, 0); + + set(SQLPlayer.birthday, new java.sql.Date(cal.getTimeInMillis())); + } + + + + + + public static SQLPlayer getPlayerFromUUID(UUID pId) throws DBException { if (pId == null) return null;