· 7 years ago · Jan 07, 2019, 09:44 PM
1/*
2 * Copyright (c) 2018, TheStonedTurtle <https://github.com/TheStonedTurtle>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25package net.runelite.client.plugins.loottracker;
26
27import java.io.BufferedWriter;
28import java.io.File;
29import java.io.FileWriter;
30import java.io.IOException;
31import java.sql.Connection;
32import java.sql.DriverManager;
33import java.sql.PreparedStatement;
34import java.sql.SQLException;
35import java.util.ArrayList;
36import java.util.Collection;
37import java.util.List;
38
39import lombok.extern.slf4j.Slf4j;
40import static net.runelite.client.RuneLite.RUNELITE_DIR;
41
42import net.runelite.api.ItemComposition;
43import net.runelite.client.callback.ClientThread;
44import net.runelite.client.game.ItemManager;
45import net.runelite.http.api.RuneLiteAPI;
46import net.runelite.http.api.loottracker.GameItem;
47import net.runelite.http.api.loottracker.LootRecord;
48
49import javax.inject.Inject;
50
51@Slf4j
52class LootRecordWriter
53{
54 @Inject
55 private ClientThread clientThread;
56
57 @Inject
58 private LootTrackerPlugin loottracker;
59
60 private static final String FILE_EXTENSION = ".log";
61 private static final File LOOT_RECORD_DIR = new File(RUNELITE_DIR, "loots");
62 final String url = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=" + System.getenv("DB_PASSWORD") + "sslmode=disable";
63
64 private long coins1 = 0;
65 private long coins2 = 0;
66 private long coins3 = 0;
67
68 private File playerFolder = LOOT_RECORD_DIR;
69 private final ItemManager itemManager;
70
71 @Inject
72 LootRecordWriter(final ItemManager itemManager)
73 {
74 this.itemManager = itemManager;
75 playerFolder.mkdir();
76 }
77
78 private static String asFileName(String name)
79 {
80 return name.toLowerCase().trim() + FILE_EXTENSION;
81 }
82
83 void setPlayerUsername(String username)
84 {
85 playerFolder = new File(LOOT_RECORD_DIR, username);
86 playerFolder.mkdir();
87 }
88
89 synchronized void addLootTrackerRecord(LootRecord rec)
90 {
91 File lootFile = new File(playerFolder, asFileName(rec.getEventId()));
92 String dataAsString = RuneLiteAPI.GSON.toJson(rec);
93
94 try
95 {
96 BufferedWriter file = new BufferedWriter(new FileWriter(String.valueOf(lootFile), true));
97 file.append(dataAsString);
98 file.newLine();
99 file.close();
100 }
101 catch (IOException ioe)
102 {
103 log.warn("Error writing loot data to file {}: {}", asFileName(rec.getEventId()), ioe.getMessage());
104 }
105 }
106
107 synchronized void addLootTrackerRecordToDB(Collection<LootRecord> records ) throws SQLException
108 {
109 for (LootRecord record : records)
110 {
111 String mob = record.getEventId();
112 LootTrackerItem[] drops = record.getDrops().stream().map(itemStack ->
113 buildLootTrackerItem(itemStack.getId(), itemStack.getQty())
114 ).toArray(LootTrackerItem[]::new);
115 }
116 String mob = "\"" + rec.getEventId() + "\"";
117 List<LootTrackerItem> drops = new ArrayList<>();
118// for (GameItem item : rec.getDrops())
119// {
120// int itemId = item.getId();
121// int itemQty = item.getQty();
122// if (itemId == 995)
123// {
124// coins1 += item.getQty(); //286,275
125// }
126// System.out.println(itemId + " | " + itemQty);
127// clientThread.invoke(() ->
128// {
129// LootTrackerItem drop = buildLootTrackerItem(itemId, itemQty);
130// if (itemId == 995)
131// {
132// coins2 += itemQty; //286,275
133// }
134// drops.add(drop);
135// });
136// }
137 for (GameItem item : rec.getDrops())
138 {
139 int itemId = item.getId();
140 int itemQty = item.getQty();
141 if (itemId == 995)
142 {
143 coins1 += item.getQty(); //286,275
144 }
145 System.out.println(itemId + " | " + itemQty);
146 LootTrackerItem drop = loottracker.buildLootTrackerItem(itemId, itemQty);
147 if (itemId == 995)
148 {
149 coins2 += itemQty; //286,275
150 }
151 drops.add(drop);
152 }
153 for (LootTrackerItem item : drops)
154 {
155 if (item.getId() == 995)
156 {
157 coins3 += item.getQuantity(); //273,959
158 }
159 }
160
161 String sql1 = "CREATE TABLE IF NOT EXISTS $tablename (index SERIAL, item_id INTEGER NOT NULL PRIMARY KEY,item_name TEXT NOT NULL, item_quantity INTEGER NOT NULL, item_price INTEGER NOT NULL, total_price BIGINT NOT NULL)".replace("$tablename", mob);
162 String sql2 = "INSERT INTO $tablename as tn (item_id, item_name, item_quantity, item_price, total_price) VALUES (?, ?, ?, ?, ?) ON CONFLICT (item_id) DO UPDATE SET item_quantity = tn.item_quantity + (?), item_price = (?), total_price = (tn.item_quantity +(?)) * (?)".replace("$tablename", mob);
163 System.out.println(sql1);
164 System.out.println(sql2);
165 Connection conn = DriverManager.getConnection(url);
166 PreparedStatement st = conn.prepareStatement(sql1);
167 PreparedStatement st2 = conn.prepareStatement(sql2);
168 st.executeUpdate();
169 System.out.println(drops);
170
171 for (LootTrackerItem drop : drops)
172 {
173 int item_id = drop.getId();
174 long total_price = drop.getPrice();
175 int item_quantity = drop.getQuantity();
176 int item_price = (int) (total_price/item_quantity);
177 String name = drop.getName();
178 System.out.println("Id: " + drop.getId() + "\tName: " + name + "\tQuantity: " + drop.getQuantity() + "\tIPrice: " + drop.getPrice()/drop.getQuantity() + "\tTPrice: " + drop.getPrice());
179
180 st2.setInt(1, item_id);
181 st2.setString(2, "\"" + name + "\"");
182 st2.setInt(3, item_quantity);
183 st2.setInt(4, item_price);
184 st2.setLong(5, total_price);
185 st2.setInt(6, item_quantity);
186 st2.setInt(7, item_price);
187 st2.setInt(8, item_quantity);
188 st2.setInt(9, item_price);
189 st2.executeUpdate();
190 }
191 st.close();
192 st2.close();
193 conn.close();
194 System.out.println("1: " + coins1 + "\n2: " + coins2 + "\n3: " + coins3);
195 }
196
197 private LootTrackerItem buildLootTrackerItem(int itemId, int quantity)
198 {
199 final ItemComposition itemComposition = itemManager.getItemComposition(itemId);
200 final int realItemId = itemComposition.getNote() != -1 ? itemComposition.getLinkedNoteId() : itemId;
201 final long price = (long) itemManager.getItemPrice(realItemId) * (long) quantity;
202 System.out.println(realItemId + " | " + itemComposition.getName() + " | " + quantity + " | " + price);
203 return new LootTrackerItem(
204 itemId,
205 itemComposition.getName(),
206 quantity,
207 price,
208 false);
209 }
210}