<commit_before>package au.com.codeka.warworlds.server.events;

import java.sql.ResultSet;
import java.util.ArrayList;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.codeka.common.model.BaseColony;
import au.com.codeka.common.model.DesignKind;
import au.com.codeka.common.model.Simulation;
import au.com.codeka.common.protobuf.Messages;
import au.com.codeka.warworlds.server.Event;
import au.com.codeka.warworlds.server.RequestException;
import au.com.codeka.warworlds.server.ctrl.BuildingController;
import au.com.codeka.warworlds.server.ctrl.ColonyController;
import au.com.codeka.warworlds.server.ctrl.EmpireController;
import au.com.codeka.warworlds.server.ctrl.FleetController;
import au.com.codeka.warworlds.server.ctrl.RealmController;
import au.com.codeka.warworlds.server.ctrl.SituationReportController;
import au.com.codeka.warworlds.server.ctrl.StarController;
import au.com.codeka.warworlds.server.data.DB;
import au.com.codeka.warworlds.server.data.SqlStmt;
import au.com.codeka.warworlds.server.model.Colony;
import au.com.codeka.warworlds.server.model.Empire;
import au.com.codeka.warworlds.server.model.Fleet;
import au.com.codeka.warworlds.server.model.Star;

public class BuildCompleteEvent extends Event {
    private final Logger log = LoggerFactory.getLogger(BuildCompleteEvent.class);

    @Override
    public String getNextEventTimeSql() {
        return "SELECT MIN(end_time) FROM build_requests";
    }

    @Override
    public void process() {
        ArrayList<Integer> processedIDs = new ArrayList<Integer>();
        String sql = "SELECT id, star_id, colony_id, empire_id, existing_building_id," +
                           " design_kind, design_id, count" +
                    " FROM build_requests" +
                    " WHERE end_time < ?";
        try (SqlStmt stmt = DB.prepare(sql)) {
            stmt.setDateTime(1, DateTime.now().plusSeconds(10)); // anything in the next 10 seconds is a candidate
            ResultSet rs = stmt.select();
            while (rs.next()) {
                int id = rs.getInt(1);
                int starID = rs.getInt(2);
                int colonyID = rs.getInt(3);
                int empireID = rs.getInt(4);
                Integer existingBuildingID = rs.getInt(5);
                if (rs.wasNull()) {
                    existingBuildingID = null;
                }
                DesignKind designKind = DesignKind.fromNumber(rs.getInt(6));
                String designID = rs.getString(7);
                float count = rs.getFloat(8);

                Star star = new StarController().getStar(starID);
                Colony colony = null;
                for (BaseColony bc : star.getColonies()) {
                    if (((Colony) bc).getID() == colonyID) {
                        colony = (Colony) bc;
                    }
                }

                try {
                    processBuildRequest(id, star, colony, empireID, existingBuildingID, designKind, designID, count);
                } catch (Exception e) {
                    log.error("Error processing build-complete event!", e);
                }
                processedIDs.add(id);
            }
        } catch(Exception e) {
            log.error("Error processing build-complete event!", e);
            // TODO: errors?
        }

        if (processedIDs.isEmpty()) {
            return;
        }

        sql = "DELETE FROM build_requests WHERE id IN ";
        for (int i = 0; i < processedIDs.size(); i++) {
            if (i == 0) {
                sql += "(";
            } else {
                sql += ", ";
            }
            sql += processedIDs.get(i);
        }
        sql += ")";<commit_msg>If there's an error processing a single build event, don't let that stop us building more.<commit_after>package au.com.codeka.warworlds.server.events;

import java.sql.ResultSet;
import java.util.ArrayList;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.codeka.common.model.BaseColony;
import au.com.codeka.common.model.DesignKind;
import au.com.codeka.common.model.Simulation;
import au.com.codeka.common.protobuf.Messages;
import au.com.codeka.warworlds.server.Event;
import au.com.codeka.warworlds.server.RequestException;
import au.com.codeka.warworlds.server.ctrl.BuildingController;
import au.com.codeka.warworlds.server.ctrl.ColonyController;
import au.com.codeka.warworlds.server.ctrl.EmpireController;
import au.com.codeka.warworlds.server.ctrl.FleetController;
import au.com.codeka.warworlds.server.ctrl.RealmController;
import au.com.codeka.warworlds.server.ctrl.SituationReportController;
import au.com.codeka.warworlds.server.ctrl.StarController;
import au.com.codeka.warworlds.server.data.DB;
import au.com.codeka.warworlds.server.data.SqlStmt;
import au.com.codeka.warworlds.server.model.Colony;
import au.com.codeka.warworlds.server.model.Empire;
import au.com.codeka.warworlds.server.model.Fleet;
import au.com.codeka.warworlds.server.model.Star;

public class BuildCompleteEvent extends Event {
    private final Logger log = LoggerFactory.getLogger(BuildCompleteEvent.class);

    @Override
    public String getNextEventTimeSql() {
        return "SELECT MIN(end_time) FROM build_requests";
    }

    @Override
    public void process() {
        ArrayList<Integer> processedIDs = new ArrayList<Integer>();
        String sql = "SELECT id, star_id, colony_id, empire_id, existing_building_id," +
                           " design_kind, design_id, count" +
                    " FROM build_requests" +
                    " WHERE end_time < ?";
        try (SqlStmt stmt = DB.prepare(sql)) {
            stmt.setDateTime(1, DateTime.now().plusSeconds(10)); // anything in the next 10 seconds is a candidate
            ResultSet rs = stmt.select();
            while (rs.next()) {
                int id = rs.getInt(1);
                processedIDs.add(id);

                int starID = rs.getInt(2);
                int colonyID = rs.getInt(3);
                int empireID = rs.getInt(4);
                Integer existingBuildingID = rs.getInt(5);
                if (rs.wasNull()) {
                    existingBuildingID = null;
                }
                DesignKind designKind = DesignKind.fromNumber(rs.getInt(6));
                String designID = rs.getString(7);
                float count = rs.getFloat(8);

                Star star = new StarController().getStar(starID);
                Colony colony = null;
                for (BaseColony bc : star.getColonies()) {
                    if (((Colony) bc).getID() == colonyID) {
                        colony = (Colony) bc;
                    }
                }

                try {
                    processBuildRequest(id, star, colony, empireID, existingBuildingID, designKind, designID, count);
                } catch (Exception e) {
                    log.error("Error processing build-complete event!", e);
                }
            }
        } catch(Exception e) {
            log.error("Error processing build-complete event!", e);
            // TODO: errors?
        }

        if (processedIDs.isEmpty()) {
            return;
        }

        sql = "DELETE FROM build_requests WHERE id IN ";
        for (int i = 0; i < processedIDs.size(); i++) {
            if (i == 0) {
                sql += "(";
            } else {
                sql += ", ";
            }
            sql += processedIDs.get(i);
        }
        sql += ")";
{'fraction_non_alphanumeric': 0.11314255431902491, 'fraction_numerical': 0.0045045045045045045, 'mean_word_length': 2.0963904840032814, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '6037239', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2154, 'n_words': 437}