go_boardgame_networking

package module
v0.0.0-...-89461f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2021 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go-boardgame-networking

Go-boardgame-networking is a Go package that provides a websocket networking layer for games built with the go-boardgame package.

This project provides a lightweight http server out of the box to quickly start playing or prototyping games as well as an easy to use networking layer that can be added to a custom http server if extra configurability is desired.

Usage

Option 1 - Lightweight Server
Create and start a server
exampleBuilder := example.Builder{} // see https://github.com/quibbble/go-boardgame/blob/main/builder.go
server := NewServer(8080, GameNetworkOptions{
    Games: []bg.BoardGameBuilder{exampleBuilder}, // all game builders to add to the network
    GameExpiry: 3 * time.Hour,                    // how long to wait before cleaning an old game
})
server.ListenAndServe()
Create a new game
POST http://localhost:8080/game/create

With body:

{
    "GameKey": "ExampleKey",
    "GameID": "ExampleID",
    "Teams": ["TeamA", "TeamB"]
}
Join a game
GET ws://localhost:8080/game/join?GameKey=ExampleKey&GameID=ExampleID

On successful join:

{
    "GameID": "ExampleID",
    "Snapshot": {
        "Turn": "TeamA",
        "Teams": ["TeamA", "TeamB"],
        "Winners": [],
        "MoreData": {},
        "Actions": []
    }
}
Send a BoardGameAction message
{
    "Team": "TeamA",
    "ActionType": "DoSomething"
}

On successful processing of action receive:

{
    "GameID": "ExampleID",
    "Snapshot": {
        "Turn": "TeamA",
        "Teams": ["TeamA", "TeamB"],
        "Winners": [],
        "MoreData": {},
        "Actions": [
            {
                "Team": "TeamA",
                "ActionType": "DoSomething"
            }
        ]
    }
}

On error processing of action receive:

{
    "GameID": "ExampleID",
    "Error": "error message"
}
Option 2 - Networking Layer

Note that the pre-existing simple server implementation can be used as a reference point when integrating this networking into a custom server.

Create the networking layer
exampleBuilder := example.Builder{} // see https://github.com/quibbble/go-boardgame/blob/main/builder.go
network := NewGameNetwork(GameNetworkOptions{
    Games: []bg.BoardGameBuilder{exampleBuilder}, // all game builders to add to the network
    GameExpiry: 3 * time.Hour,                    // how long to wait before cleaning an old game
})
Create a new game
err := network.CreateGame(CreateGameOptions{
    GameKey: "ExampleKey",
    GameID: "ExampleID",
    BoardGameOptions: bg.BoardGameOptions{
        Teams: []string{"TeamA", "TeamB"},
        MoreOptions: nil, // custom game options
    },
})
Join a game
err := network.JoinGame(JoinGameOptions{
    GameKey: "ExampleKey",
    GameID: "ExampleID",
    Conn: WEBSOCKET_CONNECTION_HERE // websocket connection of the connecting player
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(port int, options GameNetworkOptions) *http.Server

Types

type CreateGameOptions

type CreateGameOptions struct {
	GameKey string
	GameID  string
	bg.BoardGameOptions
}

CreateGameOptions are the fields necessary for creating a game

type GameErrorMessage

type GameErrorMessage struct {
	GameID string
	Error  string
}

GameErrorMessage is the message sent when there was an error

type GameMessage

type GameMessage struct {
	GameID   string
	Snapshot bg.BoardGameSnapshot
}

GameMessage is the message sent when returning the game snapshot

type GameNetwork

type GameNetwork struct {
	// contains filtered or unexported fields
}

func NewGameNetwork

func NewGameNetwork(options GameNetworkOptions) *GameNetwork

func (*GameNetwork) CreateGame

func (n *GameNetwork) CreateGame(options CreateGameOptions) error

func (*GameNetwork) JoinGame

func (n *GameNetwork) JoinGame(options JoinGameOptions) error

type GameNetworkOptions

type GameNetworkOptions struct {
	Games      []bg.BoardGameBuilder // list of game builders to add to the networking layer
	GameExpiry time.Duration         // how long after creation the game will last before being removed
}

GameNetworkOptions are the options required to create a new network

type JoinGameOptions

type JoinGameOptions struct {
	GameKey string
	GameID  string
	Conn    *websocket.Conn
}

JoinGameOptions are the fields necessary for joining a game

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址