games.py 14 KB

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