Skip to content

Commit

Permalink
Merge pull request #25 from flimzy/mvc
Browse files Browse the repository at this point in the history
Handle card types in MVC-ish manner
  • Loading branch information
flimzy committed Jan 22, 2017
2 parents f072637 + 1c0925d commit 607a9ed
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ main.js.map
.phonegap-facebook-plugin

# go-bindata
cardmodel/ankibasic/data.go
controllers/ankibasic/data.go
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ www/js/cardframe.js: webclient/js/cardframe.js
cp $< $@

.PHONY: main.js
main.js: cardmodel/ankibasic/data.go
main.js: controllers/ankibasic/data.go
rm -rf ${GOPATH}/pkg/*_js
gopherjs build --tags=debug ./webclient/*.go
# uglifyjs main.js -c -m -o $@
Expand Down Expand Up @@ -98,5 +98,5 @@ www: javascript css images $(HTML_FILES) $(I18N_FILES)
cordova-www: www
cat www/index.html | sed -e 's/<!-- Cordova Here -->/<script src="cordova.js"><\/script>/' > www/cordova.html

cardmodel/ankibasic/data.go: $(wildcard cardmodel/ankibasic/js/*)
controllers/ankibasic/data.go: $(wildcard controllers/ankibasic/js/*)
go-bindata -pkg ankibasic -nocompress -prefix "$(dir $<)" -o $@ $(dir $<)
92 changes: 0 additions & 92 deletions cardmodel/cardmodel.go

This file was deleted.

22 changes: 0 additions & 22 deletions clientstate/clientstate.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/flimzy/log"
"github.com/pkg/errors"

"github.com/FlashbackSRS/flashback-model"
"github.com/FlashbackSRS/flashback/cardmodel"
repo "github.com/FlashbackSRS/flashback/repository"
"github.com/FlashbackSRS/flashback/webclient/views/studyview"
)

// The possible faces of an Anki card
Expand All @@ -17,59 +17,59 @@ const (
AnswerFace
)

// Model is an Anki Basic model
type Model struct{}
// AnkiBasic is the controller for the Anki Basic model
type AnkiBasic struct{}

var _ cardmodel.Model = &Model{}
var _ repo.ModelController = &AnkiBasic{}

func init() {
log.Debug("Registering anki-basic model\n")
cardmodel.RegisterModel(&Model{})
repo.RegisterModelController(&AnkiBasic{})
}

// Type returns the string "anki-basic", to identify this model handler's type.
func (m *Model) Type() string {
func (m *AnkiBasic) Type() string {
return "anki-basic"
}

// IframeScript returns JavaScript to run inside the iframe.
func (m *Model) IframeScript() []byte {
func (m *AnkiBasic) IframeScript() []byte {
data, err := Asset("script.js")
if err != nil {
panic(err)
}
return data
}

var buttonMaps = map[int]cardmodel.ButtonMap{
QuestionFace: cardmodel.ButtonMap{
cardmodel.ButtonRight: cardmodel.AnswerButton{
var buttonMaps = map[int]studyview.ButtonMap{
QuestionFace: studyview.ButtonMap{
studyview.ButtonRight: studyview.ButtonState{
Name: "Show Answer",
Enabled: true,
},
},
AnswerFace: cardmodel.ButtonMap{
cardmodel.ButtonLeft: cardmodel.AnswerButton{
AnswerFace: studyview.ButtonMap{
studyview.ButtonLeft: studyview.ButtonState{
Name: "Incorrect",
Enabled: true,
},
cardmodel.ButtonCenterLeft: cardmodel.AnswerButton{
studyview.ButtonCenterLeft: studyview.ButtonState{
Name: "Difficult",
Enabled: true,
},
cardmodel.ButtonCenterRight: cardmodel.AnswerButton{
studyview.ButtonCenterRight: studyview.ButtonState{
Name: "Correct",
Enabled: true,
},
cardmodel.ButtonRight: cardmodel.AnswerButton{
studyview.ButtonRight: studyview.ButtonState{
Name: "Easy",
Enabled: true,
},
},
}

// Buttons returns the initial button state
func (m *Model) Buttons(face int) (cardmodel.ButtonMap, error) {
func (m *AnkiBasic) Buttons(face int) (studyview.ButtonMap, error) {
buttons, ok := buttonMaps[face]
if !ok {
return nil, errors.Errorf("Invalid face %d", face)
Expand All @@ -78,11 +78,7 @@ func (m *Model) Buttons(face int) (cardmodel.ButtonMap, error) {
}

// Action responds to a card action, such as a button press
func (m *Model) Action(card *fb.Card, face *int, startTime time.Time, action cardmodel.Action) (bool, error) {
if action.Button == "" {
return false, errors.New("Invalid response; no button press")
}
button := action.Button
func (m *AnkiBasic) Action(card *repo.Card, face *int, startTime time.Time, button studyview.Button) (bool, error) {
log.Debugf("%s button pressed for face %d\n", button, *face)
if btns, ok := buttonMaps[*face]; ok {
if _, valid := btns[button]; !valid {
Expand All @@ -97,24 +93,24 @@ func (m *Model) Action(card *fb.Card, face *int, startTime time.Time, action car
return false, nil
case AnswerFace:
log.Debugf("Old schedule: Due %s, Interval: %s, Ease: %f\n", card.Due, card.Interval, card.EaseFactor)
cardmodel.Schedule(card, time.Now().Sub(startTime), quality(button))
repo.Schedule(card, time.Now().Sub(startTime), quality(button))
log.Debugf("New schedule: Due %s, Interval: %s, Ease: %f\n", card.Due, card.Interval, card.EaseFactor)
return true, nil
}
log.Printf("Unexpected face/action combo: %d / %+v\n", *face, action)
log.Printf("Unexpected face/button combo: %d / %+v\n", *face, button)
return false, nil
}

func quality(button cardmodel.Button) cardmodel.AnswerQuality {
func quality(button studyview.Button) repo.AnswerQuality {
switch button {
case cardmodel.ButtonLeft:
return cardmodel.AnswerBlackout
case cardmodel.ButtonCenterLeft:
return cardmodel.AnswerCorrectDifficult
case cardmodel.ButtonCenterRight:
return cardmodel.AnswerCorrect
case cardmodel.ButtonRight:
return cardmodel.AnswerPerfect
case studyview.ButtonLeft:
return repo.AnswerBlackout
case studyview.ButtonCenterLeft:
return repo.AnswerCorrectDifficult
case studyview.ButtonCenterRight:
return repo.AnswerCorrect
case studyview.ButtonRight:
return repo.AnswerPerfect
}
return cardmodel.AnswerBlackout
return repo.AnswerBlackout
}
File renamed without changes.
35 changes: 18 additions & 17 deletions cardmodel/mock/mock.go → controllers/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,62 @@ import (
"fmt"
"time"

"github.com/FlashbackSRS/flashback-model"
"github.com/FlashbackSRS/flashback/cardmodel"
"github.com/flimzy/log"

repo "github.com/FlashbackSRS/flashback/repository"
"github.com/FlashbackSRS/flashback/webclient/views/studyview"
)

// Model is an Anki Basic model
type Model struct {
// Mock is an Anki Basic model
type Mock struct {
t string
}

var _ cardmodel.Model = &Model{}
var _ repo.ModelController = &Mock{}

// RegisterMock registers the mock Model as the requested type, for tests.
func RegisterMock(t string) {
m := &Model{t: t}
cardmodel.RegisterModel(m)
m := &Mock{t: t}
repo.RegisterModelController(m)
}

// Type returns the string "anki-basic", to identify this model handler's type.
func (m *Model) Type() string {
func (m *Mock) Type() string {
return m.t
}

// IframeScript returns JavaScript to run inside the iframe.
func (m *Model) IframeScript() []byte {
func (m *Mock) IframeScript() []byte {
return []byte(fmt.Sprintf(`
/* Mock Model */
console.log("Mock Model '%s'");
`, m.t))
}

// Buttons returns the initial buttons state
func (m *Model) Buttons(_ int) (cardmodel.ButtonMap, error) {
return cardmodel.ButtonMap{
cardmodel.ButtonLeft: cardmodel.AnswerButton{
func (m *Mock) Buttons(_ int) (studyview.ButtonMap, error) {
return studyview.ButtonMap{
studyview.ButtonLeft: studyview.ButtonState{
Name: "Incorrect",
Enabled: true,
},
cardmodel.ButtonCenterLeft: cardmodel.AnswerButton{
studyview.ButtonCenterLeft: studyview.ButtonState{
Name: "Difficult",
Enabled: true,
},
cardmodel.ButtonCenterRight: cardmodel.AnswerButton{
studyview.ButtonCenterRight: studyview.ButtonState{
Name: "Correct",
Enabled: true,
},
cardmodel.ButtonRight: cardmodel.AnswerButton{
studyview.ButtonRight: studyview.ButtonState{
Name: "Easy",
Enabled: true,
},
}, nil
}

// Action responds to a card action, such as a button press
func (m *Model) Action(card *fb.Card, face *int, _ time.Time, action cardmodel.Action) (bool, error) {
log.Debugf("face: %d, action: %+v\n", face, action)
func (m *Mock) Action(card *repo.Card, face *int, _ time.Time, button studyview.Button) (bool, error) {
log.Debugf("face: %d, button: %+v\n", face, button)
return true, nil
}
Loading

0 comments on commit 607a9ed

Please sign in to comment.