Advertisement
MegaStar

testbuilfdadsadasd

Dec 7th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. #script by MegaStar
  2. #Version 1.2
  3.  
  4.  
  5. from pyspades.constants import *
  6. from commands import add, get_player, admin
  7. from pyspades.server import grenade_packet
  8. from pyspades.common import Vertex3
  9. from pyspades.world import Grenade
  10. from pyspades.server import block_action, set_color
  11. from pyspades.common import make_color
  12. import json
  13.  
  14. RANGE_CLAIM = 10 #size of the sector, remember that it is double the size, therefore it would be 20x20 blocks
  15. MULTI_CLAIMS = True
  16.  
  17. #-------------------------------------------------------------#
  18. #Quick functions to build and remove a block
  19.  
  20.  
  21. global_claimed = []
  22.  
  23.  
  24.  
  25. def fastblock(connection, (x, y, z), color): #to place a block
  26. set_color.value = make_color(*color)
  27. set_color.player_id = 32
  28. connection.protocol.send_contained(set_color)
  29. block_action.player_id = 32
  30. block_action.value = BUILD_BLOCK
  31. block_action.x = x
  32. block_action.y = y
  33. block_action.z = z
  34. connection.protocol.send_contained(block_action)
  35. connection.protocol.map.set_point(x, y, z, color)
  36.  
  37. def removeblock(connection, (x, y, z)): #to remove a block
  38. block_action.player_id = connection.player_id
  39. block_action.value = DESTROY_BLOCK
  40. block_action.x = x
  41. block_action.y = y
  42. block_action.z = z
  43. connection.protocol.send_contained(block_action)
  44. connection.protocol.map.remove_point(x, y, z)
  45.  
  46. #-------------------------------------------------------------#
  47.  
  48.  
  49. #-------------------------------------------------------------#
  50.  
  51. #Open, edit and save data
  52.  
  53. def open_data():
  54. with open("users.json", "r") as users:
  55. datos = json.load(users)
  56. return datos
  57.  
  58. def save_data(data):
  59. with open("users.json","w") as save_data:
  60. json.dump(data, save_data)
  61.  
  62. def edit_claims(connection, key, user, type):
  63. for claim_users in connection.protocol.claim_coords:
  64. if claim_users[0] == key:
  65. if type == "remove":
  66. claim_users[3].remove(user)
  67. return
  68. elif type == "add":
  69. claim_users[3].append(user)
  70. return
  71.  
  72. #-------------------------------------------------------------#
  73.  
  74.  
  75.  
  76.  
  77. def buildregister(connection, username, password):
  78. #with open("users.json", "r") as users:
  79. datos = open_data()
  80. for key, value in datos.items():
  81. if username == key:
  82. return connection.send_chat("Este username ya esta siendo usado.")
  83. elif MULTI_CLAIMS == False:
  84. if connection.address[0] == value["ip"]:
  85. return connection.send_chat("Usted actualmente ya cuenta con un sector.")
  86.  
  87. datos[username] = {"ip": connection.address[0], "password": password, "coordx": False, "coordy": False, "shared": []}
  88. save_data(datos)
  89. print(datos)
  90. return connection.send_chat("%s acabas de registrarte correctamente, para logearte escribe /buildlogin <username> <password>" % connection.name)
  91.  
  92.  
  93. def buildlogin(connection, username, password):
  94. if not connection.login:
  95. datos = open_data()
  96. for key, value in datos.items():
  97. if username == key:
  98. if password == value["password"]:
  99. connection.login = True
  100. connection.username = username
  101. return connection.send_chat("Acabas de logearte como %s" % username)
  102. else:
  103. return connection.send_chat("La contrasenia es incorrecta.")
  104. return connection.send_chat("La cuenta con el username %s no existe." % username)
  105. return connection.send_chat("Usted actualmente ya esta logeado como %s" % connection.username)
  106.  
  107.  
  108.  
  109. def claim(connection):
  110. if connection.login:
  111. datos = open_data()
  112. posx = datos[connection.username]["coordx"]
  113. posy = datos[connection.username]["coordy"]
  114. if posx != False and posy != False:
  115. return connection.send_chat("Usted actualmente ya tiene un sector.")
  116. connection.claimed = True
  117. return connection.send_chat("Destruye un bloque en el lugar donde reclamaras tu sector.")
  118. return connection.send_chat("Usted necesita logearse antes de poder reclamar un sector.")
  119.  
  120.  
  121. def share(connection, username):
  122. if connection.login:
  123. if username == connection.username:
  124. return connection.send_chat("Ingresa un usuario valido.")
  125. datos = open_data()
  126. users = datos[connection.username]["shared"]
  127. for key, value in datos.items():
  128. if username == key:
  129. if username in users:
  130. return connection.send_chat("%s ya se encuentra en tu lista de usuarios que pueden construir y destruir en tu sector." % username)
  131. else:
  132. users.append(username)
  133. save_data(datos)
  134. edit_claims(connection, connection.username, username, "add")
  135. print(connection.protocol.claim_coords)
  136. return connection.send_chat("%s ahora podra construir y destruir en tu sector." % username)
  137.  
  138. return connection.send_chat("El usuario %s no existe o no esta registrado." % username)
  139. return connection.send_chat("Usted necesita logearse antes de compartir su sector.")
  140.  
  141.  
  142. def unshare(connection, username):
  143. if connection.login:
  144. datos = open_data()
  145. users = datos[connection.username]["shared"]
  146. if len(users) <= 0:
  147. return connection.send_chat("Actualmente no hay usuarios en tu lista.")
  148. elif username == connection.username:
  149. return connection.send_chat("Ingresa un usuario valido.")
  150. for key, value in datos.items():
  151. if username == key:
  152. if username in users:
  153. users.remove(username)
  154. save_data(datos)
  155.  
  156. edit_claims(connection, connection.username, username, "remove")
  157. print(connection.protocol.claim_coords)
  158. return connection.send_chat("%s ya no podra construir ni destruir en tu sector." % username)
  159. else:
  160. return connection.send_chat("%s actualmente no se encuentra en tu lista de usuarios que pueden destruir ni destruir en tu sector." % username)
  161. return connection.send_chat("%s no existe o no esta registrado." % username)
  162. return connection.send_chat("Usted necesita logearse antes de compartir su sector.")
  163.  
  164.  
  165.  
  166. add(claim)
  167. add(buildlogin)
  168. add(buildregister)
  169. add(share)
  170. add(unshare)
  171.  
  172. def apply_script(protocol,connection,config):
  173.  
  174. class BuildConnection(connection):
  175. claimed = False
  176. login = False
  177. username = None
  178.  
  179. def is_in_range(self, x, y):
  180.  
  181. if len(self.protocol.claim_coords) > 0:
  182. for values in self.protocol.claim_coords:
  183. pos_x = values[1]
  184. pos_y = values[2]
  185. for xx in range(pos_x, pos_x + (RANGE_CLAIM * 2)):
  186. for yy in range(pos_y - (RANGE_CLAIM * 2), pos_y):
  187. for new_x in range(x-RANGE_CLAIM,x+RANGE_CLAIM):
  188. for new_y in range(y-RANGE_CLAIM,y+RANGE_CLAIM):
  189. if xx == new_x and yy == new_y:
  190. return True
  191. return False
  192.  
  193.  
  194. def on_block_build_attempt(self,x,y,z):
  195. if len(self.protocol.claim_coords) > 0:
  196. check = self.protocol.is_claimed(x, y, z)
  197. if check:
  198. if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
  199. self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
  200. return False
  201. return connection.on_block_build_attempt(self,x,y,z)
  202.  
  203.  
  204.  
  205. def on_line_build_attempt(self, points):
  206. if len(self.protocol.claim_coords) > 0:
  207. for xyz in points:
  208. check = self.protocol.is_claimed(xyz[0], xyz[1], xyz[2])
  209. if check:
  210. if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
  211. self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
  212. return False
  213.  
  214. return connection.on_line_build_attempt(self, points)
  215.  
  216.  
  217. def on_block_destroy(self,x,y,z,mode):
  218. print(self.protocol.claim_coords)
  219.  
  220.  
  221. if self.login and self.claimed:
  222. if len(self.protocol.claim_coords) > 0:
  223. if self.is_in_range(x, y):
  224. self.send_chat("Escoge otro lugar.")
  225. return False
  226.  
  227. for new_x in range(x-RANGE_CLAIM,x+RANGE_CLAIM):
  228. for new_y in range(y-RANGE_CLAIM,y+RANGE_CLAIM):
  229. if new_x < 0 or new_x >= 512 or new_y < 0 or new_y >= 512 or z < 0 or z >= 62:
  230. return self.send_chat("Usted no puede reclamar un sector aca.")
  231.  
  232. removeblock(self, (new_x, new_y, z))
  233. fastblock(self,(new_x, new_y, z),self.color)
  234. self.send_chat("Tu sector acaba de ser guardado!")
  235. self.claimed = False
  236. datos = open_data()
  237. datos[self.username]["coordx"] = x - RANGE_CLAIM
  238. datos[self.username]["coordy"] = y + RANGE_CLAIM
  239. save_data(datos)
  240. self.protocol.claim_coords.append([self.username, x - RANGE_CLAIM, y + RANGE_CLAIM, []])
  241. return False
  242.  
  243.  
  244. if len(self.protocol.claim_coords) > 0:
  245. check = self.protocol.is_claimed(x, y, z)
  246. if check:
  247. if self.login and self.username != check[0] and self.username not in check[3] or not self.login:
  248. self.send_chat("Este lugar le pertenece al usuario %s" % check[0])
  249. return False
  250.  
  251. return connection.on_block_destroy(self,x,y,z,mode)
  252.  
  253. class BuildProtocol(protocol):
  254. claim_coords = []
  255.  
  256.  
  257. def on_map_change(self, map):
  258. datos = open_data()
  259. if len(datos) > 0:
  260. for key, value in datos.items():
  261. if value["coordx"] != False and value["coordx"] != False:
  262. self.claim_coords.append( [key, value["coordx"], value["coordy"], value["shared"]])
  263. print(self.claim_coords)
  264.  
  265. protocol.on_map_change(self, map)
  266.  
  267.  
  268.  
  269. def is_claimed(self, x, y, z):
  270. for pr in self.claim_coords:
  271. for xx in range(pr[1], pr[1] + (RANGE_CLAIM * 2)):
  272. for yy in range(pr[2] - (RANGE_CLAIM * 2), pr[2]):
  273. if x == xx and y == yy:
  274. return pr
  275. return False
  276.  
  277. return BuildProtocol, BuildConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement