diff options
Diffstat (limited to '')
-rw-r--r-- | golang/pkg/agent/contracting.go | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/golang/pkg/agent/contracting.go b/golang/pkg/agent/contracting.go index 0f07170..4e476a6 100644 --- a/golang/pkg/agent/contracting.go +++ b/golang/pkg/agent/contracting.go @@ -11,39 +11,79 @@ func (a *agent) autoContracting(ship *model.Ship) { defer a.wg.Done() contracts, err := a.client.MyContracts() if err != nil { - a.sendShipError(fmt.Errorf("failed to get my contracts: %w", err), ship) + a.channel <- fmt.Errorf("failed to get my contracts with ship %s: %w", ship.Symbol, err) return } for _, contract := range contracts { - if contract.Fullfilled { + if contract.Fulfilled { continue } now := time.Now() if now.Before(contract.Terms.Deadline) { if err := a.runContract(&contract, ship); err != nil { - a.sendShipError(fmt.Errorf("failed to run contracts: %w", err), ship) + a.channel <- fmt.Errorf("failed to run contract %s with ship %s: %w", contract.Id, ship.Symbol, err) return } } } - a.sendShipError(fmt.Errorf("failed to run contracts: negotiating new contracts is not implemented yet"), ship) - // TODO - //for { - // negotiate - // runContract - //} + a.channel <- fmt.Errorf("negotiating new contracts is not implemented yet") + for { + contract, err := a.client.NegotiateContract(ship) + if err != nil { + a.channel <- fmt.Errorf("failed to negotiate contract: %w", err) + return + } + if err := a.runContract(contract, ship); err != nil { + a.channel <- fmt.Errorf("failed to run contract %s: %w", contract.Id, err) + return + } + } } func (a *agent) runContract(contract *model.Contract, ship *model.Ship) error { - if err := a.client.Accept(contract, a.db); err != nil { - return fmt.Errorf("failed to run contract: %w", err) + if err := a.client.Accept(contract); err != nil { + return fmt.Errorf("failed to accept contract: %w", err) } //slog.Info("running contract", "contract", contract, "ship", ship.Symbol) switch contract.Type { - // TODO - //case "PROCUREMENT": + case "PROCUREMENT": + if err := a.runProcurement(contract, ship); err != nil { + return fmt.Errorf("failed to run procurement: %w", err) + } default: - return fmt.Errorf("failed to run contract: handling contracts of type %s is not implemented yet", contract.Type) + return fmt.Errorf("handling contracts of type %s is not implemented yet", contract.Type) } return nil } + +func (a *agent) runProcurement(contract *model.Contract, ship *model.Ship) error { + if contract.Fulfilled { + return nil + } + deliver := contract.Terms.Deliver[0] + // make sure we are not carrying useless stuff + if err := a.sellEverythingExcept(ship, deliver.TradeSymbol); err != nil { + return fmt.Errorf("failed to sell everything except %s for ship %s: %w", deliver.TradeSymbol, ship.Symbol, err) + } + // procure the desired goods + if ship.Cargo.Units < min(deliver.UnitsRequired-deliver.UnitsFulfilled, ship.Cargo.Capacity) { + if err := a.buyTradeGood(ship, deliver.TradeSymbol); err != nil { + return fmt.Errorf("failed to buy trade good %s with ship %s: %w", deliver.TradeSymbol, ship.Symbol, err) + } + } + // deliver the goods + if err := a.client.Navigate(ship, deliver.DestinationSymbol); err != nil { + return fmt.Errorf("failed to navigate to %s: %w", deliver.DestinationSymbol, err) + } + if err := a.client.Deliver(contract, ship); err != nil { + return fmt.Errorf("failed to deliver: %w", err) + } + deliver = contract.Terms.Deliver[0] + if deliver.UnitsRequired == deliver.UnitsFulfilled { + if err := a.client.Fulfill(contract); err != nil { + return fmt.Errorf("failed to fulfill: %w", err) + } + return nil + } + return a.runProcurement(contract, ship) +} |