book.gno
3.11 Kb · 138 lines
1package fish
2
3import (
4 "chain"
5 "strconv"
6)
7
8func List(cur realm, id int, priceUgnot int64) {
9 caller := requireUser(cur)
10 if priceUgnot <= 0 || priceUgnot > maxPrice {
11 panic("fish: price")
12 }
13 takeExact(0)
14 body := must(id)
15 if body.owner != caller {
16 panic("fish: owner")
17 }
18 guardMovable(body)
19 if body.mustHeal {
20 panic("fish: heal")
21 }
22 moveToken(caller, caller, self, id)
23 body.listed = true
24 body.seller = caller
25 body.price = priceUgnot
26 body.owner = self
27 chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
28}
29
30func UpdatePrice(cur realm, id int, priceUgnot int64) {
31 caller := requireUser(cur)
32 if priceUgnot <= 0 || priceUgnot > maxPrice {
33 panic("fish: price")
34 }
35 takeExact(0)
36 body := must(id)
37 if !body.listed {
38 panic("fish: listed")
39 }
40 if body.seller != caller {
41 panic("fish: owner")
42 }
43 body.price = priceUgnot
44 chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
45}
46
47func Cancel(cur realm, id int) {
48 caller := requireUser(cur)
49 takeExact(0)
50 body := must(id)
51 if !body.listed {
52 panic("fish: listed")
53 }
54 if body.seller != caller {
55 panic("fish: owner")
56 }
57 moveToken(self, self, body.seller, id)
58 body.owner = body.seller
59 body.seller = address("")
60 body.price = 0
61 body.listed = false
62 chain.Emit("Cancel", "id", strconv.Itoa(id))
63}
64
65func Buy(cur realm, id int) {
66 caller := requireUser(cur)
67 body := must(id)
68 if !body.listed {
69 panic("fish: listed")
70 }
71 if caller == body.seller {
72 panic("fish: self")
73 }
74 takeExact(body.price)
75 proto := body.price * protocolBps / 10000
76 royal := body.price * royaltyBps / 10000
77 if proto+royal > body.price {
78 panic("fish: fee")
79 }
80 sellerAmt := body.price - proto - royal
81 send(body.seller, sellerAmt, cur)
82 send(creator, royal, cur)
83 send(protocolSink, proto, cur)
84 moveToken(self, self, caller, id)
85 body.owner = caller
86 body.seller = address("")
87 body.price = 0
88 body.listed = false
89 chain.Emit("Buy", "id", strconv.Itoa(id), "buyer", caller.String())
90}
91
92func SellerOf(id int) address { return must(id).seller }
93func PriceOf(id int) int64 { return must(id).price }
94func ImageOf(id int) string { return must(id).image }
95func NameOf(id int) string { return must(id).name }
96
97func GrcOwnerOf(id int) string {
98 who, err := OwnerOf(strconv.Itoa(id))
99 if err != nil {
100 return ""
101 }
102 return who.String()
103}
104
105func ItemLine(id int) string {
106 body := must(id)
107 return strconv.Itoa(body.id) + "|" + body.name + "|" + addrStr(body.owner) + "|" + addrStr(body.seller) + "|" +
108 strconv.FormatInt(body.price, 10) + "|" + strconv.FormatBool(body.listed) + "|" + body.image + "|fish|" +
109 body.rarity + "|Size:" + body.size + ";Trait:" + body.trait + ";Species:" + body.species + ";HP:" + strconv.Itoa(body.hp) +
110 "|true|-1"
111}
112
113func ListOpen() string {
114 requireInit()
115 out := ""
116 n := 0
117 for _, body := range bodies {
118 if body == nil || !body.listed || body.burned {
119 continue
120 }
121 if n >= listCap {
122 break
123 }
124 if out != "" {
125 out += "\n"
126 }
127 out += ItemLine(body.id)
128 n++
129 }
130 return out
131}
132
133func addrStr(a address) string {
134 if !a.IsValid() {
135 return ""
136 }
137 return a.String()
138}