b47c47e969
Extract the FlatBuffers builders for the wire tables shared by the backend push encoder and the gateway edge transcoder — GameView, MoveRecord, StateView, AccountRef, Invitation and their nested rows — into a new scrabble/pkg/wire package. Both callers keep their local builder signatures (no call sites move) but now map their own source types (the backend's notify.* payloads and the decoded engine.MoveRecord; the gateway's backendclient.* REST DTOs) to neutral wire.* structs and delegate the construction to package wire, the single definition of the nested-table layout. Behaviour-preserving: the verified-identical field sets mean the wire bytes decode the same, and the notify + transcode round-trip tests pass unchanged. The fiddly Start/Add/End + reverse-prepend vector boilerplate now lives once; the two encode files shrink while pkg/wire carries the shared logic.
171 lines
5.7 KiB
Go
171 lines
5.7 KiB
Go
package transcode
|
|
|
|
import (
|
|
flatbuffers "github.com/google/flatbuffers/go"
|
|
|
|
"scrabble/gateway/internal/backendclient"
|
|
fb "scrabble/pkg/fbs/scrabblefb"
|
|
"scrabble/pkg/wire"
|
|
)
|
|
|
|
// Social encoders: friends, blocks, invitations, statistics and GCG. They follow
|
|
// encode.go's bottom-up rule (build every string/child vector before the table).
|
|
|
|
// buildAccountRef builds an AccountRef table and returns its offset.
|
|
func buildAccountRef(b *flatbuffers.Builder, r backendclient.AccountRefResp) flatbuffers.UOffsetT {
|
|
return wire.BuildAccountRef(b, wire.AccountRef{AccountID: r.AccountID, DisplayName: r.DisplayName})
|
|
}
|
|
|
|
// buildAccountRefVector builds a [AccountRef] vector using the table-specific
|
|
// StartXVector function and returns the vector offset.
|
|
func buildAccountRefVector(b *flatbuffers.Builder, refs []backendclient.AccountRefResp, start func(*flatbuffers.Builder, int) flatbuffers.UOffsetT) flatbuffers.UOffsetT {
|
|
offs := make([]flatbuffers.UOffsetT, len(refs))
|
|
for i, r := range refs {
|
|
offs[i] = buildAccountRef(b, r)
|
|
}
|
|
start(b, len(offs))
|
|
for i := len(offs) - 1; i >= 0; i-- {
|
|
b.PrependUOffsetT(offs[i])
|
|
}
|
|
return b.EndVector(len(offs))
|
|
}
|
|
|
|
// encodeFriendList builds a FriendList payload.
|
|
func encodeFriendList(r backendclient.FriendListResp) []byte {
|
|
b := flatbuffers.NewBuilder(256)
|
|
v := buildAccountRefVector(b, r.Friends, fb.FriendListStartFriendsVector)
|
|
fb.FriendListStart(b)
|
|
fb.FriendListAddFriends(b, v)
|
|
b.Finish(fb.FriendListEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeIncomingList builds an IncomingRequestList payload.
|
|
func encodeIncomingList(r backendclient.IncomingListResp) []byte {
|
|
b := flatbuffers.NewBuilder(256)
|
|
v := buildAccountRefVector(b, r.Requests, fb.IncomingRequestListStartRequestsVector)
|
|
fb.IncomingRequestListStart(b)
|
|
fb.IncomingRequestListAddRequests(b, v)
|
|
b.Finish(fb.IncomingRequestListEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeOutgoingList builds an OutgoingRequestList payload.
|
|
func encodeOutgoingList(r backendclient.OutgoingListResp) []byte {
|
|
b := flatbuffers.NewBuilder(256)
|
|
v := buildAccountRefVector(b, r.Requests, fb.OutgoingRequestListStartRequestsVector)
|
|
fb.OutgoingRequestListStart(b)
|
|
fb.OutgoingRequestListAddRequests(b, v)
|
|
b.Finish(fb.OutgoingRequestListEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeBlockList builds a BlockList payload.
|
|
func encodeBlockList(r backendclient.BlockListResp) []byte {
|
|
b := flatbuffers.NewBuilder(256)
|
|
v := buildAccountRefVector(b, r.Blocked, fb.BlockListStartBlockedVector)
|
|
fb.BlockListStart(b)
|
|
fb.BlockListAddBlocked(b, v)
|
|
b.Finish(fb.BlockListEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeFriendCode builds a FriendCode payload.
|
|
func encodeFriendCode(r backendclient.FriendCodeResp) []byte {
|
|
b := flatbuffers.NewBuilder(64)
|
|
code := b.CreateString(r.Code)
|
|
fb.FriendCodeStart(b)
|
|
fb.FriendCodeAddCode(b, code)
|
|
fb.FriendCodeAddExpiresAtUnix(b, r.ExpiresAtUnix)
|
|
b.Finish(fb.FriendCodeEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeRedeemResult builds a RedeemResult payload.
|
|
func encodeRedeemResult(r backendclient.RedeemResultResp) []byte {
|
|
b := flatbuffers.NewBuilder(128)
|
|
friend := buildAccountRef(b, r.Friend)
|
|
fb.RedeemResultStart(b)
|
|
fb.RedeemResultAddFriend(b, friend)
|
|
b.Finish(fb.RedeemResultEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeStats builds a StatsView payload.
|
|
func encodeStats(r backendclient.StatsResp) []byte {
|
|
b := flatbuffers.NewBuilder(64)
|
|
fb.StatsViewStart(b)
|
|
fb.StatsViewAddWins(b, int32(r.Wins))
|
|
fb.StatsViewAddLosses(b, int32(r.Losses))
|
|
fb.StatsViewAddDraws(b, int32(r.Draws))
|
|
fb.StatsViewAddMaxGamePoints(b, int32(r.MaxGamePoints))
|
|
fb.StatsViewAddMaxWordPoints(b, int32(r.MaxWordPoints))
|
|
b.Finish(fb.StatsViewEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// buildInvitation builds an Invitation table and returns its offset.
|
|
func buildInvitation(b *flatbuffers.Builder, inv backendclient.InvitationResp) flatbuffers.UOffsetT {
|
|
invitees := make([]wire.InvitationInvitee, len(inv.Invitees))
|
|
for i, iv := range inv.Invitees {
|
|
invitees[i] = wire.InvitationInvitee{
|
|
AccountID: iv.AccountID,
|
|
DisplayName: iv.DisplayName,
|
|
Seat: iv.Seat,
|
|
Response: iv.Response,
|
|
}
|
|
}
|
|
return wire.BuildInvitation(b, wire.Invitation{
|
|
ID: inv.ID,
|
|
Inviter: wire.AccountRef{AccountID: inv.Inviter.AccountID, DisplayName: inv.Inviter.DisplayName},
|
|
Invitees: invitees,
|
|
Variant: inv.Variant,
|
|
TurnTimeoutSecs: inv.TurnTimeoutSecs,
|
|
HintsAllowed: inv.HintsAllowed,
|
|
HintsPerPlayer: inv.HintsPerPlayer,
|
|
DropoutTiles: inv.DropoutTiles,
|
|
Status: inv.Status,
|
|
GameID: inv.GameID,
|
|
ExpiresAtUnix: inv.ExpiresAtUnix,
|
|
})
|
|
}
|
|
|
|
// encodeInvitation builds an Invitation payload.
|
|
func encodeInvitation(inv backendclient.InvitationResp) []byte {
|
|
b := flatbuffers.NewBuilder(512)
|
|
b.Finish(buildInvitation(b, inv))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeInvitationList builds an InvitationList payload.
|
|
func encodeInvitationList(r backendclient.InvitationListResp) []byte {
|
|
b := flatbuffers.NewBuilder(1024)
|
|
offs := make([]flatbuffers.UOffsetT, len(r.Invitations))
|
|
for i, inv := range r.Invitations {
|
|
offs[i] = buildInvitation(b, inv)
|
|
}
|
|
fb.InvitationListStartInvitationsVector(b, len(offs))
|
|
for i := len(offs) - 1; i >= 0; i-- {
|
|
b.PrependUOffsetT(offs[i])
|
|
}
|
|
v := b.EndVector(len(offs))
|
|
fb.InvitationListStart(b)
|
|
fb.InvitationListAddInvitations(b, v)
|
|
b.Finish(fb.InvitationListEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
// encodeGcg builds a GcgExport payload.
|
|
func encodeGcg(r backendclient.GcgResp) []byte {
|
|
b := flatbuffers.NewBuilder(1024)
|
|
gid := b.CreateString(r.GameID)
|
|
fn := b.CreateString(r.Filename)
|
|
content := b.CreateString(r.Content)
|
|
fb.GcgExportStart(b)
|
|
fb.GcgExportAddGameId(b, gid)
|
|
fb.GcgExportAddFilename(b, fn)
|
|
fb.GcgExportAddContent(b, content)
|
|
b.Finish(fb.GcgExportEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|