mirror of
https://github.com/Leahnaya/UBFunkeysServer.git
synced 2026-04-07 17:34:53 -05:00
Fixed u_gbl and u_ccs | Store Users on Server in HashMap
- Need to fix u_cph - Need to fix announcements to other users
This commit is contained in:
parent
ba5bd16b4f
commit
f1b11c0247
|
|
@ -75,10 +75,10 @@ public class ArkOneController implements TcpHandler {
|
|||
responses.add(userPlugin.RegisterUser(commandInfo));
|
||||
break;
|
||||
case "u_gbl":
|
||||
responses.add(userPlugin.GetBuddyList(commandInfo));
|
||||
responses.add(userPlugin.GetBuddyList(connection));
|
||||
break;
|
||||
case "u_ccs":
|
||||
responses.add(userPlugin.ChangeChatStatus(commandInfo));
|
||||
responses.add(userPlugin.ChangeChatStatus(commandInfo, connection));
|
||||
break;
|
||||
case "u_cph":
|
||||
responses.add(userPlugin.ChangePhoneStatus(commandInfo));
|
||||
|
|
@ -146,6 +146,9 @@ public class ArkOneController implements TcpHandler {
|
|||
// Update the user in the DB
|
||||
userService.save(user);
|
||||
|
||||
// Remove user from HashMap
|
||||
userService.removeUserFromServer(connection);
|
||||
|
||||
// Notify other users that they went offline
|
||||
try {
|
||||
arkOneSender.SendStatusUpdate("u_cos", "o", "0", user.getUUID());
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.icedberries.UBFunkeysServer.ArkOne.ArkOneController;
|
|||
import com.icedberries.UBFunkeysServer.ArkOne.ArkOneParser;
|
||||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import com.icedberries.UBFunkeysServer.service.UserService;
|
||||
import javagrinko.spring.tcp.Server;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
@ -19,6 +20,9 @@ import java.util.UUID;
|
|||
@Service
|
||||
public class BasePlugin {
|
||||
|
||||
@Autowired
|
||||
Server server;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
|
|
@ -183,7 +187,9 @@ public class BasePlugin {
|
|||
|
||||
// Store the UUID for our connection to the server to the DB
|
||||
user.setConnectionId(connectionId);
|
||||
userService.save(user);
|
||||
|
||||
// Save to local map and update DB
|
||||
userService.updateUserOnServer(connectionId, user);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import com.icedberries.UBFunkeysServer.ArkOne.ArkOneParser;
|
|||
import com.icedberries.UBFunkeysServer.ArkOne.ArkOneSender;
|
||||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import com.icedberries.UBFunkeysServer.service.UserService;
|
||||
import javagrinko.spring.tcp.Connection;
|
||||
import javagrinko.spring.tcp.Server;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
@ -16,11 +18,13 @@ import javax.xml.parsers.ParserConfigurationException;
|
|||
import javax.xml.transform.TransformerException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class UserPlugin {
|
||||
|
||||
@Autowired
|
||||
Server server;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
|
|
@ -78,9 +82,8 @@ public class UserPlugin {
|
|||
return ArkOneParser.RemoveXMLTag(doc);
|
||||
}
|
||||
|
||||
public String GetBuddyList(Element element) throws ParserConfigurationException, TransformerException {
|
||||
//TODO: VERIFY THE ATTRIBUTE NAME
|
||||
User user = userService.findByUUID(Integer.valueOf(element.getAttribute("id"))).orElse(null);
|
||||
public String GetBuddyList(Connection connection) throws ParserConfigurationException, TransformerException {
|
||||
User user = server.getConnectedUsers().get(connection.getClientIdentifier());
|
||||
|
||||
// Start of response
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
|
|
@ -91,7 +94,8 @@ public class UserPlugin {
|
|||
doc.appendChild(rootElement);
|
||||
|
||||
// User should never be null here but check just in case
|
||||
if (user == null) {
|
||||
// Also return if no buddies
|
||||
if (user == null || user.getRawBuddyList() == null) {
|
||||
return ArkOneParser.RemoveXMLTag(doc);
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +130,7 @@ public class UserPlugin {
|
|||
|
||||
// The client doesn't set online status. So the server sets it when it gets all the user's buddies
|
||||
user.setIsOnline(1);
|
||||
userService.save(user);
|
||||
userService.updateUserOnServer(connection, user);
|
||||
|
||||
// Let all your buddies know you are online
|
||||
if (buddyList.size() > 0) {
|
||||
|
|
@ -137,14 +141,11 @@ public class UserPlugin {
|
|||
return ArkOneParser.RemoveXMLTag(doc);
|
||||
}
|
||||
|
||||
public String ChangeChatStatus(Element element) throws ParserConfigurationException, TransformerException {
|
||||
//TODO: VERIFY THE ATTRIBUTE NAME
|
||||
User user = userService.findByUUID(Integer.valueOf(element.getAttribute("id"))).orElse(null);
|
||||
public String ChangeChatStatus(Element element, Connection connection) throws ParserConfigurationException, TransformerException {
|
||||
User user = server.getConnectedUsers().get(connection.getClientIdentifier());
|
||||
|
||||
//TODO: VERIFY THE ATTRIBUTE NAME
|
||||
// Update that user's chat status
|
||||
user.setChatStatus(Integer.valueOf(element.getAttribute("s")));
|
||||
userService.save(user);
|
||||
userService.updateUserOnServer(connection, user);
|
||||
|
||||
// Build the response
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.icedberries.UBFunkeysServer.service;
|
||||
|
||||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import javagrinko.spring.tcp.Connection;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
|
@ -20,4 +21,9 @@ public interface UserService {
|
|||
String getBuddyList(Integer uuid);
|
||||
|
||||
Optional<User> findByConnectionId(UUID connectionId);
|
||||
|
||||
User updateUserOnServer(Connection connection, User user);
|
||||
User updateUserOnServer(UUID uuid, User user);
|
||||
|
||||
void removeUserFromServer(Connection connection);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,10 @@ package com.icedberries.UBFunkeysServer.service.impl;
|
|||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import com.icedberries.UBFunkeysServer.repository.UserRepository;
|
||||
import com.icedberries.UBFunkeysServer.service.UserService;
|
||||
import javagrinko.spring.tcp.Connection;
|
||||
import javagrinko.spring.tcp.Server;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
|
@ -15,6 +18,9 @@ public class UserServiceImpl implements UserService {
|
|||
|
||||
public final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
Server server;
|
||||
|
||||
|
||||
@Override
|
||||
public Optional<User> findByUUID(Integer uuid) {
|
||||
|
|
@ -50,4 +56,24 @@ public class UserServiceImpl implements UserService {
|
|||
public Optional<User> findByConnectionId(UUID connectionId) {
|
||||
return userRepository.findByConnectionId(connectionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User updateUserOnServer(Connection connection, User user) {
|
||||
return updateUserOnServer(connection.getClientIdentifier(), user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User updateUserOnServer(UUID uuid, User user) {
|
||||
User returnedUser = save(user);
|
||||
|
||||
// Save user to local map
|
||||
server.addConnectedUser(uuid, returnedUser);
|
||||
|
||||
return returnedUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUserFromServer(Connection connection) {
|
||||
server.removeConnectedUser(connection.getClientIdentifier());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package javagrinko.spring.tcp;
|
||||
|
||||
|
||||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface Server {
|
||||
int getConnectionsCount();
|
||||
|
|
@ -10,4 +14,8 @@ public interface Server {
|
|||
void stop();
|
||||
List<Connection> getConnections();
|
||||
void addListener(Connection.Listener listener);
|
||||
HashMap<UUID, User> getConnectedUsers();
|
||||
|
||||
void addConnectedUser(UUID uuid, User user);
|
||||
void removeConnectedUser(UUID uuid);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package javagrinko.spring.tcp;
|
||||
|
||||
import com.icedberries.UBFunkeysServer.domain.User;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -8,6 +9,7 @@ import java.io.IOException;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
|
@ -21,6 +23,8 @@ public class TcpServer implements Server, Connection.Listener {
|
|||
private List<Connection> connections = new CopyOnWriteArrayList<>();
|
||||
private List<Connection.Listener> listeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private HashMap<UUID, User> connectedUsers = new HashMap<>();
|
||||
|
||||
public void setPort(Integer port) {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
|
|
@ -69,6 +73,25 @@ public class TcpServer implements Server, Connection.Listener {
|
|||
listeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<UUID, User> getConnectedUsers() {
|
||||
return connectedUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addConnectedUser(UUID uuid, User user) {
|
||||
if (connectedUsers.containsKey(uuid)) {
|
||||
connectedUsers.replace(uuid, user);
|
||||
} else {
|
||||
connectedUsers.put(uuid, user);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeConnectedUser(UUID uuid) {
|
||||
connectedUsers.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(Connection connection, byte[] bytes)
|
||||
throws InvocationTargetException, IllegalAccessException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user