Added comments
This commit is contained in:
parent
3829c91dfc
commit
32af76bca5
52
drug_bot.py
52
drug_bot.py
@ -12,18 +12,21 @@
|
|||||||
# You should have received a copy of the GNU General Public License along
|
# 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.,
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import shelve
|
import shelve
|
||||||
import _pickle
|
import _pickle
|
||||||
|
|
||||||
|
#dealer class
|
||||||
class Dealer:
|
class Dealer:
|
||||||
|
#init assigns everyone a set amount of resources
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
#create unique save files for everyone
|
||||||
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
||||||
f["name"] = self.name
|
f["name"] = self.name
|
||||||
|
#assign drugs for first time, or load existing file
|
||||||
if not "inventory" in f:
|
if not "inventory" in f:
|
||||||
f["inventory"] = {}
|
f["inventory"] = {}
|
||||||
f["inventory"]["money"] = 500
|
f["inventory"]["money"] = 500
|
||||||
@ -40,7 +43,7 @@ class Dealer:
|
|||||||
self.inventory = f["inventory"]
|
self.inventory = f["inventory"]
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
#sell function, writes to save file
|
||||||
def sell(self, drug, price, amount):
|
def sell(self, drug, price, amount):
|
||||||
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
||||||
if (int(f["inventory"][drug] >= 1)):
|
if (int(f["inventory"][drug] >= 1)):
|
||||||
@ -49,12 +52,13 @@ class Dealer:
|
|||||||
drug_respond = str(drug)
|
drug_respond = str(drug)
|
||||||
drug_respond_amount = str(f["inventory"][drug])
|
drug_respond_amount = str(f["inventory"][drug])
|
||||||
drug_respond_cash = str(f["inventory"]["money"])
|
drug_respond_cash = str(f["inventory"]["money"])
|
||||||
|
#responds to channel, then syncs
|
||||||
sendmsg(channel, ("You have " + drug_respond_amount + " " + str(drug) + " left, and $" + drug_respond_cash + " left"))
|
sendmsg(channel, ("You have " + drug_respond_amount + " " + str(drug) + " left, and $" + drug_respond_cash + " left"))
|
||||||
f.sync()
|
f.sync()
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
else:
|
||||||
sendmsg(channel, "Not enough drugs")
|
sendmsg(channel, "Not enough drugs")
|
||||||
|
#buy function
|
||||||
def buy(self, drug, price, amount):
|
def buy(self, drug, price, amount):
|
||||||
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
||||||
if (int((f["inventory"]["money"])) >= (int(amount) * int(price))):
|
if (int((f["inventory"]["money"])) >= (int(amount) * int(price))):
|
||||||
@ -63,6 +67,7 @@ class Dealer:
|
|||||||
drug_respond = str(drug)
|
drug_respond = str(drug)
|
||||||
drug_respond_amount = str(f["inventory"][drug])
|
drug_respond_amount = str(f["inventory"][drug])
|
||||||
drug_respond_cash = str(f["inventory"]["money"])
|
drug_respond_cash = str(f["inventory"]["money"])
|
||||||
|
#responds to channel, then syncs
|
||||||
sendmsg(channel, ("You have " + drug_respond_amount + " " + str(drug) + " left, and $" + drug_respond_cash + " left"))
|
sendmsg(channel, ("You have " + drug_respond_amount + " " + str(drug) + " left, and $" + drug_respond_cash + " left"))
|
||||||
f.sync()
|
f.sync()
|
||||||
f.close()
|
f.close()
|
||||||
@ -70,12 +75,11 @@ class Dealer:
|
|||||||
sendmsg(channel, "Not enough cash")
|
sendmsg(channel, "Not enough cash")
|
||||||
|
|
||||||
|
|
||||||
|
#allows user to check amounts of resources
|
||||||
def check_amount(self, drug):
|
def check_amount(self, drug):
|
||||||
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True)
|
||||||
if drug is "heroin":
|
if drug is "heroin":
|
||||||
x = f["inventory"]['heroin']
|
return(f["inventory"]['heroin'])
|
||||||
return(x)
|
|
||||||
f.sync()
|
f.sync()
|
||||||
f.close()
|
f.close()
|
||||||
if drug is "cocaine":
|
if drug is "cocaine":
|
||||||
@ -102,26 +106,29 @@ channel = "#lainchan"
|
|||||||
botnick = "drug-bot"
|
botnick = "drug-bot"
|
||||||
password = ""
|
password = ""
|
||||||
#all bytes must be converted to UTF-8
|
#all bytes must be converted to UTF-8
|
||||||
def joinchan(chan):
|
def joinchan(chan): #channel join function
|
||||||
ircsock.send(bytes("JOIN "+ chan +"\n", "UTF-8"))
|
ircsock.send(bytes("JOIN "+ chan +"\n", "UTF-8"))
|
||||||
|
|
||||||
|
#creates sockets, assigns nicks and usernames, and identifies
|
||||||
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
ircsock.connect((server, 6667))
|
ircsock.connect((server, 6667))
|
||||||
ircsock.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick +" :connected\n", "UTF-8"))
|
ircsock.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick +" :connected\n", "UTF-8"))
|
||||||
ircsock.send(bytes("NICK "+ botnick +"\n", "UTF-8"))
|
ircsock.send(bytes("NICK "+ botnick +"\n", "UTF-8"))
|
||||||
ircsock.send(bytes("NICKSERV :IDENTIFY %s\r\n" % password, "UTF-8"))
|
ircsock.send(bytes("NICKSERV :IDENTIFY %s\r\n" % password, "UTF-8"))
|
||||||
time.sleep(3)
|
#sleep for 2 to give server time to process
|
||||||
joinchan(channel)
|
time.sleep(2)
|
||||||
|
joinchan(channel) #joins channel
|
||||||
readbuffer = ""
|
readbuffer = ""
|
||||||
|
#main loop
|
||||||
while 1:
|
while 1:
|
||||||
|
#assigns random drug prices
|
||||||
drug_price = random.randrange(5,15,1)
|
drug_price = random.randrange(5,15,1)
|
||||||
def answer_query():
|
#function to send messages to IRC channel
|
||||||
return(random.choice(responses))
|
|
||||||
msg = answer_query()
|
|
||||||
def sendmsg(chan , msg):
|
def sendmsg(chan , msg):
|
||||||
ircsock.send(bytes("PRIVMSG "+ chan +" :"+ msg +"\n", "UTF-8"))
|
ircsock.send(bytes("PRIVMSG "+ chan +" :"+ msg +"\n", "UTF-8"))
|
||||||
|
#creates a buffer to process the data
|
||||||
readbuffer = readbuffer+ircsock.recv(1024).decode("UTF-8")
|
readbuffer = readbuffer+ircsock.recv(1024).decode("UTF-8")
|
||||||
|
#splits into lines then prints out in shell
|
||||||
temp = str.split(readbuffer, "\n")
|
temp = str.split(readbuffer, "\n")
|
||||||
print(readbuffer)
|
print(readbuffer)
|
||||||
readbuffer=temp.pop( )
|
readbuffer=temp.pop( )
|
||||||
@ -130,29 +137,41 @@ while 1:
|
|||||||
line = str.rstrip(line)
|
line = str.rstrip(line)
|
||||||
line = str.split(line)
|
line = str.split(line)
|
||||||
try:
|
try:
|
||||||
|
#respond to pings
|
||||||
if(line[0] == "PING"):
|
if(line[0] == "PING"):
|
||||||
ircsock.send(bytes("QUOTE :PONG %s\r\n" % line[7], "UTF-8"))
|
ircsock.send(bytes("QUOTE :PONG %s\r\n" % line[1], "UTF-8"))
|
||||||
|
#if it finds .sell, it calls the function from the drug user class
|
||||||
if(line[3] == ":.sell"):
|
if(line[3] == ":.sell"):
|
||||||
|
#get name of person who called command
|
||||||
sender = ""
|
sender = ""
|
||||||
for char in line[0]:
|
for char in line[0]:
|
||||||
if(char == "!"):
|
if(char == "!"):
|
||||||
break
|
break
|
||||||
if(char != ":"):
|
if(char != ":"):
|
||||||
sender += char
|
sender += char
|
||||||
|
#assigns player a save file
|
||||||
current_player = Dealer(sender)
|
current_player = Dealer(sender)
|
||||||
|
#drug wanted
|
||||||
drug_query = line[5]
|
drug_query = line[5]
|
||||||
|
#amount of drug
|
||||||
drug_amount = line[4]
|
drug_amount = line[4]
|
||||||
|
#calls the sell function from dealer class, passing in data collected above
|
||||||
current_player.sell(drug_query, drug_price, drug_amount)
|
current_player.sell(drug_query, drug_price, drug_amount)
|
||||||
if(line[3] == ":.buy"):
|
if(line[3] == ":.buy"):
|
||||||
|
#get name of person who called command
|
||||||
sender = ""
|
sender = ""
|
||||||
for char in line[0]:
|
for char in line[0]:
|
||||||
if(char == "!"):
|
if(char == "!"):
|
||||||
break
|
break
|
||||||
if(char != ":"):
|
if(char != ":"):
|
||||||
sender += char
|
sender += char
|
||||||
|
#assigns player a save file
|
||||||
current_player = Dealer(sender)
|
current_player = Dealer(sender)
|
||||||
|
#type of drug
|
||||||
drug_query = line[5]
|
drug_query = line[5]
|
||||||
|
#amount of drug
|
||||||
drug_amount = line[4]
|
drug_amount = line[4]
|
||||||
|
#calls the buy function from dealer class, passing in data collected above
|
||||||
current_player.buy(drug_query, drug_price, drug_amount)
|
current_player.buy(drug_query, drug_price, drug_amount)
|
||||||
if(line[3] == ":.check"):
|
if(line[3] == ":.check"):
|
||||||
sender = ""
|
sender = ""
|
||||||
@ -161,12 +180,13 @@ while 1:
|
|||||||
break
|
break
|
||||||
if(char != ":"):
|
if(char != ":"):
|
||||||
sender += char
|
sender += char
|
||||||
|
|
||||||
current_player = Dealer(sender)
|
current_player = Dealer(sender)
|
||||||
requested_item = line[4]
|
requested_item = line[4]
|
||||||
#this is the part i'm testing to try and get working
|
#returns the value of the requested item
|
||||||
if requested_item:
|
if requested_item:
|
||||||
if(requested_item == 'heroin'):
|
if(requested_item == 'heroin'):
|
||||||
sendmsg(channel, (sender + "you have " + str(current_player.check_amount('heroin')) + " heroin left" +"\n"))
|
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('heroin')) + " heroin left" +"\n"))
|
||||||
elif(requested_item == 'meth'):
|
elif(requested_item == 'meth'):
|
||||||
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('meth')) + " meth left" +"\n"))
|
sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('meth')) + " meth left" +"\n"))
|
||||||
elif(requested_item == 'money'):
|
elif(requested_item == 'money'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user