-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheet_commands.py
177 lines (132 loc) · 5.6 KB
/
sheet_commands.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from core import isNewCommand, checkAccess, isNewSeeds, getTime, debug
from session import saveConfig, loadConfig, send, reply, auth
from utility import fValue, fNonEmpty, fLower
from datetime import date
from core import getWeights
import numpy, re
# archive
def archive(update, context):
print("> archive")
if not isNewCommand(update): return
if not checkAccess(update): return
position = int(update.message.text.split(" ")[1])
archiveDo(context.bot, position)
def archiveDo(bot, position):
if debug:
send(bot, "Suggestion moved to the archive. (not really)")
return # todo move bot to a test sheet
wks = auth()
now = getTime()
archiveNames = list(filter(fNonEmpty, map(fValue, wks.range('G4:G1300'))))
suggestionNames = list(filter(fNonEmpty, map(fValue, wks.range('B4:B100'))))
lastSuggestion = len(suggestionNames) + 4
archiveLastPosition = len(archiveNames) + 4
rolledCells = wks.range('B'+str(position)+':E'+ str(position))
rolled = list(map(fValue, rolledCells))
# move archive cells down 1 row
archiveOldCells = wks.range('F4:L'+str(archiveLastPosition+1))
lenArchiveOld = len(archiveOldCells)
for i in reversed(range(len(archiveOldCells))):
if lenArchiveOld > i + 7:
archiveOldCells[i+7].value = archiveOldCells[i].value
wks.update_cells(archiveOldCells)
# move suggestion to F4
archiveCells = wks.range('F4:L4')
archiveCells[0].value = now.strftime("%d %b %y")
archiveCells[1].value = rolled[0]
archiveCells[2].value = rolled[1]
archiveCells[3].value = rolled[2]
archiveCells[4].value = rolled[3]
archiveCells[5].value = ""
archiveCells[6].value = ""
wks.update_cells(archiveCells)
# move suggestions up 1 row
suggestionCells = wks.range('B'+str(position)+':E'+str(lastSuggestion))
lenSuggestionCells = len(suggestionCells)
for i in range(len(suggestionCells)):
if lenSuggestionCells > i + 4:
suggestionCells[i].value = suggestionCells[i+4].value
wks.update_cells(suggestionCells)
send(bot, "Suggestion moved to the archive.")
# manage suggestions
def countSuggestions(update, context):
print("> countSuggestions")
if not isNewCommand(update): return
array = update.message.text.split(" ")
if len(array) < 2:
reply(update, "Enter the name to see how many albums he has suggested.")
return
name = update.message.text.split(" ")[1]
wks = auth()
originalRows = list(map(fValue, wks.range('B4:B100')))
foundRows = list(filter(lambda value: value.lower() == name.lower(), originalRows))
suggestionsCount = len(foundRows)
originalArchive = list(map(fValue, wks.range('G4:G1300')))
foundArchive = list(filter(lambda value: value.lower() == name.lower(), originalArchive))
archiveCount = len(foundArchive)
if suggestionsCount > 0:
if archiveCount > 0:
reply(update, "{} current suggestions by {}. Also {} in archive.".format(
suggestionsCount, foundRows[0], archiveCount))
else :
reply(update, "{} current suggestions by {}.".format(
suggestionsCount, foundRows[0]))
elif archiveCount > 0:
reply(update, "No current suggestion by {} but there are {} in archive.".format(
foundArchive[0]))
else:
reply(update, "No such thing.")
def addSuggestion(update, context):
print("> addSuggestion")
if not isNewCommand(update): return
array = re.split("[;\n]", update.message.text)
if len(array) != 4:
reply(update, "Enter suggester name, artist, year and title of release (use semicolon or new line to divide input)\nLike this: \"Yaro; Pink Floyd; The Dark Side of the Moon; 1973\"")
return
wks = auth()
# name artist year album
name = array[0].strip()[5:]
artist = array[1].strip()
album = array[2].strip()
year = int(array[3].strip())
currentYear = date.today().year
if year > currentYear or year < 1900:
reply(update, "Wrong year")
return
suggestionNames = list(filter(fNonEmpty, map(fValue, wks.range('B4:B100'))))
foundRows = list(filter(lambda value: value.lower() == name.lower(), suggestionNames))
if len(foundRows) >= 5:
reply(update, "This one already has enough suggestions.")
return
newSuggestion = len(suggestionNames) + 4
# add suggestion
newCells = wks.range('B'+str(newSuggestion)+':E'+str(newSuggestion))
newCells[0].value = name
newCells[1].value = artist
newCells[2].value = year
newCells[3].value = album
message = "{} by {} ({}) is now suggested by {}.".format(album, artist, year, name)
if debug:
reply(update, message + " (not really)")
return # todo move bot to a test sheet
wks.update_cells(newCells)
reply(update, message)
def rollInfo(update, context):
print("> rollInfo")
if not isNewCommand(update): return
wks = auth()
suggestions = list(filter(fNonEmpty, map(fValue, wks.range('B4:B100'))))
count = len(suggestions)
if count < 2: return
values = getWeights(count)
array = update.message.text.split(" ")
if len(array) > 1:
try:
position = int(array[1])
prob = values[position - 4] * 100
reply(update, "Probability of rolling {0} is {1:.1f}%".format(position, prob))
return
except Exception as e: print(e)
first = values[0] * 100
last = values[-1] * 100
reply(update, "Probability of getting rolled:\nOldest suggestion has: {:.1f}% chance\nNewest suggestion has: {:.9f}% chance".format(first, last))