/
rps-funtions.go
167 lines (147 loc) · 4.07 KB
/
rps-funtions.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package rps
import (
"encoding/json"
"fmt"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/pyrousnet/pyrous-gobot/internal/cache"
"github.com/pyrousnet/pyrous-gobot/internal/comms"
"github.com/pyrousnet/pyrous-gobot/internal/mmclient"
"github.com/pyrousnet/pyrous-gobot/internal/settings"
"github.com/pyrousnet/pyrous-gobot/internal/users"
"reflect"
"strings"
)
type RPS struct {
RpsPlaying string `json:"rps-playing"`
Rps string `json:"rps"`
Name string `json:"name"`
}
type RpsBotGame struct {
body string
sender string
target string
mm *mmclient.MMClient
settings *settings.Settings
ReplyChannel *model.Channel
ResponseChannel chan comms.Response
Cache cache.Cache
}
func Playing(player RPS) bool {
return player.RpsPlaying != ""
}
func sameGame(player RPS, opponent RPS) bool {
return player.RpsPlaying == "" || player.RpsPlaying == opponent.RpsPlaying
}
func differentUser(player RPS, opponent RPS) bool {
return player.Name != opponent.Name
}
func FindApponent(event RpsBotGame, forPlayer RPS, chanId string) (RPS, error) {
us, ok, err := users.GetUsers(event.Cache)
rpsUs, ok, gPerr := getPlayers(us, chanId, event.Cache)
var opponent RPS
var found = false
if us == nil {
return RPS{}, fmt.Errorf("no opponent")
}
if ok {
for _, u := range rpsUs {
if Playing(u) && sameGame(forPlayer, u) && differentUser(forPlayer, u) {
opponent = u
found = true
}
}
} else {
return RPS{}, gPerr
}
if !found {
err = fmt.Errorf("no opponent")
}
return opponent, err
}
func GetWinner(player RPS, opponent RPS) ([]RPS, bool) {
var hasWinner bool = false
var winners []RPS
if player.Rps == "" {
return []RPS{}, false
}
if opponent.Rps == "" {
return []RPS{}, false
}
if player.Rps == opponent.Rps {
winners = append(winners, player)
winners = append(winners, opponent)
hasWinner = true
} else if strings.ToLower(player.Rps) == "rock" {
if strings.ToLower(opponent.Rps) == "paper" {
winners = append(winners, opponent)
hasWinner = true
} else if strings.ToLower(opponent.Rps) == "scissors" {
winners = append(winners, player)
hasWinner = true
}
} else if strings.ToLower(player.Rps) == "paper" {
if strings.ToLower(opponent.Rps) == "scissors" {
winners = append(winners, opponent)
hasWinner = true
} else if strings.ToLower(opponent.Rps) == "rock" {
winners = append(winners, player)
hasWinner = true
}
} else if strings.ToLower(player.Rps) == "scissors" {
if strings.ToLower(opponent.Rps) == "rock" {
winners = append(winners, opponent)
hasWinner = true
} else if strings.ToLower(opponent.Rps) == "paper" {
winners = append(winners, player)
hasWinner = true
}
}
return winners, hasWinner
}
func GetPlayer(player users.User, chanId string, c cache.Cache) (RPS, error) {
key := fmt.Sprintf("%s-%s-%s", "rps", player.Name, chanId)
var rps RPS
r, ok, _ := c.Get(key)
if ok {
if reflect.TypeOf(r).String() != "[]uint8" {
json.Unmarshal([]byte(r.(string)), &rps)
} else {
json.Unmarshal(r.([]byte), &rps)
}
return rps, nil
}
return RPS{Name: player.Name}, fmt.Errorf("not found")
}
func getPlayers(pUsers []users.User, chanId string, c cache.Cache) ([]RPS, bool, error) {
var rpsUsers []RPS
for _, u := range pUsers {
key := fmt.Sprintf("%s-%s-%s", "rps", u.Name, chanId)
r, ok, _ := c.Get(key)
var rps RPS
if ok {
if reflect.TypeOf(r).String() != "[]uint8" {
json.Unmarshal([]byte(r.(string)), &rps)
} else {
json.Unmarshal(r.([]byte), &rps)
}
rpsUsers = append(rpsUsers, rps)
}
}
if len(rpsUsers) == 0 {
return []RPS{}, false, nil
}
return rpsUsers, true, nil
}
func UpdateRps(playerRps RPS, chanId string, c cache.Cache) (RPS, error) {
key := fmt.Sprintf("%s-%s-%s", "rps", playerRps.Name, chanId)
p, _ := json.Marshal(playerRps)
c.Put(key, p)
return playerRps, nil
}
func DeleteRps(playerRps RPS, chanId string, c cache.Cache) {
key := fmt.Sprintf("%s-%s-%s", "rps", playerRps.Name, chanId)
c.Clean(key)
}
func DeleteGame(uuid string, c cache.Cache) {
c.Clean(uuid)
}