Jump to content

Coupon Manager


Recommended Posts

package l2s.gameserver.model.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import l2s.commons.util.Rnd;
import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.database.DatabaseFactory;
import l2s.gameserver.model.Player;
import l2s.gameserver.network.l2.s2c.SocialActionPacket;
import l2s.gameserver.templates.item.data.ItemData;
import l2s.gameserver.utils.ItemFunctions;

public class CouponManager
{
	private static final Logger LOG = LoggerFactory.getLogger(CouponManager.class);
	private static final CouponManager INSTANCE = new CouponManager();
	private static final String DEFAULT_OWNER = "NO_OWNER";
	private static final Map<String, String> COUPONS = new HashMap<>();

	public static CouponManager getInstance() {
		return INSTANCE;
	}

	public void load() {
		try (Connection con = DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement = con.prepareStatement("SELECT * FROM coupons")) {
			try (ResultSet rset = statement.executeQuery()) {
				while (rset.next())
					COUPONS.put(rset.getString("coupon_id").replace("-", "").toUpperCase(), rset.getString("coupon_owner"));
			}
		} catch (SQLException e) {
			LOG.error("Coupon issues: ", e);
		} finally {
			LOG.info("Coupons: loaded size: " + COUPONS.size());
		}
	}

	public synchronized final boolean tryUseCoupon(Player player, String id) {
		final String owner = COUPONS.get(id.toUpperCase());
		if (owner == null) {
			player.sendMessage("Invalid coupon code.");
			return false;
		}
		if (!owner.equals(DEFAULT_OWNER)) {
			player.sendMessage("This coupon code has expired or was already used.");
			return false;
		}
		final String acc = player.getAccountName().toLowerCase();
		final Collection<String> accounts = COUPONS.values();
		for (String account : accounts) {
			if (account.equals(acc)) {
				player.sendMessage("You may only claim 1 coupon per account.");
				return false;
			}
		}
		COUPONS.put(id, player.getAccountName());
		try (Connection con = DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement = con.prepareStatement("UPDATE coupons SET coupon_owner = ? WHERE coupon_id like ?")) {
			statement.setString(1, acc);
			statement.setString(2, id);
			statement.execute();
		} catch (SQLException e) {
			LOG.warn("Coupons: update failure: " + e);
			return false;
		}
		player.sendGfxMessage("Coupon accepted! Enjoy!");
		player.broadcastPacket(new SocialActionPacket(player.getObjectId(), SocialActionPacket.BOASTING));
		for (ItemData it : CouponHolder.getInstance().getRewards()) {
			if (it.getMinEnchant() <= 0)
				ItemFunctions.addItem(player, it.getId(), it.getCount(), true, "Coupon Reward");
			else
				ItemFunctions.addItem(player, it.getId(), it.getCount(), Rnd.get(it.getMinEnchant(), it.getMaxEnchant()), true, "Coupon Reward");
		}
		return true;
	}
}

 

package l2s.gameserver.data.xml.parser;

import java.io.File;
import org.dom4j.Element;

import l2s.commons.data.xml.AbstractParser;
import l2s.gameserver.Config;
import l2s.gameserver.ConfigSelector;
import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.model.entity.CouponManager;
import l2s.gameserver.templates.item.data.ItemData;

public final class CouponParser extends AbstractParser<CouponHolder>
{
	private static final CouponParser INSTANCE = new CouponParser();

	protected CouponParser() {
		super(CouponHolder.getInstance());
	}

	public static CouponParser getInstance() {
		return INSTANCE;
	}

	public void reload() {
		info("reload start...");
		getHolder().clear();
		load();
	}

	@Override
	protected void onParsed() {
		CouponManager.getInstance().load();
	}
	
	@Override
	public File getXMLPath() {
		return new File(Config.DATAPACK_ROOT, ConfigSelector.getDataFolder() + "/Coupons.xml");
	}

	@Override
	public String getDTDFileName() {
		return "Coupons.dtd";
	}

	@Override
	protected void readData(Element rootElement) {
		for (Element listSet : rootElement.elements("config"))
			getHolder().setLength(parseInt(listSet, "code_length"));
		for (Element listSet : rootElement.elements("rewards")) {
			for (Element paramsSet : listSet.elements("item")) {
				final String p = paramsSet.toString();
				if (p.contains("minEnchant") && !p.contains("maxEnchant"))
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count"), parseInt(paramsSet, "minEnchant")));
				else if (p.contains("minEnchant") && p.contains("maxEnchant"))
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count"), parseInt(paramsSet, "minEnchant", 0), parseInt(paramsSet, "maxEnchant")));
				else
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count")));
			}
		}
	}
}

 

