https://github.com/code-game-project/go-server
The Go server library for CodeGame.
https://github.com/code-game-project/go-server
go server
Last synced: 5 months ago
JSON representation
The Go server library for CodeGame.
- Host: GitHub
- URL: https://github.com/code-game-project/go-server
- Owner: code-game-project
- License: mit
- Created: 2022-05-08T14:48:31.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-21T18:47:14.000Z (about 3 years ago)
- Last Synced: 2024-03-14T23:51:37.052Z (over 2 years ago)
- Topics: go, server
- Language: Go
- Homepage:
- Size: 158 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go-Server


[](https://pkg.go.dev/github.com/code-game-project/go-server)
The Go server library for [CodeGame](https://code-game.org).
## Installation
```sh
go get github.com/code-game-project/go-server/cg
```
## Usage
```go
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/Bananenpro/log"
"github.com/code-game-project/go-server/cg"
)
type Game struct {
cg *cg.Game
}
func (g *Game) OnPlayerJoined(player *cg.Player) {
fmt.Println("Player joined:", player.Username)
}
func (g *Game) OnPlayerLeft(player *cg.Player) {
fmt.Println("Player left:", player.Username)
}
func (g *Game) OnPlayerSocketConnected(player *cg.Player, socket *cg.GameSocket) {
fmt.Println("Player connected a new socket:", player.Username)
}
func (g *Game) OnSpectatorConnected(socket *cg.GameSocket) {
fmt.Println("A spectator connected:", socket.ID)
}
func (g *Game) pollCommands() {
for {
cmd, ok := g.cg.NextCommand() // Alternatively WaitForNextCommand()
if !ok {
return
}
g.handleCommand(cmd.Origin, cmd.Cmd)
}
}
func (g *Game) handleCommand(origin *cg.Player, cmd cg.Command) {
fmt.Printf("Received '%s' command from '%s'.\n", cmd.Name, origin.Username)
}
func (g *Game) Run() {
// Loop until the game is closed.
for g.cg.Running() {
g.pollCommands()
// game logic
// 60 FPS
time.Sleep(16 * time.Millisecond)
}
}
func main() {
server := cg.NewServer("my_game", cg.ServerConfig{
Port: 8080,
MaxPlayersPerGame: 20,
DeleteInactiveGameDelay: 15 * time.Minute,
KickInactivePlayerDelay: 15 * time.Minute,
EventsPath: "./my_game.cge",
LogoPath: "./my_logo.png",
Frontend: os.DirFS("./frontend"),
DisplayName: "My Game",
Version: "1.2.3",
Description: "This is my game.",
RepositoryURL: "https://example.com/my-game",
})
server.Run(func(cgGame *cg.Game, config json.RawMessage) {
var gameConfig struct{} // should be GameConfig from cg-gen-events.
err := json.Unmarshal(config, &gameConfig)
if err != nil {
log.Error(err)
return
}
cgGame.SetConfig(gameConfig)
game := Game{
cg: cgGame,
}
// Register callbacks.
cgGame.OnPlayerJoined = game.OnPlayerJoined
cgGame.OnPlayerLeft = game.OnPlayerLeft
cgGame.OnPlayerSocketConnected = game.OnPlayerSocketConnected
cgGame.OnSpectatorConnected = game.OnSpectatorConnected
// Run the game loop.
game.Run()
})
}
```
## License
MIT License
Copyright (c) 2022-2023 Julian Hofmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.