Guest User

Untitled

a guest
May 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. from random import randint, choice
  2. import traceback
  3. import pprint
  4.  
  5. import requests
  6. import redis
  7. import attr
  8.  
  9. from disconstants import *
  10. from errors import *
  11.  
  12.  
  13. @attr.s
  14. class Game(object):
  15. level = attr.ib(default=1)
  16. hp = attr.ib(default=15)
  17. enemy = attr.ib(default=None)
  18. potion = attr.ib(default=5)
  19. gold = attr.ib(default=10)
  20. exp = attr.ib(default=0)
  21. maxdmg = attr.ib(default=5)
  22. maxhp = attr.ib(default=15)
  23. progress = attr.ib(default=0)
  24. cavloot = attr.ib(default=0)
  25.  
  26. def gameover(self):
  27. global sessions
  28. del sessions[self.user]
  29.  
  30. def fight(self):
  31. enemy = self.enemy
  32. dmg = randint(0, self.maxdmg)
  33. if not self.enemy:
  34. return '\xe2\x9a\x94 You attack and deal %s damage to a tree since no enemy was in sight' % dmg
  35. enemy.takedmg(dmg)
  36. if not self.enemy.alive():
  37. self.enemy = None
  38. exp = (1 + enemy.lvl)
  39. self.exp += exp
  40. gold = randint(2, 3 + enemy.lvl)
  41. self.gold += gold
  42. if self.exp >= self.level * 10:
  43. self.level += 1
  44. self.maxhp += 2
  45. self.maxdmg += 1
  46. self.exp -= randint(1, self.level * 10)
  47. return '\xe2\x9a\x94 You strike the killing blow of %s damage to the %s which dies \xe2\x9a\x94 \n \xf0\x9f\x92\xb0 Searching the body you looted %s Gold\n :up: Congratz you are now Lvl.%s' % (dmg, enemy.name, gold, self.level)
  48. return '\xe2\x9a\x94 You strike the killing blow of %s damage to the %s which dies \xe2\x9a\x94 \n \xe2\x9c\xa8 You have gained %s exp \xe2\x9c\xa8 \n \xf0\x9f\x92\xb0 Searching the body you looted %s Gold' % (dmg, enemy.name, exp,gold)
  49. edmg = enemy.dodmg()
  50. self.hp -= edmg
  51. if self.hp <= 0:
  52. self.gameover()
  53. return '\xe2\x9a\x94 The %s struck you for %s damage killing you \xe2\x9a\x94' %(enemy.name, edmg)
  54. return '\xe2\x9a\x94 You swing your sword and deal %s damage to the %s which now has %s hp left\n \xe2\x9a\x94 The fierce %s countered with a %s damage attack leaving you with %s hp left' % (dmg, enemy.name, self.enemy.hp, enemy.name, edmg, self.hp)
  55.  
  56. def walk(self):
  57. if self.enemy:
  58. return 'You have no time to loot, the %s gets in the way' % (self.enemy.name,)
  59. self.progress += 1
  60. if self.progress == 10:
  61. self.hp = self.maxhp
  62. postimg(self.chan, townpic, 'You did it! you finally reached the next town. \n As with every other town you only notice a shop and blacksmith to be of use\n The shop sells potions for 10\n The blacksmith upgrades for 15')
  63. return
  64. elif self.progress == 11:
  65. self.progress -= 11
  66. return 'You continue your adventure and walk out of town, by the time you look back its too far to return'
  67. elif self.hp < self.maxhp and randint(0, 1):
  68. rnd2 = randint(1, self.maxhp - self.hp)
  69. self.hp += rnd2
  70. self.cavloot += 1
  71. return 'While you travel towards the next town you decide to take a rest \n You regain %s of %s hp\n As you linger around you notice a cave nearby\n Do you loot it?' % (self.hp, self.maxhp)
  72. else:
  73. self.enemy = Enemy.new(self.level)
  74. postimg(self.chan, enemies[self.enemy.name], ' at a steady pace you cross paths with a %s' % self.enemy)
  75. return
  76.  
  77. def loot(self):
  78. if self.enemy:
  79. return 'You have no time to loot, the %s gets in the way' % (self.enemy.name,)
  80. elif not self.cavloot:
  81. return 'You do not see any caves near you to loot'
  82. else:
  83. self.gold += randint(0, 2)
  84. self.potion += randint(0, 2)
  85. dmg = randint(0, 3)
  86. self.hp -= dmg
  87. self.cavloot -= 1
  88. if self.hp <= 0:
  89. self.gameover()
  90. return 'You cut yourself in the cave for %s damage and died' % dmg
  91. return 'As you venture deep within the cave you managed to find\n \xf0\x9f\x92\xb0 %s Gold \n \xf0\x9f\x92\x9d %s Potion\n As you climb back out you cut your self for %s damage\n You now have %s hp' % (self.gold, self.potion, dmg, self.hp)
  92.  
  93. def heal(self):
  94. if self.hp < self.maxhp:
  95. if self.potion > 0:
  96. heal = 5
  97. self.hp += heal
  98. self.potion -=1
  99. return ' \xf0\x9f\x92\x9e Your potion healed 5 hp, you now have %s hp !' % (self.hp)
  100. return 'You have no potions left'
  101. return 'You are already at full hp'
  102.  
  103. def buy(self):
  104. if self.progress != 10:
  105. return 'You are not at a shop to buy potions'
  106. elif self.gold < 15:
  107. return 'Come back later until you have enough you hobo.'
  108. else:
  109. self.potion += 5
  110. self.gold -= 15
  111. return '\xf0\x9f\x9b\x92 You bought 5 potions from the shop keeper'
  112.  
  113. def upgrade(self):
  114. if self.progress != 10:
  115. return 'You are not at a blacksmith to upgrade'
  116. elif self.gold < 15:
  117. return 'Come back later until you have enough you hobo.'
  118. else:
  119. self.maxdmg += 1
  120. self.gold -= 15
  121. return '\xe2\x9a\x92 The blacksmith sharpened your sword gaining +1 dmg'
  122.  
  123. def flee(self):
  124. if not self.enemy:
  125. return 'You fled from nothing and fell flat on your face'
  126. enemy = self.enemy
  127. edmg = enemy.dodmg()
  128. self.hp -= edmg
  129. if self.hp <= 0:
  130. self.gameover()
  131. return 'The %s gave chase and tackled you for %s damage then killed you' % (enemy.name, edmg)
  132. self.enemy = None
  133. return 'The %s managed to get one last swing of %s damage while you fled\nYou now have %s hp left' % (enemy.name,edmg, self.hp)
  134.  
  135. def status(self):
  136. return tilde(block(attr.asdict(self)))
  137.  
  138. @attr.s
  139. class Enemy(object):
  140. name = attr.ib()
  141. hp = attr.ib()
  142. atk = attr.ib()
  143. wep = attr.ib()
  144. lvl = attr.ib()
  145.  
  146. @staticmethod
  147. def new(lvl):
  148. mlvl = randint(abs(lvl - 4) + 1, lvl + 5)
  149. return Enemy(name=choice(enemies.keys()),
  150. hp=randint(abs(mlvl - 4) + 1, mlvl + 5),
  151. atk=randint(abs(mlvl - 4) + 1, mlvl + 5),
  152. wep=choice(weapons),
  153. lvl=mlvl)
  154.  
  155. def dodmg(self):
  156. return randint(0, self.atk)
  157. def takedmg(self, dmg):
  158. self.hp -= dmg
  159. return self.alive()
  160. def alive(self):
  161. return self.hp > 0
  162. def __str__(self):
  163. return 'lvl. %s %s wielding a %s with %s hp' % (self.lvl, self.name, self.wep, self.hp)
  164.  
  165.  
  166. #\xe2\x99\xa5 \xf0\x9f\x8e\xb2 \xf0\x9f\x8e\x81 \xf0\x9f\x92\x94
  167.  
  168. sessions = {}
  169.  
  170. def getsession(user, chan):
  171. global sessions
  172. n = 0
  173. s = sessions.get(user)
  174. if not s:
  175. s = Game()
  176. sessions[user] = s
  177. n = 1
  178. s.user = user
  179. s.chan = chan
  180. return s, n
  181.  
  182.  
  183. subcommands = dict(
  184. walk = 'walk',
  185. atk = 'fight',
  186. flee = 'flee',
  187. stat = 'status',
  188. heal = 'heal',
  189. buy = 'buy',
  190. upg = 'upgrade',
  191. loot = 'loot',
  192. )
  193.  
  194. def handle(server, chan, user, mid, msg):
  195. if msg[0] != '.': return
  196. if chan != gameroom: return
  197. if ' ' not in msg:
  198. return 'Available subcommands: ' + ', '.join(subcommands.keys())
  199. cmd, sub = msg.split(' ', 1)
  200. if cmd != '.g': return
  201. if sub in subcommands:
  202. g, n = getsession(user, chan)
  203. p = getattr(g, subcommands[sub])()
  204. if n:
  205. return 'Session started' + ('\n' + p if p else '')
  206. return p
  207. elif sub == 'top':
  208. v = sorted(sessions.iteritems(), key=lambda x:x[1].maxhp, reverse=1)
  209. d = []
  210. for key, value in v:
  211. if value.level == 1: continue
  212. d.append('%s %s' % (key, value.level))
  213. return tilde('\n'.join(d))
  214. elif user not in (243306235351269386, 236951005529374722): return
  215. elif sub == 'dump':
  216. return tilde(pprint.pformat(sessions), 'python')
  217. elif sub == 'off':
  218. exit(repr(sessions))
  219. else:
  220. return 'Not a valid subcommand'
  221.  
  222.  
  223. gameroom = 247480496043327489
  224.  
  225. enemies = {}
  226. for e in 'orc knight witch discork snek'.split(' '):
  227. with open('images/%s.png' % e, 'rb') as f:
  228. enemies[e] = f.read()
  229.  
  230.  
  231. with open('images/location.png', 'rb') as f:
  232. townpic = f.read()
  233.  
  234. weapons = 'club staff sword flail hammer axe'.split(' ')
  235.  
  236. # User code above
  237.  
  238. token = 'Bot ' + tokens['bot']
  239.  
  240. def block(d):
  241. out = []
  242. keys = d.keys()
  243. keys.sort()
  244. longest = len(max(keys, key=len))
  245. for k in keys:
  246. out.append('%s %s' % (k.ljust(longest), d[k]))
  247. return '\n'.join(out)
  248.  
  249. def tilde(text, type=''):
  250. return '```%s\n%s\n```' % (type, text)
  251.  
  252. def postimg(channel, imagedata, message=None):
  253. files = dict(file=('image.png', imagedata))
  254. params = dict()
  255. if message is not None:
  256. params['content'] = message
  257. headers = dict(Authorization=token)
  258. r = requests.post('%s/channels/%s/messages' % (apibase, channel),
  259. files=files, data=params, headers=headers)
  260. return r.json()
  261.  
  262. def sendmsg(chan, msg):
  263. r.publish('discord.in.c.%s' % (chan), msg)
  264.  
  265. r = redis.Redis()
  266. p = r.pubsub()
  267. p.psubscribe('discord.out.*')
  268.  
  269. sendmsg(gameroom, 'Started')
  270.  
  271. for a in p.listen():
  272. if a['type'] != 'pmessage': continue
  273. c = a['channel'].split('.', 5)
  274. if len(c) != 6: continue
  275. if not a['data']: continue
  276. serv, chan, user, msgi = map(int, c[2:])
  277. try:
  278. reply = handle(serv, chan, user, msgi, a['data'])
  279. if reply:
  280. sendmsg(chan, str(reply))
  281. except RetErr as e:
  282. sendmsg(chan, str(e))
  283. except StandardError as e:
  284. traceback.print_exc(e)
  285. sendmsg(chan, tilde(traceback.format_exc(e), 'python'))
Add Comment
Please, Sign In to add comment