package l2s.gameserver.data.xml.holder;

import java.util.ArrayList;
import java.util.List;
import l2s.commons.data.xml.AbstractHolder;
import l2s.gameserver.templates.item.data.ItemData;

public final class CouponHolder extends AbstractHolder
{
	private static final CouponHolder INSTANCE = new CouponHolder();
	private static int COUPON_LENGTH;
	private static final List<ItemData> REWARDS = new ArrayList<>();

	public static CouponHolder getInstance() {
		return INSTANCE;
	}

	public void addItem(ItemData item) {
		REWARDS.add(item);
	}

	public final List<ItemData> getRewards(){
		return REWARDS;
	}
	
	public void setLength(int i) {
		COUPON_LENGTH = i;
	}
	
	public final int getLength() {
		return COUPON_LENGTH;
	}

	@Override
	public int size() {
		return REWARDS.size();
	}

	@Override
	public void clear() {
		REWARDS.clear();
	}
}

 

Load on Gameserver or Parsers

CouponParser.getInstance().load();

 

Reload Parser (like with //reload)

CouponParser.getInstance().reload();

 

Coupons.dtd (for xml)

<!ELEMENT list (config|rewards)*>
	<!ELEMENT config (config)*>
		<!ATTLIST config
			code_length CDATA #REQUIRED>
	<!ELEMENT rewards (item)*>
		<!ELEMENT item (#PCDATA)>
		<!ATTLIST item
			id CDATA #REQUIRED
			count CDATA #REQUIRED
			minEnchant CDATA #IMPLIED
			maxEnchant CDATA #IMPLIED>

 

Coupons.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE list SYSTEM "Coupons.dtd">
<!-- minEnchant for min enchant value, maxEnchant for max enchant value -->
<list>
	<config code_length="20"/>
	<rewards>
		<item id="3031" count="2000" /> <!--2000 Spirit Ore -->
		<item id="90907" count="500" /> <!--500 Soulshot Ticket -->
		<item id="91641" count="100" /> <!--100 Sayha's Blessing -->
		<item id="91767" count="2" /> <!--2 x Enchant Kit: Talisman of Aden -->
		<item id="91864" count="2" /> <!--2 x Dragon Belt Pack -->
		<item id="91912" count="5000" /> <!--5000 Hp Potion (Exchangeable) -->
		<item id="92387" count="1" /> <!--+4 Black Ore Set -->
		<item id="93499" count="1" /> <!--Enchanted B-grade Weapon Coupon -->
		<item id="93500" count="5" /> <!--5 x Enchanted B-Grade Armor Coupon -->
		<item id="94269" count="50" /> <!--50 x Scroll: Boost Attack -->
		<item id="94271" count="50" /> <!--50 x Scroll: Boost Defense -->
	</rewards>
</list>

 

ItemData (if you dont have it)

package l2s.gameserver.templates.item.data;

/**
 * @author Bonux
 */
public class ItemData {
	private final int _id;
	private final long _count;
	private final int _minEnchant;
	private int _maxEnchant;
	private long _price;

	public ItemData(int id, long count) {
		_id = id;
		_count = count;
		_minEnchant = -1;
		_maxEnchant = 0;
		_price = 0;
	}

	public ItemData(int id, long count, int enchant) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchant;
		_price = 0;
	}
	
	public ItemData(int id, long count, int enchant, int enchantMax) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchantMax;
		_price = 0;
	}
	
	public ItemData(int id, long count, int enchant, int enchantMax, long price) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchantMax;
		_price = price;
	}
	
	public ItemData(int id, long count, long price) {
		_id = id;
		_count = count;
		_minEnchant = 0;
		_maxEnchant = 0;
		_price = price;
	}

	public int getId() {
		return _id;
	}

	public long getCount() {
		return _count;
	}

	public int getMinEnchant() {
		return _minEnchant;
	}
	
	public int getMaxEnchant() {
		return _maxEnchant;
	}
	
	public long getPrice() {
		return _price;
	}
}

 

ClientPacket (RequestPCCafeCouponUse)

package l2s.gameserver.network.l2.c2s;

import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.model.Player;
import l2s.gameserver.model.entity.CouponManager;

