state

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMetaIdent    = "erago"
	DefaultMetaIdentLen = 5

	MetaTitleLimit = 120 // 30 * 4byte char
)
View Source
const IndexNotFound = csv.IndexNotFound

IndexNotFound is a value implying index is not found. Re-declare here so that csv package is not inported explicitly to use this constant.

Variables

View Source
var (
	ErrTitleTooLarge     = errors.New("state: comment in metadata is too long")
	ErrUnknownIdentifier = errors.New("state: signature in metadata is not correct")
	ErrDifferentVersion  = errors.New("state: different game version in metadata")
)

Functions

func FillNumber

func FillNumber(slice []int64, num int64) []int64

fill slice for given number.

func StrClear

func StrClear(slice []string) []string

fill slice for empty string.

func ZeroClear

func ZeroClear(slice []int64) []int64

fill slice by 0.

Types

type CharaReferences

type CharaReferences struct {
	Indexes []int
	// contains filtered or unexported fields
}

It has references for *Character. Its capacity of array is a number of csv.Character.

The reference is actually index of []*Character. So the reference is not always equal to *Character, which case is occurred if []*Character is sorted, and relation between index and *Character are changed.

func (*CharaReferences) Clear

func (cref *CharaReferences) Clear()

all Indexes are cleared by zero.

func (CharaReferences) First

func (cref CharaReferences) First() *Character

get first character

func (CharaReferences) GetChara

func (cref CharaReferences) GetChara(idx int) *Character

get character using idx, as like chara = reference[i], exception that if index out of range return nil.

func (CharaReferences) GetIndex

func (cref CharaReferences) GetIndex(i int) int

get index of Character at i. if i is out of range return -1.

func (CharaReferences) Len

func (cref CharaReferences) Len() int

return its array size.

func (*CharaReferences) Set

func (cref *CharaReferences) Set(i int, c *Character) error

set character with index. same as reference[i] = chara. if given character is not found in original chara list, return that error.

type Character

type Character struct {
	ID     int64  // ID: means reference for csv.Character. -1 means no reference.
	UID    uint64 // Uniq ID. Indicating its identity.
	IsAssi int64  // experience of assistant

	Name       string // formal name
	CallName   string // usual name
	NickName   string // use for special case
	MasterName string // call for you

	UserVariables
}

Character has some parameters including csv character's parameters.

func (*Character) CsvInitialize

func (c *Character) CsvInitialize(csv_chara *csv.Character)

Initialize by csv parameters.

type Characters

type Characters struct {
	List          characters
	CountNewChara uint64 // count of call newCharacters()
	// contains filtered or unexported fields
}

Character pool. Its size are fit automatically to appropriate size.

Adding new character is done by Characters.AddID().

func (*Characters) AddEmptyCharacter

func (cs *Characters) AddEmptyCharacter() *Character

add empty Character that has not initialized parameters, and return added new Character.

func (*Characters) AddID

func (cs *Characters) AddID(ID int64) (*Character, error)

Add one character detected by character's ID and return added new Character. if character of given id is not found retrun that error.

func (*Characters) AddIDs

func (cs *Characters) AddIDs(IDs ...int64) ([]*Character, error)

Add Characters detected by Character's ID. return list of added Characters and error that Chara is not found.

func (*Characters) Clear

func (cs *Characters) Clear()

clear all chara

func (Characters) FindBy

func (cs Characters) FindBy(f func(*Character) bool) *Character

find Character by bool func. if not found, return nil

func (Characters) FindIndexBy

func (cs Characters) FindIndexBy(f func(*Character) bool) int

find true index by bool func. if not found, return -1

func (Characters) FindIndexByUID

func (cs Characters) FindIndexByUID(uid uint64) int

find index by compairing UID.

func (Characters) Get

func (cs Characters) Get(i int) *Character

like array access a_chara = charas[i], if idx out of range return nil

func (Characters) Len

func (cs Characters) Len() int

a current number of character

func (Characters) Less

func (cs Characters) Less(i, j int) bool

implement sort.Interface. default compare Character's ID.

func (*Characters) Remove

func (cs *Characters) Remove(idx int) bool

Remove Character at index and return IsRemoved. NOTE: after removing, the characters after given idx are shifted to fill empty index.

func (Characters) RevSortBy

func (cs Characters) RevSortBy(by func(*Character, *Character) bool)

reverse ordered sort by "less" func.

func (Characters) Set

func (cs Characters) Set(i int, c *Character)

like array access charas[i] = a_chara

func (Characters) SortBy

func (cs Characters) SortBy(by func(*Character, *Character) bool)

sort by "less" function, which returns true if first character is less than second character. NOTE: if "less" function returns inverted booleen, sort order is reversed.

func (Characters) Swap

func (cs Characters) Swap(i, j int)

implement sort.Interface. swap positions between i-th character and j-th character.

type GameState

type GameState struct {
	CSV *csv.CsvManager

	ShareData  *UserVariables
	SystemData *SystemData

	*SaveInfo
	// contains filtered or unexported fields
}

game State holds all game parameters.

func NewGameState

func NewGameState(csvdb *csv.CsvManager, repo Repository) *GameState

consturct gameState with CSV Manager and config.

func (*GameState) Clear

func (state *GameState) Clear()

clear all data, including system and share, using 0 and empty string.

func (*GameState) FileExists

func (state *GameState) FileExists(no int) bool

check whether does save[No.] exists?

func (*GameState) LoadHeader

func (state *GameState) LoadHeader(no int) (*MetaData, error)

load only header from save[No.].

func (*GameState) LoadShare

func (state *GameState) LoadShare() error

load shared data from "share.sav"

func (*GameState) LoadSystem

func (state *GameState) LoadSystem(no int) error

load game system state from save[No.].

func (*GameState) SaveShare

func (state *GameState) SaveShare() error

save shared data to "share.sav"

func (*GameState) SaveSystem

func (state *GameState) SaveSystem(no int) error

save game system state to save[No.].

func (*GameState) SaveSystemWithComment

func (state *GameState) SaveSystemWithComment(no int, comment string) error

save game system state to save[No.] with comment.

type IntParam

type IntParam struct {
	Values []int64 // it must be exported to marshall object.
	// contains filtered or unexported fields
}

IntParam can be treated as []int64. And can use string key.

func NewIntParam

func NewIntParam(vars []int64, indexer NameIndexer) IntParam

func (IntParam) Fill

func (ip IntParam) Fill(value int64)

It fills by given value to all values contained in IntParam.

func (IntParam) Get

func (ip IntParam) Get(i int) int64

it is same as ip.Values[i].

func (IntParam) GetByStr

func (ip IntParam) GetByStr(key string) (int64, bool)

same as io.Values[i] but i is obtained by using string key,

func (IntParam) GetIndex

func (ip IntParam) GetIndex(key string) int

get index by string key.

func (IntParam) Len

func (ip IntParam) Len() int

return size of its values.

func (IntParam) Set

func (ip IntParam) Set(i int, val int64)

it is same as ip.Values[i] = val.

func (IntParam) SetByStr

func (ip IntParam) SetByStr(key string, val int64) bool

same as io.Values[i] = val but i is obtained by using string key,

func (IntParam) Slice

func (ip IntParam) Slice(from, to int) IntParam

same as []int[from:to], but taking over nameIndexer.

type LimitedRangeNameIndexer added in v0.3.0

type LimitedRangeNameIndexer struct {
	// contains filtered or unexported fields
}

LimitedRangeNameIndexer limits the index range from original NameIndexer to the specified range [from:to) and maps the specified range into 0-based range, [0:to-from). It returns index -1 when the original NameIndexer returns out bounds of the specified range.

func (LimitedRangeNameIndexer) GetIndex added in v0.3.0

func (n LimitedRangeNameIndexer) GetIndex(key string) int

type MetaData

type MetaData struct {
	Identifier  string
	GameVersion int32
	Title       string
}

MetaData is saved with game data. it is referered to validate reading file.

type NameIndexer

type NameIndexer interface {
	// get index by querying string name
	// if not found return csv.IndexNotFound
	GetIndex(string) int
}

name indexer is mapping string key to index.

type NoneNameIndexer

type NoneNameIndexer struct{}

it always returns index -1 for any key. to use it, simply none_indexer := NoneNameIndexer{}.

func (NoneNameIndexer) GetIndex

func (n NoneNameIndexer) GetIndex(key string) int

type Repository

