55import com .google .common .cache .CacheLoader ;
66import com .google .common .cache .LoadingCache ;
77import dev .pgm .community .Community ;
8+ import dev .pgm .community .database .Query ;
89import dev .pgm .community .feature .SQLFeatureBase ;
910import dev .pgm .community .sessions .Session ;
1011import dev .pgm .community .sessions .SessionQuery ;
@@ -31,6 +32,12 @@ public SessionData load(@NotNull SessionQuery key) {
3132 });
3233 }
3334
35+ @ Override
36+ public void createTable () {
37+ super .createTable ();
38+ DB .executeUpdateAsync (Query .createTable (LATEST_TABLE_NAME , LATEST_TABLE_FIELDS ));
39+ }
40+
3441 @ Override
3542 public void save (Session session ) {
3643 SessionData query = sessionCache .getUnchecked (new SessionQuery (session .getPlayerId (), false ));
@@ -47,20 +54,50 @@ public void save(Session session) {
4754 session .getServerName (),
4855 session .getStartDate ().toEpochMilli (),
4956 session .getEndDate () == null ? null : session .getEndDate ().toEpochMilli ());
57+
58+ DB .executeUpdateAsync (
59+ UPSERT_LATEST_SESSION_QUERY ,
60+ session .getPlayerId ().toString (),
61+ false ,
62+ session .getSessionId ().toString (),
63+ session .isDisguised (),
64+ session .getServerName (),
65+ session .getStartDate ().toEpochMilli (),
66+ session .getEndDate () == null ? null : session .getEndDate ().toEpochMilli ());
67+
68+ if (!session .isDisguised ()) {
69+ DB .executeUpdateAsync (
70+ UPSERT_LATEST_SESSION_QUERY ,
71+ session .getPlayerId ().toString (),
72+ true ,
73+ session .getSessionId ().toString (),
74+ session .isDisguised (),
75+ session .getServerName (),
76+ session .getStartDate ().toEpochMilli (),
77+ session .getEndDate () == null ? null : session .getEndDate ().toEpochMilli ());
78+ }
5079 }
5180
5281 public void updateSessionEndTime (Session session ) {
5382 DB .executeUpdateAsync (
5483 UPDATE_SESSION_ENDTIME_QUERY ,
5584 session .getEndDate () == null ? null : session .getEndDate ().toEpochMilli (),
5685 session .getSessionId ().toString ());
86+ DB .executeUpdateAsync (
87+ UPDATE_LATEST_ENDTIME_QUERY ,
88+ session .getEndDate () == null ? null : session .getEndDate ().toEpochMilli (),
89+ session .getSessionId ().toString ());
5790 }
5891
5992 public void endOngoingSessions () {
6093 DB .executeUpdateAsync (
6194 UPDATE_ONGOING_SESSION_ENDING_QUERY ,
6295 Instant .now ().toEpochMilli (),
6396 Community .get ().getServerId ());
97+ DB .executeUpdateAsync (
98+ UPDATE_LATEST_ONGOING_SESSION_ENDING_QUERY ,
99+ Instant .now ().toEpochMilli (),
100+ Community .get ().getServerId ());
64101 }
65102
66103 @ Override
@@ -76,11 +113,12 @@ public CompletableFuture<Session> query(SessionQuery target) {
76113 return CompletableFuture .completedFuture (data .getSession ());
77114 } else {
78115 return DB .getFirstRowAsync (
79- target .ignoreDisguised () ? SELECT_DISGUISED_SESSION_QUERY : SELECT_SESSION_QUERY ,
80- target .getPlayerId ().toString ())
116+ SELECT_LATEST_SESSION_QUERY ,
117+ target .getPlayerId ().toString (),
118+ target .ignoreDisguised ())
81119 .thenApplyAsync (result -> {
82120 if (result != null ) {
83- String id = result .getString ("id " );
121+ String id = result .getString ("session_id " );
84122
85123 String player = result .getString ("player" );
86124 boolean disguised = DatabaseUtils .parseBoolean (result , "disguised" );
0 commit comments