Skip to content

Commit

Permalink
Merge pull request #12 from ascenmmo/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
ascenmmo authored Jan 9, 2025
2 parents 32e9ac0 + 673bd19 commit edad2dd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
22 changes: 16 additions & 6 deletions internal/service/dev_tools/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *connections) CreateRoom(ctx context.Context, token string, name string)
GameID: info.GameID,
CreatorID: info.UserID,
Connections: []uuid.UUID{info.UserID},
CreatedAt: time.Now().Unix(),
CreatedAt: time.Now(),
}
room.RoomCode = utills.GenerateRoomCode(room.ID)

Expand Down Expand Up @@ -235,20 +235,30 @@ func (c *connections) GetRoomsConnectionUrls(ctx context.Context, token string)
}
}()

if len(room.ExistsServers) != 0 {
return nil, nil
}

uniqueServerType := make(map[string]types.ConnectionServer)
uniqueServers := make(map[uuid.UUID]types.Server)
uniqueAllTypes := make(map[string]struct{})

for _, server := range servers {
connNum, exists, err := server.GetConnectionsNum(ctx, token)
if err != nil {
c.logger.Error().Err(err).Msg("server error create room")
uniqueServers[server.ID] = server
}

for _, server := range uniqueServers {
if _, ok := uniqueServerType[server.ServerType]; ok {
continue
}

if server.MaxConnections < connNum+len(room.Connections) && exists {
connNum, exists, err := server.GetConnectionsNum(ctx, token)
if err != nil {
c.logger.Error().Err(err).Msg("server error create room")
continue
}

if _, ok := uniqueServerType[server.ServerType]; ok {
if server.MaxConnections < connNum+len(room.Connections) || !exists {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/start/multiplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Multiplayer(logger zerolog.Logger) {
devToolsServerService := devtools.NewServerService(accessGameService, gameStorage, serverStorage, token, &logger)
devToolsGameConfigs := devtools.NewGameConfigs(accessGameService, gameConfigStorage, token, &logger)

go connectionsScheduler.RoomCleaner(context.Background(), time.Minute*15, 300)
go connectionsScheduler.RoomCleaner(context.Background(), time.Minute*15, 0)

services := []transport.Option{
transport.MaxBodySize(10 * 1024 * 1024),
Expand Down
3 changes: 3 additions & 0 deletions internal/storage/defalult_data/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
Region: "europe",
IsActive: true,
AscenmmoServer: true,
MaxConnections: 8000,
}

defaultServerWebsocket = types.Server{
Expand All @@ -27,6 +28,7 @@ var (
Region: "europe",
IsActive: true,
AscenmmoServer: true,
MaxConnections: 8000,
}

defaultServerTCP = types.Server{
Expand All @@ -38,6 +40,7 @@ var (
Region: "europe",
IsActive: true,
AscenmmoServer: true,
MaxConnections: 8000,
}
)

Expand Down
5 changes: 5 additions & 0 deletions internal/storage/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"context"
"github.com/ascenmmo/multiplayer-game-servers/internal/errors"
defalultdata "github.com/ascenmmo/multiplayer-game-servers/internal/storage/defalult_data"
"github.com/ascenmmo/multiplayer-game-servers/pkg/multiplayer/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -55,6 +56,10 @@ func (d *serversStorage) FindByIDs(serversIDs []uuid.UUID) (servers []types.Serv
return nil, err
}

if len(servers) == 0 {
return nil, errors.ErrServerCreatingRoomAllServesOffError
}

return servers, nil
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/multiplayer/types/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"github.com/google/uuid"
"time"
)

type Room struct {
Expand All @@ -17,8 +18,8 @@ type Room struct {
Servers []uuid.UUID `json:"servers" bson:"servers"`
ExistsServers []uuid.UUID `json:"existsServers" bson:"existsServers"`

IsExists bool `json:"isExists" bson:"isExists"`
CreatedAt int64 `json:"createdAt" bson:"createdAt"`
IsExists bool `json:"isExists" bson:"isExists"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}

func (r *Room) RemoveConnectionID(id uuid.UUID) {
Expand Down
1 change: 1 addition & 0 deletions pkg/multiplayer/types/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *Server) IsExists(ctx context.Context, token string) (bool, error) {
s.ServerType = settings.ServerType
s.ConnectionPort = settings.ConnectionPort
s.IsActive = true
s.MaxConnections = settings.MaxConnections

return s.IsActive, err
}
Expand Down

0 comments on commit edad2dd

Please sign in to comment.