type Repository interface {

	// Exist returns whether a persisted GameState with id exists?
	// return true if exists. return false if not exists or
	// context is canceled.
	Exist(ctx context.Context, id int) bool

	// SaveSystemData persists SystemData, the game specific data,
	// in the given GameState.
	// ID is used to identify the persisted data.
	// return nil on success. return some error on failure or
	// context is canceled.
	SaveSystemData(ctx context.Context, id int, state *SystemData, info *SaveInfo) error

	// LoadSystemData restores SystemData, the game specific data,
	// into the given GameState.
	// ID is used to identify the persisted data.
	// return nil on success. return some error on failure or
	// context is canceled.
	LoadSystemData(ctx context.Context, id int, state *SystemData, info *SaveInfo) error

	// SaveSharedData persists ShareData, sharing over different GameState,
	// in the given GameState. return nil on success. return some error
	// on failure or context is canceled.
	SaveShareData(ctx context.Context, state *UserVariables) error

	// LoadShareData restores ShareData, sharing over different GameState,
	// into the given GameState.
	// return nil on success. return some error on failure or
	// context is canceled.
	LoadShareData(ctx context.Context, state *UserVariables) error

	// LoadMetaList returns list of meta data associated to id list.
	// If id list is empty or nil, return empty list of meta data for persisted
	// GameState.
	// It also returns error if any id is not found or context is canceled.
	LoadMetaList(ctx context.Context, ids ...int) ([]*MetaData, error)
}

Repository is a abstract data-store which persists GameState to arbitrary storage.

type SaveInfo

type SaveInfo struct {
	LastLoadVer     int32
	LastLoadComment string
	SaveComment     string
}

SaveInfo has information isolated save and load.

type StrParam

type StrParam struct {
	Values []string
	// contains filtered or unexported fields
}

StrParam can be treated as []string. And can use string key.

func NewStrParam

func NewStrParam(vars []string, indexer NameIndexer) StrParam

func (StrParam) Fill

func (ip StrParam) Fill(value string)

It fills by given value to all values contained in IntParam.

func (StrParam) Get

func (ip StrParam) Get(i int) string

it is same as ip.Values[i].

func (StrParam) GetByStr

func (ip StrParam) GetByStr(key string) (string, bool)

same as io.Values[i] but i is obtained by using string key,

func (StrParam) GetIndex

func (ip StrParam) GetIndex(key string) int

get index by string key.

func (StrParam) Len

func (ip StrParam) Len() int

return size of its values.

func (StrParam) Set

func (ip StrParam) Set(i int, val string)

it is same as ip.Values[i] = val.

func (StrParam) SetByStr

func (ip StrParam) SetByStr(key string, val string) bool

same as io.Values[i] = val but i is obtained by using string key,

func (StrParam) Slice

func (ip StrParam) Slice(from, to int) StrParam

same as []string[from:to], but taking over nameIndexer.

type SystemData

type SystemData struct {
	Chara *Characters

	// references of Chara
	Target *CharaReferences
	Master *CharaReferences
	Player *CharaReferences
	Assi   *CharaReferences

	UserVariables
}

System data has data using for the game system. it is remains after end game.

func (*SystemData) Clear

func (sysdata *SystemData) Clear()

clear all data using 0, empty string and nil.

type UserVariables

type UserVariables struct {
	// exported to marshall/unmarshall object. user should not
	// access this field directory
	IntMap intParamMap
	StrMap strParamMap
	// contains filtered or unexported fields
}

UserVariables defines user defined values from csv data base. Its contents are accessed via API such as GetInt(varname) or GetStr(varname).

func (UserVariables) Clear

func (uvars UserVariables) Clear()

clear contents of UserVariables. Int vars are cleared by 0, and Str vars are empty string.

func (UserVariables) ForEachIntParam

func (usr_vars UserVariables) ForEachIntParam(f func(string, IntParam))

iteration of each int parameters.

func (UserVariables) ForEachStrParam

func (usr_vars UserVariables) ForEachStrParam(f func(string, StrParam))

iteration of each str parameters.

func (UserVariables) GetInt

func (usr_vars UserVariables) GetInt(varname string) (IntParam, bool)

Get IntParam queried by varname. return IntParam, found.

func (UserVariables) GetStr

func (usr_vars UserVariables) GetStr(varname string) (StrParam, bool)

Get []string variable queried by varname. return StrParam, found.

Directories

Path Synopsis
Package csv provides csv-parser for game parameter names.
Package csv provides csv-parser for game parameter names.

Jump to

Keyboard shortcuts

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