1
+ from pyplanet .apps .config import AppConfig
2
+ from pyplanet .core import Controller
3
+ from pyplanet .core .events import Callback , handle_generic , Signal
4
+ from pyplanet .contrib .command import Command
5
+ from pyplanet .apps .core .trackmania import callbacks as tm_signals
6
+ import os
7
+ import lxml .etree as et
8
+ import time
9
+ from peewee import RawQuery
10
+ from pyplanet .apps .contrib .pick_and_ban .models import PickAndBan
11
+ import asyncio
12
+
13
+ class PickorBan (AppConfig ):
14
+ game_dependencies = ['trackmania_next' , 'trackmania' ]
15
+
16
+ def __init__ (self , * args , ** kwargs ):
17
+ super ().__init__ (* args , ** kwargs )
18
+
19
+ async def on_start (self ):
20
+ self .context .signals .listen (tm_signals .pickban_complete , self .handle_pickban )
21
+ await self .instance .command_manager .register (
22
+ Command (command = 'pbtest' , aliases = ['pbcall' ], target = self .set_pickban , admin = True , description = 'Start Pick and Ban.' ),
23
+ )
24
+
25
+ async def set_pickban (self , player , data , ** kwargs ):
26
+ pickbandata = '{"steps":[{"team":1,"action":"ban"},{"team":1,"action":"pick"},{"team":2,"action":"pick"},{"team":3,"action":"pick"},{"team":4,"action":"pick"}],"stepDuration":60000,"resultDuration":10000}'
27
+
28
+ await self .instance .gbx .script ('PickBan.Start' , pickbandata , encode_json = False , response_id = False ),
29
+ await self .instance .chat ('$ff0Admin $fff{}$z$s$ff0 tried to do a pickban with following data: {}.' .format (
30
+ player .nickname ,pickbandata )
31
+ )
32
+ async def handle_pickban (self , * args , ** kwargs ):
33
+ try :
34
+ async with self .instance .storage .driver .open ('UserData/Maps/MatchSettings/{}.txt' .format ('testw1lla' ), 'rb+' ) as ghost_file :
35
+ #print(await ghost_file.read())
36
+ data_xml = await ghost_file .read ()
37
+ tree = et .fromstring (data_xml )
38
+ for bad in tree .xpath ("//map" ):
39
+ bad .getparent ().remove (bad ) # here I grab the parent of the element to call the remove directly on it
40
+ items = []
41
+ for uid in kwargs ['playlist' ]:
42
+ await self .instance .chat ('$f0f Following maps are being added: {}.' .format (uid ['uid' ]))
43
+ query = RawQuery (PickAndBan , 'SELECT file FROM `map` WHERE `uid` = "{}" ORDER BY `id` DESC' .format (uid ['uid' ]))
44
+ data = await PickAndBan .execute (query )
45
+ for filename_db in data :
46
+ #print(filename_db.file)
47
+ items .append (filename_db .file )
48
+ items .append (uid ['uid' ])
49
+ contentnav = tree .find (".//startindex" )
50
+ contentdiv = contentnav .getparent ()
51
+ data_new_xml = '<map><file>{}</file><ident>{}</ident></map>\n \n ' .format (filename_db .file , uid ['uid' ])
52
+ contentdiv .insert (contentdiv .index (contentnav )+ 1 ,
53
+ et .XML (data_new_xml ))
54
+ GameDataDirectory = await self .instance .gbx ('GameDataDirectory' )
55
+ MatchSettings_file = '{}{}{}.txt' .format (GameDataDirectory ,'/Maps/MatchSettings/' ,'testw1lla' ) #testwllla.txt
56
+ #print(MatchSettings_file)
57
+ with open (MatchSettings_file , 'wb' ) as f :
58
+ f .write (et .tostring (tree , pretty_print = False , xml_declaration = True , encoding = 'utf-8' ))
59
+ except FileNotFoundError as e :
60
+ return e
61
+ time .sleep (12 )
62
+ await self .instance .chat ('$f0f Going to Restart the Match with the correct Pick & Ban Maps ! Can take a while (approx: 1 Minute!) please be patient!' )
63
+ time .sleep (12 )
64
+
65
+ # Need to figure out why pyplanet doesn't want to load the matchsettings file.
0 commit comments