handlers

package
v0.0.0-...-66365b5 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

./handlers/admin.go

./handlers/alliance.go (Ensure CreateAlliance uses correct models)

./handlers/faction.go (Ensure Faction references Zone correctly)

./handlers/match.go (Ensure Match does not reference undefined types)

Index

Constants

This section is empty.

Variables

View Source
var (
	JwtKey = []byte(os.Getenv("JWT_SECRET_KEY")) // Load JWT key from env var

)

Functions

func CreateAdmin

func CreateAdmin(w http.ResponseWriter, r *http.Request)

CreateAdmin allows the creation of admin users. This should be restricted and possibly handled offline.

func CreateAlliance

func CreateAlliance(w http.ResponseWriter, r *http.Request)

CreateAlliance allows two faction leaders to form an alliance.

func CreateFaction

func CreateFaction(w http.ResponseWriter, r *http.Request)

CreateFaction allows a player to create a new faction within a game instance.

func CreateMatch

func CreateMatch(w http.ResponseWriter, r *http.Request)

CreateMatch initiates a match between two players with an initial game state.

func DeletePlayer

func DeletePlayer(w http.ResponseWriter, r *http.Request)

Example of a protected admin route

func GetLeaderboard

func GetLeaderboard(w http.ResponseWriter, r *http.Request)

GetLeaderboard handles various types of leaderboard views

func InitHandlers

func InitHandlers(database *gorm.DB, natsConn *nats.Conn)

InitHandlers sets up the shared database and NATS connection instances

func InitLeaderboard

func InitLeaderboard(database *gorm.DB, rdb *redis.Client)

InitLeaderboard initializes Redis and database

func LoginPlayer

func LoginPlayer(w http.ResponseWriter, r *http.Request)

LoginPlayer handles player login and JWT generation

func PlayTurn

func PlayTurn(w http.ResponseWriter, r *http.Request)

PlayTurn allows a player to submit their turn, updates game state, and saves it.

func PublishAlert

func PublishAlert(subject, message string)

PublishAlert sends an alert message to a specific NATS subject

func RegisterPlayer

func RegisterPlayer(w http.ResponseWriter, r *http.Request)

RegisterPlayer handles player registration

func SendAlert

func SendAlert(nc *nats.Conn, subject, message string)

SendAlert publishes an alert message to a NATS subject

func UpdateResource

func UpdateResource(w http.ResponseWriter, r *http.Request)

UpdateResource allows updating a player's resources.

func WebSocketHandler

func WebSocketHandler(w http.ResponseWriter, r *http.Request)

WebSocketHandler establishes a WebSocket connection and manages turns

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	UserID   uint   `json:"id"`
	jwt.RegisteredClaims
}

Claims structure with UserID included for authentication

type GameState

type GameState struct {
	TurnCount int          `json:"turn_count"`
	Moves     []PlayerMove `json:"moves"` // Record of moves in the game
}

GameState represents the state of the game within a match.

type PlayerConnection

type PlayerConnection struct {
	Conn     *websocket.Conn
	PlayerID uint
}

PlayerConnection represents an active WebSocket connection for a player

type PlayerMove

type PlayerMove struct {
	PlayerID uint   `json:"player_id"`
	Action   string `json:"action"`
}

PlayerMove represents a single move by a player.

Jump to

Keyboard shortcuts

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