Guest User

Untitled

a guest
Sep 16th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.09 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 for more Boing Boing! :)" % (nick, hours, minutes, seconds))
  100.                 return
  101.  
  102.             last_time= int(time.time())
  103.  
  104.  
  105.             helpers.msg(self.client, chan, "trying to insert "+nick.lower()+" to database")
  106.  
  107.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)", (nick.lower(), boin, last_time))
  108.             cursor.execute('update global_vars set var_value=? where var_name="last_trigger"', (str(last_time),))
  109.             db.commit()
  110.  
  111.             note=""
  112.             if reward<-1:
  113.                 note+="7 %dRan away from your ugly butt :( " % (abs(reward))
  114.             elif reward==-1:
  115.                 note+="takes7 1Boin ( O ) ( O ) from "
  116.             elif reward==0:
  117.                 note+="You failed to get some Boing Boing action, you drive away on your scooter in shame "
  118.             elif reward==1:
  119.                 note+="gives7 1Boin Girl to "
  120.             else:
  121.                 note+="gives7 %d Boin Girl (  o  )(  O  ) to " % reward
  122.  
  123.             note+="%s. You now have7 %d Boins in your harem of Boing Boing Whores!" % (nick, boin)
  124.                
  125.             note="\001%s %s\001" % ("ACTION", note);
  126.  
  127.             helpers.msg(self.client, chan, note);
  128.         elif parts!=None:
  129.             if command_name == 'gift':
  130.                 if(boin_bet<=0):
  131.                     notice(self.client, nick, "Must wager a positive number of boin girl slaves.")
  132.                     return
  133.    
  134.                 if( inick.lower()==nick.lower()):
  135.                     notice(self.client, nick, "You can't steal from yourself.")
  136.    
  137.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (nick.lower(), ))
  138.                 result=cursor.fetchone()
  139.    
  140.                 if not result:
  141.                     (boin, last_trigger)=(0,0)
  142.                 else:
  143.                     (dbnick, boin, last_trigger)=result
  144.    
  145.                 if( boin_bet > boin ):
  146.                     notice(self.client, nick, "Cannot donate more boin than you currently have.")
  147.                     return
  148.                
  149.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  150.                 result=cursor.fetchone()
  151.                 if not result:
  152.                     notice(self.client, nick, "%s does not exist" % inick)
  153.                     return
  154.                 else:
  155.                     (dbnick, oboin, olast)=result
  156.  
  157.                 boin -= boin_bet
  158.                 oboin += boin_bet
  159.  
  160.                 notice(self.client, inick,
  161.                            "Someone given you a gift. "
  162.                            "You gain %d Boin!" % boin_bet)
  163.                 notice(self.client, nick, "You donated %dBoin from your harem to someone" % boin_bet)
  164.  
  165.             else:
  166.                 if(boin_bet<=0):
  167.                     notice(self.client, nick, "Must wager a positive number of boin girl slaves.")
  168.                     return
  169.    
  170.                 if( inick.lower()==nick.lower()):
  171.                     notice(self.client, nick, "You can't steal from yourself.")
  172.    
  173.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (nick.lower(), ))
  174.                 result=cursor.fetchone()
  175.    
  176.                 if not result:
  177.                     (boin, last_trigger)=(0,0)
  178.                 else:
  179.                     (dbnick, boin, last_trigger)=result
  180.    
  181.                 if( boin_bet > boin ):
  182.                     notice(self.client, nick, "Cannot wager more boin than you currently have.")
  183.                     return
  184.                
  185.                 cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  186.                 result=cursor.fetchone()
  187.                 if not result:
  188.                     notice(self.client, nick, "%s does not exist" % inick)
  189.                     return
  190.                 else:
  191.                     (dbnick, oboin, olast)=result
  192.    
  193.                 if( boin_bet>oboin or random()<0.6 ):
  194.                     notice(self.client, inick,
  195.                            "Someone tried to steal from you and failed. "
  196.                            "You gain %d Boin!" % boin_bet)
  197.                     notice(self.client, nick, "Your theft failed. You lose12 %dBoin from your harem. You hand them over in shame" % boin_bet)
  198.                     boin-=boin_bet
  199.                     oboin+=boin_bet
  200.                 else:
  201.                     template="%s you have successfully stolen %d Virgin Boin Girls from %s! "
  202.                     note=template % (nick, boin_bet, inick)
  203.                     helpers.msg(self.client, chan, note)
  204.                     boin+=boin_bet
  205.                     oboin-=boin_bet
  206.            
  207.  
  208.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)",
  209.                            (nick.lower(), boin, last_trigger))
  210.            
  211.             cursor.execute("REPLACE INTO game_stats values (?, ?, ?)",
  212.                            (inick.lower(), oboin, olast))
  213.  
  214.             db.commit()
  215.  
  216.         elif inick == "top10":
  217.             print "TOP 10"
  218.             try:
  219.                 cursor.execute("SELECT boin, nick FROM game_stats ORDER BY boin DESC LIMIT 10");
  220.                 msg = "top 10."
  221.                 for boin, n in cursor.fetchall():
  222.                     print "Got TOP 10 data: %r, %r" % (boin, n)
  223.                     msg += " %s(%s)" % (n.encode('utf8'), boin)
  224.                 helpers.msg(self.client, chan, msg)
  225.             except Exception, e:
  226.                 print "TOP 10 exception: %s" % e
  227.                 notice(self.client, nick, "Boom: %s" % e)
  228.         elif inick == "rivals":
  229.             print "Rivals for %r" % nick
  230.             cursor.execute("SELECT nick, boin FROM game_stats ORDER by boin DESC")
  231.             everyone = cursor.fetchall()
  232.             my_rank = next(i for i,(n,s) in enumerate(everyone) if n == nick.lower())
  233.             indexes = range(max(0, my_rank-3), min(my_rank+3+1,len(everyone)))
  234.             msg = "Rivals: %s" % ", ".join("#%d: %s (%d)" % (i, everyone[i][0], everyone[i][1]) for i in indexes)
  235.             helpers.msg(self.client, chan, msg)            
  236.         else:
  237.             cursor.execute("SELECT * FROM game_stats WHERE nick=?", (inick.lower(), ))
  238.             result=cursor.fetchone()
  239.  
  240.             if not result:
  241.                 notice(self.client, nick, "No records for %s." % inick)
  242.             else:
  243.  
  244.               (dbnick, boin, last_trigger)=result
  245.  
  246.               cursor.execute("SELECT count(*) FROM game_stats WHERE boin > ?", (boin, ))
  247.               (rank,)=cursor.fetchone()
  248.               rank+=1
  249.  
  250.               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)
  251.              
  252.               helpers.msg(self.client, chan, message)
  253.  
  254.  
  255. def connect_cb(cli):
  256.     if( len(PASSWORD)>0 ):
  257.         helpers.identify(cli, PASSWORD)
  258.         time.sleep(10)
  259.  
  260.     for channel in CHANNELS:
  261.         helpers.join(cli, channel)
  262.  
  263.  
  264. #helper functions
  265. def notice(cli, user, msg):
  266.     cli.send("NOTICE", user, " :%s" % msg);
  267.  
  268. def lotto():
  269.     dist=[0.05, 0.05, 0.35, 0.30, 0.15, 0.05, 0.05]
  270.     result=-1
  271.     value=random()
  272.     for i in xrange(len(dist)):
  273.         value-=dist[i]
  274.         result+=1
  275.         if( value<= 0):
  276.             break
  277.        
  278.     return result
  279.    
  280.  
  281. def main():
  282.     logging.basicConfig(level=logging.ERROR)
  283.  
  284.     #load configuration
  285.  
  286.     global db, cursor
  287.     # check database
  288.     db=sqlite3.connect(DATABASE_FILE)
  289.     cursor=db.cursor()    
  290.     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='game_stats';")
  291.    
  292.     if not cursor.fetchone():
  293.         print 'Table game_stats does not exist. Creating table'
  294.        
  295.         cursor.execute("create table game_stats (nick text PRIMARY KEY, boin integer, last_trigger integer)")
  296.         db.commit()
  297.  
  298.     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='global_vars';")
  299.    
  300.     if not cursor.fetchone():
  301.         print 'Table global_vars does not exist. Creating table'
  302.        
  303.         cursor.execute("create table global_vars (var_name text PRIMARY KEY, var_value text)")
  304.         cursor.execute("insert into global_vars (var_name, var_value) values ('last_trigger','')")
  305.         db.commit()
  306.  
  307.  
  308.     cli = IRCClient(EventHandler, host=HOST, port=PORT, nick=NICK,
  309.                     connect_cb=connect_cb)
  310.     conn = cli.connect()
  311.  
  312.     while True:
  313.         conn.next()      ## python 2
  314.         # next(conn)     ## python 3
  315.  
  316.  
  317. if __name__ == '__main__':
  318.     main()
Add Comment
Please, Sign In to add comment