Skip to content

Commit 32d7e1d

Browse files
committed
feat: service driven player transfers, send to hub when removed as a builder, drain map when the owner begins verification
1 parent fdb0172 commit 32d7e1d

6 files changed

Lines changed: 57 additions & 2 deletions

File tree

api/mapsV3/intnl/server_map.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"github.com/prometheus/client_golang/prometheus"
109
"math"
1110
"strconv"
1211
"strings"
1312
"time"
1413

14+
"github.com/prometheus/client_golang/prometheus"
15+
1516
"github.com/gohugoio/hashstructure"
1617
"github.com/hollow-cube/api-server/internal/mapdb"
1718
"github.com/hollow-cube/api-server/internal/object"
@@ -574,6 +575,18 @@ func (s *server) BeginMapVerification(ctx context.Context, request BeginMapVerif
574575
return nil, fmt.Errorf("failed to update map: %w", err)
575576
}
576577

578+
// If we are starting to verify, send out a drain to destory any possible editing worlds.
579+
// TODO: This doesnt really work. If there are people in the world it wont have time to save
580+
// and the verifying world will be out of date which would allow publishing impossible maps
581+
err = s.jetStream.PublishJSONAsync(ctx, model.MapUpdateMessage{
582+
Action: model.MapUpdate_Drain,
583+
ID: request.MapId,
584+
DrainReason: new("verification"),
585+
})
586+
if err != nil {
587+
return nil, fmt.Errorf("failed to publish map update: %w", err)
588+
}
589+
577590
return BeginMapVerification200Response{}, nil
578591
}
579592

@@ -1246,4 +1259,4 @@ func createMapSearchCacheKey(params *mapdb.SearchMapsParams) (string, bool) {
12461259

12471260
func init() {
12481261
prometheus.MustRegister(mapUploadDuration)
1249-
}
1262+
}

api/v4Internal/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
sessiondb "github.com/hollow-cube/api-server/internal/db"
55
"github.com/hollow-cube/api-server/internal/interaction"
66
"github.com/hollow-cube/api-server/internal/mapdb"
7+
"github.com/hollow-cube/api-server/internal/pkg/natsutil"
78
"github.com/hollow-cube/api-server/internal/pkg/notification"
89
"github.com/hollow-cube/api-server/internal/playerdb"
910
"go.uber.org/fx"
@@ -17,6 +18,7 @@ type ServerParams struct {
1718
PlayerStore *playerdb.Store
1819
MapStore *mapdb.Store
1920
SessionStore *sessiondb.Queries
21+
JetStream *natsutil.JetStreamWrapper
2022

2123
Notifications notification.Manager
2224
Interactions *interaction.Handler
@@ -28,6 +30,7 @@ type Server struct {
2830
playerStore *playerdb.Store
2931
mapStore *mapdb.Store
3032
sessionStore *sessiondb.Queries
33+
jetStream *natsutil.JetStreamWrapper
3134

3235
notifications notification.Manager
3336
interactions *interaction.Handler
@@ -39,6 +42,7 @@ func NewServer(p ServerParams) (*Server, error) {
3942
playerStore: p.PlayerStore,
4043
mapStore: p.MapStore,
4144
sessionStore: p.SessionStore,
45+
jetStream: p.JetStream,
4246
notifications: p.Notifications,
4347
interactions: p.Interactions,
4448
}

api/v4Internal/server_maps.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hollow-cube/api-server/internal/mapdb"
1010
"github.com/hollow-cube/api-server/internal/pkg/notification"
11+
"github.com/hollow-cube/api-server/internal/pkg/player"
1112
"github.com/hollow-cube/api-server/internal/playerdb"
1213
"github.com/hollow-cube/api-server/pkg/ox"
1314
)
@@ -127,6 +128,15 @@ func (s *Server) RemoveMapBuilder(ctx context.Context, request MapPlayerRequest)
127128
return fmt.Errorf("failed to claw back invite notifications: %w", err)
128129
}
129130

131+
// Transfer the player back to the hub in case they are currently in the map.
132+
// TODO: we should include a message here with the same message logic extracted from interactions
133+
err = s.jetStream.PublishJSONAsync(ctx, player.TransferMessage{
134+
PlayerId: request.PlayerID,
135+
From: &m.ID,
136+
To: "hub",
137+
State: "playing",
138+
})
139+
130140
return nil
131141
}
132142

internal/pkg/model/map.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ func (a MapUpdateAction) String() string {
149149
type MapUpdateMessage struct {
150150
Action MapUpdateAction `json:"action"`
151151
ID string `json:"id"`
152+
153+
DrainReason *string `json:"drainReason"`
152154
}
153155

154156
func (m MapUpdateMessage) Subject() string {

internal/pkg/player/model.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package player
2+
3+
type TransferMessage struct {
4+
PlayerId string `json:"playerId"`
5+
6+
From *string `json:"from"` // A map id, 'hub', or nil for anywhere
7+
To string `json:"to"` // A map id, or 'hub'
8+
State string `json:"state"` // playing/building/verifying/etc. Must be playing for hub.
9+
}
10+
11+
func (t TransferMessage) Subject() string {
12+
return "player.transfer"
13+
}

internal/pkg/player/tracker.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ func NewTracker(p TrackerParams) (*Tracker, error) {
6666
return nil, err
6767
}
6868

69+
err = p.JetStream.UpsertStream(context.Background(), jetstream.StreamConfig{
70+
Name: "PLAYER_MANAGEMENT",
71+
Subjects: []string{"player.>"},
72+
Retention: jetstream.LimitsPolicy,
73+
Storage: jetstream.FileStorage,
74+
MaxAge: 10 * time.Minute,
75+
Duplicates: 60 * time.Second,
76+
})
77+
if err != nil {
78+
reportCancel()
79+
return nil, err
80+
}
81+
6982
return &Tracker{
7083
queries: p.Queries,
7184
jetStream: p.JetStream,

0 commit comments

Comments
 (0)