freego_api/session.go
2022-03-07 15:21:37 -05:00

80 lines
1.6 KiB
Go

package main
import (
"errors"
"fmt"
"log"
"git.saintnet.tech/stryan/freego"
)
//Session represents an active game
type Session struct {
simulator *freego.Game
redPlayer *Player
bluePlayer *Player
moveNum int
moveList []freego.ParsedCommand
}
//Player is a player in a match
type Player struct {
Ready bool
Team freego.Colour
}
//ID returns player ID
func (p *Player) ID() int {
panic("not implemented") // TODO: Implement
}
//Colour returns player team
func (p *Player) Colour() freego.Colour {
return p.Team
}
//NewSession creates a new game session
func NewSession() *Session {
sim := freego.NewGame()
return &Session{
simulator: sim,
redPlayer: &Player{false, freego.Red},
bluePlayer: &Player{false, freego.Blue},
moveNum: 1,
moveList: make([]freego.ParsedCommand, 20),
}
}
func (s *Session) tryMove(player *Player, move string) (*freego.ParsedCommand, error) {
raw, err := freego.NewRawCommand(move)
if err != nil {
return nil, err
}
p, err := s.simulator.Parse(player, raw)
if err != nil {
return nil, err
}
return p, nil
}
func (s *Session) mutate(p *freego.ParsedCommand) (string, error) {
success, err := s.simulator.Mutate(p)
if err != nil {
return "", err
}
if success {
s.moveList[s.moveNum] = *p
s.moveNum++
return fmt.Sprintf("%v %v", s.moveNum-1, p.String()), nil
}
return "", nil
}
func (s *Session) getMove(p *Player, num int) (string, error) {
if num <= 0 || num >= s.moveNum {
log.Printf("tried to get move number %v when move is %v", num, s.moveNum)
return "", errors.New("invalid move number")
}
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
}