mirror of
https://github.com/Leahnaya/UBFunkeysServer.git
synced 2026-03-23 10:14:28 -05:00
Implement Already Logged In and Scheduled Offline Inactive Users
This commit is contained in:
parent
873b2f702a
commit
fe498c8fcc
|
|
@ -11,15 +11,20 @@ import javagrinko.spring.tcp.Server;
|
|||
import javagrinko.spring.tcp.TcpController;
|
||||
import javagrinko.spring.tcp.TcpHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@TcpController
|
||||
@EnableScheduling
|
||||
public class ArkOneController implements TcpHandler {
|
||||
|
||||
public static final String IP_ADDRESS = "127.0.0.1";
|
||||
|
|
@ -107,7 +112,7 @@ public class ArkOneController implements TcpHandler {
|
|||
responses.add(userPlugin.DeleteBuddyResponse(commandInfo, connection));
|
||||
break;
|
||||
case "p":
|
||||
responses.add(userPlugin.Ping());
|
||||
responses.add(userPlugin.Ping(connection));
|
||||
break;
|
||||
|
||||
// Plugin 7 (Galaxy)
|
||||
|
|
@ -198,4 +203,31 @@ public class ArkOneController implements TcpHandler {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run this every 60 seconds to check for inactive online users
|
||||
@Scheduled(fixedRate = 60000)
|
||||
public void setOfflineInactiveUsers() {
|
||||
List<User> onlineUsers = userService.getOnlineUsers();
|
||||
|
||||
// If there is at least one user in the list
|
||||
if (onlineUsers.size() > 0) {
|
||||
for (User user : onlineUsers) {
|
||||
// Make sure the user has a last ping else turn them offline
|
||||
// A ping should be set on login
|
||||
if (user.getLastPing() == null) {
|
||||
user.setIsOnline(0);
|
||||
userService.save(user);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate how many milliseconds since last ping/login
|
||||
long difference = Math.abs(Duration.between(user.getLastPing(), LocalDateTime.now()).toMillis());
|
||||
if (difference > 60000) {
|
||||
// USer has been online for more than 60 seconds without a new ping - set them offline
|
||||
user.setIsOnline(0);
|
||||
userService.save(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@ import javax.xml.parsers.DocumentBuilder;
|
|||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
|
|
@ -183,17 +184,27 @@ public class BasePlugin {
|
|||
responseCode = 4;
|
||||
} else {
|
||||
// Login success
|
||||
uuid = String.valueOf(user.getUUID());
|
||||
|
||||
// Store the UUID for our connection to the server to the DB
|
||||
user.setConnectionId(connectionId);
|
||||
// Check if user already logged in
|
||||
if (user.getIsOnline() == 1) {
|
||||
responseCode = 2;
|
||||
} else {
|
||||
// User not in game
|
||||
uuid = String.valueOf(user.getUUID());
|
||||
|
||||
// Set they are online now
|
||||
user.setChatStatus(0);
|
||||
user.setIsOnline(1);
|
||||
// Store the UUID for our connection to the server to the DB
|
||||
user.setConnectionId(connectionId);
|
||||
|
||||
// Save to local map and update DB
|
||||
userService.updateUserOnServer(connectionId, user);
|
||||
// Set their last ping time to now
|
||||
user.setLastPing(LocalDateTime.now());
|
||||
|
||||
// Set they are online now
|
||||
user.setChatStatus(0);
|
||||
user.setIsOnline(1);
|
||||
|
||||
// Save to local map and update DB
|
||||
userService.updateUserOnServer(connectionId, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import javax.xml.parsers.DocumentBuilder;
|
|||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
|
@ -435,7 +436,13 @@ public class UserPlugin {
|
|||
return ArkOneParser.RemoveXMLTag(resp);
|
||||
}
|
||||
|
||||
public String Ping() {
|
||||
public String Ping(Connection connection) {
|
||||
User user = server.getConnectedUsers().get(connection.getClientIdentifier());
|
||||
|
||||
user.setLastPing(LocalDateTime.now());
|
||||
|
||||
userService.updateUserOnServer(connection, user);
|
||||
|
||||
return "<p t=\"30\" />";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import javax.persistence.GenerationType;
|
|||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -68,6 +69,9 @@ public class User {
|
|||
// Connection ID to send data to from other Users
|
||||
private String connectionId = "";
|
||||
|
||||
// Last ping time
|
||||
private LocalDateTime lastPing;
|
||||
|
||||
public java.util.UUID getConnectionId() {
|
||||
return java.util.UUID.fromString(connectionId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import org.springframework.data.repository.CrudRepository;
|
|||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -27,4 +28,7 @@ public interface UserRepository extends CrudRepository<User, Integer> {
|
|||
|
||||
@Query("select user from User user where user.connectionId = :connectionId")
|
||||
Optional<User> findByConnectionId(@Param("connectionId") UUID connectionId);
|
||||
|
||||
@Query("select user from User user where user.isOnline = 1")
|
||||
List<User> getAllOnlineUsers();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.icedberries.UBFunkeysServer.service;
|
|||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import javagrinko.spring.tcp.Connection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -26,4 +27,6 @@ public interface UserService {
|
|||
User updateUserOnServer(UUID uuid, User user);
|
||||
|
||||
void removeUserFromServer(Connection connection);
|
||||
|
||||
List<User> getOnlineUsers();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import lombok.RequiredArgsConstructor;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -76,4 +77,9 @@ public class UserServiceImpl implements UserService {
|
|||
public void removeUserFromServer(Connection connection) {
|
||||
server.removeConnectedUser(connection.getClientIdentifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getOnlineUsers() {
|
||||
return userRepository.getAllOnlineUsers();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user