import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class VoteAndBonusManager {
private static final String SERVER = "127.0.0.1";
private static final String DATABASE = "lin2db";
private static final String USERNAME = "user";
private static final String PASSWORD = "password";
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String CONNECTION_STRING = String.format(
"jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s",
SERVER, DATABASE, USERNAME, PASSWORD
);
private static final String VOTE_URL = "https://l2top.ru/editServ/?adminAct=lastVotes&uid=ВАШ_УИД&key=ВАШ_КЛЮЧ";
private static final String CACHED_IP = "127.0.0.1";
private static final int CACHED_PORT = 2012;
private static final int ITEM_ID = 57;
private static final int ITEM_COUNT = 1;
public static void main(String[] args) {
parseAndInsertVotes();
manageCharacterBonuses();
}
private static void parseAndInsertVotes() {
try (Connection conn = DriverManager.getConnection(CONNECTION_STRING);
PreparedStatement stmt = conn.prepareStatement("EXEC InsertVote @Date = ?, @char_name = ?")) {
VoteParser.parseAndInsertVotes(VOTE_URL, stmt);
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void manageCharacterBonuses() {
try (Connection conn = DriverManager.getConnection(CONNECTION_STRING);
PreparedStatement getCharNamesStmt = conn.prepareStatement("EXEC GetPendingBonusCharNames");
PreparedStatement getCharIdStmt = conn.prepareStatement("EXEC GetCharIdByCharName @char_name = ?");
PreparedStatement giveBonus = conn.prepareStatement("EXEC GiveBonus @Date = ?, @char_name = ?")) {
CacheD cacheD = new CacheD(CACHED_IP, CACHED_PORT);
try (ResultSet rs = getCharNamesStmt.executeQuery()) {
while (rs.next()) {
LocalDateTime date = LocalDateTime.parse(rs.getString(1), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String charName = rs.getString(2);
getCharIdStmt.setString(1, charName);
try (ResultSet charIdRs = getCharIdStmt.executeQuery()) {
if (charIdRs.next() && charIdRs.getInt(1) != 0) {
int charId = charIdRs.getInt(1);
if (cacheD.checkCharacterPacket(charId)) {
cacheD.kickCharacterPacket(charId);
Thread.sleep(1000);
cacheD.addItem2Packet(charId, 0, ITEM_ID, ITEM_COUNT, 0, 0, 0, 0);
} else {
cacheD.addItem2Packet(charId, 0, ITEM_ID, ITEM_COUNT, 0, 0, 0, 0);
}
}
}
giveBonus.setString(1, date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
giveBonus.setString(2, charName);
giveBonus.executeUpdate();
}
}
conn.commit();
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
}