Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xdnw/locutus
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Oct 26, 2024
2 parents da1aa52 + 3499c79 commit 67ed389
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,12 @@ public String auto(ValueStore store, @Me GuildDB db, SpreadSheet sheet, @Switch(
Map<String, Map.Entry<SelectionAlias, SheetTemplate>> customTabs = new LinkedHashMap<>();

Map<Integer, String> tabs = sheet.fetchTabs();


Map<String, SelectionAlias> customTabsToFetch = new LinkedHashMap<>();
for (Map.Entry<Integer, String> entry : tabs.entrySet()) {
String tabName = entry.getValue();
SelectionAlias selection;
SheetTemplate template = null;

try {
selection = SheetBindings.selectionAlias(true, manager, store, tabName);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -446,11 +447,19 @@ public String auto(ValueStore store, @Me GuildDB db, SpreadSheet sheet, @Switch(
continue;
}
}
customTabsToFetch.put(tabName, selection);
}

Map<String, List<List<Object>>> headerRows = sheet.fetchHeaderRows(customTabsToFetch.keySet());
for (Map.Entry<String, List<List<Object>>> entry : headerRows.entrySet()) {
String tabName = entry.getKey();
SelectionAlias selection = customTabsToFetch.get(tabName);

Placeholders ph = phMap.get(selection.getType());

List<List<Object>> row = sheet.fetchRange(tabName, "1:1");
List<List<Object>> row = entry.getValue();
List<String> header = row == null || row.isEmpty() ? null : row.get(0).stream().map(o -> o == null ? "" : o.toString()).toList();
SheetTemplate template = null;
if (header == null || header.isEmpty()) {
errors.add("Tab `" + tabName + "` has no header row");
} else {
Expand All @@ -460,9 +469,10 @@ public String auto(ValueStore store, @Me GuildDB db, SpreadSheet sheet, @Switch(
if (template == null) {
continue;
}

customTabs.put(tabName, Map.entry(selection, template));
}


if (customTabs.isEmpty()) {
errors.add("No tabs found. No tabs will be updated");
return "**Result**:\n- " + StringMan.join(errors, "\n- ");
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/link/locutus/discord/db/NationDB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package link.locutus.discord.db;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.politicsandwar.graphql.model.*;
import com.ptsmods.mysqlw.query.builder.SelectBuilder;
import com.ptsmods.mysqlw.table.ColumnType;
Expand Down Expand Up @@ -61,6 +63,7 @@
import link.locutus.discord.apiv1.enums.WarPolicy;
import link.locutus.discord.apiv1.enums.city.JavaCity;
import link.locutus.discord.util.scheduler.ThrowingTriConsumer;
import org.apache.commons.collections4.map.PassiveExpiringMap;
import org.apache.commons.lang3.tuple.Triple;

import java.io.IOException;
Expand Down Expand Up @@ -2888,23 +2891,36 @@ public Map<Long, Map<Continent, Double>> getRadiationByTurns() {
}
return result;
}

private final Cache<Long, Map<Continent, Double>> radiationCache =
CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();

public Map<Continent, Double> getRadiationByTurn(long turn) {
Map<Continent, Double> cachedResult = radiationCache.getIfPresent(turn);
if (cachedResult != null) {
return cachedResult;
}

Map<Continent, Double> result = new Object2ObjectOpenHashMap<>();
try (PreparedStatement stmt = getConnection().prepareStatement("SELECT continent, radiation FROM RADIATION_BY_TURN where turn = ?")) {
stmt.setLong(1,turn);
stmt.setLong(1, turn);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Continent continent = Continent.values[(rs.getInt(1))];
double radiation = rs.getInt(2) / 100d;
result.put(continent, radiation);
}
}
radiationCache.put(turn, result);
return result;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public synchronized void addRadiationByTurn(Continent continent, long turn, double radiation) {
try (PreparedStatement stmt = getConnection().prepareStatement("INSERT OR IGNORE INTO RADIATION_BY_TURN (continent, radiation, turn) VALUES (?, ?, ?)")) {
stmt.setInt(1, continent.ordinal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1539,10 +1539,18 @@ public TransferResult transferUnsafe2(Auth auth, NationOrAlliance receiver, Map<
}

private TransferResult createTransfer(PoliticsAndWarV3 api, NationOrAlliance receiver, Map<ResourceType, Double> transfer, String note) {
Bankrec result = api.transferFromBank(ResourceType.resourcesToArray(transfer), receiver, note);
double[] amt = ResourceType.fromApiV3(result, ResourceType.getBuffer());
String amtStr = ResourceType.resourcesToString(amt);
return new TransferResult(TransferStatus.SUCCESS, receiver, amt, note).addMessage("Success: " + amtStr);
try {
Bankrec result = api.transferFromBank(ResourceType.resourcesToArray(transfer), receiver, note);
double[] amt = ResourceType.fromApiV3(result, ResourceType.getBuffer());
String amtStr = ResourceType.resourcesToString(amt);
return new TransferResult(TransferStatus.SUCCESS, receiver, amt, note).addMessage("Success: " + amtStr);
} catch (HttpClientErrorException.Unauthorized e) {
return new TransferResult(TransferStatus.INVALID_API_KEY, receiver, transfer, note).addMessage("Invalid API key");
} catch (RuntimeException e) {
e.printStackTrace();
String msg = e.getMessage();
return categorize(receiver, transfer, note, StringMan.stripApiKey(msg));
}
}

public TransferResult transferUnsafe(Auth auth, NationOrAlliance receiver, Map<ResourceType, Double> transfer, String note) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static GoogleDoc create(String id, Docs api, String title) throws Genera
return doc;
}

private static final PassiveExpiringMap<String, GoogleDoc> CACHE = new PassiveExpiringMap<String, GoogleDoc>(5, TimeUnit.MINUTES);
private static final PassiveExpiringMap<String, GoogleDoc> CACHE = new PassiveExpiringMap<>(5, TimeUnit.MINUTES);

private Docs service;
private StringBuilder content;
Expand Down
72 changes: 52 additions & 20 deletions src/main/java/link/locutus/discord/util/sheet/SpreadSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.sheets.v4.model.AddSheetRequest;
import com.google.api.services.sheets.v4.model.CellData;
import com.google.api.services.sheets.v4.model.DeleteSheetRequest;
import com.google.api.services.sheets.v4.model.ExtendedValue;
import com.google.api.services.sheets.v4.model.Sheet;
import com.google.api.services.sheets.v4.model.SheetProperties;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.sheets.v4.model.*;
import link.locutus.discord.commands.manager.v2.command.IMessageBuilder;
import link.locutus.discord.commands.manager.v2.command.IMessageIO;
import link.locutus.discord.commands.manager.v2.impl.pw.binding.PWBindings;
Expand All @@ -30,19 +27,6 @@
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.ClearValuesRequest;
import com.google.api.services.sheets.v4.model.ClearValuesResponse;
import com.google.api.services.sheets.v4.model.GridCoordinate;
import com.google.api.services.sheets.v4.model.GridRange;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.RowData;
import com.google.api.services.sheets.v4.model.Spreadsheet;
import com.google.api.services.sheets.v4.model.SpreadsheetProperties;
import com.google.api.services.sheets.v4.model.UpdateCellsRequest;
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;
import com.opencsv.CSVWriter;
import link.locutus.discord.apiv1.enums.ResourceType;
import org.apache.commons.collections4.map.PassiveExpiringMap;
Expand Down Expand Up @@ -400,8 +384,19 @@ private SpreadSheet(String id, Sheets api) throws GeneralSecurityException, IOEx

private static Sheets getServiceAPI() throws GeneralSecurityException, IOException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
Credential credentials = getCredentials(HTTP_TRANSPORT);
HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(final HttpRequest httpRequest) throws IOException {
credentials.initialize(httpRequest);
// httpRequest.setConnectTimeout(10_000);
// httpRequest.setReadTimeout(10_000);
httpRequest.setNumberOfRetries(3);
}
};
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName(APPLICATION_NAME)
.setHttpRequestInitializer(requestInitializer)
.build();
}

Expand Down Expand Up @@ -955,6 +950,43 @@ public List<List<Object>> fetchRange(String tab, String range, Consumer<Sheets.S
}
}

public Map<String, List<List<Object>>> fetchHeaderRows(Set<String> tabNames) {
Map<String, List<List<Object>>> headers = new LinkedHashMap<>();
if (service == null) {
throw new IllegalStateException("Google Sheets service is not initialized.");
}

try {
// Prepare the ranges for batch request
List<String> ranges = tabNames.stream()
.map(tab -> tab + "!1:1")
.collect(Collectors.toList());

// Create the batch request
Sheets.Spreadsheets.Values.BatchGet request = service.spreadsheets().values().batchGet(spreadsheetId)
.setRanges(ranges);

// Execute the batch request
BatchGetValuesResponse response = request.execute();

// Process the response
List<ValueRange> valueRanges = response.getValueRanges();
for (ValueRange valueRange : valueRanges) {
String range = valueRange.getRange();
String tabName = range.split("!")[0];
if (tabName.startsWith("'") && tabName.endsWith("'")) {
tabName = tabName.substring(1, tabName.length() - 1);
}
List<List<Object>> values = valueRange.getValues();
headers.put(tabName, values != null ? values : Collections.emptyList());
}
} catch (IOException e) {
throw new RuntimeException("Failed to fetch header rows", e);
}

return headers;
}

public void updateClearCurrentTab() throws IOException {
if (this.defaultTab == null || this.defaultTab.isEmpty()) {
updateClearFirstTab();
Expand Down

0 comments on commit 67ed389

Please sign in to comment.