Birthday stuff in SQLPlayer and IOffPlayer

This commit is contained in:
Marc Baloup 2022-03-28 20:09:01 +02:00
parent 6ef8557c34
commit f73c07bea3
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
3 changed files with 67 additions and 1 deletions

View File

@ -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);
}
}
/*

View File

@ -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);
}

View File

@ -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<SQLPlayer> {
/** 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<SQLPlayer> {
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;