2022-03-03 13:28:00 -05:00
|
|
|
package main
|
|
|
|
|
2022-03-07 14:47:21 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.saintnet.tech/stryan/freego"
|
|
|
|
)
|
2022-03-03 13:28:00 -05:00
|
|
|
|
|
|
|
//Session represents an active game
|
|
|
|
type Session struct {
|
2022-03-07 14:47:21 -05:00
|
|
|
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
|
2022-03-03 13:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewSession creates a new game session
|
|
|
|
func NewSession() *Session {
|
2022-03-07 14:47:35 -05:00
|
|
|
sim := freego.NewGame()
|
2022-03-03 13:28:00 -05:00
|
|
|
return &Session{
|
2022-03-07 14:47:35 -05:00
|
|
|
simulator: sim,
|
2022-03-07 14:47:21 -05:00
|
|
|
redPlayer: &Player{false, freego.Red},
|
|
|
|
bluePlayer: &Player{false, freego.Blue},
|
|
|
|
moveNum: 0,
|
|
|
|
moveList: []freego.ParsedCommand{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) error {
|
|
|
|
success, err := s.simulator.Mutate(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
return errors.New("invalid move")
|
|
|
|
}
|
|
|
|
s.moveList[s.moveNum] = *p
|
|
|
|
s.moveNum++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) getMove(p *Player, num int) (string, error) {
|
|
|
|
if num < 0 || num > s.moveNum {
|
|
|
|
return "", errors.New("invalid move number")
|
2022-03-03 13:28:00 -05:00
|
|
|
}
|
2022-03-07 14:47:21 -05:00
|
|
|
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
|
2022-03-03 13:28:00 -05:00
|
|
|
}
|