spicy/spicy_bot.py
2019-01-06 10:03:03 -05:00

222 lines
8.5 KiB
Python

#!/usr/bin/env python3
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import socket
import random
import time
import shelve
import time
#dealer class
class Dealer:
#init assigns everyone a set amount of resources
def __init__(self, name):
self.name = name
#create unique save files for everyone
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
f["name"] = self.name
#assign spices for first time, or load existing file
if not "inventory" in f:
f["inventory"] = {}
f["inventory"]["twigs"] = 500
f["inventory"]["wasabi"] = 10
f["inventory"]["onions"] = 10
f["inventory"]["garlic"] = 10
f.sync()
self.inventory = {}
self.inventory["twigs"] = 500
self.inventory["wasabi"] = 10
self.inventory["onions"] = 10
self.inventory["garlic"] = 10
else:
self.inventory = f["inventory"]
f.close()
#sell function, writes to save file
def sell(self, spice, price, amount):
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
if (int(f["inventory"][spice] >= 1)):
f["inventory"][spice] -= int(amount)
f["inventory"]["twigs"] += (int(amount) * int(price))
spice_respond = str(spice)
spice_respond_amount = str(f["inventory"][spice])
spice_respond_cash = str(f["inventory"]["twigs"])
#responds to channel, then syncs
sendmsg(channel, ("You have " + spice_respond_amount + " " + str(spice) + " left, and $" + spice_respond_cash + " left"))
f.sync()
f.close()
else:
sendmsg(channel, "Sorry, missing some spices.")
#buy function
def buy(self, spice, price, amount):
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
if (int((f["inventory"]["twigs"])) >= (int(amount) * int(price))):
f["inventory"][spice] += int(amount)
f["inventory"]["twigs"] -= (int(amount) * int(price))
spice_respond = str(spice)
spice_respond_amount = str(f["inventory"][spice])
spice_respond_cash = str(f["inventory"]["twigs"])
#responds to channel, then syncs
sendmsg(channel, ("You have " + spice_respond_amount + " " + str(spice) + " left, and $" + spice_respond_cash + " left"))
f.sync()
f.close()
else:
sendmsg(channel, "Dude, you need more twigs!")
#allows user to check amounts of resources
def check_amount(self, spice):
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
if spice is "onions":
return(f["inventory"]['onions'])
f.sync()
f.close()
if spice is "garlic":
return(self.inventory['garlic'])
f.sync()
f.close()
if spice is "wasabi":
return(self.inventory['wasabi'])
f.sync()
f.close()
if spice is "twigs":
return(self.inventory['twigs'])
f.sync()
f.close()
else:
return("What do you want?")
f.close()
#******** IRC SHIT *********
server = "km3jy7nrj3e2wiju.onion"
channel = "#anarchyplanet"
botuser = "herbs"
botnick = "spicy"
password = ""
def joinchan(chan): #channel join function
ircsock.send(bytes("JOIN "+ chan +"\n"))
#creates sockets, assigns nicks and usernames, and identifies
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircsock.send(bytes("USER "+ botuser +" "+ botuser +" "+ botuser +" :connected\n"))
ircsock.send(bytes("NICK "+ botnick +"\n"))
#ircsock.send(bytes("NICKSERV :IDENTIFY %s\r\n" % password))
#sleep some to give server time to process
time.sleep(10)
joinchan(channel) #joins channel
readbuffer = ""
#main loop
while 1:
#assigns random spice prices
spice_price = random.randrange(5,15,1)
#function to send messages to IRC channel
def sendmsg(chan , msg):
ircsock.send(bytes("PRIVMSG "+ chan +" :"+ msg +"\n"))
#creates a buffer to process the data
buf = readbuffer+ircsock.recv(1024)
#splits into lines then prints out in shell
temp = str.split(readbuffer, "\n")
if readbuffer is None:
time.sleep(1)
continue
print(readbuffer)
readbuffer=temp.pop( )
for line in temp:
line = str.rstrip(line)
line = str.split(line)
try:
#respond to pings
if(line[0] == "PING"):
ircsock.send(bytes("QUOTE :PONG %s\r\n" % line[1]))
#if it finds .sell, it calls the function from the spice user class
if(line[3] == ":.help"):
#get name of person who called command
sender = ""
for char in line[0]:
if(char == "!"):
break
if(char != ":"):
sender += char
#assigns player a save file
current_player = Dealer(sender)
sendmsg(channel, ("Check your stuff with .count. Use .get and .drop to juggle them around."))
if(line[3] == ":.drop"):
#get name of person who called command
sender = ""
for char in line[0]:
if(char == "!"):
break
if(char != ":"):
sender += char
#assigns player a save file
current_player = Dealer(sender)
#spice wanted
spice_query = line[5]
#amount of spice
spice_amount = line[4]
#calls the sell function from dealer class, passing in data collected above
current_player.sell(spice_query, spice_price, spice_amount)
if(line[3] == ":.get"):
#get name of person who called command
sender = ""
for char in line[0]:
if(char == "!"):
break
if(char != ":"):
sender += char
#assigns player a save file
current_player = Dealer(sender)
#type of spice
spice_query = line[5]
#amount of spice
spice_amount = line[4]
#calls the buy function from dealer class, passing in data collected above
current_player.buy(spice_query, spice_price, spice_amount)
if(line[3] == ":.count"):
sender = ""
for char in line[0]:
if(char == "!"):
break
if(char != ":"):
sender += char
current_player = Dealer(sender)
requested_item = line[4]
#returns the value of the requested item
if requested_item:
if(requested_item == 'onions'):
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('onions')) + " onions left" +"\n"))
elif(requested_item == 'wasabi'):
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('wasabi')) + " wasabi left" +"\n"))
elif(requested_item == 'twigs'):
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('twigs')) + " twigs left" +"\n" ))
elif(requested_item == 'garlic'):
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('garlic')) + " garlic left" +"\n"))
else:
sendmsg(channel, ("Ey, use .check with onions, wasabi, garlic, or twigs"))
except:
continue