Files
inp2025/models/state.go
2025-11-10 14:52:16 +08:00

82 lines
1.2 KiB
Go

package models
import "fmt"
const (
BlockNull byte = '0'
BlockI byte = 'I'
BlockJ byte = 'J'
BlockL byte = 'L'
BlockO byte = 'O'
BlockS byte = 'S'
BlockT byte = 'T'
BlockZ byte = 'Z'
)
const (
RowCnt int = 20
ColCnt int = 10
)
type BlockState struct {
Block byte `json:"b"`
PosX int `json:"x"`
PosY int `json:"y"`
}
type ActiveBlockState struct {
*BlockState `json:",inline"`
Direc int `json:"d"`
}
type CompressedGameState struct {
UserId int
Tick int
Score int
Level int
RLE string
Active ActiveBlockState
}
type GameState struct {
UserId int
Tick int
Score int
Level int
Blocks [RowCnt][ColCnt]BlockState
Active ActiveBlockState
}
func (self CompressedGameState) Uncompress() (GameState, error) {
var cnt int = 0
var ch byte = '0'
var blocks [RowCnt][ColCnt]BlockState
for x := range blocks {
for y := range blocks[x] {
if cnt <= 0 {
_, err := fmt.Sscanf(self.RLE, "%d%c", &cnt, &ch)
if err != nil {
return GameState{}, err
}
}
blocks[x][y] = BlockState{
Block: ch,
PosX: x,
PosY: y,
}
}
}
return GameState{
UserId: self.UserId,
Tick: self.Tick,
Score: self.Score,
Level: self.Level,
Blocks: blocks,
Active: self.Active,
}, nil
}