cc_fb

package
v0.0.0-...-a558ee2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2017 License: MIT Imports: 9 Imported by: 0

README

db

Database Connector for Cookie Clicker

go test \
    --credentials=/path/to/Google/credentials.json \
    --project=some-string-project-id \
    --environment=(dev|prod)

Documentation

Overview

Package cc_fb handles CookieClicker game state representation in the Firebase DB.

TODO(cripplet): Move to github.com/cripplet/clicker-rest/db.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResetEnvironment

func ResetEnvironment(t *testing.T)

Reset the Firebase DB at the preset project path.

func SaveGameState

func SaveGameState(g FBGameState, eTag string) error

SaveGameState commits a game to the Firebase DB. ETag was supplied previously when loading the game. LoadGameState will fail if the given ETag does not match the current DB ETag. The user is responsible for retrying the load / save cycle in case of failure.

Types

type FBGameMetadata

type FBGameMetadata struct {
	// Data useful for discouraging spamming large amount of REST click
	// requests.
	ClickHash []byte `json:"click_hash"`

	// Last time that cookies have been added to the game due to physical
	// click contribution.
	ClickTime time.Time `json:"click_time"`

	// Last time that cookies have been added to the game due to CPS
	// contributions have been calculated.
	MineTime time.Time `json:"mine_time"`
}

FBGameMetadata is the metadata specific to the REST server. The actual game has no knowledge of this data.

type FBGameObservableData

type FBGameObservableData struct {
	// Number of cookies added to the game per click.
	CookiesPerClick float64 `json:"cookies_per_click"`

	// Number of cookies added to the game per second.
	CPS float64 `json:"cps"`

	// The number of cookies necessary to buy a building of that type.
	// The key of the JSON representation is translated from the
	// cookie_clicker.BuildingType enum, via
	// cookie_clicker.BUILDING_TYPE_LOOKUP.
	BuildingCost map[cookie_clicker.BuildingType]float64 `json:"building_cost"`

	// The number of cookies necessary to buy the given upgrade. The key of
	// the JSON representation is translated via
	// cookie_clicker.UPGRADE_ID_LOOKUP.
	UpgradeCost map[cookie_clicker.UpgradeID]float64 `json:"upgrade_cost"`
}

FBGameObservableData is calculated from the game and is read-only. As with FBGameState, this is not the "true" JSON output as seen when pulling from the database.

func GenerateFBGameObservableData

func GenerateFBGameObservableData(g *cookie_clicker.GameStateStruct) FBGameObservableData

GenerateFBGameObservableData will construct a representation of the read-only game fields. Note that these fields may be slightly inaccurate, e.g. CookiesPerClick may vary by time, and the client may fall out of sync if they do not monitor the only source of truth regarding the number of cookies in the bank (that is, the DB).

type FBGameState

type FBGameState struct {
	// Unique game ID.
	ID string `json:"id"`

	// Placeholder boolean; when loading a nonexistent game JSON, this
	// field will be the boolean zero value, i.e. false. This field is
	// explictly set to true for all games.
	Exist bool `json:"exist"`

	// Imported game data, used to initialize a game. Due to Firebase
	// shenanigans, this struct's map[ENUM]VALUE properties are instead
	// set to map[STRING]VALUE instead. See FBGameObservableData for
	// related comments.
	GameData cookie_clicker.GameStateData `json:"data"`

	// Calculated data from a game. These fields are read-only fields
	// and will be automatically updated when the game has mutated.
	GameObservables FBGameObservableData `json:"observables"`

	// Metadata kept by the REST server.
	Metadata FBGameMetadata `json:"metadata"`
}

FBGameState is the exported state representation of Firebase database data. Note that this is not the "true" JSON output as seen when pulling from the database. The JSON field names are included for convenience, but this type is never actually serialized.

func LoadGameState

func LoadGameState(id string) (FBGameState, string, error)

LoadGameState retrieves a game from the Firebase DB (including all related metadata). If the supplied ID is empty, construct a new game instead (and commit to the DB). Returns an ETag value alongside the game, which must be passed into SaveGameState. A load / save cycle is atomic (or will fail).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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