/**
 * format: chS
 */
public class RequestPCCafeCouponUse extends L2GameClientPacket
{
	// format: (ch)S
	private String _code;

	@Override
	protected boolean readImpl() {
		_code = readS();
		return true;
	}

	@Override
	protected void runImpl() {
		final Player activeChar = getClient().getActiveChar();
		if (activeChar == null)
			return;
		if (_code == null || _code.length() != CouponHolder.getInstance().getLength()) {
			activeChar.sendGfxMessage("Your code cannot be empty or less than " + CouponHolder.getInstance().getLength() + " letters/digits");
			activeChar.sendActionFailed();
			return;
		}
		if (CouponManager.getInstance().tryUseCoupon(activeChar, _code))
			activeChar.sendGfxMessage("Your coupon was activated, thank you for playing!");
	}
}

 

ServerPacket (ShowPCCafeCouponShowUI)

package l2s.gameserver.network.l2.s2c;

import l2s.gameserver.network.l2.ServerPacketOpcodes;

/**
 * Даный пакет показывает менюшку для ввода серийника. Можно что-то придумать :)
 * Format: (ch)
 */
public class ShowPCCafeCouponShowUI extends L2GameServerPacket {
	public static final ShowPCCafeCouponShowUI STATIC = new ShowPCCafeCouponShowUI();

	@Override
	protected final void writeImpl() {
		writeOpcode(ServerPacketOpcodes.ShowPCCafeCouponShowUI);
		//
	}
}

 

Open Coupon Window (from bypass)

player.sendPacket(ShowPCCafeCouponShowUI.STATIC);

 

This system is old and no longer useful (at least for me), maybe someone else can make use of it 🙂

Done on l2scripts and tested with protocol 311 (should work starting from h5 but i'm not sure)

 

SQL: https://pastebin.com/FTYqSAWx

 

Generator: https://www.voucherify.io/generator

Character Set: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

86DvcqL.png

 

QnCSzqb.pngjOPE86X.png

 

Edited by eMommy
  • Like 3
  • Upvote 1
Link to comment
Share on other sites

  • 7 months later...
  • 7 months later...
On 7/21/2022 at 12:49 AM, eMommy said:
package l2s.gameserver.model.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import l2s.commons.util.Rnd;
import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.database.DatabaseFactory;
import l2s.gameserver.model.Player;
import l2s.gameserver.network.l2.s2c.SocialActionPacket;
import l2s.gameserver.templates.item.data.ItemData;
import l2s.gameserver.utils.ItemFunctions;

public class CouponManager
{
	private static final Logger LOG = LoggerFactory.getLogger(CouponManager.class);
	private static final CouponManager INSTANCE = new CouponManager();
	private static final String DEFAULT_OWNER = "NO_OWNER";
	private static final Map<String, String> COUPONS = new HashMap<>();

	public static CouponManager getInstance() {
		return INSTANCE;
	}

	public void load() {
		try (Connection con = DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement = con.prepareStatement("SELECT * FROM coupons")) {
			try (ResultSet rset = statement.executeQuery()) {
				while (rset.next())
					COUPONS.put(rset.getString("coupon_id").replace("-", "").toUpperCase(), rset.getString("coupon_owner"));
			}
		} catch (SQLException e) {
			LOG.error("Coupon issues: ", e);
		} finally {
			LOG.info("Coupons: loaded size: " + COUPONS.size());
		}
	}

	public synchronized final boolean tryUseCoupon(Player player, String id) {
		final String owner = COUPONS.get(id.toUpperCase());
		if (owner == null) {
			player.sendMessage("Invalid coupon code.");
			return false;
		}
		if (!owner.equals(DEFAULT_OWNER)) {
			player.sendMessage("This coupon code has expired or was already used.");
			return false;
		}
		final String acc = player.getAccountName().toLowerCase();
		final Collection<String> accounts = COUPONS.values();
		for (String account : accounts) {
			if (account.equals(acc)) {
				player.sendMessage("You may only claim 1 coupon per account.");
				return false;
			}
		}
		COUPONS.put(id, player.getAccountName());
		try (Connection con = DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement = con.prepareStatement("UPDATE coupons SET coupon_owner = ? WHERE coupon_id like ?")) {
			statement.setString(1, acc);
			statement.setString(2, id);
			statement.execute();
		} catch (SQLException e) {
			LOG.warn("Coupons: update failure: " + e);
			return false;
		}
		player.sendGfxMessage("Coupon accepted! Enjoy!");
		player.broadcastPacket(new SocialActionPacket(player.getObjectId(), SocialActionPacket.BOASTING));
		for (ItemData it : CouponHolder.getInstance().getRewards()) {
			if (it.getMinEnchant() <= 0)
				ItemFunctions.addItem(player, it.getId(), it.getCount(), true, "Coupon Reward");
			else
				ItemFunctions.addItem(player, it.getId(), it.getCount(), Rnd.get(it.getMinEnchant(), it.getMaxEnchant()), true, "Coupon Reward");
		}
		return true;
	}
}

 

package l2s.gameserver.data.xml.parser;

import java.io.File;
import org.dom4j.Element;

import l2s.commons.data.xml.AbstractParser;
import l2s.gameserver.Config;
import l2s.gameserver.ConfigSelector;
import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.model.entity.CouponManager;
import l2s.gameserver.templates.item.data.ItemData;

public final class CouponParser extends AbstractParser<CouponHolder>
{
	private static final CouponParser INSTANCE = new CouponParser();

	protected CouponParser() {
		super(CouponHolder.getInstance());
	}

	public static CouponParser getInstance() {
		return INSTANCE;
	}

	public void reload() {
		info("reload start...");
		getHolder().clear();
		load();
	}

	@Override
	protected void onParsed() {
		CouponManager.getInstance().load();
	}
	
	@Override
	public File getXMLPath() {
		return new File(Config.DATAPACK_ROOT, ConfigSelector.getDataFolder() + "/Coupons.xml");
	}

	@Override
	public String getDTDFileName() {
		return "Coupons.dtd";
	}

	@Override
	protected void readData(Element rootElement) {
		for (Element listSet : rootElement.elements("config"))
			getHolder().setLength(parseInt(listSet, "code_length"));
		for (Element listSet : rootElement.elements("rewards")) {
			for (Element paramsSet : listSet.elements("item")) {
				final String p = paramsSet.toString();
				if (p.contains("minEnchant") && !p.contains("maxEnchant"))
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count"), parseInt(paramsSet, "minEnchant")));
				else if (p.contains("minEnchant") && p.contains("maxEnchant"))
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count"), parseInt(paramsSet, "minEnchant", 0), parseInt(paramsSet, "maxEnchant")));
				else
					getHolder().addItem(new ItemData(parseInt(paramsSet, "id"), parseInt(paramsSet, "count")));
			}
		}
	}
}

 

package l2s.gameserver.data.xml.holder;

import java.util.ArrayList;
import java.util.List;
import l2s.commons.data.xml.AbstractHolder;
import l2s.gameserver.templates.item.data.ItemData;

public final class CouponHolder extends AbstractHolder
{
	private static final CouponHolder INSTANCE = new CouponHolder();
	private static int COUPON_LENGTH;
	private static final List<ItemData> REWARDS = new ArrayList<>();

	public static CouponHolder getInstance() {
		return INSTANCE;
	}

	public void addItem(ItemData item) {
		REWARDS.add(item);
	}

	public final List<ItemData> getRewards(){
		return REWARDS;
	}
	
	public void setLength(int i) {
		COUPON_LENGTH = i;
	}
	
	public final int getLength() {
		return COUPON_LENGTH;
	}

	@Override
	public int size() {
		return REWARDS.size();
	}

	@Override
	public void clear() {
		REWARDS.clear();
	}
}

 

Load on Gameserver or Parsers

CouponParser.getInstance().load();

 

Reload Parser (like with //reload)

CouponParser.getInstance().reload();

 

Coupons.dtd (for xml)

<!ELEMENT list (config|rewards)*>
	<!ELEMENT config (config)*>
		<!ATTLIST config
			code_length CDATA #REQUIRED>
	<!ELEMENT rewards (item)*>
		<!ELEMENT item (#PCDATA)>
		<!ATTLIST item
			id CDATA #REQUIRED
			count CDATA #REQUIRED
			minEnchant CDATA #IMPLIED
			maxEnchant CDATA #IMPLIED>

 

Coupons.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE list SYSTEM "Coupons.dtd">
<!-- minEnchant for min enchant value, maxEnchant for max enchant value -->
<list>
	<config code_length="20"/>
	<rewards>
		<item id="3031" count="2000" /> <!--2000 Spirit Ore -->
		<item id="90907" count="500" /> <!--500 Soulshot Ticket -->
		<item id="91641" count="100" /> <!--100 Sayha's Blessing -->
		<item id="91767" count="2" /> <!--2 x Enchant Kit: Talisman of Aden -->
		<item id="91864" count="2" /> <!--2 x Dragon Belt Pack -->
		<item id="91912" count="5000" /> <!--5000 Hp Potion (Exchangeable) -->
		<item id="92387" count="1" /> <!--+4 Black Ore Set -->
		<item id="93499" count="1" /> <!--Enchanted B-grade Weapon Coupon -->
		<item id="93500" count="5" /> <!--5 x Enchanted B-Grade Armor Coupon -->
		<item id="94269" count="50" /> <!--50 x Scroll: Boost Attack -->
		<item id="94271" count="50" /> <!--50 x Scroll: Boost Defense -->
	</rewards>
</list>

 

ItemData (if you dont have it)

package l2s.gameserver.templates.item.data;

/**
 * @author Bonux
 */
public class ItemData {
	private final int _id;
	private final long _count;
	private final int _minEnchant;
	private int _maxEnchant;
	private long _price;

	public ItemData(int id, long count) {
		_id = id;
		_count = count;
		_minEnchant = -1;
		_maxEnchant = 0;
		_price = 0;
	}

	public ItemData(int id, long count, int enchant) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchant;
		_price = 0;
	}
	
	public ItemData(int id, long count, int enchant, int enchantMax) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchantMax;
		_price = 0;
	}
	
	public ItemData(int id, long count, int enchant, int enchantMax, long price) {
		_id = id;
		_count = count;
		_minEnchant = enchant;
		_maxEnchant = enchantMax;
		_price = price;
	}
	
	public ItemData(int id, long count, long price) {
		_id = id;
		_count = count;
		_minEnchant = 0;
		_maxEnchant = 0;
		_price = price;
	}

	public int getId() {
		return _id;
	}

	public long getCount() {
		return _count;
	}

	public int getMinEnchant() {
		return _minEnchant;
	}
	
	public int getMaxEnchant() {
		return _maxEnchant;
	}
	
	public long getPrice() {
		return _price;
	}
}

 

ClientPacket (RequestPCCafeCouponUse)

package l2s.gameserver.network.l2.c2s;

import l2s.gameserver.data.xml.holder.CouponHolder;
import l2s.gameserver.model.Player;
import l2s.gameserver.model.entity.CouponManager;

/**
 * format: chS
 */
public class RequestPCCafeCouponUse extends L2GameClientPacket
{
	// format: (ch)S
	private String _code;

	@Override
	protected boolean readImpl() {
		_code = readS();
		return true;
	}

	@Override
	protected void runImpl() {
		final Player activeChar = getClient().getActiveChar();
		if (activeChar == null)
			return;
		if (_code == null || _code.length() != CouponHolder.getInstance().getLength()) {
			activeChar.sendGfxMessage("Your code cannot be empty or less than " + CouponHolder.getInstance().getLength() + " letters/digits");
			activeChar.sendActionFailed();
			return;
		}
		if (CouponManager.getInstance().tryUseCoupon(activeChar, _code))
			activeChar.sendGfxMessage("Your coupon was activated, thank you for playing!");
	}
}

 

ServerPacket (ShowPCCafeCouponShowUI)

package l2s.gameserver.network.l2.s2c;

import l2s.gameserver.network.l2.ServerPacketOpcodes;

/**
 * Даный пакет показывает менюшку для ввода серийника. Можно что-то придумать :)
 * Format: (ch)
 */
public class ShowPCCafeCouponShowUI extends L2GameServerPacket {
	public static final ShowPCCafeCouponShowUI STATIC = new ShowPCCafeCouponShowUI();

	@Override
	protected final void writeImpl() {
		writeOpcode(ServerPacketOpcodes.ShowPCCafeCouponShowUI);
		//
	}
}

 

Open Coupon Window (from bypass)

player.sendPacket(ShowPCCafeCouponShowUI.STATIC);

 

This system is old and no longer useful (at least for me), maybe someone else can make use of it 🙂

Done on l2scripts and tested with protocol 311 (should work starting from h5 but i'm not sure)

 

SQL: https://pastebin.com/FTYqSAWx

 

Generator: https://www.voucherify.io/generator

Character Set: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

86DvcqL.png

 

QnCSzqb.pngjOPE86X.png

 

can u share the implement of this gfx chat packet?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...