summaryrefslogtreecommitdiff
path: root/golang/pkg/agent/contracting.go
blob: 6d67d6d54b6fb6d19559678f74e7ccbe592b7357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.channel <- fmt.Errorf("failed to get my contracts with ship %s: %w", ship.Symbol, err)
		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.channel <- fmt.Errorf("failed to run contract %s with ship %s: %w", contract.Id, ship.Symbol, err)
				return
			}
		}
	}
	a.channel <- fmt.Errorf("negotiating new contracts is not implemented yet")
	// 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 accept contract: %w", err)
	}
	//slog.Info("running contract", "contract", contract, "ship", ship.Symbol)
	switch contract.Type {
	case "PROCUREMENT":
		if err := a.runProcurement(contract, ship); err != nil {
			return fmt.Errorf("failed to run procurement: %w", err)
		}
	default:
		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 {
	deliveryCargo := contract.Terms.Deliver[0].TradeSymbol
	deliveryWaypoint, err := a.client.GetWaypoint(contract.Terms.Deliver[0].DestinationSymbol, a.db)
	if err != nil {
		return fmt.Errorf("failed to get delivery waypoint: %w", err)
	}
	for !contract.Fullfilled {
		_ = deliveryCargo
		_ = deliveryWaypoint
		return fmt.Errorf("not implemented")
	}
	return fmt.Errorf("not implemented")
}