Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #390 from antistic/feat-answer-card
Browse files Browse the repository at this point in the history
add new action: answerCards
  • Loading branch information
FooSoft authored Jun 16, 2023
2 parents 41ce156 + 3892481 commit f27e7b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,44 @@ corresponding to when the API was available for use.
```
</details>
#### `answerCards`
* Answer cards. Ease is between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "answerCards",
"version": 6,
"params": {
"answers": [
{
"cardId": 1498938915662,
"ease": 2
},
{
"cardId": 1502098034048,
"ease": 4
}
]
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [true, true],
"error": null
}
```
</details>
---
### Deck Actions
Expand Down
18 changes: 18 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,24 @@ def relearnCards(self, cards):
self.stopEditing()


@util.api()
def answerCards(self, answers):
scheduler = self.scheduler()
success = []
for answer in answers:
try:
cid = answer['cardId']
ease = answer['ease']
card = self.getCard(cid)
card.start_timer()
scheduler.answerCard(card, ease)
success.append(True)
except NotFoundError:
success.append(False)

return success


@util.api()
def cardReviews(self, deck, startID):
return self.database().all(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ def test_forgetCards(setup):

def test_relearnCards(setup):
ac.relearnCards(cards=setup.card_ids)


class TestAnswerCards:
def test_answerCards(self, setup):
ac.scheduler().reset()
answers = [
{"cardId": a, "ease": b} for a, b in zip(setup.card_ids, [2, 1, 4, 3])
]
result = ac.answerCards(answers)
assert result == [True] * 4

def test_answerCards_with_invalid_card_id(self, setup):
ac.scheduler().reset()
result = ac.answerCards([{"cardId": 123, "ease": 2}])
assert result == [False]

0 comments on commit f27e7b4

Please sign in to comment.