Skip to content

code-game-project/go-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Server

CodeGame Version Go version GoDoc

The Go server library for CodeGame.

Installation

go get github.com/code-game-project/go-server/cg

Usage

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.

About

The Go server library for CodeGame.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages