Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.0.2
github.com/aws/aws-sdk-go-v2/service/s3 v1.2.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/domino14/macondo v0.4.5-0.20210902160115-30135ceb7940
github.com/domino14/macondo v0.4.5-0.20220119032411-9abf690996ed
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v1.8.2
github.com/hashicorp/golang-lru v0.5.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc h1:VRRKCwnzq
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/domino14/macondo v0.4.5-0.20210902160115-30135ceb7940 h1:B5WWhRVvx94oli8ysjv+ud8T3VH5h62vVmZqhZBSfKU=
github.com/domino14/macondo v0.4.5-0.20210902160115-30135ceb7940/go.mod h1:PrS4/yiDGFM65QpBVqa6tJm5UJ7m5QU8BeGPqeRsv2Q=
github.com/domino14/macondo v0.4.5-0.20220119032411-9abf690996ed h1:qnAIho8ixOFOuR8uFoTHgdp1u7cb4poZL0bC/7WQBJ0=
github.com/domino14/macondo v0.4.5-0.20220119032411-9abf690996ed/go.mod h1:PrS4/yiDGFM65QpBVqa6tJm5UJ7m5QU8BeGPqeRsv2Q=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
Expand Down
18 changes: 10 additions & 8 deletions pkg/bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/domino14/liwords/pkg/config"
"github.com/domino14/liwords/pkg/entity"
"github.com/domino14/liwords/pkg/gameplay"
"github.com/domino14/liwords/pkg/ipc"
"github.com/domino14/liwords/pkg/mod"
"github.com/domino14/liwords/pkg/sessions"
"github.com/domino14/liwords/pkg/stats"
Expand Down Expand Up @@ -77,10 +78,10 @@ type Bus struct {
subscriptions []*nats.Subscription
subchans map[string]chan *nats.Msg

gameEventChan chan *entity.EventWrapper
tournamentEventChan chan *entity.EventWrapper
gameEventChan chan *ipc.EventWrapper
tournamentEventChan chan *ipc.EventWrapper

genericEventChan chan *entity.EventWrapper
genericEventChan chan *ipc.EventWrapper
}

func NewBus(cfg *config.Config, stores Stores, redisPool *redis.Pool) (*Bus, error) {
Expand All @@ -104,9 +105,9 @@ func NewBus(cfg *config.Config, stores Stores, redisPool *redis.Pool) (*Bus, err
subscriptions: []*nats.Subscription{},
subchans: map[string]chan *nats.Msg{},
config: cfg,
gameEventChan: make(chan *entity.EventWrapper, 64),
tournamentEventChan: make(chan *entity.EventWrapper, 64),
genericEventChan: make(chan *entity.EventWrapper, 64),
gameEventChan: make(chan *ipc.EventWrapper, 64),
tournamentEventChan: make(chan *ipc.EventWrapper, 64),
genericEventChan: make(chan *ipc.EventWrapper, 64),
redisPool: redisPool,
}
bus.gameStore.SetGameEventChan(bus.gameEventChan)
Expand Down Expand Up @@ -177,7 +178,7 @@ outerfor:
if len(subtopics) > 5 {
userID := subtopics[4]
connID := subtopics[5]
b.pubToConnectionID(connID, userID, entity.WrapEvent(&pb.ErrorMessage{Message: err.Error()},
b.pubToConnectionID(connID, userID, ipc.WrapEvent(&pb.ErrorMessage{Message: err.Error()},
pb.MessageType_ERROR_MESSAGE))
}
}
Expand All @@ -203,7 +204,7 @@ outerfor:
if err != nil {
log.Err(err).Msg("marshalling-error")
} else {
b.natsconn.Publish(msg.Reply, data)
msg.Respond(data)
}
}
}()
Expand Down Expand Up @@ -285,6 +286,7 @@ outerfor:
// Publish to the right realm.
// XXX: This is identical to tournamentEventChan. Should possibly merge.
log.Debug().Interface("msg", msg).Msg("generic event chan")

topics := msg.Audience()
data, err := msg.Serialize()
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/bus/gameplay.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func (b *Bus) handleBotMoveInternally(ctx context.Context, g *entity.Game, onTur
case *macondopb.BotResponse_Move:
timeRemaining := g.TimeRemaining(onTurn)

m := game.MoveFromEvent(r.Move, g.Alphabet(), g.Board())
m, err := game.MoveFromEvent(r.Move, g.Alphabet(), g.Board())
if err != nil {
log.Err(err).Msg("move-from-evt")
}
err = gameplay.PlayMove(ctx, g, b.gameStore, b.userStore, b.notorietyStore, b.listStatStore, b.tournamentStore, userID, onTurn, timeRemaining, m)
if err != nil {
log.Err(err).Msg("bot-cant-move-play-error")
Expand Down
163 changes: 0 additions & 163 deletions pkg/entity/event.go

This file was deleted.

32 changes: 0 additions & 32 deletions pkg/entity/event_test.go

This file was deleted.

96 changes: 96 additions & 0 deletions pkg/ipc/event_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ipc

import (
"strconv"
"strings"

"google.golang.org/protobuf/proto"

pb "github.com/domino14/liwords/rpc/api/proto/ipc"
"github.com/nats-io/nats.go"
)

type EventAudienceType string

const EventTransport = "pb"

const (
AudGame EventAudienceType = "game"
AudGameTV = "gametv"
AudUser = "user"
AudLobby = "lobby"
AudTournament = "tournament"
// AudChannel is used for a general channel.
AudChannel = "channel"
)

type Audience struct {
tp EventAudienceType
suffix string
}

func (a Audience) recipient(mt pb.MessageType) string {
// Create a NATS topic for this audience.
// It looks something like:
// user.pb.17.abcdefgh
// where 17 is the type of the message that is being sent.
var topic strings.Builder
topic.WriteString(string(a.tp))
topic.WriteString(".")
topic.WriteString(EventTransport)
topic.WriteString(".")
topic.WriteString(strconv.Itoa(int(mt)))
if a.suffix != "" {
topic.WriteString(".")
topic.WriteString(a.suffix)
}
return topic.String()
}

// EventWrapper wraps some useful things around messages.
type EventWrapper struct {
Type pb.MessageType
Event proto.Message

audience []Audience
}

func WrapEvent(event proto.Message, messageType pb.MessageType) *EventWrapper {
return &EventWrapper{
Type: messageType,
Event: event,
}
}

// AddAudience sets the audience(s) for this event. It is in the form of a NATS
// channel name. This is not required to be set in order to deliver a message,
// but certain functions will use it in the gameplay/entity module.
func (e *EventWrapper) AddAudience(audType EventAudienceType, suffix string) {
if e.audience == nil {
e.audience = []Audience{}
}
e.audience = append(e.audience, Audience{tp: audType, suffix: suffix})

}

// // SetAudience sets a single audience in string format.
// func (e *EventWrapper) SetAudience(audType Eve) {
// e.audience = []string{a}
// }

// Serialize just writes the event out as binary.
func (e *EventWrapper) Serialize() ([]byte, error) {
return proto.Marshal(e.Event)
}

func (e *EventWrapper) PublishToNATS(natsconn *nats.Conn) error {
data, err := e.Serialize()
if err != nil {
return err
}
for _, r := range e.audience {
// Generate topic name from recipient.
natsconn.Publish(r.recipient(e.Type), data)
}
return nil
}