oirc/modules/rpn.py

94 lines
3.2 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
2020-06-05 20:37:30 +00:00
def isfloat(value):
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):
if chan not in self.rpnhist:
self.rpnhist[chan] = [0]
2020-06-07 01:40:02 +00:00
try:
2020-10-22 01:54:33 +00:00
msg = msg.replace('+',' + ')
msg = msg.replace('a',' a ')
#msg = msg.replace('-',' - ')
2020-10-22 01:54:33 +00:00
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:]
2020-08-05 19:02:58 +00:00
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]
2020-10-22 01:19:05 +00:00
elif m == '\\':
self.rpnhist[chan].insert(0,self.rpnhist[chan][0])
2020-08-05 19:02:58 +00:00
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, '[\x036rpn\x0f] {}'.format(str(self.rpnhist[chan])))
return
else:
return
2020-06-07 01:40:02 +00:00
except OverflowError:
2020-06-08 15:45:20 +00:00
if chan in self.rpnprint:
await self.message(chan, '[\x036rpn\x0f] no u ur numbers are too phat')
2020-06-05 20:58:34 +00:00
return
2020-06-08 15:45:20 +00:00
if chan in self.rpnprint:
await self.message(chan, '[\x036rpn\x0f] '+str(self.rpnhist[chan][0]))
2020-10-22 01:54:33 +00:00
async def rpntinp(self, chan, nick, msg):
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-06-08 15:45:20 +00:00
async def rpntoggle(self, chan, nick, msg):
if chan in self.rpnprint:
self.rpnprint.remove(chan)
await self.message(chan, '[\x036rpn\x0f] rpn outputting has been disabled')
else:
self.rpnprint.append(chan)
await self.message(chan, '[\x036rpn\x0f] rpn outputting has been enabled')
2020-06-05 20:37:30 +00:00
async def init(self):
#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
2020-06-05 20:37:30 +00:00
self.rpnhist = {}
2020-06-08 15:45:20 +00:00
self.rpnprint = []