From 32af76bca57b93a1ffc3591a8d28efaae335d55c Mon Sep 17 00:00:00 2001 From: BrofessionalBrogrammer Date: Tue, 24 Nov 2015 09:29:10 -0800 Subject: [PATCH] Added comments --- drug_bot.py | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/drug_bot.py b/drug_bot.py index 08b2ee8..dc42cf4 100644 --- a/drug_bot.py +++ b/drug_bot.py @@ -12,18 +12,21 @@ # 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 _pickle +#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 drugs for first time, or load existing file if not "inventory" in f: f["inventory"] = {} f["inventory"]["money"] = 500 @@ -40,7 +43,7 @@ class Dealer: self.inventory = f["inventory"] f.close() - + #sell function, writes to save file def sell(self, drug, price, amount): f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True) if (int(f["inventory"][drug] >= 1)): @@ -49,12 +52,13 @@ class Dealer: drug_respond = str(drug) drug_respond_amount = str(f["inventory"][drug]) 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")) f.sync() f.close() else: sendmsg(channel, "Not enough drugs") - + #buy function def buy(self, drug, price, amount): f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True) if (int((f["inventory"]["money"])) >= (int(amount) * int(price))): @@ -63,6 +67,7 @@ class Dealer: drug_respond = str(drug) drug_respond_amount = str(f["inventory"][drug]) 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")) f.sync() f.close() @@ -70,12 +75,11 @@ class Dealer: sendmsg(channel, "Not enough cash") - + #allows user to check amounts of resources def check_amount(self, drug): f = shelve.open("{}.dat".format(self.name), flag="c", writeback=True) if drug is "heroin": - x = f["inventory"]['heroin'] - return(x) + return(f["inventory"]['heroin']) f.sync() f.close() if drug is "cocaine": @@ -102,26 +106,29 @@ channel = "#lainchan" botnick = "drug-bot" password = "" #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")) +#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 "+ botnick +" "+ botnick +" "+ botnick +" :connected\n", "UTF-8")) ircsock.send(bytes("NICK "+ botnick +"\n", "UTF-8")) ircsock.send(bytes("NICKSERV :IDENTIFY %s\r\n" % password, "UTF-8")) -time.sleep(3) -joinchan(channel) +#sleep for 2 to give server time to process +time.sleep(2) +joinchan(channel) #joins channel readbuffer = "" +#main loop while 1: + #assigns random drug prices drug_price = random.randrange(5,15,1) - def answer_query(): - return(random.choice(responses)) - msg = answer_query() + #function to send messages to IRC channel def sendmsg(chan , msg): ircsock.send(bytes("PRIVMSG "+ chan +" :"+ msg +"\n", "UTF-8")) - + #creates a buffer to process the data readbuffer = readbuffer+ircsock.recv(1024).decode("UTF-8") + #splits into lines then prints out in shell temp = str.split(readbuffer, "\n") print(readbuffer) readbuffer=temp.pop( ) @@ -130,29 +137,41 @@ while 1: 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[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"): + #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) + #drug wanted drug_query = line[5] + #amount of drug 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) if(line[3] == ":.buy"): + #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 drug drug_query = line[5] + #amount of drug 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) if(line[3] == ":.check"): sender = "" @@ -161,12 +180,13 @@ while 1: break if(char != ":"): sender += char + current_player = Dealer(sender) 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 == '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'): sendmsg(channel, (sender + ", you have " + str(current_player.check_amount('meth')) + " meth left" +"\n")) elif(requested_item == 'money'):