84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
package rooms
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Person struct {
|
|
PersonId int64
|
|
Name string
|
|
PasswordHash string
|
|
}
|
|
|
|
type Room struct {
|
|
RoomId int64
|
|
AdminIds []int64
|
|
Name string
|
|
PasswordHash string
|
|
Paricipants []Person
|
|
// TODO hands, for each type of hand?
|
|
// i guess participants order fixed for now?
|
|
// and i'll still need 'current' for each hand level
|
|
}
|
|
|
|
// well, it seems that i'd better do marshalling into bytes then
|
|
// see https://github.com/redis/go-redis/issues/2512
|
|
func (r *Room) MarshalBinary() (data []byte, err error) {
|
|
return json.Marshal(r)
|
|
}
|
|
func (r *Room) UnmarshalBinary(data []byte) error {
|
|
return json.Unmarshal(data, r)
|
|
}
|
|
|
|
|
|
// let's check whether it will be possible to save nested structs
|
|
|
|
var ctx = context.Background()
|
|
|
|
type RedisRM struct {
|
|
Rdb *redis.Client
|
|
}
|
|
|
|
func (redisRM RedisRM) Test() {
|
|
testRoom := Room{
|
|
RoomId: 1234,
|
|
AdminIds: []int64{1111, 2222},
|
|
Name: "test room",
|
|
PasswordHash: "abcde",
|
|
Paricipants: []Person{
|
|
{PersonId: 1111,
|
|
Name: "admin1",
|
|
PasswordHash: "abcde1",
|
|
},
|
|
{
|
|
PersonId: 2222,
|
|
Name: "admin2",
|
|
PasswordHash: "abcdeee",
|
|
},
|
|
{
|
|
PersonId: 3333,
|
|
Name: "nonadmin yay",
|
|
PasswordHash: "abcdee123",
|
|
},
|
|
},
|
|
}
|
|
err := redisRM.Rdb.Set(ctx, "room:my-room-1", &testRoom, 0).Err()
|
|
if err != nil {
|
|
log.Printf("error writing room: %s", err)
|
|
} else {
|
|
log.Printf("successfully written room %d", testRoom.RoomId)
|
|
}
|
|
|
|
var readRoom Room
|
|
err = redisRM.Rdb.Get(ctx, "room:my-room-1").Scan(&readRoom)
|
|
if err != nil {
|
|
log.Printf("error reading room %s", err)
|
|
} else {
|
|
log.Printf("successfully read room %+v", readRoom)
|
|
}
|
|
}
|