-
Notifications
You must be signed in to change notification settings - Fork 5
/
goods.go
64 lines (54 loc) · 1.7 KB
/
goods.go
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
package space_trader
import (
"encoding/json"
"fmt"
"strconv"
"github.com/HOWZ1T/space_trader/events"
"github.com/HOWZ1T/space_trader/models"
)
// Buys the specified good at the specified quantity for the specified ship.
// Returns ShipOrder
func (st *SpaceTrader) BuyGood(shipID string, good string, quantity int) (models.ShipOrder, error) {
uri := users + st.username + "/purchase-orders"
byts, err := json.Marshal(map[string]string{
"shipId": shipID,
"good": good,
"quantity": strconv.Itoa(quantity),
})
if err != nil {
return models.ShipOrder{}, err
}
var shipOrder models.ShipOrder
err = st.doShaped("POST", uri, string(byts), map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", st.token),
}, nil, &shipOrder)
if err != nil {
return models.ShipOrder{}, err
}
st.eventManager.Emit(events.ShipOrder{}.New(events.T_BUY, shipOrder))
return shipOrder, nil
}
// Sells the specified good at the specified quantity for the specified ship.
// Returns ShipOrder
func (st *SpaceTrader) SellGood(shipID string, good string, quantity int) (models.ShipOrder, error) {
uri := users + st.username + "/sell-orders"
byts, err := json.Marshal(map[string]string{
"shipId": shipID,
"good": good,
"quantity": strconv.Itoa(quantity),
})
if err != nil {
return models.ShipOrder{}, err
}
var shipOrder models.ShipOrder
err = st.doShaped("POST", uri, string(byts), map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", st.token),
}, nil, &shipOrder)
if err != nil {
return models.ShipOrder{}, err
}
st.eventManager.Emit(events.ShipOrder{}.New(events.T_SELL, shipOrder))
return shipOrder, nil
}