snengame/internal/game/game.go

363 lines
8.3 KiB
Go
Raw Normal View History

2021-07-22 15:37:25 -04:00
package game
2021-07-15 19:26:57 -04:00
import (
2021-07-16 14:53:45 -04:00
"fmt"
2021-07-23 18:33:17 -04:00
"log"
2021-07-15 19:26:57 -04:00
"strconv"
"strings"
)
2021-07-16 14:53:45 -04:00
type GameStatus int
const (
StatusLobby = iota
StatusReady
StatusPlaying
StatusStop
StatusSentinalWin
StatusScourgeWin
2021-07-22 13:43:28 -04:00
StatusDraw
)
const (
SentinalID = 1
ScourgeID = 2
2021-07-16 14:53:45 -04:00
)
func (g GameStatus) String() string {
2021-07-23 18:33:17 -04:00
return []string{"Lobby", "Ready", "Playing", "Stopped", "SentinalWin", "ScougeWin", "Draw"}[g]
2021-07-16 14:53:45 -04:00
}
2021-07-15 19:26:57 -04:00
type Game struct {
GameBoard *Board
SentinalPlayer *Player
ScourgePlayer *Player
2021-07-16 14:53:45 -04:00
SentinalDeck *Deck
ScourgeDeck *Deck
2021-07-15 19:26:57 -04:00
CurrentTurn int
CardBuffer *Deck
2021-07-15 19:26:57 -04:00
CanDraw bool
HasDrawn bool
2021-07-16 14:53:45 -04:00
Status GameStatus
2021-07-15 19:26:57 -04:00
}
func NewGame() *Game {
deckA := NewDeck(SentinalID)
deckB := NewDeck(ScourgeID)
2021-07-15 19:26:57 -04:00
deckA.Shuffle()
deckB.Shuffle()
return &Game{
GameBoard: NewBoard(),
SentinalPlayer: NewPlayer("Sentinal", SentinalID),
ScourgePlayer: NewPlayer("Scourge", ScourgeID),
2021-07-16 14:53:45 -04:00
SentinalDeck: deckA,
ScourgeDeck: deckB,
2021-07-15 19:26:57 -04:00
CurrentTurn: 0, //start with no turn
2021-07-23 18:33:17 -04:00
CardBuffer: DeckFromCards([]*Card{}),
2021-07-15 19:26:57 -04:00
CanDraw: false,
HasDrawn: false,
2021-07-16 14:53:45 -04:00
Status: StatusLobby,
2021-07-15 19:26:57 -04:00
}
}
2021-07-16 14:53:45 -04:00
func (g *Game) String() string {
return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\nStatus:%v Draw:%v Turn:%v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CurrentTurn, g.CardBuffer)
2021-07-16 14:53:45 -04:00
}
2021-07-22 13:09:56 -04:00
func (g *Game) Parse(cmd *Command) *CommandResult {
2021-07-22 13:43:28 -04:00
if g.Status == StatusDraw || g.Status == StatusScourgeWin || g.Status == StatusSentinalWin || g.Status == StatusStop {
2021-07-22 13:43:28 -04:00
return &CommandResult{
PlayerID: cmd.PlayerID,
ResultType: cmd.Type,
StateResult: NewView(cmd.PlayerID, g),
ActionResult: nil,
DebugResult: g,
}
}
2021-07-22 13:09:56 -04:00
var state_res *GameView
var action_res *Deck
2021-07-22 13:09:56 -04:00
var debug_res *Game
var res_type CmdType
2021-07-23 17:42:12 -04:00
if cmd.Type == ActCmd && g.Status == StatusPlaying {
2021-07-22 13:09:56 -04:00
action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd)
state_res = nil
debug_res = nil
2021-07-22 13:09:56 -04:00
res_type = ActCmd
} else if cmd.Type == StateCmd {
2021-07-22 13:21:49 -04:00
state_res = g.PlayerStateAct(cmd.PlayerID, cmd.Cmd)
action_res = nil
debug_res = nil
2021-07-22 13:09:56 -04:00
res_type = StateCmd
2021-07-23 17:42:12 -04:00
} else if cmd.Type == DebugCmd {
state_res = nil
action_res = nil
2021-07-22 13:21:49 -04:00
debug_res = g
res_type = DebugCmd
2021-07-23 17:42:12 -04:00
} else {
state_res = nil
action_res = nil
debug_res = nil
res_type = InvalidCmd
2021-07-22 13:09:56 -04:00
}
2021-07-22 13:43:28 -04:00
g.StateChanges()
2021-07-22 13:09:56 -04:00
return &CommandResult{
PlayerID: cmd.PlayerID,
ResultType: res_type,
StateResult: state_res,
ActionResult: action_res,
DebugResult: debug_res,
}
}
2021-07-22 13:43:28 -04:00
func (g *Game) StateChanges() {
//check for no actions first
if len(g.SentinalPlayer.Hand) == 0 && g.SentinalDeck.Size() == 0 {
if g.GameBoard.Empty(SentinalID) {
g.Status = StatusScourgeWin
}
}
if len(g.ScourgePlayer.Hand) == 0 && g.ScourgeDeck.Size() == 0 {
if g.GameBoard.Empty(ScourgeID) {
g.Status = StatusSentinalWin
}
}
2021-07-25 15:33:47 -04:00
//reapply card effects
g.GameBoard.ResetCards()
for _, v := range g.GameBoard.Sentinal {
OracleTick(v, g) //apply effect stacks first
OracleEffect(v, g) //actually activate effects
}
for _, v := range g.GameBoard.Scourge {
OracleTick(v, g)
OracleEffect(v, g)
}
2021-07-22 13:43:28 -04:00
//check life
//this is second on purpose so that it shadows deck out win conditions
//if you use your last action to win the game, you should actually win
if g.SentinalPlayer.Life < 0 {
g.Status = StatusScourgeWin
}
if g.ScourgePlayer.Life < 0 {
g.Status = StatusSentinalWin
}
if g.ScourgePlayer.Life < 0 && g.SentinalPlayer.Life < 0 {
g.Status = StatusDraw
}
}
2021-07-22 13:21:49 -04:00
func (g *Game) PlayerStateAct(id int, cmd string) *GameView {
2021-07-15 20:58:21 -04:00
switch cmd {
2021-07-16 14:53:45 -04:00
case "g":
//game state
2021-07-22 13:21:49 -04:00
return NewView(id, g)
2021-07-16 14:53:45 -04:00
case "b":
//begin game
g.Status = StatusReady
//TODO check for ready on both accounts first
g.SentinalPlayer.Hand = g.SentinalDeck.Cards[len(g.SentinalDeck.Cards)-5 : len(g.SentinalDeck.Cards)]
g.SentinalDeck.Cards = g.SentinalDeck.Cards[0 : len(g.SentinalDeck.Cards)-5]
g.ScourgePlayer.Hand = g.ScourgeDeck.Cards[len(g.ScourgeDeck.Cards)-5 : len(g.ScourgeDeck.Cards)]
g.SentinalDeck.Cards = g.ScourgeDeck.Cards[0 : len(g.ScourgeDeck.Cards)-5]
2021-07-15 20:58:21 -04:00
case "s":
//start turn
if g.Status == StatusReady { //first turn
2021-07-16 14:53:45 -04:00
g.Status = StatusPlaying
g.CurrentTurn = id
g.CanDraw = false
2021-07-22 13:09:56 -04:00
g.HasDrawn = true
2021-07-16 15:53:02 -04:00
} else {
g.CanDraw = true
g.HasDrawn = false
2021-07-16 14:53:45 -04:00
}
2021-07-15 20:58:21 -04:00
if id != g.CurrentTurn {
2021-07-22 13:21:49 -04:00
return nil
2021-07-15 20:58:21 -04:00
}
if id == SentinalID {
2021-07-15 21:44:45 -04:00
for _, v := range g.GameBoard.Sentinal {
2021-07-23 18:33:17 -04:00
OracleUpkeep(v, g)
2021-07-15 21:44:45 -04:00
}
} else {
for _, v := range g.GameBoard.Scourge {
2021-07-23 18:33:17 -04:00
OracleUpkeep(v, g)
2021-07-15 21:44:45 -04:00
}
}
2021-07-15 20:58:21 -04:00
case "e":
//end turn and clean up
2021-07-15 21:44:45 -04:00
if id != g.CurrentTurn {
2021-07-22 13:21:49 -04:00
return nil
2021-07-15 21:44:45 -04:00
}
2021-07-23 18:33:17 -04:00
g.CardBuffer = DeckFromCards([]*Card{})
if id == SentinalID {
2021-07-15 21:44:45 -04:00
for _, v := range g.GameBoard.Sentinal {
2021-07-23 18:33:17 -04:00
OracleEndstep(v, g)
2021-07-15 21:44:45 -04:00
}
} else {
for _, v := range g.GameBoard.Scourge {
2021-07-23 18:33:17 -04:00
OracleEndstep(v, g)
2021-07-15 21:44:45 -04:00
}
}
if g.CurrentTurn == SentinalID {
g.CurrentTurn = ScourgeID
2021-07-15 20:58:21 -04:00
} else {
g.CurrentTurn = SentinalID
2021-07-15 20:58:21 -04:00
}
}
2021-07-22 13:21:49 -04:00
return NewView(id, g)
2021-07-15 20:58:21 -04:00
}
func (g *Game) PlayerAct(id int, cmd string) *Deck {
2021-07-15 19:26:57 -04:00
if id != g.CurrentTurn {
return nil
}
var curr *Player
var opp *Player
var currD *Deck
if id == g.SentinalPlayer.Id {
curr = g.SentinalPlayer
opp = g.ScourgePlayer
2021-07-16 14:53:45 -04:00
currD = g.SentinalDeck
2021-07-15 19:26:57 -04:00
} else {
curr = g.ScourgePlayer
opp = g.SentinalPlayer
2021-07-16 14:53:45 -04:00
currD = g.ScourgeDeck
2021-07-15 19:26:57 -04:00
}
cmd_s := strings.Split(cmd, " ")
if len(cmd_s) < 1 {
return nil
}
switch cmd_s[0] {
case "s":
//scry: return scry options off top of deck
2021-07-25 15:33:47 -04:00
if !g.CanDraw {
2021-07-15 20:58:21 -04:00
return nil
}
2021-07-25 15:33:47 -04:00
if g.CardBuffer.Size() <= 0 {
g.CardBuffer = DeckFromCards(currD.Scry(curr.Life))
}
2021-07-16 14:53:45 -04:00
return g.CardBuffer
2021-07-15 19:26:57 -04:00
case "d":
//draw: return player hand
2021-07-16 14:53:45 -04:00
if !g.CanDraw {
return nil
}
2021-07-15 20:58:21 -04:00
if len(cmd_s) != 2 || !g.CanDraw {
2021-07-15 19:26:57 -04:00
return nil
}
x_i, err := strconv.Atoi(cmd_s[1])
2021-07-25 15:33:47 -04:00
if x_i > 2 || x_i < 0 {
return nil
}
2021-07-15 19:26:57 -04:00
if err != nil {
panic(err)
}
x := g.CardBuffer.Cards[x_i]
2021-07-16 14:53:45 -04:00
buf := g.CardBuffer
for i, v := range buf.Cards {
2021-07-15 19:26:57 -04:00
if v == x {
buf.Cards = append(buf.Cards[:i], buf.Cards[i+1:]...)
2021-07-15 19:26:57 -04:00
}
}
currD.Bottom(buf.Cards)
2021-07-16 14:53:45 -04:00
curr.Hand = append(curr.Hand, x)
g.CanDraw = false
g.HasDrawn = true
return DeckFromCards(curr.Hand)
2021-07-15 19:26:57 -04:00
case "m":
//move: return player board or [] if invalid
if len(cmd_s) != 3 {
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
2021-07-23 18:33:17 -04:00
if g.GameBoard.CanMove(g.CurrentTurn, x_i, y_i) {
OracleMove(g.GameBoard.GetCard(g.CurrentTurn, x_i), x_i, y_i, g)
2021-07-23 18:33:17 -04:00
g.GameBoard.Move(g.CurrentTurn, x_i, y_i)
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
2021-07-15 19:26:57 -04:00
} else {
2021-07-23 18:33:17 -04:00
return DeckFromCards([]*Card{})
2021-07-15 19:26:57 -04:00
}
case "a":
//attack
if len(cmd_s) != 3 {
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
2021-07-23 18:33:17 -04:00
if g.GameBoard.CanAttack(g.CurrentTurn, x_i, y_i) {
OracleAttack(g.GameBoard.GetCard(g.CurrentTurn, x_i), g)
res := g.GameBoard.Attack(g.CurrentTurn, x_i, y_i)
2021-07-25 15:33:47 -04:00
var aid, did int
if id == SentinalID {
aid = SentinalID
did = ScourgeID
2021-07-23 18:33:17 -04:00
} else {
2021-07-25 15:33:47 -04:00
aid = ScourgeID
did = SentinalID
}
attacker := g.GameBoard.GetRow(aid)[x_i]
defender := g.GameBoard.GetRow(did)[y_i]
switch res {
case 1:
opp.Life = opp.Life - 1
case 2:
OracleLeaves(defender, g)
g.GameBoard.Remove(defender)
case 3:
OracleLeaves(attacker, g)
g.GameBoard.Remove(attacker)
case 4:
OracleLeaves(attacker, g)
OracleLeaves(defender, g)
g.GameBoard.Remove(attacker)
g.GameBoard.Remove(defender)
2021-07-23 18:33:17 -04:00
}
2021-07-25 15:33:47 -04:00
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
2021-07-15 19:26:57 -04:00
} else {
2021-07-23 18:33:17 -04:00
log.Println("can't attack")
return DeckFromCards([]*Card{})
2021-07-15 19:26:57 -04:00
}
case "p":
//play: return player boad or [] if invalid
2021-07-16 14:53:45 -04:00
if len(cmd_s) != 3 {
fmt.Println("not enough arguments")
2021-07-15 19:26:57 -04:00
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
2021-07-15 21:44:45 -04:00
card := curr.Hand[x_i]
2021-07-23 18:33:17 -04:00
shouldPlace := true
if g.GameBoard.CanPlay(g.CurrentTurn, card, y_i) {
2021-07-25 15:33:47 -04:00
shouldPlace = OracleCast(card, g)
placed := g.GameBoard.Play(g.CurrentTurn, card, y_i, shouldPlace)
if placed {
2021-07-25 15:33:47 -04:00
OracleEnters(card, g)
}
2021-07-15 20:58:21 -04:00
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
2021-07-15 19:26:57 -04:00
} else {
fmt.Println("couldn't play")
2021-07-23 18:33:17 -04:00
return DeckFromCards([]*Card{})
2021-07-15 19:26:57 -04:00
}
default:
fmt.Println("Invalid act command")
2021-07-15 19:26:57 -04:00
return nil
}
return nil
}