<commit_before>package com.skelril.skree.service.internal.market;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.skelril.nitro.Clause;
import com.skelril.nitro.probability.Probability;
import com.skelril.skree.SkreePlugin;
import com.skelril.skree.db.SQLHandle;
import com.skelril.skree.db.schema.tables.records.ItemDataRecord;
import com.skelril.skree.service.MarketService;
import com.skelril.skree.service.internal.market.deducer.DeducerOfSimpleType;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.scheduler.Task;

import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.skelril.skree.db.schema.tables.ItemAliases.ITEM_ALIASES;
import static com.skelril.skree.db.schema.tables.ItemData.ITEM_DATA;

public class MarketServiceImpl implements MarketService {

  private final Path marketValuesPath;

  public MarketServiceImpl(Path marketValuesPath) {
    this.marketValuesPath = marketValuesPath;
  }

  private void validateAlias(String alias) {
    if (!alias.matches(VALID_ALIAS_REGEX)) {
      throw new IllegalArgumentException("Aliases must match the pattern " + VALID_ALIAS_REGEX);
    }
  }

  @Override
  public Optional<ItemStack> getItem(String alias) {
    validateAlias(alias);

    try (Connection con = SQLHandle.getConnection()) {
      DSLContext create = DSL.using(con);
      Record2<String, String> result = create.select(ITEM_DATA.MC_ID, ITEM_DATA.VARIANT)
          .from(ITEM_DATA)
          .where(ITEM_DATA.ID.equal(
              create.select(ITEM_ALIASES.ITEM_ID)
                  .from(ITEM_ALIASES)
                  .where(ITEM_ALIASES.ALIAS.equal(alias.toLowerCase()))
              )
          ).fetchOne();
      return result == null ? Optional.empty() : Optional.ofNullable(
          getItemStack(
              new Clause<>(
                  result.getValue(ITEM_DATA.MC_ID),
                  result.getValue(ITEM_DATA.VARIANT)
              )
          )
      );
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return Optional.empty();
  }

  @Override
  public BigDecimal getSellFactor(BigDecimal buyPrice) {
    return new BigDecimal(0.7);
  }

  private BigDecimal getNewValue(BigDecimal baseValue) {
    double random = 1D / (5 + Probability.getRandom(5));

    BigDecimal multiplier = new BigDecimal(random);
    BigDecimal change = baseValue.multiply(multiplier);

    if (Probability.getChance(2)) {
      change = change.negate();
    }

    return baseValue.add(change);
  }

  private int getNewStock(BigDecimal baseValue, int existingStock) {
    int adjustedBaseValue;
    try {
      adjustedBaseValue = baseValue.round(MathContext.DECIMAL64).intValueExact();
    } catch (ArithmeticException ex) {
      adjustedBaseValue = Integer.MAX_VALUE;
    }

    adjustedBaseValue = (int) Math.sqrt(adjustedBaseValue);
    adjustedBaseValue = Math.max(3, adjustedBaseValue);

    int changeUnit = Math.max(100, (Probability.getRangedRandom(20, 200) - adjustedBaseValue) * 100);
    int baseChange = Probability.getChance(32) ? Probability.getRandom(20) * changeUnit : changeUnit;
    int change = Probability.getRandom(Probability.getRandom(Math.max(1, baseChange - adjustedBaseValue)));

    if (Probability.getChance(adjustedBaseValue)) {
      existingStock += change;
    } else {
      existingStock -= change;
    }

    return Math.max(0, existingStock);
  }

  private void writeMarketJson() {
    Task.builder().execute(() -> {
      try (Connection con = SQLHandle.getConnection()) {
        DSLContext create = DSL.using(con);
        Result<Record3<String, BigDecimal, Integer>> results = create.select(ITEM_ALIASES.ALIAS, ITEM_DATA.CURRENT_VALUE, ITEM_DATA.STOCK)
            .from(ITEM_DATA).join(ITEM_ALIASES).on(ITEM_ALIASES.ITEM_ID.equal(ITEM_DATA.ID))
            .fetch();

        JsonArray items = new JsonArray();

        results.forEach((record) -> {
          JsonObject entry = new JsonObject();

          entry.addProperty("name", record.get(ITEM_ALIASES.ALIAS));
          entry.addProperty("value", record.get(ITEM_DATA.CURRENT_VALUE));
          entry.addProperty("stock", record.get(ITEM_DATA.STOCK));

          items.add(entry);
        });<commit_msg>Dump the buy and sell price rather than the value<commit_after>package com.skelril.skree.service.internal.market;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.skelril.nitro.Clause;
import com.skelril.nitro.probability.Probability;
import com.skelril.skree.SkreePlugin;
import com.skelril.skree.db.SQLHandle;
import com.skelril.skree.db.schema.tables.records.ItemDataRecord;
import com.skelril.skree.service.MarketService;
import com.skelril.skree.service.internal.market.deducer.DeducerOfSimpleType;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.scheduler.Task;

import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.skelril.skree.db.schema.tables.ItemAliases.ITEM_ALIASES;
import static com.skelril.skree.db.schema.tables.ItemData.ITEM_DATA;

public class MarketServiceImpl implements MarketService {

  private final Path marketValuesPath;

  public MarketServiceImpl(Path marketValuesPath) {
    this.marketValuesPath = marketValuesPath;
  }

  private void validateAlias(String alias) {
    if (!alias.matches(VALID_ALIAS_REGEX)) {
      throw new IllegalArgumentException("Aliases must match the pattern " + VALID_ALIAS_REGEX);
    }
  }

  @Override
  public Optional<ItemStack> getItem(String alias) {
    validateAlias(alias);

    try (Connection con = SQLHandle.getConnection()) {
      DSLContext create = DSL.using(con);
      Record2<String, String> result = create.select(ITEM_DATA.MC_ID, ITEM_DATA.VARIANT)
          .from(ITEM_DATA)
          .where(ITEM_DATA.ID.equal(
              create.select(ITEM_ALIASES.ITEM_ID)
                  .from(ITEM_ALIASES)
                  .where(ITEM_ALIASES.ALIAS.equal(alias.toLowerCase()))
              )
          ).fetchOne();
      return result == null ? Optional.empty() : Optional.ofNullable(
          getItemStack(
              new Clause<>(
                  result.getValue(ITEM_DATA.MC_ID),
                  result.getValue(ITEM_DATA.VARIANT)
              )
          )
      );
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return Optional.empty();
  }

  @Override
  public BigDecimal getSellFactor(BigDecimal buyPrice) {
    return new BigDecimal(0.7);
  }

  private BigDecimal getNewValue(BigDecimal baseValue) {
    double random = 1D / (5 + Probability.getRandom(5));

    BigDecimal multiplier = new BigDecimal(random);
    BigDecimal change = baseValue.multiply(multiplier);

    if (Probability.getChance(2)) {
      change = change.negate();
    }

    return baseValue.add(change);
  }

  private int getNewStock(BigDecimal baseValue, int existingStock) {
    int adjustedBaseValue;
    try {
      adjustedBaseValue = baseValue.round(MathContext.DECIMAL64).intValueExact();
    } catch (ArithmeticException ex) {
      adjustedBaseValue = Integer.MAX_VALUE;
    }

    adjustedBaseValue = (int) Math.sqrt(adjustedBaseValue);
    adjustedBaseValue = Math.max(3, adjustedBaseValue);

    int changeUnit = Math.max(100, (Probability.getRangedRandom(20, 200) - adjustedBaseValue) * 100);
    int baseChange = Probability.getChance(32) ? Probability.getRandom(20) * changeUnit : changeUnit;
    int change = Probability.getRandom(Probability.getRandom(Math.max(1, baseChange - adjustedBaseValue)));

    if (Probability.getChance(adjustedBaseValue)) {
      existingStock += change;
    } else {
      existingStock -= change;
    }

    return Math.max(0, existingStock);
  }

  private void writeMarketJson() {
    Task.builder().execute(() -> {
      try (Connection con = SQLHandle.getConnection()) {
        DSLContext create = DSL.using(con);
        Result<Record3<String, BigDecimal, Integer>> results = create.select(ITEM_ALIASES.ALIAS, ITEM_DATA.CURRENT_VALUE, ITEM_DATA.STOCK)
            .from(ITEM_DATA).join(ITEM_ALIASES).on(ITEM_ALIASES.ITEM_ID.equal(ITEM_DATA.ID))
            .fetch();

        JsonArray items = new JsonArray();

        results.forEach((record) -> {
          JsonObject entry = new JsonObject();

          entry.addProperty("name", record.get(ITEM_ALIASES.ALIAS));
          entry.addProperty("buy_price", record.get(ITEM_DATA.CURRENT_VALUE));
          entry.addProperty("sell_price", record.get(ITEM_DATA.CURRENT_VALUE).multiply(getSellFactor(record.get(ITEM_DATA.CURRENT_VALUE))));
          entry.addProperty("stock", record.get(ITEM_DATA.STOCK));

          items.add(entry);
        });
{'fraction_non_alphanumeric': 0.11253362022592792, 'fraction_numerical': 0.006024744486282948, 'mean_word_length': 3.4649375600384245, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4233220', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2850, 'n_words': 491}