Skip to content

Commit ecbf811

Browse files
authored
add pick and ban for pyplanet
1 parent 8977e21 commit ecbf811

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

pick_and_ban/__init__.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.
3.96 KB
Binary file not shown.

pick_and_ban/app.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
from . import PickorBan as PickorBan
3+
4+
PickorBan = PickorBan

pick_and_ban/models/PickAndBan.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import datetime
2+
from peewee import *
3+
from pyplanet.apps.core.maniaplanet.models import Map
4+
from pyplanet.core.db import Model
5+
6+
7+
class PickAndBan(Model):
8+
map_uid = CharField()

pick_and_ban/models/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .PickAndBan import PickAndBan
2+
3+
__all__ = [
4+
'PickAndBan',
5+
]
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)