Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.21 KB | None | 0 0
  1. import praw
  2. import config
  3. import random
  4. import firebase_admin
  5. import time
  6. import os
  7. import re
  8. from firebase_admin import credentials, firestore, db
  9.  
  10. print("Initializing Firebase...")
  11. cred = credentials.Certificate("credentials.json")
  12. app = firebase_admin.initialize_app(cred)
  13. db = firestore.client()
  14. print("OK!")
  15.  
  16. print("Logging into Reddit...")
  17. reddit = praw.Reddit(user_agent='KickOpenTheDoor bot by u/RPG_Ivan and u/VirtuousVermin',
  18.                   client_id=config.client_id,
  19.                   client_secret=config.client_secret,
  20.                   username=config.username,
  21.                   password=config.password)
  22. print("OK!")
  23.  
  24.  
  25. #Use kiot as a test sub for now.
  26. subreddit = reddit.subreddit('kickopenthedoor')
  27.  
  28.  
  29. nu = 0
  30. x = 0
  31.  
  32. #COMMANDS VIA COMMENTING
  33. comments = subreddit.stream.comments(pause_after=None, skip_existing=True)
  34. def run():
  35.     for comment in comments:
  36.         if comment is None:
  37.             break
  38.         #def process():
  39.         #NEW USER CREATION
  40.         users_ref = db.collection('users')
  41.         users = users_ref.get()
  42.         author = comment.author.name   
  43.         docs = []
  44.         for document in users:
  45.             docs.append(document.id)
  46.         if author not in docs:
  47.             print("Generating...")
  48.             doc = db.collection('users').document(author)
  49.             doc.set({
  50.                 'inventory': {},
  51.                 'discord_id':'',
  52.                 'race':''
  53.                 })
  54.             print('Generated new profile.')
  55.             reddit.redditor(author).message('Welcome to r/KickOpenTheDoor!', '#Welcome to r/KickOpenTheDoor!'+'\n\n'+'Your user profile has been created. You have been granted:'+'\n\n'+'*0 Gold'+'\n\n'+'Have fun slaying them monsters!')
  56.         if comment.body == '!balance' and comment.id not in processed:
  57.             if comment is None:
  58.                 pass
  59.             else:
  60.                 print('Getting balance...')
  61.                 user_ref = db.collection('users').document(str(comment.author))
  62.                 user = user_ref.get()
  63.                 gold = user.get('gold')
  64.                 comment.reply(f'Your balance is: {gold} gold.')
  65.                 f = open("processed.txt", "a")
  66.                 f.write(comment.id + ", ")
  67.         #ATTACK SYSTEM
  68.         monsters_ref = db.collection('monsters')
  69.         monsters = monsters_ref.get()
  70.         post_id = comment.link_id[3:]
  71.         print(post_id)
  72.         #SHOP SYSTEM
  73.         shop2 = db.collection(u'shop').get()
  74.         items = []
  75.         items2 = []
  76.         items3 = []
  77.         for document in shop2:
  78.             items.append(document.id)
  79.         comment_buying = []
  80.         random_low_item = db.collection('lowtieritems').get()
  81.         for document in random_low_item:
  82.             items2.append(document.id)
  83.         #Command parsing goes here
  84.         content = comment.body
  85.         if "!attack" in content and comment.id not in processed:
  86.             for document in monsters:
  87.                 if document.id == post_id:
  88.                     #Normal attacking (without a weapon)
  89.                     weaponname = content[8:]
  90.                     if weaponname == "":
  91.                         wname = 'Your Fists'
  92.                         extradamage = 0
  93.                     else:
  94.                         inventory_ref = db.collection('users').document(comment.author.name)
  95.                         inventory = inventory_ref.get()
  96.                         inv = inventory_ref.get('inventory')
  97.                         #try:
  98.                         weapon = inventory.get(weaponname)
  99.                         extradamage = weapon.get('damage')
  100.                         wname = weapon.get('name')
  101.                         #except Exception:
  102.                             #comment.reply('Either you don\'t have that weapon, or it doesn\'t exist!')
  103.                             #pass
  104.                     monster_ref = monsters_ref.document(post_id)
  105.                     monster = monster_ref.get()
  106.                     name = monster.get('name')
  107.                     strdamage = str('damage')
  108.                     oldhp = monster.get('hp')
  109.                     attacked = monster.get('attacked')
  110.                     if comment.author.name in attacked:
  111.                         comment.reply('You have already attacked this monster!')
  112.                     elif comment.author.name not in attacked:
  113.                         if oldhp > 0:
  114.                             user_ref = db.collection('users').document(comment.author.name)
  115.                             user = user_ref.get()
  116.                             oldgold = user.get('gold')
  117.                             basedamage = random.randint(1, 3)
  118.                             damage = basedamage+extradamage
  119.                             cash = 10*damage
  120.                             user_ref.update({
  121.                                 'gold':oldgold+cash
  122.                                 })
  123.                             monster_ref.update({
  124.                                 'hp': oldhp - damage,
  125.                                 'attacked': attacked + [comment.author.name],
  126.                                 })
  127.                             time.sleep(10)
  128.                             monster = monster_ref.get()
  129.                             hp = str(monster.get('hp'))
  130.                         if int(hp) <= 0:
  131.                             comment.submission.mod.flair("Slain!")
  132.                             comment.reply(f'You attack {name}! You deal {basedamage} base damage and using {wname} deals an extra {extradamage} damage! It has {hp} health remaining! The beast has been slain!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')                             
  133.                         elif int(hp) > 0:
  134.                             comment.submission.mod.flair(f'{hp} HP remaining!')
  135.                             comment.reply(f'You attack {name}! You deal {basedamage} base damage and using {wname} deals an extra {extradamage} damage! It has {hp} health remaining!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')
  136.                         elif oldhp <= 0:
  137.                             comment.reply('Sorry, but this beast has already been slain!')
  138.                     print('Comment processed!')
  139.                     f = open("processed.txt", "a")
  140.                     f.write(comment.id + ", ")
  141.         if "!buy" in comment.body and comment.id not in processed:
  142.             while True:
  143.                 print('Buy Request Found!')
  144.                 global nu
  145.                 global x
  146.                 #Used for checking if item being purchased exists, if not add it to the excluded list and break out of while True:
  147.                 if nu > len(items):
  148.                     nu=0
  149.                     x = x + 1
  150.                 if x > 1:
  151.                     f = open("processed", "a")
  152.                     f.write(comment.id + ", ")
  153.                     x = 0
  154.                     comment.reply('That item does not exist. Please try one of these: patrician, plebian.')
  155.                     break
  156.                 #Defining more variables
  157.                 all_items = ''.join(str(e) for e in items[-nu])
  158.                 item_in_comment = re.findall(all_items, content)
  159.                 comment_buying.extend(item_in_comment)
  160.                 #Checking if there's an item in the comment, it iterates through the list one at a time
  161.                 if bool(item_in_comment) is False:
  162.                     nu = nu + 1
  163.                     print(nu)
  164.                     continue
  165.                 else:
  166.                     #More variables
  167.                     comment_shop = ''.join(str(e) for e in comment_buying[-1])
  168.                     print(comment_shop)
  169.                     all_users = db.collection('users').document(str(comment.author))
  170.                     total_users = all_users.get()
  171.                     gold = total_users.get('gold')
  172.                     shop_ref = db.collection('shop').document(comment_shop)
  173.                     shop = shop_ref.get()
  174.                     price = shop.get('price')
  175.                 #Putting the item in the user's inventory & subtracting the price
  176.                 if int(gold) < int(price):
  177.                     print('Transaction Declined!')
  178.                     comment.reply(f'You do not have enough gold for the {comment_shop} treasure card!')
  179.                 else:
  180.                     print('Item Bought!')
  181.                     if 'plebian' in comment_shop:
  182.                         low_tier_items = ''.join(str(e) for e in items2[random.randint(0,8)])
  183.                         low_tier_refs = db.collection('lowtieritems').document(low_tier_items)
  184.                         low_tier = low_tier_refs.get()
  185.                         low_items = low_tier.get('name')
  186.                         low_items_dmg = low_tier.get('dmg')
  187.                         all_users.set({'inventory': {low_items:{'name': low_items,
  188.                                                                 'damage': int(low_items_dmg),
  189.                                                                 'amount': 1,
  190.                                                                 'uses': 5,
  191.                                                                 'equipped': False}}}, merge=True)
  192.                         comment.reply(f'Ta-Da! You have recieved the {low_items}. It deals {low_items_dmg} bonus damage!')
  193.  
  194.                         ammount_final = gold - price
  195.                         all_users.update({'gold':ammount_final})
  196.                     if 'patrician' in comment_shop:
  197.                         high_tier_items = ''.join(str(e) for e in items2[random.randint(0,8)])
  198.                         high_tier_refs = db.collection('lowtieritems').document(high_tier_items)
  199.                         high_tier = high_tier_refs.get()
  200.                         high_items = high_tier.get('name')
  201.                         high_items_dmg = high_tier.get('dmg')
  202.                         all_users.set({'inventory': {high_items:{'name': high_items,
  203.                                                                     'damage': int(high_items_dmg),
  204.                                                                     'amount': 1,
  205.                                                                     'uses': 10,
  206.                                                                     'equipped': False}}}, merge=True)
  207.                         comment.reply(f'Ta-Da! You have recieved the {high_items}. It deals {high_items_dmg} bonus damage!')
  208.  
  209.                         ammount_final = gold - price
  210.                         all_users.update({'gold':ammount_final})
  211.                     if 'tuba' in comment_shop:
  212.                         pass
  213.                 f = open("processed", "a")
  214.                 f.write(comment.id + ", ")
  215.                 break
  216.         #Ending the while True:
  217.         if comment.id in processed:
  218.             break
  219.        
  220.                
  221.  
  222.  
  223. f = open("processed.txt", "r")
  224. processed = f.read()
  225. pfixed = processed.split(', ')
  226.  
  227. #Just in case something weird happens...
  228. while True:
  229.     run()
  230.     print('Restarting')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement