Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import pymysql.cursors
  4. import re
  5. from __main__ import user_allowed, send_cmd_help
  6. import os
  7.  
  8. #global bool
  9. global topCommandInUse
  10. global mtopCommandInUse
  11.  
  12. topCommandInUse = False
  13. mtopCommandInUse = False
  14.  
  15. # Connect to the database
  16. def conconnect():
  17. try:
  18. self.connection.ping(True)
  19. except:
  20. connection = pymysql.connect(host='amethyst.relentlesshosting.com.au',
  21. user='thomasle_surf',
  22. password='TAJeTHX19bfK',
  23. db='thomasle_SurfTimer',
  24. charset='utf8mb4',
  25. cursorclass=pymysql.cursors.DictCursor)
  26. return connection
  27.  
  28. def convertTime(maptime): #convert seconds to min:second.millesecond
  29. """ FLOAT -> String
  30. takes float sever time and changes it to min:sec.miliseconds
  31. """
  32.  
  33. readtime = int(maptime)
  34. timeMin = readtime//60
  35. timeSec = readtime % 60
  36. timemili = int((maptime*1000)%1000)
  37.  
  38. if (timeMin<10):
  39. strMin = "0" + str(timeMin)
  40. else:
  41. strMin = str(timeMin)
  42.  
  43. if (timeSec<10):
  44. strSec = "0" + str(timeSec)
  45. else:
  46. strSec = str(timeSec)
  47. return strMin + ":" + strSec + "." + str(timemili)
  48.  
  49. class Surftimer:
  50. """Surftimer commands"""
  51.  
  52. def __init__(self, bot):
  53. self.bot = bot
  54. conconnect();
  55.  
  56. @commands.command(pass_context=True)
  57. async def wr(self, ctx, text : str):
  58. """<mapname> - Displays wr on map"""
  59. connection = conconnect();
  60. if text == ():
  61. await send_cmd_help(ctx)
  62. return
  63. server = ctx.message.server
  64. to_replace = ctx.message.content.find(text[0])
  65. text = ctx.message.content[to_replace:]
  66. with connection.cursor() as cursor:
  67. sqlwr = "SELECT db2.runtimepro, db1.name, db1.steamid FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db1.steamid = db2.steamid WHERE db2.mapname LIKE %s AND db2.runtimepro > -1.0 ORDER BY db2.runtimepro ASC LIMIT 1"
  68. cursor.execute(sqlwr, (text))
  69. sqlwrresult = cursor.fetchone()
  70. playername = sqlwrresult.get('name')
  71. maptime = sqlwrresult.get('runtimepro')
  72.  
  73. await self.bot.say("SurfTimer | **%s** holds the record with time: **%s** on **%s**" % (playername, convertTime(maptime), text))
  74.  
  75. @commands.command()
  76. async def top(self):
  77. """ - Shows top 10 players on the server"""
  78. if topCommandInUse:
  79. await self.bot.say("Surftimer | Command is already in use.")
  80. else:
  81. topCommandInUse = True
  82. connection = conconnect();
  83. with connection.cursor() as cursor:
  84. sqltop = "SELECT * FROM `ck_playerrank` ORDER BY `points` DESC LIMIT 0, 10"
  85. cursor.execute(sqltop)
  86.  
  87. sqltopresult = cursor.fetchone()
  88. await self.bot.say("SurfTimer | **Top 10 Players:**")
  89. playerrank = 1
  90.  
  91. while sqltopresult is not None:
  92. playertopname = sqltopresult.get('name')
  93. points = sqltopresult.get('points')
  94. finishedmaps = sqltopresult.get('finishedmapspro')
  95. country = sqltopresult.get('country')
  96.  
  97. await self.bot.say("Rank: **%i** Name: **%s** Points: **%i** Finished Maps: **%i** Country: **%s**" % (playerrank, playertopname, points, finishedmaps, country))
  98. playerrank += 1
  99. sqltopresult = cursor.fetchone()
  100.  
  101. if playerrank == 10:
  102. topCommandInUse = False
  103.  
  104.  
  105. @commands.command(pass_context=True)
  106. async def mtop(self, ctx, text : str):
  107. """<mapname> - Shows top 10 players on map"""
  108. connection = conconnect();
  109. if text == ():
  110. await send_cmd_help(ctx)
  111. return
  112. server = ctx.message.server
  113. to_replace = ctx.message.content.find(text[0])
  114. text = ctx.message.content[to_replace:]
  115. with connection.cursor() as cursor:
  116. sqlmtop = "SELECT * FROM `ck_playertimes` WHERE `mapname` = %s ORDER BY runtimepro ASC LIMIT 0, 10"
  117. cursor.execute(sqlmtop, (text))
  118.  
  119. sqlmtopresult = cursor.fetchone()
  120. await self.bot.say("SurfTimer | Top 10 Players on map: **%s**" % (text))
  121. playerrank = 1
  122.  
  123. while sqlmtopresult is not None:
  124. playername = sqlmtopresult.get('name')
  125. maptime = sqlmtopresult.get('runtimepro')
  126.  
  127. await self.bot.say("Rank: **%i** Name: **%s** Time: **%s**" % (playerrank, playername, convertTime(maptime)))
  128. playerrank += 1
  129. sqlmtopresult = cursor.fetchone()
  130.  
  131.  
  132. @commands.command(pass_context=True)
  133. async def m(self, ctx, text : str):
  134. """<mapname> - shows map info for map"""
  135. connection = conconnect();
  136. if text == ():
  137. await send_cmd_help(ctx)
  138. return
  139. server = ctx.message.server
  140. to_replace = ctx.message.content.find(text[0])
  141. text = ctx.message.content[to_replace:]
  142. with connection.cursor() as cursor:
  143. sqlmtier = "SELECT `tier` FROM `ck_maptier` WHERE `mapname` = %s"
  144. cursor.execute(sqlmtier, (text))
  145.  
  146. sqlmtierresult = cursor.fetchone()
  147. maptier = sqlmtierresult.get('tier')
  148.  
  149. sqlmtype = "SELECT * FROM `ck_zones` WHERE `mapname` = %s AND `zonetype` = '3'"
  150. cursor.execute(sqlmtype, (text))
  151.  
  152. maptyperesult = cursor.rowcount
  153. maptype = ""
  154.  
  155. sqlbcount = "SELECT COUNT(DISTINCT zonegroup) as bcount FROM `ck_zones` WHERE `mapname` = %s AND `zonegroup` >= 1"
  156. cursor.execute(sqlbcount, (text))
  157.  
  158. sqlbcountresult = cursor.fetchone()
  159. bcount = sqlbcountresult.get('bcount')
  160.  
  161.  
  162. if maptyperesult:
  163. maptype = 'Staged'
  164. sqlstagecount = "SELECT COUNT(zonetype) as stagecount FROM `ck_zones` WHERE `mapname` = %s AND `zonegroup` = 0 AND `zonetype` = 3"
  165. cursor.execute(sqlstagecount, (text))
  166.  
  167. sqlstagecountresult = cursor.fetchone()
  168. stagecount = sqlstagecountresult.get('stagecount') + 1
  169.  
  170. await self.bot.say("SurfTimer | Map: **%s** - Type: **%s** - Tier: **%s** - Stages: **%i** - Bonuses: **%i**" % (text, maptype, maptier, stagecount, bcount))
  171. else:
  172. maptype = 'Linear'
  173. await self.bot.say("SurfTimer | Map: **%s** - Type: **%s** - Tier: **%s** - Bonuses: **%i**" % (text, maptype, maptier, bcount))
  174.  
  175.  
  176.  
  177. def setup(bot):
  178. bot.add_cog(Surftimer(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement