#Any idea how to run java-code from a Json/text file from a website to make a "Coupon-code-system"?

64 messages · Page 1 of 1 (latest)

formal tiger
#

originally this idea was for minecraft modding, but i made it more simpler, for general purposes

Basically i want to create a coupon system. On a website (bitbucket/gitlab) i store a file, in that file there is a coupon and an action.
If people type that coupon into the console/Scanner Class, some action should happen. And you should not be able to run the same coupon again on that machine.
The coupon should be re-usable for different machines/clients.

Any idea if this is possible, and how to start? And where/how should you store the "Coupon already used on this machine/client" information?

Example:

{
"coupon": "XMAS-2023",
"expiration-time-unix": "1703632534955",
"action": "System.out.println(\"Merry Christmas! You got a cookie\");"
}

Java 11 , no external library, if possible

grizzled lanternBOT
#

This post has been reserved for your question.

Hey @formal tiger! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

jaunty rampart
#

@formal tiger Ahhhhhh

#

okay so you want to serialize arbitrary java code to a json file

#

thats strange as hell, but aight

#

and you need to store info about whether a coupon is used

#

so the 2nd one is easier

#

if you trust your clients, just put that info in a file locally

#

if you don't trust your clients, you need a server which manages that data

#

the formats that make sense for local storage of that kind of data are

JSON
SQLITE
XML
Java Serialization Format

#

the last 2 don't require a library, but are unideal for other reasons

formal tiger
#

Json then, the google.gson library is already shipped with the java it looks like

#

and for the coupon, i think i can make that bit easier, cause expiration date is not realy needed, i can just remove the coupon from the file, when i wanna do it expire

#
    public static void main(String[] args) {
        // https://bytebucket.org/waifujanna/pixelmagics/wiki/codes.txt

        String urlline1 = "XMAS-2023,Merry Christmas!";
        String urlline2 = "NYEA-R024,Happy New Year!";

        Map<String,String> couponlist = Arrays.stream(urlline1.split(",")).map(k -> k.split(",")).collect(Collectors.toMap(k -> k[0],k -> k[0]));
       // couponlist.put(urlline1.replace(",(.*?)$",""),urlline1.replace("^(.*?),",""));
       // couponlist.put(urlline2.replace(",(.*?)$",""),urlline2.replace("^(.*?),",""));

        couponlist.entrySet().stream().forEach(System.out::println);

    }

i made bit testing, but my 2 ideas there not working, also i need to read the data from the url. somehow

#

wanted the String before the "," is the key, while the message is after the ",".

#

_
So i wanna get all the Lines from that url, in a Map , and then i use that map to make checks, and if that work i might wanna store the data to check for "already used" in Json.
But for now, i wanna just Read the lines, and split them in a Map

jaunty rampart
jaunty rampart
#

What are you trying to do here, exactly?

#

and who are you doing it for

#
  • any other constraints I should know about
formal tiger
#

yea, i made the "txt" file with the codes and hosted then as a hidden file under my wiki.

#

i do it for minecraft mod, but for simplification, it should work as a console/Scanner window for now.

jaunty rampart
#

i do it for minecraft mod
So that is where the 11 constraint comes in?

formal tiger
#

yes

jaunty rampart
#

and perhaps minecraft does include google's json library. that is possible

#

so okay you want people to enter a code on your mod

#

and then that will unlock something on their end

#

so for storing that they entered the code, thats kinda easy

#

i'm sure there is some minecraft specific way to do it

#

and if someone wants to hack this...meh?

#

the bigger issue is the "when code redeemed run arbitrary code" part

formal tiger
#
  • Java 11,

  • Constructor should load the lines from the text file from my URL.

  • The line should be spitted into a Map. where Key is the Code, and Value is the String message

  • Then if that works, i am already happy. Cause then i can work with that map, Check if persons message is containing in the map, and then i do actions.

  • Then Next Step is to, save the "user used this coupon" somewhere, so they cannot do it again.

Thats basically it.

jaunty rampart
#

which is actually a bit hard

formal tiger
#

forget the code execution. i just want a string for now. later i will change that String to some integers, and based on that integer i run stuff with a big switch/case statement. but that all for "after the map has done"

jaunty rampart
#

okay

#

so to get data from your url, use HttpClient

#

then once you have your file as a string

#

parse

formal tiger
#

does he go line by line, or just put the whole txt file in 1 string?

#

guess it works 😄

#

but curious if there is easyier way, then with the messy Arrays

jaunty rampart
#

i mean, you've invented CSV

#

the easier way would be a properties file

#

since its only key value pairs

#

1 sec i'll give you an example

#

@formal tiger here

#
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws IOException {
        String stuff = "XMAS-2023=00000001\n" +
                       "NYEA-R024=10000001\n" +
                       "GIVE-CODE=01000001\n" +
                       "TEST-TEST=00100001\n" +
                       "SAYH-ELLO=00000000";

        Properties properties = new Properties();
        properties.load(new StringReader(stuff));

        System.out.println(properties.get("XMAS-2023"));
    }
}
#

use = instead of , and you can just load it as a properties file

formal tiger
#

so i need save it as .prop?

jaunty rampart
#

nah

#

you can load from a string

#

like you see there

#

the file extension on github doesn't matter and you aren't saving it locally, i don't think

formal tiger
#

ok, i try that later, its 2:21 here i go sleep now, also need to think about a system, how i decode the 1 Byte data to actions. 256 switch statements should be enaugh for content 😂

mental patrol
#

Why do you need to do that?

#

Instead of serializing Java code, you might want to either have an enum (or a class list) identifying which code and a parameter list

#

e.g. you create an interface JsonAccessible (or similar) like that:

interface JsonAccessible{
    void run();
}

and in the JSON, you have a field containing the class name of the implementation and another field, e.g. args containing a list of constructor arguments

#

then you invoke the constructor using reflection

#

and call the run method