savegame

package
v0.0.0-...-1f00af5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration pretty prints a time.Duration in MM:SS format.

func Migrate

func Migrate() error

Migrate the savegame.json file to re-save it as its newest file format.

v0: we stored LevelPack filenames + level filenames to store high scores. This was brittle in case a level gets moved into another levelpack later, or either it or the levelpack is renamed.

v1: levels get UUID numbers and we store them by that. You can re-roll a UUID in the level editor if you want to break high scores for your new level version.

Types

type Level

type Level struct {
	Completed   bool           `json:"completed"`
	BestTime    *time.Duration `json:"bestTime"`
	PerfectTime *time.Duration `json:"perfectTime"`
}

Level holds high score information for a level.

type LevelPack

type LevelPack struct {
	Levels map[string]*Level `json:"levels"`
}

LevelPack holds savegame process for a level pack.

func NewLevelPack

func NewLevelPack() *LevelPack

NewLevelPack initializes a LevelPack struct.

type SaveGame

type SaveGame struct {
	// DEPRECATED: savegame state spelled out by level packs and
	// filenames.
	LevelPacks map[string]*LevelPack `json:"levelPacks,omitempty"`

	// New data format: store high scores by level UUID. Adds a
	// nice layer of obfuscation + is more robust in case levels
	// move around between levelpacks, get renamed, etc. that
	// the user should be able to keep their high score.
	Levels map[string]*Level
}

SaveGame holds the user's progress thru level packs.

func GetOrCreate

func GetOrCreate() (*SaveGame, error)

GetOrCreate the save game JSON. If the save file isn't found OR has an invalid checksum, it is created. Always returns a valid SaveGame struct and the error may communicate if there was a problem reading an existing file.

func Load

func Load() (*SaveGame, error)

Load the save game JSON from the user's profile directory.

func New

func New() *SaveGame

New creates a new SaveGame.

func (*SaveGame) CountCompleted

func (sg *SaveGame) CountCompleted(levelpack *levelpack.LevelPack) int

CountCompleted returns the number of completed levels in a levelpack.

func (*SaveGame) GetLevelScore

func (sg *SaveGame) GetLevelScore(levelpack, filename, uuid string) *Level

GetLevelScore finds or creates a default Level score.

func (*SaveGame) MarkCompleted

func (sg *SaveGame) MarkCompleted(levelpack, filename, uuid string)

MarkCompleted is a helper function to mark a levelpack level completed. Parameters are the filename of the levelpack and the level therein. Extra path info except the base filename is stripped from both.

func (*SaveGame) NewHighScore

func (sg *SaveGame) NewHighScore(levelpack, filename, uuid string, isPerfect bool, elapsed time.Duration, rules level.GameRule) bool

NewHighScore may set a new highscore for a level.

The level will be marked Completed and if the given score is better than the stored one it will update.

Returns true if a new high score was logged.

func (*SaveGame) Save

func (sg *SaveGame) Save() error

Save the savegame.json to disk.

Jump to

Keyboard shortcuts

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