'use strict'; /* * Arrival of a scan or an attack at a backbone landing site (hooked from combat.arriveSite, runs inside the `arrive` event). * A site is defended by a static value only: rules.backboneDefence(seed, x, y, world age, node level). Beating it as a member * of an alliance makes that alliance the holder (level, stockpile and a running build stay with the site). The site never * loses anything, so there is no loot and no raid; survivors drive home. Re-validates at arrival like every movement. */ const R = require('../../public/js/rules.js'); const D = require('../../public/js/data.js'); const U = require('./units'); const M = require('./movements'); const E = require('../endgame'); const { createReport } = require('./reports'); const { bounce, townBrief, opBrief } = require('./arrivals'); const SCAN_STRENGTH = D.ENDGAME.scanStrength; // a landing site counts as this many defending scanners function sendHome(ctx, mv, survivors) { if (U.isEmpty(survivors)) return null; const dest = U.homeTownFor(ctx.db, mv.ownerId, mv.fromTownId); if (!dest) return null; return M.launchReturn(ctx, { worldId: mv.worldId, ownerId: mv.ownerId, fromTownId: null, at: mv.to, to: dest, units: survivors, cargo: {}, parentId: mv.id, extra: {} }); } module.exports = function arriveLanding(ctx, mv, now, world, tile) { const { db } = ctx; if (mv.type !== 'scan' && mv.type !== 'attack') return bounce(ctx, mv, now, 'invalid_target'); const rows = E.ensureSites(db, world, now, ctx); const site = rows.find((r) => r.idx === tile.site); const attackerOp = U.getOperator(db, mv.ownerId); const origin = mv.fromTownId ? db.get('SELECT * FROM towns WHERE id = ?', mv.fromTownId) : null; const uid = U.userOfOperator(db, attackerOp.id); const where = { x: site.x, y: site.y, index: site.idx, siteId: site.id, level: site.level, holder: E.allianceInfo(db, site.holder_alliance_id) }; const staticDef = R.backboneDefence(world.seed, site.x, site.y, E.ageDays(world, now), site.level); if (mv.type === 'scan') { const sa = R.scanStrength(mv.units.port_scanner || 0, mv.extra.research); const res = R.resolveScan(sa, SCAN_STRENGTH); const loss = R.applyLoss(mv.units, res.lossFraction); const intel = res.success ? { defence: staticDef, level: site.level, holder: where.holder, build: site.build_to ? { toLevel: site.build_to, dueAt: site.build_due_at } : null } : null; createReport(ctx, { worldId: mv.worldId, type: 'scan', now, summary: { success: res.success, site: Object.assign({ type: 'backbone' }, where) }, data: { success: res.success, detail: res.detail, site: Object.assign({ type: 'backbone' }, where), scanners: { sent: mv.units, lost: loss.lost, survivors: loss.survivors }, intel }, recipients: [{ userId: uid, role: 'attacker' }], }); sendHome(ctx, mv, loss.survivors); return; } // attack: alliance members only, and not against the own alliance or a pact partner (the world may have changed since it left) if (!attackerOp.alliance_id) return bounce(ctx, mv, now, 'no_alliance'); try { E.denyIfFriendly(db, attackerOp, site); } catch (e) { return bounce(ctx, mv, now, e.details.reason || 'invalid_target'); } const b = R.resolveBattle({ kind: 'attack', attackers: { units: mv.units, research: mv.extra.research || {} }, defenders: [], firewall: 0, staticDefence: staticDef }); const survivors = Object.assign({}, b.attacker.survivors); const oldHolder = site.holder_alliance_id; if (b.attackerWins) E.transferSite(ctx, site, attackerOp, now); const ret = sendHome(ctx, mv, survivors); const data = { outcome: b.attackerWins ? 'attacker_won' : 'defender_won', arrivedAt: now, site: Object.assign({ type: 'backbone' }, where), origin: townBrief(origin), attacker: { operator: opBrief({ id: attackerOp.id, name: attackerOp.name, as_number: attackerOp.as_number }), alliance: E.allianceInfo(db, attackerOp.alliance_id), units: { sent: mv.units, lost: b.attacker.lost, survivors } }, power: { attack: b.A, defence: b.D }, taken: b.attackerWins, returnMovementId: ret ? ret.id : null, }; const recipients = [{ userId: uid, role: 'attacker' }]; if (oldHolder) for (const m of db.all('SELECT o.user_id AS uid FROM alliance_members am JOIN operators o ON o.id = am.operator_id WHERE am.alliance_id = ?', oldHolder)) recipients.push({ userId: m.uid, role: 'defender' }); createReport(ctx, { worldId: mv.worldId, type: 'landing', now, summary: { outcome: data.outcome, site: data.site, attacker: data.attacker.operator, taken: b.attackerWins }, data, recipients }); };