habit

package module
v0.0.0-...-964326b Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: MIT Imports: 14 Imported by: 0

README

Example Import

import "github.com/mr-joshcrane/habit"

Example of Library Usage

Local Tracker

// Error handling omitted for brevity
func main() {
	// File based persistance for our habits
    store, err := pbfilestore.Open("store")
	// A habit tracker backed by our store
    tracker := habit.NewTracker(store)
	habit.RunCLI(tracker)

Client/Server Tracker

// Error handling omitted for brevity
func main() {
	uri := "yourDynamodbUriConnectionString"
	tablename := "your dynamodb table name"
	// A dynamodb backed persistance store for our habits
	store, err := dynamodbstore.Open(uri, tablename)
	// A habit tracker backed by our store
	tracker := habit.NewTracker(store)
    // Creation of a GRPC Habit server that performs habit logic
	server, err := habit.NewServer(tracker)
    // Our CLI runs using our generated client
	habit.RunCLI(server.Client())
}

Example of CLI usage

Perform a new habit
go run cmd/habit.go violin
Create a new challenge code from a habit
go run cmd/habit.go -c violin
Join a new challenge with a code
go run cmd/habit.go -v="CCODE" violin

What is it and why would I use it?

Habits are hard to create, so why not leverage the power of social contracts to create them? Available in a single user mode (local users only) or a multi user over the network (some assembly required!)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BattleCodeGenerator = generateBattleCode
View Source
var Now = time.Now

Functions

func Render

func Render(input string, std *os.File)

func RunCLI

func RunCLI(p Tracker)

Types

type Battle

type Battle struct {
	HabitOne *Habit
	HabitTwo *Habit
	Code     BattleCode
	Winner   string
}

func CreateChallenge

func CreateChallenge(h *Habit) *Battle

func JoinBattle

func JoinBattle(h *Habit, b *Battle) (*Battle, error)

func (*Battle) DetermineWinner

func (b *Battle) DetermineWinner() string

func (*Battle) IsPending

func (b *Battle) IsPending() bool

type BattleCode

type BattleCode string

type Habit

type Habit struct {
	HabitName     string
	Streak        int
	LastPerformed time.Time
	Username      string
}

func (*Habit) Perform

func (h *Habit) Perform()

type HabitID

type HabitID string

type LocalTracker

type LocalTracker struct {
	Store Store
}

func NewTracker

func NewTracker(s Store) *LocalTracker

func (*LocalTracker) DisplayHabits

func (t *LocalTracker) DisplayHabits(username Username) []string

func (*LocalTracker) GetBattleAssociations

func (t *LocalTracker) GetBattleAssociations(username Username, habitID HabitID) []BattleCode

func (*LocalTracker) JoinBattle

func (t *LocalTracker) JoinBattle(code BattleCode, username Username, habitID HabitID) error

func (*LocalTracker) PerformHabit

func (t *LocalTracker) PerformHabit(username Username, habitID HabitID) (int, error)

func (*LocalTracker) RegisterBattle

func (t *LocalTracker) RegisterBattle(username Username, habitID HabitID) (BattleCode, error)

type RPCClient

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

func NewRPCClient

func NewRPCClient(p int) (*RPCClient, error)

func (*RPCClient) DisplayHabits

func (c *RPCClient) DisplayHabits(username Username) []string

func (*RPCClient) GetBattleAssociations

func (c *RPCClient) GetBattleAssociations(username Username, habitID HabitID) []BattleCode

func (*RPCClient) JoinBattle

func (c *RPCClient) JoinBattle(code BattleCode, username Username, habitID HabitID) error

func (*RPCClient) PerformHabit

func (c *RPCClient) PerformHabit(username Username, habitID HabitID) (int, error)

func (*RPCClient) RegisterBattle

func (c *RPCClient) RegisterBattle(username Username, habitID HabitID) (BattleCode, error)

type Server

type Server struct {
	habitpb.UnimplementedHabitServiceServer

	Tracker
	// contains filtered or unexported fields
}

func NewServer

func NewServer(tracker Tracker) (*Server, error)

func (*Server) Client

func (s *Server) Client() *RPCClient

func (*Server) DisplayHabits

func (*Server) JoinBattle

func (s *Server) JoinBattle(ctx context.Context, req *habitpb.BattleRequest) (*habitpb.BattleResponse, error)

func (*Server) PerformHabit

func (*Server) RegisterBattle

func (s *Server) RegisterBattle(ctx context.Context, req *habitpb.BattleRequest) (*habitpb.BattleResponse, error)

type Store

type Store interface {
	GetHabit(Username, HabitID) (*Habit, error)
	UpdateHabit(*Habit) error
	ListHabits(Username) ([]*Habit, error)
	GetBattle(BattleCode) (*Battle, error)
	UpdateBattle(*Battle) error
	ListBattlesByUser(Username) ([]*Battle, error)
}

type TimeOption

type TimeOption func() time.Time

type Tracker

type Tracker interface {
	PerformHabit(Username, HabitID) (int, error)
	DisplayHabits(Username) []string
	RegisterBattle(Username, HabitID) (BattleCode, error)
	JoinBattle(BattleCode, Username, HabitID) error
	GetBattleAssociations(Username, HabitID) []BattleCode
}

type Username

type Username string

Directories

Path Synopsis
proto
stores

Jump to

Keyboard shortcuts

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