-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.go
106 lines (90 loc) · 2.4 KB
/
machine.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"clout/display"
"clout/keys"
"clout/models"
"clout/network"
"clout/session"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
func HandleMachine() {
id := argMap["id"]
if id == "" {
return
}
LoopThruAllComments(id)
}
func ThreeEmojiWorkForAmount(s string, amount int) bool {
sum := 0
for _, r := range []rune(s) {
hex := fmt.Sprintf("%x", r)
sum += int(SumIt(hex))
}
return sum == amount
}
func AwardMonies(to, coin string, amount int) {
mnemonic := session.ReadLoggedInWords()
if mnemonic == "" {
return
}
pub58, priv := keys.ComputeKeysFromSeed(session.SeedBytes(mnemonic))
creator := session.UsernameToPub58(coin)
amountInNanos := int64(amount)
bigString := network.SubmitTransferCoin(pub58, creator, to, amountInNanos)
var tx models.TxReady
json.Unmarshal([]byte(bigString), &tx)
jsonString := network.SubmitTx(tx.TransactionHex, priv)
if jsonString != "" {
fmt.Println("SubmitTx Success!")
}
}
func CheckForValidEntry(username, body string) {
tokens := strings.Split(body, "\n")
if len(tokens) > 1 {
fmt.Printf("%s %s\n", display.LeftAligned(username, 20), "ERR has newlines")
} else {
tokens = strings.Split(tokens[0], "=")
if len(tokens) == 2 {
three := strings.TrimSpace(tokens[0])
amount := strings.TrimSpace(tokens[1])
if strings.HasPrefix(amount, "$") {
intAmount, _ := strconv.Atoi(amount[1:])
if intAmount > 0 {
if ThreeEmojiWorkForAmount(three, intAmount) {
fmt.Printf("%s %s %d\n", display.LeftAligned(username, 20), "SUCCESS", intAmount)
AwardMonies(username, "TheClown", intAmount) // TODO fix amnount
} else {
fmt.Printf("%s %s\n", display.LeftAligned(username, 20), "emoji != amount")
}
} else {
fmt.Printf("%s %s\n", display.LeftAligned(username, 20), "bad amount")
}
} else {
fmt.Printf("%s %s\n", display.LeftAligned(username, 20), "missing $")
}
} else {
fmt.Printf("%s %s\n", display.LeftAligned(username, 20), "Bad =")
}
}
}
func LoopThruAllComments(key string) {
offset := int64(0)
pub58 := session.LoggedInPub58()
for {
js := network.GetSinglePostWithOffset(offset, pub58, key)
var ps models.PostStateless
json.Unmarshal([]byte(js), &ps)
if len(ps.PostFound.Comments) == 0 {
break
}
for _, p := range ps.PostFound.Comments {
CheckForValidEntry(p.ProfileEntryResponse.Username, p.Body)
}
offset += 20
time.Sleep(time.Second * 1)
}
}