forked from Monacraft/AnkiDiscord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
162 lines (135 loc) · 3.91 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from anki.hooks import addHook
from aqt import mw, gui_hooks
from aqt.utils import showInfo
from aqt.qt import *
from . import rpc
import time
# Rich presence connection
client_id = '583084701510533126'
# Check if discord is running, prevent error message
connected = True
try:
rpc_obj = rpc.DiscordIpcClient.for_platform(client_id)
except:
connected = False
pass
# Start time to show elapsed time
start_time = round(time.time())
# Globals
dueMessage = ""
skipEdit = 0
skipAnswer = 0
##### UPDATE: Changes the RPC status
# state is N. of cards due
# details is current activity
# pic is for mini-icon related to current activity
#
def update(state, details, pic):
activity = {
"state": state,
"details": details,
"timestamps": {
"start": start_time
},
"assets": {
"small_text": "Flashcards",
"small_image": pic,
"large_text": "Anki",
"large_image": "anki"
}
}
# send to server:
try:
rpc_obj.set_activity(activity)
except:
connected = False
pass
##### DUETODAY: Calculates reviews due
# Stored in global variable 'dueMessage'
# Don't call before Anki has loaded
#
def dueToday():
# Globals and reset variables
global dueMessage
dueCount = 0
# Loop through deckDueTree to find cards due
if mw.col == None:
pass # Patch: Crash on new Version
else:
for i in mw.col.sched.deckDueTree():
name, did, due, lrn, new, children = i
dueCount += due + lrn + new
# Correct for single or no cards
if dueCount == 0:
dueMessage = "No cards left"
elif dueCount == 1:
dueMessage = "(" + str(dueCount) + " card left)"
else:
dueMessage = "(" + str(dueCount) + " cards left)"
##### ONSTATE: Updates with state of anki
# Takes current state and oldState from hook
# If opening browse, skips 'edit' hook
# List of base STATES:
# - deckBrowser
# - review
# - overview (ignored)
#
def onState(state, oldState):
global skipEdit
# Check if connected
if connected:
# Update numbe due
dueToday()
# debug for states
#showInfo(state + ", " + oldState)
# Check states:
if state == "deckBrowser":
update(dueMessage, "Chilling in the menus", "zzz")
if state == "review":
update(dueMessage, "Daily reviews", "tick-dark")
if state == "browse":
skipEdit = 1
update(dueMessage, "Browsing decks", "search")
if state == "edit":
update(dueMessage, "Adding cards", "ellipsis-dark")
##### Simulated states
## onBrowse --> when loading browser menu
#
def onBrowse(x):
onState("browse", "dummy")
#
#
## onEdit --> when loading editor
#
def onEditor(x, y):
global skipEdit
# if skipEdit 1, opening browse
if skipEdit == 0:
onState("edit", "dummy")
# reset
skipEdit = 0
#
#
## onAnswer --> new answer (update cards left)
#
def onAnswer():
global skipAnswer
# Skip every 3 cards, unneccesary load
if skipAnswer >= 2:
onState("review", "review")
skipAnswer = 0
skipAnswer += 1
##### Adding Hooks
# afterStateChange --> base states
# browser.setupMenus --> loading browser
# setupEditorShortcuts --> editor (in browser and add)
# showAnswer --> new answer
# AddCards.onHistory --> opening browser via Add Cards
# (Note: Decided to remove last one since obsolete)
#
#addHook("afterStateChange", onState)
gui_hooks.state_did_change.append(onState) # New Hook for new Version
addHook("browser.setupMenus", onBrowse)
addHook("setupEditorShortcuts", onEditor)
addHook("showAnswer", onAnswer)
#addHook("AddCards.onHistory", onEditor)