summaryrefslogtreecommitdiff
path: root/golang/pkg/agent/contracting.go
blob: 44d7753158569b14a5ac323a6306f8847ce302e5 (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
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
}