This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
95 lines (79 loc) · 2.18 KB
/
redis.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
package main
import (
"encoding/json"
"fmt"
"github.com/simonz05/godis/redis"
"net/url"
"os"
)
type RedisClient struct {
c *redis.Client
rpcKey string
playersKey string
maintenanceKey string
protocolKey string
badProtocolKey string
motdKey string
}
func NewRedisConnection(prismId string) *RedisClient {
urlString := os.Getenv("PARTY_CLOUD_REDIS")
if urlString == "" {
urlString = "redis://localhost:6379/"
}
redisUrl, err := url.Parse(urlString)
if err != nil {
panic(err)
}
password := ""
if redisUrl.User != nil {
password, _ = redisUrl.User.Password()
}
return &RedisClient{
c: redis.New("tcp:"+redisUrl.Host, 0, password),
rpcKey: fmt.Sprintf("prism:%s:connection_request", prismId),
playersKey: fmt.Sprintf("prism:%s:players", prismId),
maintenanceKey: fmt.Sprintf("prism:%s:maintenance", prismId),
protocolKey: fmt.Sprintf("prism:%s:protocol", prismId),
badProtocolKey: fmt.Sprintf("prism:%s:bad_protocol", prismId),
motdKey: fmt.Sprintf("prism:%s:motd", prismId),
}
}
func (r *RedisClient) Quit() {
r.c.Quit()
}
func (r *RedisClient) ClearPlayerSet() {
r.c.Del(r.playersKey)
}
func (r *RedisClient) AddPlayer(username string) {
r.c.Sadd(r.playersKey, username)
}
func (r *RedisClient) RemovePlayer(username string) {
r.c.Srem(r.playersKey, username)
}
func (r *RedisClient) GetMaintenenceMsg() string {
msg, _ := r.c.Get(r.maintenanceKey)
return msg.String()
}
func (r *RedisClient) Protocol() string {
val, _ := r.c.Get(r.protocolKey)
return val.String()
}
func (r *RedisClient) BadProtocolMsg() string {
val, _ := r.c.Get(r.badProtocolKey)
return val.String()
}
func (r *RedisClient) Motd() string {
val, _ := r.c.Get(r.motdKey)
return val.String()
}
func (r *RedisClient) ReplyKey(username string) string {
return fmt.Sprintf("%s:%s", r.rpcKey, username)
}
func (r *RedisClient) PushConnectionReq(req *ConnectionRequest) {
req.ReplyKey = r.ReplyKey(req.Username)
reqJson, _ := json.Marshal(req)
r.c.Lpush("players:connection_request", reqJson)
}
func (r *RedisClient) ConnectionReqReply(username string) (*redis.Sub, error) {
return r.c.Subscribe(r.ReplyKey(username))
}