private static void HandleAttack(object state) { if (celestials.Count == 0) { DateTime time = ogamedService.GetServerTime(); int interval = Helpers.CalcRandomInterval(IntervalType.SomeSeconds); DateTime newTime = time.AddMilliseconds(interval); defenderTimer.Change(interval, Timeout.Infinite); Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Unable to handle attack at the moment: bot is still getting account info."); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Next check at " + newTime.ToString()); return; } foreach (AttackerFleet attack in attacks) { if ((bool)settings.TelegramMessenger.Active && (bool)settings.Defender.TelegramMessenger.Active) { telegramMessenger.SendMessage("Player " + attack.AttackerName + " (" + attack.AttackerID + ") is attacking your planet " + attack.Destination.ToString() + " arriving at " + attack.ArrivalTime.ToString()); } Celestial attackedCelestial = celestials.SingleOrDefault(planet => planet.Coordinate.Galaxy == attack.Destination.Galaxy && planet.Coordinate.System == attack.Destination.System && planet.Coordinate.Position == attack.Destination.Position && planet.Coordinate.Type == attack.Destination.Type); attackedCelestial = UpdatePlanet(attackedCelestial, UpdateType.Ships); Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Player " + attack.AttackerName + " (" + attack.AttackerID + ") is attacking your planet " + attackedCelestial.ToString() + " arriving at " + attack.ArrivalTime.ToString()); if (settings.Defender.SpyAttacker.Active) { UpdateSlots(); if (slots.Free > 0) { if (attackedCelestial.Ships.EspionageProbe == 0) { Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Could not spy attacker: no probes available."); } else { try { Coordinate destination = attack.Origin; Ships ships = new Ships { EspionageProbe = (int)settings.Defender.SpyAttacker.Probes }; int fleetId = SendFleet(attackedCelestial, ships, destination, Missions.Spy, Speeds.HundredPercent); Fleet fleet = fleets.Single(fleet => fleet.ID == fleetId); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Spying attacker from " + attackedCelestial.ToString() + " to " + destination.ToString() + " with " + settings.Defender.SpyAttacker.Probes + " probes. Arrival at " + fleet.ArrivalTime.ToString()); } catch (Exception e) { Helpers.WriteLog(LogType.Error, LogSender.Defender, "Could not spy attacker: an exception has occurred: " + e.Message); } } } else { Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Could not send probes: no slots available."); } } if (settings.Defender.MessageAttacker.Active) { try { Random random = new Random(); string[] messages = settings.Defender.MessageAttacker.Messages; int messageIndex = random.Next(0, messages.Length); string message = messages[messageIndex]; ogamedService.SendMessage(attack.AttackerID, messages[messageIndex]); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Sent message \"" + message + "\" to attacker" + attack.AttackerName); } catch (Exception e) { Helpers.WriteLog(LogType.Error, LogSender.Defender, "Could not message attacker: an exception has occurred: " + e.Message); } } if (settings.Defender.Autofleet.Active) { UpdateSlots(); if (slots.Free > 0) { try { attackedCelestial = UpdatePlanet(attackedCelestial, UpdateType.Resources); Celestial destination; destination = celestials .Where(planet => planet.ID != attackedCelestial.ID) .Where(planet => planet.Coordinate.Type == (attackedCelestial.Coordinate.Type == Celestials.Moon ? Celestials.Planet : Celestials.Moon)) .Where(planet => Helpers.CalcDistance(attackedCelestial.Coordinate, planet.Coordinate, serverData) == 5) .FirstOrDefault() ?? new Celestial { ID = 0 }; if (destination.ID == 0) { destination = celestials .Where(planet => planet.ID != attackedCelestial.ID) .Where(planet => planet.Coordinate.Type == Celestials.Moon) .OrderBy(planet => Helpers.CalcDistance(attackedCelestial.Coordinate, planet.Coordinate, serverData)) .FirstOrDefault() ?? new Celestial { ID = 0 }; } if (destination.ID == 0) { destination = celestials .Where(planet => planet.ID != attackedCelestial.ID) .OrderBy(planet => Helpers.CalcDistance(attackedCelestial.Coordinate, planet.Coordinate, serverData)) .FirstOrDefault() ?? new Celestial { ID = 0 }; } if (destination.ID == 0) { Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Could not fleetsave: no valid destination exists"); DateTime time = ogamedService.GetServerTime(); int interval = Helpers.CalcRandomInterval(IntervalType.AFewSeconds); DateTime newTime = time.AddMilliseconds(interval); defenderTimer.Change(interval, Timeout.Infinite); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Next check at " + newTime.ToString()); return; } Ships ships = attackedCelestial.Ships; ships.Crawler = 0; ships.SolarSatellite = 0; if (ships.IsEmpty()) { Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Could not fleetsave: there is no fleet."); } else { Resources resources = Helpers.CalcMaxTransportableResources(ships, attackedCelestial.Resources, researches.HyperspaceTechnology, userInfo.Class); int fleetId = SendFleet(attackedCelestial, ships, destination.Coordinate, Missions.Deploy, Speeds.TenPercent, resources, true); if (fleetId != 0) { Fleet fleet = fleets.Single(fleet => fleet.ID == fleetId); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Fleetsaved to " + destination.ToString() + ". Arrival at " + fleet.ArrivalTime.ToString()); } } } catch (Exception e) { Helpers.WriteLog(LogType.Error, LogSender.Defender, "Could not fleetsave: an exception has occurred: " + e.Message); DateTime time = ogamedService.GetServerTime(); int interval = Helpers.CalcRandomInterval(IntervalType.AFewSeconds); DateTime newTime = time.AddMilliseconds(interval); defenderTimer.Change(interval, Timeout.Infinite); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Next check at " + newTime.ToString()); } } else { Helpers.WriteLog(LogType.Warning, LogSender.Defender, "Could not fleetsave: no slots available."); DateTime time = ogamedService.GetServerTime(); int interval = Helpers.CalcRandomInterval(IntervalType.AFewSeconds); DateTime newTime = time.AddMilliseconds(interval); defenderTimer.Change(interval, Timeout.Infinite); Helpers.WriteLog(LogType.Info, LogSender.Defender, "Next check at " + newTime.ToString()); } } } }