summaryrefslogtreecommitdiff
path: root/golang/pkg/agent/contracting.go
diff options
context:
space:
mode:
authorJulien Dessaux2025-02-14 00:14:15 +0100
committerJulien Dessaux2025-02-14 00:14:15 +0100
commitd97985a694b218713ddf63ed684b6a509f931f3b (patch)
tree84609f4e242419bf89301e0ead4927f450d7bfe5 /golang/pkg/agent/contracting.go
parent[golang] Bootstrap contracting and refactor the agent code (diff)
downloadspacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.tar.gz
spacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.tar.bz2
spacetraders-d97985a694b218713ddf63ed684b6a509f931f3b.zip
[golang] implement automation loop and add contract accepting
Diffstat (limited to '')
-rw-r--r--golang/pkg/agent/contracting.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/golang/pkg/agent/contracting.go b/golang/pkg/agent/contracting.go
new file mode 100644
index 0000000..44d7753
--- /dev/null
+++ b/golang/pkg/agent/contracting.go
@@ -0,0 +1,47 @@
+package agent
+
+import (
+ "fmt"
+ "time"
+
+ "git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
+)
+
+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)
+ return
+ }
+ for _, contract := range contracts {
+ if contract.Fullfilled {
+ 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)
+ return
+ }
+ }
+ }
+ // TODO
+ //for {
+ // negotiate
+ // runContract
+ //}
+}
+
+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)
+ }
+ switch contract.Type {
+ // TODO
+ //case "PROCUREMENT":
+ default:
+ return fmt.Errorf("failed to run contract: handling contracts of type %s is not implemented yet", contract.Type)
+ }
+ return nil
+}