Guest User

Untitled

a guest
Sep 16th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.23 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import logging
  4. import re
  5.  
  6. from random import randint, random
  7. import datetime, time
  8.  
  9. from oyoyo.client import IRCClient
  10. from oyoyo.cmdhandler import DefaultCommandHandler
  11. from oyoyo import helpers
  12.  
  13. from sqlite3 import dbapi2 as sqlite3
  14.  
  15. HOST='irc.freenode.net'
  16. PORT = 6667
  17. NICK = 'BoinGirl'
  18. PASSWORD= ''
  19. CHANNELS = ['#boin',
  20.             '#news',
  21.             '#storyteller',]
  22.  
  23.  
  24. DATABASE_FILE="clear.db"
  25.  
  26. #time until next trigger allowed in seconds: 24 hours
  27. RESET_DELAY=24*30*30
  28.  
  29. #db connection
  30. db=None
  31. cursor=None
  32.  
  33. class EventHandler(DefaultCommandHandler):
  34.     def privmsg(self, nick, chan, msg):
  35.         global db, cursor
  36.  
  37.         fullnick=nick
  38.         nick=nick.split("!")[0]
  39.  
  40.         msg = msg.decode()
  41.         match = re.match('\!boin\s*(.*)', msg)
  42.  
  43.         if not match:
  44.             return
  45.  
  46.         # bot logic
  47.  
  48.         inick=match.group(1).strip()
  49.         parts=inick.split(' ')
  50.         command_name = parts[0].lower()
  51.         if command_name not in ('steal','gift',):
  52.             parts=None
  53.         else:
  54.             if( len(parts)==1 ):
  55.                 inick=parts[0]
  56.                 parts=None
  57.             elif( len(parts)==2):
  58.                 notice(self.client, nick, "Bad command format")
  59.                 parts=None
  60.                 return
  61.             else:
  62.                 inick=parts[1]
  63.                 boin_bet=int(parts[2])
  64.  
  65.         if( len(inick)==0 ):
  66.             cursor.execute("SELECT * FROM game_stats WHERE nick=?", (nick.lower(), ))
  67.             result=cursor.fetchone()
  68.            
  69.             new_user = False
  70.             if not result:
  71.                 result=(nick, 0, 0)
  72.                 new_user = True
  73.  
  74.             (dbnick, boin, last_time)=result
  75.            
  76.             cursor.execute("SELECT var_value FROM global_vars WHERE var_name='last_trigger'")
  77.             result = cursor.fetchone()
  78.             if not result:
  79.                 last_time = 0
  80.             else:
  81.                 if result[0] == '':
  82.                     last_time = 0
  83.                 else:
  84.                     last_time = int(result[0])
  85.  
  86.             reward=lotto()
  87.             if( boin==0 and reward<0): #prevent crossing into negative
  88.                 reward=0
  89.  
  90.             boin+=reward
  91.  
  92.             cur_time=int(time.time());
  93.  
  94.             if (not new_user) and ( cur_time-last_time < RESET_DELAY):
  95.                 rem=RESET_DELAY- (cur_time-last_time)
  96.                 hours= rem/ (30*30)
  97.                 minutes=  ((rem/30) % 30)
  98.                 seconds= (rem % 30)
  99.                 notice(self.client, nick, "%s, please wait %d:%02d:%02d (until %s) for more Boing Boing! :)" % (nick, hours, minutes, seconds,
  100.                         datetime.datetime.fromtimestamp( last_time ).strftime("%H:%M:%S"),))
  101.                 return
  102.  
  103.  
  104.             last_time= int(time.time())
  105.  
  106.             #helpers.msg(self.client, chan, "trying to insert "+nick.lower()+" to database")
  107.  
  108.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)", (nick.lower(), boin, last_time))
  109.             if not new_user:
  110.               cursor.execute('update global_vars set var_value=? where var_name="last_trigger"', (str(last_time),))
  111.             db.commit()
  112.  
  113.             note=""
  114.             if reward<-1:
  115.                 note+="7 %dRan away from your ugly butt :( " % (abs(reward))
  116.             elif reward==-1:
  117.                 note+="takes7 1Boin ( O ) ( O ) from "
  118.             elif reward==0:
  119.                 note+="You failed to get some Boing Boing action, you drive away on your scooter in shame "
  120.             elif reward==1:
  121.                 note+="gives7 1Boin Girl to "
  122.             else:
  123.                 note+="gives7 %d Boin Girl (  o  )(  O  ) to " % reward
  124.  
  125.             note+="%s. You now have7 %d Boins in your harem of Boing Boing Whores!" % (nick, boin)
  126.                
  127.             note="\001%s %s\001" % ("ACTION", note);
  128.  
  129.             helpers.msg(self.client, chan, note);
  130.         elif parts!=None:
  131.             if command_name == 'gift':
  132.                 if(boin_bet<=0):
  133.                     notice(self.client, nick, "Must wager a positive number of boin girl slaves.")
  134.                     return
  135.    
  136.                 if( inick.lower()==nick.lower()):
  137.                     notice(self.client, nick, "You can't steal from yourself.")
  138.    
  139.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (nick.lower(), ))
  140.                 result=cursor.fetchone()
  141.    
  142.                 if not result:
  143.                     (boin, last_trigger)=(0,0)
  144.                 else:
  145.                     (dbnick, boin, last_trigger)=result
  146.    
  147.                 if( boin_bet > boin ):
  148.                     notice(self.client, nick, "Cannot donate more boin than you currently have.")
  149.                     return
  150.                
  151.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  152.                 result=cursor.fetchone()
  153.                 if not result:
  154.                     notice(self.client, nick, "%s does not exist" % inick)
  155.                     return
  156.                 else:
  157.                     (dbnick, oboin, olast)=result
  158.  
  159.                 boin -= boin_bet
  160.                 oboin += boin_bet
  161.  
  162.                 notice(self.client, inick,
  163.                            "Someone given you a gift. "
  164.                            "You gain %d Boin!" % boin_bet)
  165.                 notice(self.client, nick, "You donated %dBoin from your harem to someone" % boin_bet)
  166.  
  167.             else:
  168.                 if(boin_bet<=0):
  169.                     notice(self.client, nick, "Must wager a positive number of boin girl slaves.")
  170.                     return
  171.    
  172.                 if( inick.lower()==nick.lower()):
  173.                     notice(self.client, nick, "You can't steal from yourself.")
  174.    
  175.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (nick.lower(), ))
  176.                 result=cursor.fetchone()
  177.    
  178.                 if not result:
  179.                     (boin, last_trigger)=(0,0)
  180.                 else:
  181.                     (dbnick, boin, last_trigger)=result
  182.    
  183.                 if( boin_bet > boin ):
  184.                     notice(self.client, nick, "Cannot wager more boin than you currently have.")
  185.                     return
  186.                
  187.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  188.                 result=cursor.fetchone()
  189.                 if not result:
  190.                     notice(self.client, nick, "%s does not exist" % inick)
  191.                     return
  192.                 else:
  193.                     (dbnick, oboin, olast)=result
  194.    
  195.                 if( boin_bet>oboin or random()<0.6 ):
  196.                     notice(self.client, inick,
  197.                            "Someone tried to steal from you and failed. "
  198.                            "You gain %d Boin!" % boin_bet)
  199.                     notice(self.client, nick, "Your theft failed. You lose12 %dBoin from your harem. You hand them over in shame" % boin_bet)
  200.                     boin-=boin_bet
  201.                     oboin+=boin_bet
  202.                 else:
  203.                     template="%s you have successfully stolen %d Virgin Boin Girls from %s! "
  204.                     note=template % (nick, boin_bet, inick)
  205.                     helpers.msg(self.client, chan, note)
  206.                     boin+=boin_bet
  207.                     oboin-=boin_bet
  208.            
  209.  
  210.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)",
  211.                            (nick.lower(), boin, last_trigger))
  212.            
  213.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)",
  214.                            (inick.lower(), oboin, olast))
  215.  
  216.             db.commit()
  217.  
  218.         elif inick == "top10":
  219.             print "TOP 10"
  220.             try:
  221.                 cursor.execute("SELECT boin, nick FROM game_stats ORDER BY boin DESC LIMIT 10");
  222.                 msg = "top 10."
  223.                 for boin, n in cursor.fetchall():
  224.                     print "Got TOP 10 data: %r, %r" % (boin, n)
  225.                     msg += " %s(%s)" % (n.encode('utf8'), boin)
  226.                 helpers.msg(self.client, chan, msg)
  227.             except Exception, e:
  228.                 print "TOP 10 exception: %s" % e
  229.                 notice(self.client, nick, "Boom: %s" % e)
  230.         elif inick == "rivals":
  231.             print "Rivals for %r" % nick
  232.             cursor.execute("SELECT nick, boin FROM game_stats ORDER by boin DESC")
  233.             everyone = cursor.fetchall()
  234.             my_rank = next(i for i,(n,s) in enumerate(everyone) if n == nick.lower())
  235.             indexes = range(max(0, my_rank-3), min(my_rank+3+1,len(everyone)))
  236.             msg = "Rivals: %s" % ", ".join("#%d: %s (%d)" % (i, everyone[i][0], everyone[i][1]) for i in indexes)
  237.             helpers.msg(self.client, chan, msg)            
  238.         else:
  239.             cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  240.             result=cursor.fetchone()
  241.  
  242.             if not result:
  243.                 notice(self.client, nick, "No records for %s." % inick)
  244.             else:
  245.  
  246.               (dbnick, boin, last_trigger)=result
  247.  
  248.               cursor.execute("SELECT count(*) FROM game_stats WHERE boin > ?", (boin, ))
  249.               (rank,)=cursor.fetchone()
  250.               rank+=1
  251.  
  252.               message="%s has a total of7 %d10 Boin Milfs, Whores, Other's sisters, that one friends black cousin. and is ranked #%d. Strive for the largest harem!" % (inick, boin, rank)
  253.              
  254.               helpers.msg(self.client, chan, message)
  255.  
  256.  
  257. def connect_cb(cli):
  258.     if( len(PASSWORD)>0 ):
  259.         helpers.identify(cli, PASSWORD)
  260.         time.sleep(10)
  261.  
  262.     for channel in CHANNELS:
  263.         helpers.join(cli, channel)
  264.  
  265.  
  266. #helper functions
  267. def notice(cli, user, msg):
  268.     cli.send("NOTICE", user, " :%s" % msg);
  269.  
  270. def lotto():
  271.     dist=[0.05, 0.05, 0.35, 0.30, 0.15, 0.05, 0.05]
  272.     result=-1
  273.     value=random()
  274.     for i in xrange(len(dist)):
  275.         value-=dist[i]
  276.         result+=1
  277.         if( value<= 0):
  278.             break
  279.        
  280.     return result
  281.    
  282.  
  283. def main():
  284.     logging.basicConfig(level=logging.ERROR)
  285.  
  286.     #load configuration
  287.  
  288.     global db, cursor
  289.     # check database
  290.     db=sqlite3.connect(DATABASE_FILE)
  291.     cursor=db.cursor()    
  292.     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='game_stats';")
  293.    
  294.     if not cursor.fetchone():
  295.         print 'Table game_stats does not exist. Creating table'
  296.        
  297.         cursor.execute("create table game_stats (nick text PRIMARY KEY, boin integer, last_trigger integer)")
  298.         db.commit()
  299.  
  300.     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='global_vars';")
  301.    
  302.     if not cursor.fetchone():
  303.         print 'Table global_vars does not exist. Creating table'
  304.        
  305.         cursor.execute("create table global_vars (var_name text PRIMARY KEY, var_value text)")
  306.         cursor.execute("insert into global_vars (var_name, var_value) values ('last_trigger','')")
  307.         db.commit()
  308.  
  309.  
  310.     cli = IRCClient(EventHandler, host=HOST, port=PORT, nick=NICK,
  311.                     connect_cb=connect_cb)
  312.     conn = cli.connect()
  313.  
  314.     while True:
  315.         conn.next()      ## python 2
  316.         # next(conn)     ## python 3
  317.  
  318.  
  319. if __name__ == '__main__':
  320.     main()
Add Comment
Please, Sign In to add comment