Make bots stay after restart (#405)

This commit is contained in:
Daniel Ginovker
2020-08-03 10:52:16 -04:00
committed by GitHub
parent 6085111d7c
commit c1df673033
3 changed files with 67 additions and 47 deletions
@@ -13,6 +13,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import com.rebotted.game.bots.BotHandler;
import org.apache.mina.common.IoAcceptor; import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig; import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
@@ -195,6 +196,7 @@ public class GameEngine {
GlobalDropsHandler.initialize(); GlobalDropsHandler.initialize();
Connection.initialize(); Connection.initialize();
HostBlacklist.loadBlacklist(); HostBlacklist.loadBlacklist();
BotHandler.loadPlayerShops();
startMinutesCounter(); startMinutesCounter();
setMinutesCounter(minutesCounter); setMinutesCounter(minutesCounter);
@@ -15,7 +15,7 @@ public class Bot {
private Client botClient; private Client botClient;
static Timer timer = new Timer(); static Timer timer = new Timer();
public Bot(String username, int x, int y, int z) { public Bot(String username, Integer x, Integer y, Integer z) {
botClient = new Client(null); botClient = new Client(null);
botClient.playerName = username; botClient.playerName = username;
@@ -33,9 +33,11 @@ public class Bot {
botClient.npcCanAttack = false; botClient.npcCanAttack = false;
GameEngine.playerHandler.newPlayerClient(botClient); GameEngine.playerHandler.newPlayerClient(botClient);
botClient.getPlayerAssistant().movePlayer(x, y, z);
loadPlayerInfo(botClient, username, "bot_password", false); loadPlayerInfo(botClient, username, "bot_password", false);
if (x != null) {
botClient.getPlayerAssistant().movePlayer(x, y, z);
}
new TradeChat().run(); new TradeChat().run();
} }
@@ -1,5 +1,6 @@
package com.rebotted.game.bots; package com.rebotted.game.bots;
import java.io.File;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -13,12 +14,12 @@ import com.rebotted.game.shops.ShopHandler;
import com.rebotted.util.Misc; import com.rebotted.util.Misc;
public class BotHandler { public class BotHandler {
static final List<Bot> botList = new ArrayList<>(BotConstants.MAX_BOTS); static final List<Bot> botList = new ArrayList<>(BotConstants.MAX_BOTS);
static final Random random = new SecureRandom(); static final Random random = new SecureRandom();
static final int currency = 995; static final int currency = 995;
public static Bot connectBot(String username, int x, int y, int z) { public static Bot connectBot(String username, Integer x, Integer y, Integer z) {
Bot bot; Bot bot;
if (PlayerHandler.playerCount >= GameConstants.MAX_PLAYERS) { if (PlayerHandler.playerCount >= GameConstants.MAX_PLAYERS) {
System.out.println("Bot could not be connected, server is full."); System.out.println("Bot could not be connected, server is full.");
@@ -30,71 +31,87 @@ public class BotHandler {
return bot; return bot;
} }
public static void playerShop(Player player){ public static void loadPlayerShops() {
// Must be in the correct zones OR have developer rights File dir = new File(System.getProperty("user.dir") + "/data/characters/");
if (!player.inPlayerShopArea() && player.playerRights < 3) { File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
if (child.getName().startsWith("")) {
String playerName = child.getName().split("")[1];
playerName = playerName.substring(0, playerName.length() - 4);
System.out.println("Loading " + playerName + " shop");
String shopName = getShopName(playerName);
Bot bot = connectBot(shopName, null, null, null);
Client playerShop = bot.getBotClient();
ShopHandler.createPlayerShop(playerShop);
}
}
}
}
public static void playerShop(Player player) {
if (!player.inPlayerShopArea()) {
player.getPacketSender().sendMessage("You need to be in a bank zone or trade area for this."); player.getPacketSender().sendMessage("You need to be in a bank zone or trade area for this.");
return; return;
} }
player.getPacketSender().sendMessage("Shop commands- ::withdrawshop, ::closeshop"); player.getPacketSender().sendMessage("Shop commands- ::shop, ::withdrawshop, ::closeshop");
Client playerShop = getPlayerShop(player); Client playerShop = getPlayerShop(player.playerName);
if (playerShop == null) { if (playerShop == null) {
String shopName = getShopName(player); String shopName = getShopName(player.playerName);
Bot bot = connectBot(shopName, player.getX(), player.getY(), player.getH()); Bot bot = connectBot(shopName, player.getX(), player.getY(), player.getH());
playerShop = bot.getBotClient(); playerShop = bot.getBotClient();
ShopHandler.createPlayerShop(playerShop); ShopHandler.createPlayerShop(playerShop);
} }
Client client = playerShop; playerShop.getPlayerAssistant().movePlayer(player.getX(), player.getY(), player.getH());
client.getPlayerAssistant().movePlayer(player.getX(), player.getY(), player.getH());
client.faceUpdate(player.face); playerShop.faceUpdate(player.face);
client.turnPlayerTo(player.getX() + Misc.random(-1, 1), player.getY() + Misc.random(-1, 1)); playerShop.turnPlayerTo(player.getX() + Misc.random(-1, 1), player.getY() + Misc.random(-1, 1));
int i = 0; int i = 0;
// Remove all inventory items except money // Remove all inventory items except money
for (int item_id : client.playerItems) { for (int item_id : playerShop.playerItems) {
if (item_id > 0 && item_id != currency + 1){ if (item_id > 0 && item_id != currency + 1) {
client.playerItems[i] = 0; playerShop.playerItems[i] = 0;
client.playerItemsN[i] = 0; playerShop.playerItemsN[i] = 0;
} }
i++; i++;
} }
// Set bot to same level as player // Set bot to same level as player
i = 0; i = 0;
for (int level : player.playerLevel) { for (int level : player.playerLevel) {
client.playerLevel[i] = level; playerShop.playerLevel[i] = level;
client.getPlayerAssistant().refreshSkill(i); playerShop.getPlayerAssistant().refreshSkill(i);
i++; i++;
} }
// Take the appearance of the player // Take the appearance of the player
i = 0; i = 0;
for (int id : player.playerAppearance) { for (int id : player.playerAppearance) {
client.playerAppearance[i] = id; playerShop.playerAppearance[i] = id;
i++; i++;
} }
// Dress the bot the same as the player // Dress the bot the same as the player
i = 0; i = 0;
for (int item_id : player.playerEquipment) { for (int item_id : player.playerEquipment) {
client.playerEquipment[i] = item_id; playerShop.playerEquipment[i] = item_id;
client.playerEquipmentN[i] = 1; playerShop.playerEquipmentN[i] = 1;
i++; i++;
} }
} }
private static String getShopName(Player player){ private static String getShopName(String playerName) {
return "" + player.playerName; return "" + playerName;
} }
private static Client getPlayerShop(Player player){ private static Client getPlayerShop(String playerName) {
String shopName = getShopName(player); String shopName = getShopName(playerName);
for(Bot bot : botList) { for (Bot bot : botList) {
if(bot != null && bot.getBotClient() != null) { if (bot != null && bot.getBotClient() != null) {
Client botClient = bot.getBotClient(); Client botClient = bot.getBotClient();
if(botClient.playerName.equalsIgnoreCase(shopName)) { if (botClient.playerName.equalsIgnoreCase(shopName)) {
return botClient; return botClient;
} }
} }
@@ -102,11 +119,11 @@ public class BotHandler {
return null; return null;
} }
private static Client getPlayerShop(int shop_id){ private static Client getPlayerShop(int shop_id) {
for(Bot bot : botList) { for (Bot bot : botList) {
if(bot != null && bot.getBotClient() != null) { if (bot != null && bot.getBotClient() != null) {
Client botClient = bot.getBotClient(); Client botClient = bot.getBotClient();
if(botClient.shopId == shop_id) { if (botClient.shopId == shop_id) {
return botClient; return botClient;
} }
} }
@@ -115,40 +132,39 @@ public class BotHandler {
} }
public static void closeShop(Player player) { public static void closeShop(Player player) {
Client shop = getPlayerShop(player); Client shop = getPlayerShop(player.playerName);
if (shop == null) return; if (shop == null) return;
shop.getPlayerAssistant().movePlayer(0,0,0); shop.getPlayerAssistant().movePlayer(0, 0, 0);
new Thread(() -> { new Thread(() -> {
try { try {
Thread.sleep(500); Thread.sleep(500);
shop.disconnected = true; shop.disconnected = true;
shop.logout(true); shop.logout(true);
for (int index = 0; index < botList.size(); index++){ for (int index = 0; index < botList.size(); index++) {
if (botList.get(index).getBotClient().properName.equalsIgnoreCase(player.properName)) { if (botList.get(index).getBotClient().properName.equalsIgnoreCase(player.properName)) {
botList.remove(index); botList.remove(index);
return; return;
} }
index++; index++;
} }
} } catch (Exception e) {
catch (Exception e){
System.err.println(e); System.err.println(e);
} }
}).start(); }).start();
} }
public static void addCoins(int shop_id, int amount){ public static void addCoins(int shop_id, int amount) {
Client shop = getPlayerShop(shop_id); Client shop = getPlayerShop(shop_id);
if (shop == null) return; if (shop == null) return;
shop.getItemAssistant().addItem(currency, amount); shop.getItemAssistant().addItem(currency, amount);
} }
public static void takeCoins(Player player){ public static void takeCoins(Player player) {
if (!player.getItemAssistant().playerHasItem(currency) && player.getItemAssistant().freeSlots() <= 0) { if (!player.getItemAssistant().playerHasItem(currency) && player.getItemAssistant().freeSlots() <= 0) {
player.getPacketSender().sendMessage("You don't have enough space in your inventory."); player.getPacketSender().sendMessage("You don't have enough space in your inventory.");
return; return;
} }
Client shop = getPlayerShop(player); Client shop = getPlayerShop(player.playerName);
if (shop == null) return; if (shop == null) return;
if (!shop.getItemAssistant().playerHasItem(currency)) { if (!shop.getItemAssistant().playerHasItem(currency)) {
player.getPacketSender().sendMessage("There are no coins to collect."); player.getPacketSender().sendMessage("There are no coins to collect.");
@@ -160,19 +176,19 @@ public class BotHandler {
player.getPacketSender().sendMessage("You collected " + totalCoins + " coins."); player.getPacketSender().sendMessage("You collected " + totalCoins + " coins.");
} }
public static void addTobank(int shop_id, int item_id, int amount){ public static void addTobank(int shop_id, int item_id, int amount) {
Client shop = getPlayerShop(shop_id); Client shop = getPlayerShop(shop_id);
if (shop == null) return; if (shop == null) return;
shop.getItemAssistant().addItemToBank(item_id, amount); shop.getItemAssistant().addItemToBank(item_id, amount);
} }
public static void removeFrombank(int shop_id, int item_id, int amount){ public static void removeFrombank(int shop_id, int item_id, int amount) {
Client shop = getPlayerShop(shop_id); Client shop = getPlayerShop(shop_id);
if (shop == null) return; if (shop == null) return;
shop.getItemAssistant().removeItemFromBank(item_id, amount); shop.getItemAssistant().removeItemFromBank(item_id, amount);
} }
public static int getItemPrice(int shop_id, int item_id){ public static int getItemPrice(int shop_id, int item_id) {
item_id++; item_id++;
Client shop = getPlayerShop(shop_id); Client shop = getPlayerShop(shop_id);
if (shop == null) return 0; if (shop == null) return 0;
@@ -184,7 +200,7 @@ public class BotHandler {
return 0; return 0;
} }
public static void setPrice(int shop_id, int item_id, int amount){ public static void setPrice(int shop_id, int item_id, int amount) {
item_id++; item_id++;
Client shop = getPlayerShop(shop_id); Client shop = getPlayerShop(shop_id);
if (shop == null) return; if (shop == null) return;