aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/mod/utils/AhUtils.java
blob: 6e6bdf52ab63407c8ff8b160e4d9c1a9d8b453fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *     Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 *     Copyright (C) 2021  cyoung06
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     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 <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.mod.utils;

import com.google.common.base.Throwables;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import kr.syeyoung.dungeonsguide.auth.AuthManager;
import kr.syeyoung.dungeonsguide.auth.AuthUtil;
import kr.syeyoung.dungeonsguide.mod.events.impl.AuthChangedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.*;

public class AhUtils {
    public static volatile Map<String, AuctionData> auctions = new HashMap<String, AuctionData>();

    static Logger logger = LogManager.getLogger("AhUtils");

    public static Timer timer = new Timer();

    public static int totalAuctions = 0;

    @SubscribeEvent
    public void onAuthChanged(AuthChangedEvent event) {
        if(AuthManager.getInstance().isPlebUser()){
            registerTimer();
        }
    }

    public static void registerTimer() {
        timer.schedule(new TimerTask() {
            public void run() {
                try {
                    AhUtils.loadAuctions();
                } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | KeyStoreException | IllegalBlockSizeException | KeyManagementException e) {
                    logger.error("Error loading auctions {}", String.valueOf(Throwables.getRootCause(e)));
                }
            }
        },  0L, 1800000L);
    }

    public static void loadAuctions() throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, BadPaddingException, KeyStoreException, IllegalBlockSizeException, KeyManagementException {
        try {

            Map<String, AuctionData> semi_auctions = new HashMap<String, AuctionData>();
            JsonElement object = AuthUtil.getJsonSecured("https://dungeons.guide/resource/keys");
            for (JsonElement element : object.getAsJsonArray()) {
                JsonObject object1 = element.getAsJsonObject();
                AuctionData auctionData = new AuctionData(object1.get("id").getAsString());
                auctionData.lowestBin = object1.get("lowestBin").getAsInt();
                auctionData.sellPrice = object1.get("sellPrice").getAsInt();
                auctionData.buyPrice = object1.get("buyPrice").getAsInt();
                semi_auctions.put(auctionData.id, auctionData);
            }
            auctions = semi_auctions;
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static class AuctionData {
        public String id;

        public long lowestBin = -1;

        public int sellPrice = -1;

        public int buyPrice = -1;

        public AuctionData(String id) {
            this.id = id;
        }
    }
}