games.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import random
  2. from common import font
  3. from commands.common import CommandHelpers as CH
  4. from commands.common import GameHelpers
  5. def do_command(self, connection, event):
  6. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  7. # Do nothing if there is no command.
  8. if not command:
  9. return
  10. try:
  11. command.split()[0]
  12. except:
  13. return
  14. # Do noting if the games channel function is off and it's a channel message.
  15. if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
  16. return
  17. if command == "cmd" or command == "cmds" or command == "commands":
  18. if cmdtype == "cmd":
  19. connection.privmsg(replyto, font.grey + "Games: " + CH.ccc(self, "8ball") + CH.ccc(self, "dice") + CH.ccc(self, "player") + CH.ccc(self, "players") + CH.ccc(self, "levelup") + CH.ccc(self, "givecoin")[:-2] + ".")
  20. connection.privmsg(replyto, font.grey + "Game help: " + blue + self.helpchar + "level" + font.grey + ", " + blue + self.helpchar + "xp" + font.grey + ", " + blue + self.helpchar + "ap" + font.grey + ", " + blue + self.helpchar + "coin" + font.grey + ", " + blue + self.helpchar + "karma" + font.grey + ".")
  21. elif command.split()[0] == "8ball":
  22. if cmdtype == "help": #Display help text.
  23. connection.privmsg(replyto, "Ask a question of the mighty and illusive 8-Ball.")
  24. connection.privmsg(replyto, font.grey + "Usage: " + blue + self.cmdchar + "8ball " + font.reset + font.italic + "question")
  25. elif cmdtype == "cmd":
  26. if len(command.split()) < 2: # Command contains only !8ball.
  27. messages = [
  28. "Don't forget to ask a question...",
  29. "Hey, that's not a question!",
  30. "What would you like to know?",
  31. "You want me to predict nothing?",
  32. "Are you intentionally not asking a question?",
  33. "Ask a question you tease!",
  34. "You do not seem to grasp this, for help type: " + blue + self.helpchar + "8ball",
  35. "You will die alone.",
  36. ]
  37. connection.privmsg(replyto, random.choice(messages))
  38. else:
  39. messages = [
  40. "Yes.",
  41. "No.",
  42. "Affirmative.",
  43. "No way!",
  44. "Negative.",
  45. "Positive.",
  46. "Correct.",
  47. "Incorrect.",
  48. "Likely",
  49. "Unlikely",
  50. "Maybe.",
  51. "Definately!",
  52. "Perhaps?",
  53. "Most indubitably.",
  54. "Does the pope shit in the woods?",
  55. "When hell freezes over.",
  56. "Only between 9 and 5.",
  57. "Only just before you die.",
  58. font.red + font.bold + "ERROR: " + font.bold + "probability failure.",
  59. "Ask again later.",
  60. "I don't know.",
  61. "Unpredictable.",
  62. ]
  63. connection.privmsg(replyto, random.choice(messages))
  64. elif command.split()[0] == "dice":
  65. if cmdtype == "help": #Display help text.
  66. if len(command.split()) is not 1:
  67. return
  68. connection.privmsg(replyto, "Rolls multiple dices of chosen type. Specifying type or amount is optional.")
  69. connection.privmsg(replyto, font.grey + "Usage: " + blue + self.cmdchar + "dice " + font.reset + font.italic + "amount type")
  70. connection.privmsg(replyto, font.grey + "Example: " + blue + self.cmdchar + "dice " + font.reset + font.italic + "2 d10")
  71. connection.privmsg(replyto, font.grey + "Note: " + font.reset + "The dice type is specified as a " + font.italic + "d " + font.reset + "followed by the amount of sides.")
  72. elif cmdtype == "cmd":
  73. arguments = len(command.split()) - 1
  74. if arguments == 0:
  75. connection.privmsg(replyto, str(random.randint(1, 6)) + " & " + str(random.randint(1, 6)) + ".")
  76. elif arguments == 1:
  77. try:
  78. diceamount = int(command.split()[1])
  79. except ValueError: # Argument is not an integer.
  80. if command.split()[1].startswith("d"): # Specific dice type.
  81. try:
  82. dicetype = int(command.split()[1][1:])
  83. except ValueError: # "d" not followd by interger.
  84. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + font.reset + ".")
  85. return
  86. if dicetype < 1:
  87. connection.action(replyto, "can not create objects with less then one side.")
  88. else:
  89. connection.privmsg(replyto, str(GameHelpers.roll_dice(1, dicetype)[0]))
  90. else: # Argument does not start with "d" and is not an integer.
  91. connection.privmsg(replyto, "Invalid number or type of dice. For help type: " + blue + self.helpchar + "dice" + font.reset + ".")
  92. return
  93. if diceamount < 1:
  94. connection.privmsg(replyto, "Rolling " + font.bold + "no " + font.reset + "dice.")
  95. elif diceamount > 10:
  96. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  97. else:
  98. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, 6)) + ".") # Roll x amount of dice.
  99. elif arguments == 2:
  100. try:
  101. diceamount = int(command.split()[1])
  102. except ValueError: # First argument not an integer.
  103. connection.privmsg(replyto, "Invalid number of dice. For help type: " + blue + self.helpchar + "dice" + font.reset + ".")
  104. return
  105. try:
  106. dicetype = int(command.split()[2][1:])
  107. except ValueError: # Second argument not a dice type.
  108. connection.privmsg(replyto, "Invalid type of dice. For help type: " + blue + self.helpchar + "dice" + font.reset + ".")
  109. return
  110. if diceamount < 1:
  111. connection.privmsg(replyto, "Rolling " + font.bold + "no " + font.reset + "dice.")
  112. elif diceamount > 10:
  113. connection.action(replyto, "can not fit that many dice into it's robot hands.")
  114. elif dicetype < 1:
  115. connection.action(replyto, "can not create objects with less then one side.")
  116. elif dicetype > 9999999999 and diceamount > 1:
  117. connection.action(replyto, "Those dices are so large i can only roll one at a time.")
  118. else:
  119. connection.privmsg(replyto, ", ".join(str(rolls) for rolls in GameHelpers.roll_dice(diceamount, dicetype)) + ".") # Roll x amount of x type dice.
  120. else: # Invalid amount of arguments.
  121. connection.privmsg(replyto, "Too many arguments. For help type: " + blue + self.helpchar + "dice" + font.reset + ".")
  122. elif command.split()[0] == "player":
  123. if cmdtype == "help": #Display help text.
  124. connection.privmsg(replyto, "Displays a users game statistics. User optional.")
  125. elif cmdtype == "cmd":
  126. if len(command.split()) == 1:
  127. user = event.source.nick.lower()
  128. message = font.grey + "Your statistics. " + reset
  129. elif len(command.split()) == 2:
  130. user = command.split()[1]
  131. if not self.db.one("SELECT id FROM users WHERE LOWER(name)=%s AND network='" + self.network + "'", (user, )):
  132. connection.action(replyto, "does not know of a " + font.red + trigger.split()[1] + font.reset + ".")
  133. return
  134. if user == connection.get_nickname().lower():
  135. connection.privmsg(replyto, "The game does not play the master.")
  136. return
  137. if user == event.source.nick.lower():
  138. message = font.grey + "Your statistics. " + reset
  139. else:
  140. message = font.grey + "Statistics for " + font.red + trigger.split()[1] + font.reset + ". "
  141. else:
  142. connection.privmsg(replyto, "Too many arguments, For help type " + blue + self.helpchar + "players " + font.reset + ".")
  143. return
  144. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, ap, apspent = GameHelpers.get_info(self, user)
  145. if ap < 0:
  146. ap = 0
  147. info = GameHelpers.player_info(self, user)
  148. connection.privmsg(replyto, message + info)
  149. elif command.split()[0] == "levelup":
  150. user = event.source.nick.lower()
  151. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, ap, apspent = GameHelpers.get_info(self, user)
  152. if cmdtype == "help": #Display help text.
  153. connection.privmsg(replyto, "Spend " + str(int(level * 2.7)) + " XP to gain your next level. Gain multiple levels (for the price of the lowest level) by specifying the amount.")
  154. elif cmdtype == "cmd":
  155. if len(command.split()) == 1:
  156. if xp < int(level * 2.7):
  157. connection.privmsg(replyto, "Insufficient XP, you need at least " + str(int(level * 2.7)) + ".")
  158. elif ap < 1:
  159. connection.privmsg(replyto, "Insufficient AP, you need at least 1.")
  160. else:
  161. self.db.run("UPDATE users SET level=level+1, xp_spent=xp_spent+%s, ap_spent=ap_spent+1 WHERE LOWER(name)=%s AND network=%s", (int(level * 2.7), user, self.network, ))
  162. self.db.run("UPDATE users SET coin=coin+0.3 WHERE level>0 AND network=%s", (self.network, ))
  163. info = GameHelpers.player_info(self, user)
  164. connection.privmsg(replyto, "Your new statistics: " + info)
  165. elif len(command.split()) == 2:
  166. try:
  167. levels = int(command.split()[1])
  168. except:
  169. connection.privmsg(replyto, "Invalid amount.")
  170. return
  171. if levels < 1:
  172. connection.privmsg(replyto, "Invalid amount.")
  173. elif xp < int(level * 2.7):
  174. connection.privmsg(replyto, "Insufficient XP, you need at least " + str(int(level * 2.7)) + ".")
  175. elif ap < levels:
  176. connection.privmsg(replyto, "Insufficient AP, you need at least 1.")
  177. else:
  178. self.db.run("UPDATE users SET level=level+%s, xp_spent=xp_spent+%s, ap_spent=ap_spent+%s WHERE LOWER(name)=LOWER(%s) AND network=%s", (levels, int(level * 2.7), levels, user, self.network, ))
  179. self.db.run("UPDATE users SET coin=coin+%s WHERE level>0 AND network=%s", (levels * 0.3, self.network, ))
  180. info = GameHelpers.player_info(self, user)
  181. connection.privmsg(replyto, "Your new statistics: " + info)
  182. else:
  183. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "levelup " + font.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, font.grey + "Usage: " + blue + self.cmdchar + "givecoin " + font.reset + font.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" + font.reset + ".")
  191. return
  192. elif len(command.split()) > 3:
  193. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "givecoin" + font.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" + font.reset + ".")
  197. return
  198. elif len(command.split()) == 3:
  199. stop = False
  200. try:
  201. if float(command.split()[2]) < 0:
  202. connection.privmsg(replyto, "You clever abuser! The " + font.red + self.cmdchar + font.bold + "give" + font.bold + "coin" + font.reset + " command is not designed for robbing. There will be consequences...")
  203. self.db.run("UPDATE users SET coin=coin-3, karma_correction=karma_correction-0.5 WHERE name=%s AND network=%s", (event.source.nick, self.network))
  204. stop = True
  205. except TypeError:
  206. connection.privmsg(replyto, "Invalid amount. For help type " + blue + self.helpchar + "givecoin" + font.reset + ".")
  207. return
  208. if stop:
  209. return
  210. elif not event.target == connection.get_nickname():
  211. if not command.split()[1] in self.channels[event.target].users():
  212. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is not present.")
  213. return
  214. level, xp, xpspent, totalxp, karma, coin, coinspent, coingiven, ap, apspent = GameHelpers.get_info(self, event.source.nick)
  215. receivingrecord = self.db.one("SELECT level, away FROM users WHERE LOWER(name)=%s AND network=%s", (command.split()[1], self.network, ))
  216. if level < 1:
  217. connection.privmsg(replyto, "You need to " + blue + self.cmdchar + "levelup " + font.reset + "to be able to give coin.")
  218. return
  219. elif coin < 1:
  220. connection.privmsg(replyto, "You have no coin to give.")
  221. return
  222. elif not receivingrecord:
  223. connection.action(replyto, "does not know of any \"" + font.red + trigger.split()[1] + font.reset + "\".")
  224. return
  225. elif receivingrecord[0] == 0:
  226. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is not playing the game.")
  227. return
  228. elif receivingrecord[1] == True:
  229. connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + "is not here right now.")
  230. return
  231. elif ap < 1:
  232. connection.privmsg(replyto, "You have no action points, go use IRC some more...")
  233. return
  234. if len(command.split()) == 2:
  235. self.db.run("UPDATE users SET coin=coin-1, coin_spent=coin_spent+1, coin_given=coin_given+1, ap_spent=ap_spent+1 WHERE name=%s AND network=%s", (event.source.nick, self.network, ))
  236. self.db.run("UPDATE users SET coin=coin+1 WHERE LOWER(name)=%s AND network=%s", (command.split()[1], self.network, ))
  237. elif len(command.split()) == 3:
  238. self.db.run("UPDATE users SET coin=coin-%s, coin_spent=coin_spent+%s, coin_given=coin_given+%s, ap_spent=ap_spent+1 WHERE name=%s AND network=%s", (command.split()[2], command.split()[2], command.split()[2], event.source.nick, self.network, ))
  239. self.db.run("UPDATE users SET coin=coin+%s WHERE LOWER(name)=%s AND network=%s", (command.split()[2], command.split()[1], self.network))
  240. info = GameHelpers.player_info(self, event.source.nick)
  241. connection.notice(event.source.nick, "Your new statistics: " + info)
  242. elif command.split()[0] == "players":
  243. if cmdtype == "help": #Display help text.
  244. connection.privmsg(replyto, "Display top player rankings.")
  245. elif cmdtype == "cmd":
  246. toplevel = GameHelpers.list_top_players(self, "level")
  247. if toplevel:
  248. connection.notice(event.source.nick, "Ranking level: " + toplevel)
  249. topxp = GameHelpers.list_top_players(self, "xp_spent")
  250. if topxp:
  251. connection.notice(event.source.nick, "Ranking experience: " + topxp)
  252. topcoin = GameHelpers.list_top_players(self, "coin")
  253. if topcoin:
  254. connection.notice(event.source.nick, "Ranking wealth: " + topcoin)
  255. if not toplevel and not topxp and not topcoin:
  256. connection.privmsg(replyto, "Nobody is playing the game.")
  257. elif command.split()[0] == "level":
  258. if cmdtype == "help": #Display help text.
  259. connection.privmsg(replyto, "For 10 XP you can use !levelup to gain a level. As your level increases, things become more difficult, while more capabilities and options are unlocked.")
  260. elif command.split()[0] == "xp":
  261. if cmdtype == "help": #Display help text.
  262. connection.privmsg(replyto, "XP is earned by using IRC and playing the game, in channels with " + font.red + connection.get_nickname() + font.reset + ". Ask any operator in " + font.red + self.homechannel + font.reset + " to add a channel. XP is used to level up, advance classes, as a limit and as a multiplier. Once XP is expended it keeps counting towards your total.")
  263. elif command.split()[0] == "ap":
  264. if cmdtype == "help": #Display help text.
  265. connection.privmsg(replyto, "AP is earned by using IRC and playing the game in channels with " + font.red + connection.get_nickname() + font.reset + ". Ask any operator in " + font.red + self.homechannel + font.reset + " to add a channel. AP is expended for every action you take in the game.")
  266. elif command.split()[0] == "coin":
  267. if cmdtype == "help": #Display help text.
  268. connection.privmsg(replyto, "Coin is earned when certain events occur, for instance when a player expends XP. Coin is used to buy items and classes. To give a player coin use " + blue + self.cmdchar + "givecoin" + font.reset + ". Coin affects karma.")
  269. elif command.split()[0] == "karma":
  270. if cmdtype == "help": #Display help text.
  271. connection.privmsg(replyto, "Karma is a secret formula, based upon on your IRC events and how you play the game. It does not increase like XP. Some events, skills and items are karma based.")
  272. # elif command.split()[0] == "classup":
  273. # if cmdtype == "help": #Display help text.
  274. # connection.privmsg(replyto, "Spend 10 XP to gain a class in your current level. List available classes with " + blue + self.helpchar + "classup available " + font.reset + ".")
  275. # connection.privmsg(replyto, font.grey + "Usage: " + blue + self.cmdchar + "classup " + font.reset + font.italic + "class")
  276. # elif cmdtype == "cmd":
  277. #
  278. # if len(command.split()) == 1:
  279. # connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "classup" + font.reset + ".")
  280. # if len(command.split()) == 2:
  281. # if command.split()[1] == "available":
  282. # level, xp, xpspent, karma = GameHelpers.get_info(self, event.source.nick)
  283. # if level == 0:
  284. # connection.privmsg(replyto, "There are no level 0 classes.")
  285. # if level == 1:
  286. # connection.privmsg(replyto, "Level 1 classes: Maggot, nubcake, ")
  287. # if level == 2:
  288. # connection.privmsg(replyto, "Level 2 classes: Retard, ")
  289. # if level == 3:
  290. # connection.privmsg(replyto, "Level 3 classes: ")
  291. # if level == 4:
  292. # connection.privmsg(replyto, "Level 3 classes: ")
  293. # if level == 5:
  294. # connection.privmsg(replyto, "Level 4 classes: ")
  295. # else:
  296. # else:
  297. # connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "classup" + font.reset + ".")