games.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import random
  2. from commands.common import CommandHelpers as CH
  3. from commands.common import GameHelpers
  4. bold = "\x02"
  5. italic = "\x1D"
  6. underline = "\x1F"
  7. reverse = "\x16" # swap background and foreground colors ("reverse video")
  8. reset = "\x0F"
  9. blue = "\x0302"
  10. green = "\x0303"
  11. red = "\x0304"
  12. grey = "\x0314"
  13. def do_command(self, connection, event):
  14. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  15. # Do nothing if there is no command.
  16. if not command:
  17. return
  18. try:
  19. command.split()[0]
  20. except:
  21. return
  22. # Do noting if the games channel function is off and it's a channel message.
  23. if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
  24. return
  25. if command == "cmd" or command == "cmds" or command == "commands":
  26. if cmdtype == "cmd":
  27. connection.privmsg(replyto, grey + "Games: " + CH.ccc(self, "8ball") + CH.ccc(self, "dice") + CH.ccc(self, "player") + CH.ccc(self, "levelup") + CH.ccc(self, "givecoin")[:-2] + ".")
  28. elif command.split()[0] == "8ball":
  29. if cmdtype == "help": #Display help text.
  30. connection.privmsg(replyto, "Ask a question of the mighty and illusive 8-Ball.")
  31. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "8ball " + reset + italic + "question")
  32. elif cmdtype == "cmd":
  33. if len(command.split()) < 2: # Command contains only !8ball.
  34. messages = [
  35. "Don't forget to ask a question...",
  36. "Hey, that's not a question!",
  37. "What would you like to know?",
  38. "You want me to predict nothing?",
  39. "Are you intentionally not asking a question?",
  40. "Ask a question you tease!",
  41. "You do not seem to grasp this, for help type: " + blue + self.helpchar + "8ball",
  42. "You will die alone.",
  43. ]
  44. connection.privmsg(replyto, random.choice(messages))
  45. else:
  46. messages = [
  47. "Yes.",
  48. "No.",
  49. "Affirmative.",
  50. "No way!",
  51. "Negative.",
  52. "Positive.",
  53. "Correct.",
  54. "Incorrect.",
  55. "Likely",
  56. "Unlikely",
  57. "Maybe.",
  58. "Definately!",
  59. "Perhaps?",
  60. "Most indubitably.",
  61. "Does the pope shit in the woods?",
  62. "When hell freezes over.",
  63. "Only between 9 and 5.",
  64. "Only just before you die.",
  65. red + bold + "ERROR: " + bold + "probability failure.",
  66. "Ask again later.",
  67. "I don't know.",
  68. "Unpredictable.",
  69. ]
  70. connection.privmsg(replyto, random.choice(messages))
  71. elif command.split()[0] == "dice":
  72. if cmdtype == "help": #Display help text.
  73. if len(command.split()) is not 1:
  74. return
  75. connection.privmsg(replyto, "Rolls multiple dices of chosen type. Specifying type or amount is optional.")
  76. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "dice " + reset + italic + "amount type")
  77. connection.privmsg(replyto, grey + "Example: " + blue + self.cmdchar + "dice " + reset + italic + "2 d10")
  78. connection.privmsg(replyto, grey + "Note: " + reset + "The dice type is specified as a " + italic + "d " + reset + "followed by the amount of sides.")
  79. elif cmdtype == "cmd":
  80. arguments = len(command.split()) - 1
  81. if arguments == 0:
  82. connection.privmsg(replyto, str(random.randint(1, 6)) + " & " + str(random.randint(1, 6)) + ".")
  83. elif arguments == 1:
  84. try:
  85. diceamount = int(command.split()[1])
  86. except ValueError: # Argument is not an integer.
  87. if command.split()[1].startswith("d"): # Specific dice type.
  88. try:
  89. dicetype = int(command.split()[1][1:])
  90. except ValueError: # "d" not followd by interger.
  91. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  92. return
  93. if dicetype < 1:
  94. connection.action(replyto, "can not create objects with less then one side.")
  95. else:
  96. connection.privmsg(replyto, str(GameHelpers.roll_dice(1, dicetype)[0]))
  97. else: # Argument does not start with "d" and is not an integer.
  98. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  99. return
  100. if diceamount < 1:
  101. connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
  102. elif diceamount > 10:
  103. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  104. else:
  105. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, 6)) + ".") # Roll x amount of dice.
  106. elif arguments == 2:
  107. try:
  108. diceamount = int(command.split()[1])
  109. except ValueError: # First argument not an integer.
  110. connection.privmsg(replyto, "Invalid number of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  111. return
  112. try:
  113. dicetype = int(command.split()[2][1:])
  114. except ValueError: # Second argument not a dice type.
  115. connection.privmsg(replyto, "Invalid type of dice. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  116. return
  117. if diceamount < 1:
  118. connection.privmsg(replyto, "Rolling " + bold + "no " + reset + "dice.")
  119. elif diceamount > 10:
  120. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  121. elif dicetype < 1:
  122. connection.action(replyto, "can not create objects with less then one side.")
  123. elif dicetype > 9999999999 and diceamount > 1:
  124. connection.action(replyto, "Those dices are so large i can only roll one at a time.")
  125. else:
  126. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, dicetype)) + ".") # Roll x amount of x type dice.
  127. else: # Invalid amount of arguments.
  128. connection.privmsg(replyto, "Too many arguments. For help type: " + blue + self.helpchar + "dice" + reset + ".")
  129. elif command.split()[0] == "player":
  130. if cmdtype == "help": #Display help text.
  131. connection.privmsg(replyto, "Displays a users game info. User optional.")
  132. elif cmdtype == "cmd":
  133. if len(command.split()) == 1:
  134. user = event.source.nick.lower()
  135. message = grey + "Your info. " + reset
  136. elif len(command.split()) == 2:
  137. user = command.split()[1]
  138. if not self.db.one("SELECT id FROM users WHERE LOWER(name)=%s AND network='" + self.network + "'", (user, )):
  139. connection.action(replyto, "does not know of a " + red + trigger.split()[1] + reset + ".")
  140. return
  141. if user == connection.get_nickname().lower():
  142. connection.privmsg(replyto, "The game does not play the master.")
  143. return
  144. if user == event.source.nick.lower():
  145. message = grey + "Your info. " + reset
  146. else:
  147. message = grey + "Info for " + red + trigger.split()[1] + reset + ". "
  148. else:
  149. connection.privmsg(replyto, "Too many arguments, For help type " + blue + self.helpchar + "players " + reset + ".")
  150. return
  151. level, xp, xpspent, karma, coin = GameHelpers.get_info(self, user)
  152. connection.privmsg(replyto, message + "Level: " + str(level) + ", XP: " + str(xp) + ", coin: " + str(coin) + ", karma: " + str(karma))
  153. elif command.split()[0] == "levelup":
  154. if cmdtype == "help": #Display help text.
  155. connection.privmsg(replyto, "Spend 10 XP per level gained. Gain multiple levels by specifying the amount.")
  156. elif cmdtype == "cmd":
  157. user = event.source.nick.lower()
  158. level, xp, xpspent, karma, coin = GameHelpers.get_info(self, user)
  159. print(xp)
  160. if len(command.split()) == 1:
  161. print("XP < 10: " + str(xp < 10))
  162. if xp < 10:
  163. connection.privmsg(replyto, "Insuficcient XP, you need at least 10.")
  164. else:
  165. self.db.run("UPDATE users SET level=level+1, xp_spent=xp_spent+10 WHERE LOWER(name)='" + user + "' AND network='" + self.network + "'")
  166. self.db.run("UPDATE users SET coin=coin+0.3 WHERE level>0")
  167. elif len(command.split()) == 2:
  168. try:
  169. levels = int(command.split()[1])
  170. except:
  171. connection.privmsg(replyto, "Invalid amount.")
  172. return
  173. print("XP < level * 10: " + str(xp < levels * 10))
  174. if levels < 1:
  175. connection.privmsg(replyto, "Invalid amount.")
  176. elif xp < levels * 10:
  177. connection.privmsg(replyto, "Insuficcient XP, you need at least " + str(levels * 10) + ".")
  178. else:
  179. print("UPDATE users SET level=level+" + str(levels) + ", xp_spent=xp_spent+" + str(levels * 10) + " WHERE LOWER(name)='" + user + "' AND network='" + self.network + "'")
  180. self.db.run("UPDATE users SET level=level+" + str(levels) + ", xp_spent=xp_spent+" + str(levels * 10) + " WHERE LOWER(name)='" + user + "' AND network='" + self.network + "'")
  181. self.db.run("UPDATE users SET coin=coin+" + str(0.3 * levels) + " WHERE level>0")
  182. else:
  183. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "levelup " + reset + ".")
  184. elif command.split()[0] == "givecoin":
  185. if cmdtype == "help": #Display help text.
  186. connection.privmsg(replyto, "Give coins to another player. Amount optional.")
  187. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "givecoin " + reset + italic + "user amount")
  188. elif cmdtype == "cmd":
  189. if len(command.split()) == 1:
  190. connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  191. return
  192. elif len(command.split()) > 3:
  193. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  194. return
  195. elif command.split()[1] == event.source.nick.lower():
  196. connection.privmsg(replyto, "You already have your own coin. For help type " + blue + self.helpchar + "givecoin" + reset + ".")
  197. return
  198. level, xp, xpspent, karma, coin = GameHelpers.get_info(self, event.source.nick)
  199. if level < 1:
  200. connection.privmsg(replyto, "You need to " + blue + self.cmdchar + "levelup " + reset + "to be able to give coin.")
  201. return
  202. elif coin < 1:
  203. connection.privmsg(replyto, "You have no coin to give.")
  204. return
  205. elif not self.db.one("SELECT id FROM users WHERE LOWER(name)=%s AND network='" + self.network + "'", (command.split()[1], )):
  206. connection.action(replyto, "does not know of any \"" + red + trigger.split()[1] + reset + "\".")
  207. return
  208. elif not self.db.one("SELECT id FROM users WHERE LOWER(name)=%s AND network='" + self.network + "' AND LEVEL>0", (command.split()[1], )):
  209. connection.privmsg(replyto, red + trigger.split()[1] + reset + " is not playing the game.")
  210. return
  211. if len(command.split()) == 2:
  212. self.db.run("UPDATE users SET coin=coin-1 WHERE name=%s AND network='" + self.network + "'", (event.source.nick, ))
  213. self.db.run("UPDATE users SET coin=coin+1 WHERE LOWER(name)=%s AND network='" + self.network + "'", (command.split()[1], ))
  214. elif len(command.split()) == 3:
  215. self.db.run("UPDATE users SET coin=coin-%s WHERE name=%s AND network='" + self.network + "'", (command.split()[2], event.source.nick, ))
  216. self.db.run("UPDATE users SET coin=coin+%s WHERE LOWER(name)=%s AND network='" + self.network + "'", (command.split()[2], command.split()[1], ))
  217. # elif command.split()[0] == "classup":
  218. # if cmdtype == "help": #Display help text.
  219. # connection.privmsg(replyto, "Spend 10 XP to gain a class in your current level. List available classes with " + blue + self.helpchar + "classup available " + reset + ".")
  220. # connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "classup " + reset + italic + "class")
  221. # elif cmdtype == "cmd":
  222. #
  223. # if len(command.split()) == 1:
  224. # connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "classup" + reset + ".")
  225. # if len(command.split()) == 2:
  226. # if command.split()[1] == "available":
  227. # level, xp, xpspent, karma = GameHelpers.get_info(self, event.source.nick)
  228. # if level == 0:
  229. # connection.privmsg(replyto, "There are no level 0 classes.")
  230. # if level == 1:
  231. # connection.privmsg(replyto, "Level 1 classes: Maggot, nubcake, ")
  232. # if level == 2:
  233. # connection.privmsg(replyto, "Level 2 classes: Retard, ")
  234. # if level == 3:
  235. # connection.privmsg(replyto, "Level 3 classes: ")
  236. # if level == 4:
  237. # connection.privmsg(replyto, "Level 3 classes: ")
  238. # if level == 5:
  239. # connection.privmsg(replyto, "Level 4 classes: ")
  240. # else:
  241. # else:
  242. # connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "classup" + reset + ".")