Move MapleCodeCouponGenerator to main module

This commit is contained in:
P0nk
2021-07-10 20:17:47 +02:00
parent e93db8b48f
commit 45f3cfdfa6
4 changed files with 124 additions and 283 deletions

View File

@@ -1,78 +1,45 @@
/* package tools.mapletools;
This file is part of the HeavenMS MapleStory Server
Copyleft (L) 2016 - 2019 RonanLana
This program is free software: you can redistribute it and/or modify import tools.Pair;
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful, import java.io.*;
but WITHOUT ANY WARRANTY; without even the implied warranty of import java.nio.charset.StandardCharsets;
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the import java.sql.*;
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package maplecodecoupongenerator;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/** /**
*
* @author RonanLana * @author RonanLana
* <p>
This application parses the coupon descriptor XML file and automatically generates * This application parses the coupon descriptor XML file and automatically generates
code entries on the DB reflecting the descriptions found. Parse time relies on the * code entries on the DB reflecting the descriptions found. Parse time relies on the
sum of coupon codes created and amount of current codes on DB. * sum of coupon codes created and amount of current codes on DB.
* <p>
Estimated parse time: 2 minutes (for 100 code entries) * Estimated parse time: 2 minutes (for 100 code entries)
*/ */
public class MapleCodeCouponGenerator { public class CodeCouponGenerator {
static String host = "jdbc:mysql://localhost:3306/cosmic"; private static final File INPUT_FILE = ToolConstants.getInputFile("CouponCodes.img.xml");
static String driver = "com.mysql.jdbc.Driver"; private static final int INITIAL_STRING_LENGTH = 250;
static String username = "cosmic_server"; private static final Connection con = SimpleDatabaseConnection.getConnection();
static String password = "snailshell";
static Connection con = null; private static final List<CodeCouponDescriptor> activeCoupons = new ArrayList<>();
static InputStreamReader fileReader = null; private static final Set<String> usedCodes = new HashSet<>();
static BufferedReader bufferedReader = null; private static final List<Pair<Integer, Integer>> itemList = new ArrayList<>();
static String fileName = "lib/CouponCodes.img.xml"; private static BufferedReader bufferedReader = null;
static long currentTime; private static long currentTime;
private static String name;
static int initialStringLength = 250; private static boolean active;
private static int quantity;
static String name; private static int duration;
static boolean active; private static int maplePoint;
static int quantity, duration; private static int nxCredit;
static int maplePoint, nxCredit, nxPrepaid; private static int nxPrepaid;
private static Pair<Integer, Integer> item;
static List<Pair<Integer, Integer>> itemList = new ArrayList<>(); private static List<Integer> generatedKeys;
static Pair<Integer, Integer> item; private static byte status;
static List<CodeCouponDescriptor> activeCoupons = new ArrayList<>();
static List<Integer> generatedKeys;
static Set<String> usedCodes = new HashSet<>();
static byte status;
private static void resetCouponPackage() { private static void resetCouponPackage() {
name = null; name = null;
@@ -94,7 +61,7 @@ public class MapleCodeCouponGenerator {
i = token.indexOf("\"", i) + 1; //lower bound of the string i = token.indexOf("\"", i) + 1; //lower bound of the string
j = token.indexOf("\"", i); //upper bound j = token.indexOf("\"", i); //upper bound
dest = new char[initialStringLength]; dest = new char[INITIAL_STRING_LENGTH];
try { try {
token.getChars(i, j, dest, 0); token.getChars(i, j, dest, 0);
} catch (StringIndexOutOfBoundsException e) { } catch (StringIndexOutOfBoundsException e) {
@@ -105,12 +72,11 @@ public class MapleCodeCouponGenerator {
e.printStackTrace(); e.printStackTrace();
try { try {
Thread.sleep(100000000); Thread.sleep(100000000);
} catch (Exception ex) {} } catch (Exception ex) {
}
} }
return new String(dest).trim();
d = new String(dest);
return(d.trim());
} }
private static String getValue(String token) { private static String getValue(String token) {
@@ -122,37 +88,35 @@ public class MapleCodeCouponGenerator {
i = token.indexOf("\"", i) + 1; //lower bound of the string i = token.indexOf("\"", i) + 1; //lower bound of the string
j = token.indexOf("\"", i); //upper bound j = token.indexOf("\"", i); //upper bound
dest = new char[initialStringLength]; dest = new char[INITIAL_STRING_LENGTH];
token.getChars(i, j, dest, 0); token.getChars(i, j, dest, 0);
d = new String(dest); d = new String(dest);
return(d.trim()); return (d.trim());
} }
private static void forwardCursor(int st) { private static void forwardCursor(int st) {
String line = null; String line = null;
try { try {
while(status >= st && (line = bufferedReader.readLine()) != null) { while (status >= st && (line = bufferedReader.readLine()) != null) {
simpleToken(line); simpleToken(line);
} }
} } catch (Exception e) {
catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void simpleToken(String token) { private static void simpleToken(String token) {
if(token.contains("/imgdir")) { if (token.contains("/imgdir")) {
status -= 1; status -= 1;
} } else if (token.contains("imgdir")) {
else if(token.contains("imgdir")) {
status += 1; status += 1;
} }
} }
private static void translateToken(String token) { private static void translateToken(String token) {
if(token.contains("/imgdir")) { if (token.contains("/imgdir")) {
status -= 1; status -= 1;
if (status == 1) { if (status == 1) {
@@ -164,8 +128,7 @@ public class MapleCodeCouponGenerator {
} else if (status == 3) { } else if (status == 3) {
itemList.add(item); itemList.add(item);
} }
} } else if (token.contains("imgdir")) {
else if(token.contains("imgdir")) {
status += 1; status += 1;
if (status == 4) { if (status == 4) {
@@ -176,14 +139,13 @@ public class MapleCodeCouponGenerator {
System.out.println(" Reading coupon '" + d + "'"); System.out.println(" Reading coupon '" + d + "'");
name = d; name = d;
} }
} } else {
else {
String d = getName(token); String d = getName(token);
if (status == 2) { if (status == 2) {
switch (d) { switch (d) {
case "active": case "active":
if (Integer.valueOf(getValue(token)) == 0) { if (Integer.parseInt(getValue(token)) == 0) {
forwardCursor(status); forwardCursor(status);
resetCouponPackage(); resetCouponPackage();
} else { } else {
@@ -192,19 +154,19 @@ public class MapleCodeCouponGenerator {
break; break;
case "quantity": case "quantity":
quantity = Integer.valueOf(getValue(token)); quantity = Integer.parseInt(getValue(token));
break; break;
case "duration": case "duration":
duration = Integer.valueOf(getValue(token)); duration = Integer.parseInt(getValue(token));
break; break;
case "maplePoint": case "maplePoint":
maplePoint = Integer.valueOf(getValue(token)); maplePoint = Integer.parseInt(getValue(token));
break; break;
case "nxCredit": case "nxCredit":
nxCredit = Integer.valueOf(getValue(token)); nxCredit = Integer.parseInt(getValue(token));
break; break;
case "nxPrepaid": case "nxPrepaid":
nxPrepaid = Integer.valueOf(getValue(token)); nxPrepaid = Integer.parseInt(getValue(token));
break; break;
} }
} else if (status == 4) { } else if (status == 4) {
@@ -267,7 +229,9 @@ public class MapleCodeCouponGenerator {
} }
private static void commitCodeCouponDescription(CodeCouponDescriptor recipe) throws SQLException { private static void commitCodeCouponDescription(CodeCouponDescriptor recipe) throws SQLException {
if (recipe.quantity < 1) return; if (recipe.quantity < 1) {
return;
}
System.out.println(" Generating coupon '" + recipe.name + "'"); System.out.println(" Generating coupon '" + recipe.name + "'");
generatedKeys = null; generatedKeys = null;
@@ -275,7 +239,7 @@ public class MapleCodeCouponGenerator {
PreparedStatement ps = con.prepareStatement("INSERT IGNORE INTO `nxcode` (`code`, `expiration`) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS); PreparedStatement ps = con.prepareStatement("INSERT IGNORE INTO `nxcode` (`code`, `expiration`) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
ps.setLong(2, currentTime + ((long) recipe.duration * 60 * 60 * 1000)); ps.setLong(2, currentTime + ((long) recipe.duration * 60 * 60 * 1000));
for(int i = 0; i < recipe.quantity; i++) { for (int i = 0; i < recipe.quantity; i++) {
ps.setString(1, generateCouponCode()); ps.setString(1, generateCouponCode());
ps.addBatch(); ps.addBatch();
} }
@@ -303,7 +267,7 @@ public class MapleCodeCouponGenerator {
ps2.setInt(4, recipe.nxCredit); ps2.setInt(4, recipe.nxCredit);
List<Integer> keys = getGeneratedKeys(ps); List<Integer> keys = getGeneratedKeys(ps);
for(Integer codeid : keys) { for (Integer codeid : keys) {
ps2.setInt(1, codeid); ps2.setInt(1, codeid);
ps2.addBatch(); ps2.addBatch();
} }
@@ -314,7 +278,7 @@ public class MapleCodeCouponGenerator {
ps2.setInt(4, recipe.maplePoint); ps2.setInt(4, recipe.maplePoint);
List<Integer> keys = getGeneratedKeys(ps); List<Integer> keys = getGeneratedKeys(ps);
for(Integer codeid : keys) { for (Integer codeid : keys) {
ps2.setInt(1, codeid); ps2.setInt(1, codeid);
ps2.addBatch(); ps2.addBatch();
} }
@@ -325,7 +289,7 @@ public class MapleCodeCouponGenerator {
ps2.setInt(4, recipe.nxPrepaid); ps2.setInt(4, recipe.nxPrepaid);
List<Integer> keys = getGeneratedKeys(ps); List<Integer> keys = getGeneratedKeys(ps);
for(Integer codeid : keys) { for (Integer codeid : keys) {
ps2.setInt(1, codeid); ps2.setInt(1, codeid);
ps2.addBatch(); ps2.addBatch();
} }
@@ -346,8 +310,8 @@ public class MapleCodeCouponGenerator {
ps.close(); ps.close();
} }
private static void generateCodeCoupons(String fileName) throws IOException { private static void generateCodeCoupons(File file) throws IOException {
fileReader = new InputStreamReader(new FileInputStream(fileName), "UTF-8"); InputStreamReader fileReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
bufferedReader = new BufferedReader(fileReader); bufferedReader = new BufferedReader(fileReader);
resetCouponPackage(); resetCouponPackage();
@@ -355,7 +319,7 @@ public class MapleCodeCouponGenerator {
System.out.println("Reading XML coupon information..."); System.out.println("Reading XML coupon information...");
String line; String line;
while((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
translateToken(line); translateToken(line);
} }
@@ -364,9 +328,6 @@ public class MapleCodeCouponGenerator {
System.out.println(); System.out.println();
try { try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host, username, password);
System.out.println("Loading DB coupon codes..."); System.out.println("Loading DB coupon codes...");
loadUsedCouponCodes(); loadUsedCouponCodes();
System.out.println(); System.out.println();
@@ -380,16 +341,16 @@ public class MapleCodeCouponGenerator {
con.close(); con.close();
System.out.println("Done."); System.out.println("Done.");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
try { try {
generateCodeCoupons(fileName); generateCodeCoupons(INPUT_FILE);
} catch(IOException ex) { } catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'"); System.out.println("Error reading file '" + INPUT_FILE.getAbsolutePath() + "'");
} }
} }
} }

View File

@@ -1,121 +0,0 @@
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation. You may not use, modify
or distribute this program under any other version of the
GNU Affero General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package maplecodecoupongenerator;
/**
* Represents a pair of values.
*
* @author Frz
* @since Revision 333
* @version 1.0
*
* @param <E> The type of the left value.
* @param <F> The type of the right value.
*/
public class Pair<E, F> {
public E left;
public F right;
/**
* Class constructor - pairs two objects together.
*
* @param left The left object.
* @param right The right object.
*/
public Pair(E left, F right) {
this.left = left;
this.right = right;
}
/**
* Gets the left value.
*
* @return The left value.
*/
public E getLeft() {
return left;
}
/**
* Gets the right value.
*
* @return The right value.
*/
public F getRight() {
return right;
}
/**
* Turns the pair into a string.
*
* @return Each value of the pair as a string joined with a colon.
*/
@Override
public String toString() {
return left.toString() + ":" + right.toString();
}
/**
* Gets the hash code of this pair.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((left == null) ? 0 : left.hashCode());
result = prime * result + ((right == null) ? 0 : right.hashCode());
return result;
}
/**
* Checks to see if two pairs are equal.
*/
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair other = (Pair) obj;
if (left == null) {
if (other.left != null) {
return false;
}
} else if (!left.equals(other.left)) {
return false;
}
if (right == null) {
if (other.right != null) {
return false;
}
} else if (!right.equals(other.right)) {
return false;
}
return true;
}
}

View File

@@ -2,3 +2,4 @@
!*/ !*/
!/cosmetics/** !/cosmetics/**
!.gitignore !.gitignore
!CouponCodes.img.xml

View File

@@ -4,17 +4,17 @@
<short name="active" value="0"/> <short name="active" value="0"/>
<int name="quantity" value="100"/> <int name="quantity" value="100"/>
<int name="duration" value="1848"/> <int name="duration" value="1848"/>
<int name="maplePoint" value="7777"> <int name="maplePoint" value="7777"/>
<int name="nxCredit" value="1000"> <int name="nxCredit" value="1000"/>
<int name="nxPrepaid" value="100"> <int name="nxPrepaid" value="100"/>
<imgdir name="items"> <imgdir name="items">
<imgdir name="0"> <imgdir name="0">
<int name="id" value="4000001"> <int name="id" value="4000001"/>
<int name="count" value="100"> <int name="count" value="100"/>
</imgdir> </imgdir>
<imgdir name="1"> <imgdir name="1">
<int name="id" value="2030100"> <int name="id" value="2030100"/>
<int name="count" value="12"> <int name="count" value="12"/>
</imgdir> </imgdir>
</imgdir> </imgdir>
</imgdir> </imgdir>
@@ -22,17 +22,17 @@
<short name="active" value="1"/> <short name="active" value="1"/>
<int name="quantity" value="100"/> <int name="quantity" value="100"/>
<int name="duration" value="960"/> <int name="duration" value="960"/>
<int name="maplePoint" value="5000"> <int name="maplePoint" value="5000"/>
<int name="nxCredit" value="14"> <int name="nxCredit" value="14"/>
<int name="nxPrepaid" value="77"> <int name="nxPrepaid" value="77"/>
<imgdir name="items"> <imgdir name="items">
<imgdir name="0"> <imgdir name="0">
<int name="id" value="1302000"> <int name="id" value="1302000"/>
<int name="count" value="1"> <int name="count" value="1"/>
</imgdir> </imgdir>
<imgdir name="1"> <imgdir name="1">
<int name="id" value="5000001"> <int name="id" value="5000001"/>
<int name="count" value="20"> <int name="count" value="20"/>
</imgdir> </imgdir>
</imgdir> </imgdir>
</imgdir> </imgdir>