Skip to content

Add a function that simulates players joining and leaving to generate metrics through query #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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 simple-game-server-go/go.mod
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ module github.com/Unity-Technologies/multiplay-examples/simple-game-server-go
go 1.20

require (
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.3.1
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.4.1
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
)
2 changes: 2 additions & 0 deletions simple-game-server-go/go.sum
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ github.com/FZambia/eagle v0.0.2 h1:35qHDuXSQevZ4w9A51k4wU7OE/tPHTEWXoywA93hvkY=
github.com/FZambia/sentinel v1.1.0 h1:qrCBfxc8SvJihYNjBWgwUI93ZCvFe/PJIPTHKmlp8a8=
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.3.1 h1:piV2hAtoc5ke3ywkIvUs82J+CphjWXoN3219isDmY00=
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.3.1/go.mod h1:WgDwSafd4alCs+HdK0z+7htBVZIe+LUrLQgM738WDd0=
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.4.1 h1:oM4+Pi8ZA9qVoqclK/S1dDUQd9QZoKWrDLY7gJzIHAw=
github.com/Unity-Technologies/unity-gaming-services-go-sdk v0.4.1/go.mod h1:WgDwSafd4alCs+HdK0z+7htBVZIe+LUrLQgM738WDd0=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/centrifugal/centrifuge v0.21.1 h1:7saDm953rxzke4YfzoNekxKnpckmdTnz8EzbSribwos=
github.com/centrifugal/centrifuge-go v0.8.4 h1:X23NuXmXTqdlp6PNrZLj1bWcPpehVq5u8LxuM2+NrIA=
29 changes: 27 additions & 2 deletions simple-game-server-go/internal/game/allocated.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import (
"github.com/sirupsen/logrus"
)

const defaultMaxPlayers = 4
const defaultMaxPlayers = 64

// allocated starts a game after the server has been allocated.
func (g *Game) allocated(allocationID string) {
@@ -58,6 +58,8 @@ func (g *Game) launchGame(port int64) {
return
}

go g.simulatePlayers()

g.gameBind = gs

for {
@@ -71,11 +73,34 @@ func (g *Game) launchGame(port int64) {

continue
}

go g.handleClient(client)
}
}

const startPlayers = 30

func (g *Game) simulatePlayers() {
// Setup baseline of players
for i := 0; i < startPlayers; i++ {
g.Server.PlayerJoined()
}
for {
j, err := rand.Int(rand.Reader, big.NewInt(4))
if err != nil {
g.logger.Debug(err)
fmt.Println("j: ", j)
return
}

if j.Int64() == 0 || j.Int64() == 1 {
g.Server.PlayerLeft()
} else {
g.Server.PlayerJoined()
}
time.Sleep(20 * time.Second)
}
}

// acceptClient accepts a new TCP connection and updates internal state.
func (g *Game) acceptClient(server *net.TCPListener) (*net.TCPConn, error) {
client, err := server.AcceptTCP()