Added initial version
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package ms.world;
|
||||
|
||||
import ms.net.IoSession;
|
||||
import ms.net.packet.WorldPacketRepository;
|
||||
import ms.system.mysql.SQLEntryHandler;
|
||||
import ms.system.mysql.WorldListSQLHandler;
|
||||
import ms.system.util.TaskExecutor;
|
||||
import ms.world.info.Response;
|
||||
import ms.world.info.WorldInfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Represents a game world.
|
||||
* @author Emperor
|
||||
*
|
||||
*/
|
||||
public final class GameServer {
|
||||
|
||||
/**
|
||||
* The world info.
|
||||
*/
|
||||
private final WorldInfo info;
|
||||
|
||||
/**
|
||||
* The I/O session.
|
||||
*/
|
||||
private IoSession session;
|
||||
|
||||
/**
|
||||
* The players active on the game server.
|
||||
*/
|
||||
private final Map<String, PlayerSession> players = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The scheduled future for the updating task.
|
||||
*/
|
||||
private ScheduledFuture<?> future;
|
||||
|
||||
/**
|
||||
* constructs a new {@code GameServer} {@code Object}.
|
||||
* @param info The world info.
|
||||
*/
|
||||
public GameServer(WorldInfo info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the game server.
|
||||
* @param session The I/O session.
|
||||
*/
|
||||
public void configure(IoSession session) {
|
||||
this.session = session;
|
||||
session.setGameServer(this);
|
||||
future = TaskExecutor.getExecutor().scheduleAtFixedRate(() -> {
|
||||
SQLEntryHandler.write(new WorldListSQLHandler(GameServer.this));
|
||||
if (!isActive()) {
|
||||
future.cancel(false);
|
||||
future = null;
|
||||
}
|
||||
}, 0, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a player.
|
||||
* @param player The player.
|
||||
*/
|
||||
public void register(PlayerSession player) {
|
||||
players.put(player.getUsername(), player);
|
||||
player.setWorld(this);
|
||||
player.setActive(true);
|
||||
player.setWorldId(info.getWorldId());
|
||||
player.configure();
|
||||
WorldPacketRepository.sendRegistryResponse(this, player, Response.SUCCESSFUL);
|
||||
player.getCommunication().sync();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the game server is active.
|
||||
* @return {@code True} if so.
|
||||
*/
|
||||
public boolean isActive() {
|
||||
return session.isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the playerAmount
|
||||
*/
|
||||
public int getPlayerAmount() {
|
||||
return players.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the info
|
||||
*/
|
||||
public WorldInfo getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the session
|
||||
*/
|
||||
public IoSession getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param session the session to set
|
||||
*/
|
||||
public void setSession(IoSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the players value.
|
||||
* @return The players.
|
||||
*/
|
||||
public Map<String, PlayerSession> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
package ms.world;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import ms.ServerConstants;
|
||||
import ms.system.communication.ClanRepository;
|
||||
import ms.system.communication.CommunicationInfo;
|
||||
import ms.system.mysql.SQLManager;
|
||||
import ms.world.info.UIDInfo;
|
||||
|
||||
/**
|
||||
* Represents a player session.
|
||||
* @author Emperor
|
||||
*
|
||||
*/
|
||||
public final class PlayerSession {
|
||||
|
||||
/**
|
||||
* The player's communication info.
|
||||
*/
|
||||
private CommunicationInfo communication = new CommunicationInfo(this);
|
||||
|
||||
/**
|
||||
* The game server the player is currently in.
|
||||
*/
|
||||
private GameServer world;
|
||||
|
||||
/**
|
||||
* The current clan.
|
||||
*/
|
||||
private ClanRepository clan;
|
||||
|
||||
/**
|
||||
* The player's UID info.
|
||||
*/
|
||||
private UIDInfo uid;
|
||||
|
||||
/**
|
||||
* The username.
|
||||
*/
|
||||
private final String username;
|
||||
|
||||
/**
|
||||
* The password.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* The player's rights.
|
||||
*/
|
||||
private int rights;
|
||||
|
||||
/**
|
||||
* The world id.
|
||||
*/
|
||||
private int worldId;
|
||||
|
||||
/**
|
||||
* If the player session is active.
|
||||
*/
|
||||
private boolean active;
|
||||
|
||||
/**
|
||||
* The time stamp of last disconnection.
|
||||
*/
|
||||
private long disconnectionTime = 0l;
|
||||
|
||||
/**
|
||||
* How long the player is banned for.
|
||||
*/
|
||||
private long banTime;
|
||||
|
||||
/**
|
||||
* How long the player is muted for.
|
||||
*/
|
||||
private long muteTime;
|
||||
|
||||
/**
|
||||
* The last world the player logged in.
|
||||
*/
|
||||
private int lastWorld = -1;
|
||||
|
||||
/**
|
||||
* The chat icon.
|
||||
*/
|
||||
private int chatIcon;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code PlayerSession} {@code Object}.
|
||||
* @param username The username.
|
||||
* @param password The password.
|
||||
* @param ipAddress The ip address.
|
||||
* @param macAddress The mac address.
|
||||
* @param computerName The computer name.
|
||||
* @param serial The motherboard serial key.
|
||||
*/
|
||||
public PlayerSession(String username, String password, UIDInfo uid) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the session's data from the database.
|
||||
* @return {@code True} if parsed.
|
||||
*/
|
||||
public boolean parse() {
|
||||
Connection connection = SQLManager.getConnection();
|
||||
if (connection == null) {
|
||||
return false;
|
||||
}
|
||||
ResultSet result = null;
|
||||
PreparedStatement statement;
|
||||
try {
|
||||
statement = connection.prepareStatement("SELECT * FROM " + "members" + " WHERE " + "" + "username" + "='" + username.toLowerCase() + "' LIMIT 1");
|
||||
result = statement.executeQuery();
|
||||
if (result == null || !result.next()) {
|
||||
SQLManager.close(connection);
|
||||
return false;
|
||||
}
|
||||
rights = result.getInt("rights");
|
||||
disconnectionTime = result.getLong("disconnectTime");
|
||||
lastWorld = result.getInt("lastWorld");
|
||||
banTime = result.getLong("banTime");
|
||||
communication.parse(result);
|
||||
uid.parse(result);
|
||||
SQLManager.close(connection);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
SQLManager.close(connection);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a session is removed.
|
||||
*/
|
||||
public void remove() {
|
||||
if (world != null && world.getInfo().getRevision() == 498) {
|
||||
return;
|
||||
}
|
||||
Connection connection = SQLManager.getConnection();
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
PreparedStatement statement;
|
||||
try {
|
||||
statement = connection.prepareStatement("UPDATE members SET disconnectTime = ?, lastWorld = ?, contacts = ?, blocked = ?, clanName = ?, currentClan = ?, clanReqs = ?, chatSettings = ?, online= ? WHERE username = ?");
|
||||
statement.setLong(1, System.currentTimeMillis());
|
||||
statement.setInt(2, worldId);
|
||||
communication.save(statement);
|
||||
statement.setBoolean(9, false);
|
||||
statement.setString(10, username);
|
||||
statement.executeUpdate();
|
||||
SQLManager.close(connection);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
SQLManager.close(connection);
|
||||
}
|
||||
communication.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a player session.
|
||||
* @param username the username.
|
||||
* @return the player session.
|
||||
*/
|
||||
public static PlayerSession get(String username) {
|
||||
PlayerSession session = new PlayerSession(username, "", new UIDInfo());
|
||||
if (!session.parse()) {
|
||||
return null;
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the player session.
|
||||
*/
|
||||
public void configure() {
|
||||
ClanRepository clan = ClanRepository.getClans().get(username);
|
||||
if (clan != null) {
|
||||
clan.setOwner(this);
|
||||
}
|
||||
Connection connection = SQLManager.getConnection();
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
PreparedStatement statement;
|
||||
try {
|
||||
statement = connection.prepareStatement("UPDATE members SET online = ?, lastWorld = ?, lastLogin = ? WHERE username = ?");
|
||||
statement.setBoolean(1, true);
|
||||
statement.setInt(2, worldId);
|
||||
statement.setLong(3, System.currentTimeMillis());
|
||||
statement.setString(4, username);
|
||||
statement.execute();
|
||||
SQLManager.close(connection);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
SQLManager.close(connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the player has just moved worlds.
|
||||
* @return {@code True} if so.
|
||||
*/
|
||||
public boolean hasMovedWorld() {
|
||||
if (rights == 2) {
|
||||
return false;
|
||||
}
|
||||
return System.currentTimeMillis() - disconnectionTime < ServerConstants.WORLD_SWITCH_DELAY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the username value.
|
||||
* @return The username.
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the password value.
|
||||
* @return The password.
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the chat icon.
|
||||
* @return The chat icon.
|
||||
*/
|
||||
public int getChatIcon() {
|
||||
return chatIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ipAddress value.
|
||||
* @return The ipAddress.
|
||||
*/
|
||||
public String getIpAddress() {
|
||||
return uid.getIp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the macAddress value.
|
||||
* @return The macAddress.
|
||||
*/
|
||||
public String getMacAddress() {
|
||||
return uid.getMac();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the computerName value.
|
||||
* @return The computerName.
|
||||
*/
|
||||
public String getComputerName() {
|
||||
return uid.getCompName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the worldId value.
|
||||
* @return The worldId.
|
||||
*/
|
||||
public int getWorldId() {
|
||||
return worldId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the worldId value.
|
||||
* @param worldId The worldId to set.
|
||||
*/
|
||||
public void setWorldId(int worldId) {
|
||||
this.worldId = worldId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the active value.
|
||||
* @return The active.
|
||||
*/
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the active value.
|
||||
* @param active The active to set.
|
||||
*/
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the serialKey value.
|
||||
* @return The serialKey.
|
||||
*/
|
||||
public String getSerialKey() {
|
||||
return uid.getSerial();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the rights value.
|
||||
* @return The rights.
|
||||
*/
|
||||
public int getRights() {
|
||||
return rights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the rights value.
|
||||
* @param rights The rights to set.
|
||||
*/
|
||||
public void setRights(int rights) {
|
||||
this.rights = rights;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the player is banned.
|
||||
* @return {@code true} if so.
|
||||
*/
|
||||
public boolean isBanned() {
|
||||
return banTime > System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the banTime value.
|
||||
* @return The banTime.
|
||||
*/
|
||||
public long getBanTime() {
|
||||
return banTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the muteTime.
|
||||
* @return the muteTime.
|
||||
*/
|
||||
public long getMuteTime() {
|
||||
return muteTime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the muteTime.
|
||||
* @param muteTime the muteTime to set
|
||||
*/
|
||||
public void setMuteTime(long muteTime) {
|
||||
this.muteTime = muteTime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the disconnectionTime value.
|
||||
* @return The disconnectionTime.
|
||||
*/
|
||||
public long getDisconnectionTime() {
|
||||
return disconnectionTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the lastWorld value.
|
||||
* @return The lastWorld.
|
||||
*/
|
||||
public int getLastWorld() {
|
||||
return lastWorld;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the lastWorld value.
|
||||
* @param lastWorld The lastWorld to set.
|
||||
*/
|
||||
public void setLastWorld(int lastWorld) {
|
||||
this.lastWorld = lastWorld;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the communication value.
|
||||
* @return The communication.
|
||||
*/
|
||||
public CommunicationInfo getCommunication() {
|
||||
return communication;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the communication value.
|
||||
* @param communication The communication to set.
|
||||
*/
|
||||
public void setCommunication(CommunicationInfo communication) {
|
||||
this.communication = communication;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the world value.
|
||||
* @return The world.
|
||||
*/
|
||||
public GameServer getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the world value.
|
||||
* @param world The world to set.
|
||||
*/
|
||||
public void setWorld(GameServer world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the clan value.
|
||||
* @return The clan.
|
||||
*/
|
||||
public ClanRepository getClan() {
|
||||
return clan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the clan value.
|
||||
* @param clan The clan to set.
|
||||
*/
|
||||
public void setClan(ClanRepository clan) {
|
||||
this.clan = clan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the chat icon.
|
||||
* @param chatIcon
|
||||
*/
|
||||
public void setChatIcon(int chatIcon) {
|
||||
this.chatIcon = chatIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the uid.
|
||||
* @return the uid.
|
||||
*/
|
||||
public UIDInfo getUid() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the uid.
|
||||
* @param uid the uid to set
|
||||
*/
|
||||
public void setUid(UIDInfo uid) {
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the disconnect time.
|
||||
* @param time the time.
|
||||
*/
|
||||
public void setDisconnectTime(long time) {
|
||||
this.disconnectionTime = time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return username.equals(((PlayerSession) o).username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "player [name=" + username + ", pass=" + password + ", ip=" + uid.getIp() + ", mac=" + uid.getMac() + ", comp=" + uid.getCompName() + ", msk=" + uid.getSerial() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
package ms.world;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import ms.ServerConstants;
|
||||
import ms.net.IoSession;
|
||||
import ms.net.packet.IoBuffer;
|
||||
import ms.world.info.CountryFlag;
|
||||
import ms.world.info.WorldInfo;
|
||||
|
||||
/**
|
||||
* Holds all the world servers.
|
||||
* @author Emperor
|
||||
*
|
||||
*/
|
||||
public class WorldDatabase {
|
||||
|
||||
/**
|
||||
* The game servers.
|
||||
*/
|
||||
public static final GameServer[] DATABASE = new GameServer[ServerConstants.WORLD_LIMIT];
|
||||
|
||||
/**
|
||||
* The update time stamp.
|
||||
*/
|
||||
private static long updateStamp = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Used to prevent {@code WorldDatabase} from getting instantiated.
|
||||
*/
|
||||
private WorldDatabase() {
|
||||
/*
|
||||
* empty.
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the packet to update the world list in the lobby.
|
||||
* @param player The player.
|
||||
* @param worldConfiguration If the configuration should be added.
|
||||
* @param worldStatus If the status should be added.
|
||||
* @return The {@code OutgoingPacket} to write.
|
||||
*/
|
||||
public static void sendUpdate(IoSession session, int updateStamp) {
|
||||
ByteBuffer buf = ByteBuffer.allocate(1024);
|
||||
buf.put((byte) 0);
|
||||
buf.putShort((short) 0);
|
||||
buf.put((byte) 1);
|
||||
IoBuffer buffer = new IoBuffer();
|
||||
if (updateStamp != (int) WorldDatabase.updateStamp) {
|
||||
buf.put((byte) 1); // Indicates an update occured.
|
||||
putWorldListinfo(buffer);
|
||||
} else {
|
||||
buf.put((byte) 0);
|
||||
}
|
||||
putPlayerInfo(buffer);
|
||||
if (buffer.toByteBuffer().position() > 0) {
|
||||
buf.put((ByteBuffer) buffer.toByteBuffer().flip());
|
||||
}
|
||||
buf.putShort(1, (short) (buf.position() - 3));
|
||||
session.queue((ByteBuffer) buf.flip());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the world status on the packet.
|
||||
* @param buffer The current packet.
|
||||
*/
|
||||
private static void putPlayerInfo(IoBuffer buffer) {
|
||||
for (GameServer server : DATABASE) {
|
||||
if (server != null) {
|
||||
WorldInfo w = server.getInfo();
|
||||
buffer.putSmart(w.getWorldId());
|
||||
buffer.putShort(server.isActive() ? server.getPlayerAmount() : -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the countries for each world.
|
||||
* @param buffer The current packet.
|
||||
*/
|
||||
private static void putCountryInfo(IoBuffer buffer) {
|
||||
for (CountryFlag country : CountryFlag.values()) {
|
||||
buffer.putSmart(country.getId());
|
||||
buffer.putJagString(capitalize(country.name().toLowerCase()));
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Stole this from a library so we don't need the dependency
|
||||
*/
|
||||
private static String capitalize(String name) {
|
||||
if (name != null && name.length() != 0) {
|
||||
char[] chars = name.toCharArray();
|
||||
chars[0] = Character.toUpperCase(chars[0]);
|
||||
return new String(chars);
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the world configuration on the packet.
|
||||
* @param buffer The current packet.
|
||||
*/
|
||||
private static void putWorldListinfo(IoBuffer buffer) {
|
||||
buffer.putSmart(CountryFlag.values().length);
|
||||
putCountryInfo(buffer);
|
||||
buffer.putSmart(0);
|
||||
buffer.putSmart(DATABASE.length);
|
||||
buffer.putSmart(getRegisteredAmount());
|
||||
for (GameServer server : DATABASE) {
|
||||
if (server != null) {
|
||||
WorldInfo w = server.getInfo();
|
||||
buffer.putSmart(w.getWorldId());
|
||||
buffer.put(w.getCountry().ordinal());
|
||||
buffer.putInt(w.getSettings());
|
||||
buffer.putJagString(w.getActivity());
|
||||
buffer.putJagString(w.getAddress());
|
||||
}
|
||||
}
|
||||
buffer.putInt((int) updateStamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount of worlds registered.
|
||||
* @return The amount of worlds registered.
|
||||
*/
|
||||
public static int getRegisteredAmount() {
|
||||
int count = 0;
|
||||
for (GameServer server : DATABASE) {
|
||||
if (server != null) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a game server.
|
||||
* @param info The game world info.
|
||||
*/
|
||||
public static GameServer register(WorldInfo info) {
|
||||
GameServer server = DATABASE[info.getWorldId()];
|
||||
if (server != null && server.getSession().isActive() && !server.getSession().getAddress().equals(info.getAddress())) {
|
||||
throw new IllegalStateException("World " + info.getWorldId() + " is already registered!");
|
||||
}
|
||||
flagUpdate();
|
||||
System.out.println("Registered world - [id=" + info.getWorldId() + ", ip=" + info.getAddress() + ", country=" + info.getCountry().name().toLowerCase() + ", revision=" + info.getRevision() + "]!");
|
||||
return DATABASE[info.getWorldId()] = new GameServer(info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the world id of the player.
|
||||
* @param username The username of the player.
|
||||
* @return The world id.
|
||||
*/
|
||||
public static int getWorldId(String username) {
|
||||
return getWorldId(getPlayer(username));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the world id of the player.
|
||||
* @param player The player.
|
||||
* @return The world id.
|
||||
*/
|
||||
public static int getWorldId(PlayerSession player) {
|
||||
if (player == null || !player.isActive()) {
|
||||
return 0;
|
||||
}
|
||||
return player.getWorldId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the game world is active.
|
||||
* @param worldId The world id.
|
||||
* @return {@code True} if so.
|
||||
*/
|
||||
public static boolean isActive(int worldId) {
|
||||
GameServer server = get(worldId);
|
||||
return server != null && server.isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the player session for the given name is active.
|
||||
* @param username The player's username.
|
||||
* @return {@code True} if so.
|
||||
*/
|
||||
public static boolean isActivePlayer(String username) {
|
||||
PlayerSession session = getPlayer(username);
|
||||
return session != null && session.isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player session for the given name.
|
||||
* @param username The player's username.
|
||||
* @return The player session.
|
||||
*/
|
||||
public static PlayerSession getPlayer(String username) {
|
||||
return getPlayer(username, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player session for the given name.
|
||||
* @param username The player's username.
|
||||
* @param load If we load the users data.
|
||||
* @return The player session.
|
||||
*/
|
||||
public static PlayerSession getPlayer(String username, boolean load) {
|
||||
for (GameServer server : DATABASE) {
|
||||
if (server != null && server.isActive()) {
|
||||
PlayerSession player = server.getPlayers().get(username);
|
||||
if (player != null) {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (load) {
|
||||
return PlayerSession.get(username);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the game server for the given world id.
|
||||
* @param worldId The world id.
|
||||
* @return The game server.
|
||||
*/
|
||||
public static GameServer get(int worldId) {
|
||||
return DATABASE[worldId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the game servers.
|
||||
* @return The list of game servers.
|
||||
*/
|
||||
public static GameServer[] getWorlds() {
|
||||
return DATABASE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the updateStamp.
|
||||
* @return the updateStamp
|
||||
*/
|
||||
public static long getUpdateStamp() {
|
||||
return updateStamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the updateStamp.
|
||||
* @param updateStamp the updateStamp to set.
|
||||
*/
|
||||
public static void flagUpdate() {
|
||||
WorldDatabase.updateStamp = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package ms.world.info;
|
||||
|
||||
/**
|
||||
* The country flags.
|
||||
* @author Emperor
|
||||
*
|
||||
*/
|
||||
public enum CountryFlag {
|
||||
|
||||
/**
|
||||
* Belgium
|
||||
*/
|
||||
BELGIUM(22),
|
||||
|
||||
/**
|
||||
* Canada.
|
||||
*/
|
||||
CANADA(38),
|
||||
|
||||
/**
|
||||
* The united kingdom.
|
||||
*/
|
||||
UNITED_KINGDOM(77),
|
||||
|
||||
/**
|
||||
* Netherlands
|
||||
*/
|
||||
NETHERLANDS(161),
|
||||
|
||||
/**
|
||||
* Sweden
|
||||
*/
|
||||
SWEDEN(191),
|
||||
|
||||
/**
|
||||
* Finland
|
||||
*/
|
||||
FINLAND(69),
|
||||
|
||||
/**
|
||||
* Australia.
|
||||
*/
|
||||
AUSTRALIA(16),
|
||||
|
||||
/**
|
||||
* America (USA)
|
||||
*/
|
||||
UNITED_STATES_OF_AMERICA(225),
|
||||
|
||||
/**
|
||||
* Norway
|
||||
*/
|
||||
NORWAY(162),
|
||||
|
||||
/**
|
||||
* Ireland
|
||||
*/
|
||||
IRELAND(101),
|
||||
|
||||
/**
|
||||
* DENMARK
|
||||
*/
|
||||
DENMARK(58),
|
||||
|
||||
/**
|
||||
* Brazil
|
||||
*/
|
||||
BRAZIL(31),
|
||||
|
||||
/**
|
||||
* Mexico
|
||||
*/
|
||||
MEXICO(152),
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* The flag id.
|
||||
*/
|
||||
private final int id;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code CountryFlag} {@code Object}.
|
||||
* @param id The flag id.
|
||||
*/
|
||||
private CountryFlag(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the id
|
||||
*/
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package ms.world.info;
|
||||
|
||||
/**
|
||||
* Holds the response codes during the attempt to login.
|
||||
* @author Emperor
|
||||
*/
|
||||
public enum Response {
|
||||
|
||||
/**
|
||||
* An unexpected server response occurred.
|
||||
*/
|
||||
UNEXPECTED_RESPONSE(0),
|
||||
|
||||
/**
|
||||
* Could not display advertisement video, logging in in x seconds.
|
||||
*/
|
||||
COULD_NOT_DISPLAY_AD(1),
|
||||
|
||||
/**
|
||||
* A successful login.
|
||||
*/
|
||||
SUCCESSFUL(2),
|
||||
|
||||
/**
|
||||
* Invalid username or password has been entered.
|
||||
*/
|
||||
INVALID_CREDENTIALS(3),
|
||||
|
||||
/**
|
||||
* This account is banned.
|
||||
*/
|
||||
ACCOUNT_DISABLED(4),
|
||||
|
||||
/**
|
||||
* This account is already logged in.
|
||||
*/
|
||||
ALREADY_ONLINE(5),
|
||||
|
||||
/**
|
||||
* We have updated and client needs to be reloaded.
|
||||
*/
|
||||
UPDATED(6),
|
||||
|
||||
/**
|
||||
* The world is full.
|
||||
*/
|
||||
FULL_WORLD(7),
|
||||
|
||||
/**
|
||||
* Login server is offline.
|
||||
*/
|
||||
LOGIN_SERVER_OFFLINE(8),
|
||||
|
||||
/**
|
||||
* The login limit has been exceeded.
|
||||
*/
|
||||
LOGIN_LIMIT_EXCEEDED(9),
|
||||
|
||||
/**
|
||||
* The session key was invalid.
|
||||
*/
|
||||
BAD_SESSION_ID(10),
|
||||
|
||||
/**
|
||||
* The password is too weak, and should be improved.
|
||||
*/
|
||||
WEAK_PASSWORD(11),
|
||||
|
||||
/**
|
||||
* When trying to connect to a members world while being f2p.
|
||||
*/
|
||||
MEMBERS_WORLD(12),
|
||||
|
||||
/**
|
||||
* Could not login.
|
||||
*/
|
||||
COULD_NOT_LOGIN(13),
|
||||
|
||||
/**
|
||||
* The server is currently updating.
|
||||
*/
|
||||
UPDATING(14),
|
||||
|
||||
/**
|
||||
* Too many incorrect login attempts from your address.
|
||||
*/
|
||||
TOO_MANY_INCORRECT_LOGINS(16),
|
||||
|
||||
/**
|
||||
* When logging on a free world while standing in members area.
|
||||
*/
|
||||
STANDING_IN_MEMBER(17),
|
||||
|
||||
/**
|
||||
* This account is locked as it might have been stolen.
|
||||
*/
|
||||
LOCKED(18),
|
||||
|
||||
/**
|
||||
* Closed beta going on.
|
||||
*/
|
||||
CLOSED_BETA(19),
|
||||
|
||||
/**
|
||||
* The login server connected to is invalid.
|
||||
*/
|
||||
INVALID_LOGIN_SERVER(20),
|
||||
|
||||
/**
|
||||
* Moving from another world...
|
||||
*/
|
||||
MOVING_WORLD(21),
|
||||
|
||||
/**
|
||||
* When the player's saved file exists, but is unable to be loaded.
|
||||
*/
|
||||
ERROR_LOADING_PROFILE(24),
|
||||
|
||||
/**
|
||||
* This computer address is disabled as it was used to break our rules.
|
||||
*/
|
||||
BANNED(26);
|
||||
|
||||
/**
|
||||
* The buffer.
|
||||
*/
|
||||
private final int opcode;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code Response} {@code Object}.
|
||||
* @param opcode The login response opcode.
|
||||
*/
|
||||
Response(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the opcode.
|
||||
* @return The opcode.
|
||||
*/
|
||||
public int opcode() {
|
||||
return opcode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package ms.world.info;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import ms.system.util.ByteBufferUtils;
|
||||
|
||||
/**
|
||||
* The unique info of a player.
|
||||
* @author Vexia
|
||||
*
|
||||
*/
|
||||
public class UIDInfo {
|
||||
|
||||
/**
|
||||
* The ip address.
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* The computer name.
|
||||
*/
|
||||
private String compName;
|
||||
|
||||
/**
|
||||
* The mac-address.
|
||||
*/
|
||||
private String mac;
|
||||
|
||||
/**
|
||||
* The motherboard serial of the user.
|
||||
*/
|
||||
private String serial;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new {@code UIDInfo} {@code Object}
|
||||
*/
|
||||
public UIDInfo() {
|
||||
/*
|
||||
* empty.
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new {@code UIDInfo} {@code Object}
|
||||
* @param ip the ip.
|
||||
* @param compName the computer name.
|
||||
* @param mac the mac.
|
||||
* @param serial the serial.
|
||||
*/
|
||||
public UIDInfo(String ip, String compName, String mac, String serial) {
|
||||
this.ip = ip;
|
||||
this.compName = compName;
|
||||
this.mac = mac;
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the data from a prepared statement.
|
||||
* @param set The result set
|
||||
* @throws SQLException The exception if thrown.
|
||||
*/
|
||||
public void parse(ResultSet set) throws SQLException {
|
||||
setIp(parseFormat(set.getString("ip")));
|
||||
setCompName(parseFormat(set.getString("computerName")));
|
||||
setMac(parseFormat(set.getString("mac")));
|
||||
setSerial(parseFormat(set.getString("serial")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the UID data on the buffer.
|
||||
* @param buffer The buffer.
|
||||
*/
|
||||
public void save(ByteBuffer buffer) {
|
||||
save(buffer, ip, 1);
|
||||
save(buffer, compName, 2);
|
||||
save(buffer, mac, 3);
|
||||
save(buffer, serial, 4);
|
||||
buffer.put((byte) 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the UID data from the buffer.
|
||||
* @param buffer The buffer.
|
||||
*/
|
||||
public void parse(ByteBuffer buffer) {
|
||||
int opcode;
|
||||
while ((opcode = buffer.get()) != 0) {
|
||||
switch (opcode) {
|
||||
case 1:
|
||||
ip = ByteBufferUtils.getString(buffer);
|
||||
break;
|
||||
case 2:
|
||||
compName = ByteBufferUtils.getString(buffer);
|
||||
break;
|
||||
case 3:
|
||||
mac = ByteBufferUtils.getString(buffer);
|
||||
break;
|
||||
case 4:
|
||||
serial = ByteBufferUtils.getString(buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string with a certain format.
|
||||
* @param string the string.
|
||||
* @return the string.
|
||||
*/
|
||||
private String parseFormat(String string) {
|
||||
if (string == null || string == "") {
|
||||
return null;
|
||||
}
|
||||
StringTokenizer token = new StringTokenizer(string, "|");
|
||||
String s = "";
|
||||
int t = token.countTokens();
|
||||
for (int i = 0; i < t; i++) {
|
||||
s = token.nextToken();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a string value to the buffer.
|
||||
* @param buffer the buffer.
|
||||
* @param value the value.
|
||||
* @param opcode the opcode.
|
||||
*/
|
||||
private void save(ByteBuffer buffer, String value, int opcode) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
ByteBufferUtils.putString(value, buffer.put((byte) opcode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a to string in format mode for an admin or mod.
|
||||
* @param admin the admin.
|
||||
* @return the string.
|
||||
*/
|
||||
public String toString(boolean admin) {
|
||||
String format = toString();
|
||||
if (!admin) {//formats for non-admins
|
||||
String[] tokens = format.split("serial=");
|
||||
format = format.replace("serial=", "uid=").replace(tokens[tokens.length - 1], "*****");
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the compName.
|
||||
* @return the compName
|
||||
*/
|
||||
public String getCompName() {
|
||||
return compName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ip.
|
||||
* @return the ip
|
||||
*/
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mac.
|
||||
* @return the mac
|
||||
*/
|
||||
public String getMac() {
|
||||
return mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the serial.
|
||||
* @return the serial
|
||||
*/
|
||||
public String getSerial() {
|
||||
return serial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the ip.
|
||||
* @param ip the ip to set.
|
||||
*/
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the compName.
|
||||
* @param compName the compName to set.
|
||||
*/
|
||||
public void setCompName(String compName) {
|
||||
this.compName = compName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the mac.
|
||||
* @param mac the mac to set.
|
||||
*/
|
||||
public void setMac(String mac) {
|
||||
this.mac = mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serial.
|
||||
* @param serial the serial to set.
|
||||
*/
|
||||
public void setSerial(String serial) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[ip=" + ip + ", compName=" + compName + ", mac=" + mac + ", serial=" + serial + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package ms.world.info;
|
||||
|
||||
/**
|
||||
* Holds the info of a world server.
|
||||
* @author Emperor
|
||||
*
|
||||
*/
|
||||
public final class WorldInfo {
|
||||
|
||||
/**
|
||||
* The world id.
|
||||
*/
|
||||
private final int worldId;
|
||||
|
||||
/**
|
||||
* The IP-address.
|
||||
*/
|
||||
private final String address;
|
||||
|
||||
/**
|
||||
* The revision of the world.
|
||||
*/
|
||||
private final int revision;
|
||||
|
||||
/**
|
||||
* The country this world is located in.
|
||||
*/
|
||||
private final CountryFlag country;
|
||||
|
||||
/**
|
||||
* The world activity.
|
||||
*/
|
||||
private final String activity;
|
||||
|
||||
/**
|
||||
* If the world is members only.
|
||||
*/
|
||||
private final boolean members;
|
||||
|
||||
/**
|
||||
* If the world is a PvP world.
|
||||
*/
|
||||
private final boolean pvp;
|
||||
|
||||
/**
|
||||
* If the world is a quick chat only world.
|
||||
*/
|
||||
private final boolean quickChat;
|
||||
|
||||
/**
|
||||
* If the world has lootshare option enabled.
|
||||
*/
|
||||
private final boolean lootshare;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code WorldInfo} {@code Object}.
|
||||
* @param id The world id.
|
||||
* @param address The address.
|
||||
* @param country The country flag.
|
||||
* @param members If the world is members only.
|
||||
*/
|
||||
public WorldInfo(int id, String address, int revision, CountryFlag country, String activity, boolean members, boolean pvp, boolean quickChat, boolean lootshare) {
|
||||
this.worldId = id;
|
||||
this.address = address;
|
||||
this.revision = revision;
|
||||
this.country = country;
|
||||
this.activity = activity;
|
||||
this.members = members;
|
||||
this.pvp = pvp;
|
||||
this.quickChat = quickChat;
|
||||
this.lootshare = lootshare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the settings hash.
|
||||
* @return The settings hash.
|
||||
*/
|
||||
public int getSettings() {
|
||||
int settings = 0;
|
||||
if (members) {
|
||||
settings |= 0x1;
|
||||
}
|
||||
if (quickChat) {
|
||||
settings |= 0x2;
|
||||
}
|
||||
if (pvp) {
|
||||
settings |= 0x4;
|
||||
}
|
||||
if (lootshare) {
|
||||
settings |= 0x8;
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the worldId
|
||||
*/
|
||||
public int getWorldId() {
|
||||
return worldId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the address
|
||||
*/
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the country
|
||||
*/
|
||||
public CountryFlag getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the members value.
|
||||
* @return The members.
|
||||
*/
|
||||
public boolean isMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the activity value.
|
||||
* @return The activity.
|
||||
*/
|
||||
public String getActivity() {
|
||||
return activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the pvp value.
|
||||
* @return The pvp.
|
||||
*/
|
||||
public boolean isPvp() {
|
||||
return pvp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the quickChat value.
|
||||
* @return The quickChat.
|
||||
*/
|
||||
public boolean isQuickChat() {
|
||||
return quickChat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the lootshare value.
|
||||
* @return The lootshare.
|
||||
*/
|
||||
public boolean isLootshare() {
|
||||
return lootshare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the revision.
|
||||
* @return the revision.
|
||||
*/
|
||||
public int getRevision() {
|
||||
return revision;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user