-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (108 loc) · 2.69 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/hugolgst/rich-go/client"
)
type rpcItem struct {
RpcAppId string `json:"rpcAppId"`
RpcState string `json:"rpcState"`
RpcDetails string `json:"rpcDetails"`
RpcLargeImg string `json:"rpcLargeImg"`
RpcLargeTxt string `json:"rpcLargeTxt"`
RpcSmallImg string `json:"rpcSmallImg"`
RpcSmallTxt string `json:"rpcSmallTxt"`
RpcPartyID string `json:"rpcPartyID"`
RpcPartyNum string `json:"rpcPartyNum"`
RpcPartyMax string `json:"rpcPartyMax"`
RpcStart string `json:"rpcStart"`
RpcEnd string `json:"rpcEnd"`
}
func homePage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("webapp.html"))
tmpl.Execute(w, nil)
}
// constructor function
func (rpcItem *rpcItem) fill_defaults() {
if rpcItem.RpcAppId == "" {
rpcItem.RpcAppId = "935173881256345751"
}
if rpcItem.RpcState == "" {
rpcItem.RpcState = "In Progress"
}
if rpcItem.RpcDetails == "" {
rpcItem.RpcDetails = "A game of chess"
}
if rpcItem.RpcLargeImg == "" {
rpcItem.RpcLargeImg = "https://i.imgur.com/BJjw1VN.png"
}
if rpcItem.RpcLargeTxt == "" {
rpcItem.RpcLargeTxt = "Chess"
}
if rpcItem.RpcSmallImg == "" {
rpcItem.RpcSmallImg = "https://i.imgur.com/BJjw1VN.png"
}
if rpcItem.RpcSmallTxt == "" {
rpcItem.RpcSmallTxt = "Chess"
}
if rpcItem.RpcPartyID == "" {
rpcItem.RpcPartyID = "party-id"
}
if rpcItem.RpcPartyNum == "" {
rpcItem.RpcPartyNum = "1"
}
if rpcItem.RpcPartyMax == "" {
rpcItem.RpcPartyMax = "2"
}
}
func setRPC(w http.ResponseWriter, r *http.Request) {
var newRPC rpcItem
err := json.NewDecoder(r.Body).Decode(&newRPC)
newRPC.fill_defaults()
bodyBytes, _ := ioutil.ReadAll(r.Body)
//print result
bodyString := string(bodyBytes)
fmt.Println(bodyString)
if err != nil {
panic(err)
}
go rpc(newRPC)
fmt.Fprintf(w, "RPC set")
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", homePage)
router.HandleFunc("/setRPC", setRPC).Methods("POST")
http.ListenAndServe(":8080", router)
}
func rpc(rpcItem rpcItem) {
err := client.Login(rpcItem.RpcAppId)
if err != nil {
panic(err)
}
party, _ := strconv.Atoi(rpcItem.RpcPartyNum)
max, _ := strconv.Atoi(rpcItem.RpcPartyMax)
err = client.SetActivity(client.Activity{
State: rpcItem.RpcState,
Details: rpcItem.RpcDetails,
LargeImage: rpcItem.RpcLargeImg,
LargeText: rpcItem.RpcLargeTxt,
SmallImage: rpcItem.RpcSmallImg,
SmallText: rpcItem.RpcSmallTxt,
Party: &client.Party{
ID: rpcItem.RpcPartyID,
Players: party,
MaxPlayers: max,
},
})
if err != nil {
panic(err)
}
time.Sleep(time.Second * 5)
}