oirc/modules/rpn.py

108 lines
3.7 KiB
Python
Raw Normal View History

2020-06-05 15:58:47 +00:00
import subprocess
from bot import *
2020-06-05 15:58:47 +00:00
2022-01-27 02:22:42 +00:00
2020-06-05 20:37:30 +00:00
def isfloat(value):
2022-01-27 02:22:42 +00:00
try:
float(value)
return True
except ValueError:
return False
2020-06-05 15:58:47 +00:00
2020-06-05 20:37:30 +00:00
async def rpninp(self, chan, nick, msg):
2022-01-27 02:22:42 +00:00
if chan not in self.rpnhist:
self.rpnhist[chan] = [0]
try:
msg = msg.replace("+", " + ")
msg = msg.replace("a", " a ")
# msg = msg.replace('-',' - ')
msg = msg.replace("s", " s ")
msg = msg.replace("\\", " \\ ")
msg = msg.replace("*", " * ")
msg = msg.replace("x", " x ")
msg = msg.replace("m", " m ")
msg = msg.replace("/", " / ")
msg = msg.replace("d", " d ")
msg = msg.replace("^", " ^ ")
msg = msg.replace("e", " e ")
for m in msg.split():
self.rpnhist[chan].append(0)
del self.rpnhist[chan][15:]
if isfloat(m):
self.rpnhist[chan].insert(0, float(m))
continue
elif m == "+" or m == "a":
self.rpnhist[chan][0] = self.rpnhist[chan][0] + self.rpnhist[chan].pop(
1
)
elif m == "-" or m == "s":
self.rpnhist[chan][0] = (
self.rpnhist[chan].pop(1) - self.rpnhist[chan][0]
)
elif m == "\\":
self.rpnhist[chan].insert(0, self.rpnhist[chan][0])
elif m == "*" or m == "x" or m == "m":
self.rpnhist[chan][0] = (
self.rpnhist[chan].pop(1) * self.rpnhist[chan][0]
)
elif m == "/" or m == "d":
try:
self.rpnhist[chan][0] = (
self.rpnhist[chan].pop(1) / self.rpnhist[chan][0]
)
except ZeroDivisionError:
self.rpnhist[chan][0] = float("NaN")
elif m == "^" or m == "e":
self.rpnhist[chan][0] = (
self.rpnhist[chan].pop(1) ** self.rpnhist[chan][0]
)
elif msg == "p":
pass # just dont do anything lol
elif msg == "r":
if chan in self.rpnprint:
await self.message(
chan, str(self.rpnhist[chan])
2022-01-27 02:22:42 +00:00
)
return
else:
return
except OverflowError:
if chan in self.rpnprint:
await self.message(chan, "no u ur numbers are too big chonk")
2022-01-27 02:22:42 +00:00
return
2020-06-08 15:45:20 +00:00
if chan in self.rpnprint:
await self.message(chan, str(self.rpnhist[chan][0]))
2020-06-08 15:45:20 +00:00
2020-10-22 01:54:33 +00:00
async def rpntinp(self, chan, nick, msg):
2022-01-27 02:22:42 +00:00
if chan in self.rpnprint:
await rpninp(self, chan, nick, msg)
else:
self.rpnprint.append(chan)
await rpninp(self, chan, nick, msg)
self.rpnprint.remove(chan)
2020-10-22 01:54:33 +00:00
2020-06-08 15:45:20 +00:00
async def rpntoggle(self, chan, nick, msg):
2022-01-27 02:22:42 +00:00
if chan in self.rpnprint:
self.rpnprint.remove(chan)
await self.message(chan, "rpn outputting has been disabled")
2022-01-27 02:22:42 +00:00
else:
self.rpnprint.append(chan)
await self.message(chan, "rpn outputting has been enabled")
2022-01-27 02:22:42 +00:00
2020-06-05 20:37:30 +00:00
async def init(self):
2022-01-27 02:22:42 +00:00
# self.help['rpn'] = ['rpn <inp> - simple reverse polish notation calculator (more)', 'it has an alias of . so you can just do {}. <inp>, and if enabled it will also parse floats and functions as input. there are 4 functions, add (+|a), subtract (-|s), multiply (*|x|m), and devide (/|d), and p to print register 0'.format(self.prefix)]
shared.commands["rpn"] = rpntinp
shared.commands["."] = rpntinp
shared.rawm["rpn"] = rpninp
shared.commands["rt"] = rpntoggle
# self.help['rt'] = ['rt - toggle the output of rpn calculatons into the channel', 'rpn is cool']
2020-06-05 20:58:34 +00:00
2022-01-27 02:22:42 +00:00
self.rpnhist = {}
2020-06-08 15:45:20 +00:00
2022-01-27 02:22:42 +00:00
self.rpnprint = []