[HL2:DM] Mapchooser
Posted: Sat Mar 30, 2019 5:50 pm
Hello,
I get this message lately when I switch from Deathmatch to a co-op.
If I want to switch back to Deathmatch then nothing works.
The map does not change anymore.
and here is the mapvoting.py
I get this message lately when I switch from Deathmatch to a co-op.
If I want to switch back to Deathmatch then nothing works.
The map does not change anymore.
Code: Select all
18:38:46
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/packages/site-packages/path.py", line 100, in io_error_compat
yield
File "../addons/source-python/packages/site-packages/path.py", line 743, in open
return io.open(self, *args, **kwargs)
FileNotFoundError: [Errno 2] No such file or directory: Path('../addons/source-python/plugins/supermod/cfg/mapconfigs/map_default.cfg')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "../addons/source-python/plugins/supermod/modules/mapvoting.py", line 62, in on_changelevel
mapconfig.execute_config(new_map)
File "../addons/source-python/plugins/supermod/modules/mapconfig.py", line 44, in execute_config
execute_config("map_default")
File "../addons/source-python/plugins/supermod/modules/mapconfig.py", line 40, in execute_config
with mappath.open('r') as f:
File "../addons/source-python/packages/site-packages/path.py", line 743, in open
return io.open(self, *args, **kwargs)
File "../addons/source-python/Python3/contextlib.py", line 100, in __exit__
self.gen.throw(type, value, traceback)
File "../addons/source-python/packages/site-packages/path.py", line 106, in io_error_compat
raise os_err
FileNotFoundError: [Errno 2] No such file or directory: Path('../addons/source-python/plugins/supermod/cfg/mapconfigs/map_default.cfg')
and here is the mapvoting.py
Syntax: Select all
import random
import operator
from cvars import cvar
from cvars import ConVar
from menus import PagedMenu, PagedOption, Text
from players.entity import Player as PlayerEntity
from players.helpers import index_from_playerinfo
from filters.players import PlayerIter
from listeners import OnLevelInit
from listeners.tick import Delay
from listeners.tick import Repeat
from commands.say import SayCommand
import math
from messages import SayText2
from commands.server import ServerCommand
from commands import CommandReturn
from messages import HintText
from supermod import functions
from supermod.cfg.mapcycle import mapcycle
from listeners import OnLevelShutdown
from engines.server import queue_command_string
import core
from supermod.modules import mapconfig
mapvoting = 1
sp_nextmap = ConVar('sp_nextmap')
sp_nextmap.set_string('')
core.console_message("\n[Supermod] Mapvoting loaded!")
@OnLevelInit
def map_start(mapname):
global mapvoting
mapvoting = 1
map_voting.map_start()
sp_nextmap.set_string('')
@ServerCommand('changelevel')
def on_changelevel(command):
new_map = sp_nextmap.get_string()
true_map = command.arg_string
if new_map in ('', '0') or len(command) <= 1:
if true_map in mapconfig.maps:
mapconfig.execute_config(true_map)
else:
mapconfig.execute_config("map_default")
return
if new_map in command.command_string:
mapconfig.execute_config(new_map)
return
if new_map in mapconfig.maps:
mapconfig.execute_config(new_map)
else:
mapconfig.execute_config("map_default")
queue_command_string('changelevel %s' % new_map)
#return CommandReturn.BLOCK'''
def unload():
map_voting.stop_delay()
def disable_mapvoting():
global mapvoting
mapvoting = 0
class MapVoting(object):
def __init__(self):
self.timelimit = cvar.find_var('mp_timelimit').get_int()
self.active = False
self.delay = None
self.votedelay = None
self.type = 0
self.maplist = []
self.recent = []
self.chosen = []
self.voting = {}
self.players = 0
self.votedmap = None
self.repeat = Repeat(self.hud)
self.menu = PagedMenu()
self.menu.select_callback = self.menu_callback
for category in mapcycle:
for map in mapcycle[category]:
self.maplist.append(map)
def map_start(self):
self.active = False
self.stop_delay()
self.start_delay()
def start_delay(self):
self.delay = Delay((self.timelimit * 60) - 300, self.start_voting)
def stop_delay(self):
try:
self.delay.cancel()
except:
pass
def start_voting(self):
global mapvoting
if not mapvoting:
return
if not len(self.maplist):
return functions.msg('maplist empty')
if self.active:
self.stop_mapvote(cancel=True)
self.type = 0
self.menu.title = "Choose a map"
self.menu.description = None
functions.msg('A mapvote is active! Press ESC to vote.')
self.voting.clear()
del self.chosen[:]
temp = self.maplist
map1 = random.choice(temp)
self.chosen.append(map1)
temp.remove(map1)
if len(temp):
map2 = random.choice(temp)
self.chosen.append(map2)
temp.remove(map2)
if len(temp):
map3 = random.choice(temp)
self.chosen.append(map3)
temp.remove(map3)
if len(temp):
map4 = random.choice(temp)
self.chosen.append(map4)
temp.remove(map4)
if len(temp):
map5 = random.choice(temp)
self.chosen.append(map5)
temp.remove(map5)
self.menu.clear()
for map in self.chosen:
self.menu.append(PagedOption(map, map))
self.active = True
self.menu.send()
self.votedelay = Delay(30, self.stop_voting)
self.repeat.start(1, math.inf)
def stop_voting(self, cancel=False):
self.active = False
self.menu.close()
self.repeat.pause()
if cancel:
return
if not self.voting:
map = random.choice(self.chosen)
functions.msg('Map vote failed! Noone voted... Randomly choosing %s as next map'%map)
Delay(0.5, functions.servercommand, ('sp_nextmap %s'%map,))
return
votes = self.voting['votes']
del self.voting['votes']
winner = max(self.voting, key=operator.itemgetter(1))
map = [winner, self.voting[winner]]
percentage = int((map[1] / votes) * 100)
functions.msg('Next map will be %s (%s%% of %s votes)'%(map[0], percentage, votes))
Delay(0.5, functions.servercommand, ('sp_nextmap %s'%map[0],))
def votemap(self, index):
if self.active:
return functions.tell(index, 'A map vote is currently running!')
m = PagedMenu()
m.title='Start a mapvote'
m.select_callback = self.category_callback
for category in mapcycle:
m.append(PagedOption(category.title(), category))
m.send(index)
def category_callback(self, menu, index, option):
category = option.value
m = PagedMenu()
m.title='Start a mapvote'
m.select_callback = self.votemap_callback
for map in mapcycle[category]:
m.append(PagedOption(map, map))
m.send(index)
def votemap_callback(self, menu, index, option):
if self.active:
return functions.tell(index, 'A map vote is currently running!')
self.start_mapvote(index, option.value)
def start_mapvote(self, index, mapname):
self.type = 1
self.voting.clear()
self.active = True
self.votedmap = mapname
self.players = len(PlayerIter('human'))
name = PlayerEntity(index).name
functions.msg('%s started a mapvote! Press ESC to vote'%name)
self.menu.title = "Change map?"
self.menu.description = "%s wants to change the map to %s"%(name, mapname)
self.menu.clear()
self.menu.append(PagedOption('Yes', 'yes'))
self.menu.append(PagedOption('No', 'no'))
self.menu.send()
self.votedelay = Delay(30, self.stop_mapvote)
self.repeat.start(1, math.inf)
def stop_mapvote(self):
self.active = False
self.menu.close()
self.repeat.pause()
if not self.voting:
return functions.msg('Mapvote failed! Not enough votes...')
if 'yes' not in self.voting:
return functions.msg('Mapvote failed! Not enough votes...')
if self.voting['yes'] < 1:
return functions.msg('Mapvote failed! Not enough votes...')
if (self.voting['yes'] / self.players) * 100 < 60:
return functions.msg('Mapvote failed! Not enough votes...')
functions.msg('The mapvote was successful! Changing map to %s'%self.votedmap)
Delay(0.5, functions.servercommand, ('sp_nextmap %s'%self.votedmap,))
Delay(3, functions.servercommand, ('changelevel %s'%self.votedmap,))
def menu_callback(self, menu, index, option):
if not self.active:
return
if option.value == 'yes' or option.value == 'no':
if 'votes' not in self.voting:
self.voting['votes'] = 0
if option.value not in self.voting:
self.voting[option.value] = 0
self.voting['votes'] += 1
self.voting[option.value] += 1
if self.voting['votes'] >= len(PlayerIter('human')):
self.stop_mapvote()
self.votedelay.cancel()
return
functions.tell(index, 'You voted for %s'%option.value)
if 'votes' not in self.voting:
self.voting['votes'] = 0
if option.value not in self.voting:
self.voting[option.value] = 0
self.voting['votes'] += 1
self.voting[option.value] += 1
if self.voting['votes'] >= len(PlayerIter('human')):
self.stop_voting()
self.votedelay.cancel()
def hud(self):
if not self.active:
return
if self.type == 0:
text = "Pending Vote: \n\n"
for map in self.chosen:
votes = 0
if self.voting:
if map in self.voting:
votes = self.voting[map]
text += "%s: %s \n"%(map, votes)
functions.hudmsg(text, channel=4, x=0.81, y=0.05, holdtime=2.1)
elif self.type == 1:
text = "Change map to \n%s \n"%self.votedmap
yes = 0
no = 0
if self.voting:
if 'yes' in self.voting:
yes = self.voting['yes']
if 'no' in self.voting:
no = self.voting['no']
text += "Yes: %s \n"%yes
text += "No: %s \n"%no
functions.hudmsg(text, channel=4, x=0.02, y=0.12, holdtime=2.1)
@SayCommand(['nextmap', '!nextmap'])
def nextmap_command(command, index, teamonly):
if sp_nextmap.get_string() != "":
SayText2("The next map is: "+sp_nextmap.get_string()).send()
else:
SayText2("The next map is not yet decided!").send()
@SayCommand(['!votemap', 'votemap', '!mapvote', 'mapvote', 'vote', '!vote'])
def votemap_command(command, index, teamonly):
map_voting.votemap(index)
map_voting = MapVoting()
map_voting.map_start()