Page 1 of 2

Weapon restrict

Posted: Thu Sep 24, 2015 11:56 am
by Jerome69

Posted: Thu Sep 24, 2015 8:41 pm
by BackRaw
Try that:

Syntax: Select all

# ../sprestrict/sprestrict.py

"""Simple weapon restriction plugin."""


# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict

# Souce.Python Imports
# Entities
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import LevelInit
from listeners import LevelShutdown
# Memory
from memory import make_object
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import PlayerEntity
# Weapons
from weapons.manager import weapon_manager


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')

# Hold any found items in the file in this list
restrictions = defaultdict(list)


# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with open(restrictions_txt) as file_object:

# Store the lines read
lines = filter(lambda x: weapon_manager.prefix in x,
file_object.read().replace('\r', '').replace('@', '').split('\n')
)

# Loop through each line
for line in lines:

# Get the team and the weapon
team, weapon = line.split(' ')

# Append the weapon
restrictions[team].append(weapon)


# =============================================================================
# >> LISTENERS
# =============================================================================
@LevelInit
def level_init(mapname):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()


@LevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()


# =============================================================================
# >> ENTITY HOOKS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Called when a player is about to buy something."""
# Get a PlayerEntity object from the pointer passed
player = make_object(PlayerEntity, args[0])

# Get the weapon name the player is about to buy
weapon = weapon_manager.prefix + args[1]

# Loop through the restrictions
for team in restrictions:

# Is the weapon restricted?
if weapon in restrictions[team]:

# Is the player in the right team?
if player.index in PlayerIter(is_filters=team):

# If yes, return False to block the purchase
return False


# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()


def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()


Create the file: ../<game>/addons/source-python/data/plugins/sprestrict/restrictions.txt

Its contents should read:

Code: Select all

@all weapon_awp
@all weapon_sg550
@all weapon_scout
@all weapon_g3sg1
That's it. Load the plugin via

Code: Select all

sp load sprestrict
You can also put that into your autoexec.cfg, of course.

Note: Everytime the plugin is reloaded or the map has changed, the file plugin reads the restrictions.txt file again.

Edit: Had to update it, because I forgot the team restriction side of things.

Posted: Fri Sep 25, 2015 9:27 am
by Jerome69
Thanks a lot, I will try it.

A question : with my menu, I have options for the weapons :
Lock weapons
Unlock weapons
Lock smokegrenade
Unlock smokegrenade

Each command load a cfg file:

Lock Smoke.cfg
es_load ezrestrict
ezrestrict @all weapon_smokegrenade


Lock weapons.cfg
es_load ezrestrict
ezrestrict @all weapon_awp
ezrestrict @all weapon_sg550
ezrestrict @all weapon_scout
ezrestrict @all weapon_g3sg1



Lock Smoke.cfg
es_load ezrestrict
ezrestrict @all weapon_smokegrenade


Unlock weapons.cfg
ezunrestrict @all weapon_awp
ezunrestrict @all weapon_sg550
ezunrestrict @all weapon_scout
ezunrestrict @all weapon_g3sg1


Unlock smoke.cfg
ezunrestrict @all weapon_smokegrenade


So, I can lock smoke without locking weapons, locking weapons without smoke, or smoke and weapons together.

Can I do the same with your plugin? And how?

Thanks a lot.

[edit]I tried your plugin (it loads with atoexec.cfg). I can't buy any weapon in the restrictions.txt file, but bots can buy the weapons.

If I am in the same team with bots I can't buy weapons buy they can too...

Any idea?[/edit]

Posted: Fri Sep 25, 2015 1:42 pm
by BackRaw
Hhhmmmm that's strange, maybe bots don't use the buy command. I gotta look at that. Yes the menus are possible.

Posted: Fri Sep 25, 2015 1:50 pm
by satoon101
Just so you know, we are still working on adding a basic restriction system to the API. It will hook CCSPlayer::HandleCommand_Buy_Internal to stop weapon purchasing.

Posted: Fri Sep 25, 2015 4:29 pm
by BackRaw
satoon101 wrote:Just so you know, we are still working on adding a basic restriction system to the API. It will hook CCSPlayer::HandleCommand_Buy_Internal to stop weapon purchasing.


Good point. Maybe I can use that until the system is ready.

Posted: Fri Sep 25, 2015 6:34 pm
by BackRaw
I edited my post above, but I'll post here again. Try this one if it fixes the bot buying - only tested with CSS:

Syntax: Select all

# ../sprestrict/sprestrict.py

"""Simple weapon restriction plugin."""


# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict

# Souce.Python Imports
# Entities
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import LevelInit
from listeners import LevelShutdown
# Memory
from memory import make_object
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import PlayerEntity
# Weapons
from weapons.manager import weapon_manager


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')

# Hold any found items in the file in this list
restrictions = defaultdict(list)


# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with open(restrictions_txt) as file_object:

# Store the lines read
lines = filter(lambda x: weapon_manager.prefix in x,
file_object.read().replace('\r', '').replace('@', '').split('\n')
)

# Loop through each line
for line in lines:

# Get the team and the weapon
team, weapon = line.split(' ')

# Append the weapon
restrictions[team].append(weapon)


# =============================================================================
# >> LISTENERS
# =============================================================================
@LevelInit
def level_init(mapname):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()


@LevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()


# =============================================================================
# >> ENTITY HOOKS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Called when a player is about to buy something."""
# Get a PlayerEntity object from the pointer passed
player = make_object(PlayerEntity, args[0])

# Get the weapon name the player is about to buy
weapon = weapon_manager.prefix + args[1]

# Loop through the restrictions
for team in restrictions:

# Is the weapon restricted?
if weapon in restrictions[team]:

# Is the player in the right team?
if player.index in PlayerIter(is_filters=team):

# If yes, return False to block the purchase
return False


# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()


def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()

Posted: Sat Sep 26, 2015 3:02 pm
by Jerome69
Works fine :)

I think it's my "bot.cfg" which was the problem.

Thanks a lot.

Now I will try to make a new menu without eventscript (I don't want to surcharge CSS ^^).

Posted: Sun Sep 27, 2015 1:53 pm
by BackRaw
Jerome69 wrote:Works fine :)

I think it's my "bot.cfg" which was the problem.

Thanks a lot.

Now I will try to make a new menu without eventscript (I don't want to surcharge CSS ^^).


I'm on it already :)

Posted: Mon Nov 02, 2015 4:58 pm
by Jerome69
Hello,

I have done my menu :) Now, I would like to add the possibility to lock and unlock weapons or grenades with this menu as I told you.

How can I do? Have I to use a cfg or a txt file, as I used with and older plugin?

Thanks

Posted: Tue Feb 02, 2016 3:46 pm
by Jerome69
Since an update, the plugin doesn't load:

ImportError : cannot import name 'LevelInit'.

It tried to find anything on this, but nothing.

Can you help me please?

Thanks

Posted: Tue Feb 02, 2016 4:09 pm
by Ayuto
Development status update (November 2015) wrote:All listeners are now starting with the "On" prefix.

http://forums.sourcepython.com/showthread.php?1002-Development-status-update-%28November-2015%29

Posted: Tue Feb 09, 2016 11:52 am
by Jerome69
Ok, i have made change.

There is the new code :

Syntax: Select all

# ../sprestrict/sprestrict.py
"""Simple weapon restriction plugin."""

# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict

# Souce.Python Imports
# Entities
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import OnLevelInit
from listeners import OnLevelShutdown
# Memory
from memory import make_object
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import Player
# Weapons
from weapons.manager import weapon_manager

# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')

# Hold any found items in the file in this list
restrictions = defaultdict(list)

# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with open(restrictions_txt) as file_object:

# Store the lines read
lines = filter(lambda x: weapon_manager.prefix in x,
file_object.read().replace('\r', '').replace('@', '').split('\n')
)

# Loop through each line
for line in lines:
# Get the team and the weapon
team, weapon = line.split(' ')

# Append the weapon
restrictions[team].append(weapon)

# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelInit
def level_init(mapname):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()

@OnLevelShutdown
def on_level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()

# =============================================================================
# >> ENTITY HOOKS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Called when a player is about to buy something."""
# Get a Player object from the pointer passed
player = make_object(Player, args[0])

# Get the weapon name the player is about to buy
weapon = weapon_manager.prefix + args[1]

# Loop through the restrictions
for team in restrictions:
# Is the weapon restricted?
if weapon in restrictions[team]:
# Is the player in the right team?
if player.index in PlayerIter(is_filters=team):
# If yes, return False to block the purchase
return False

# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()

def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()


The plugin loads, but I can buy weapons which are in the "restrictions.txt" file.

Do you know why?

Thanks

Posted: Fri Feb 12, 2016 5:50 pm
by BackRaw
Got it working:

Syntax: Select all

# ../sprestrict/sprestrict.py

"""Simple weapon restriction plugin."""


# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict

# Souce.Python Imports
# Colors
from colors import RED
from colors import WHITE
# Commands
from commands.client import ClientCommand
# Filters
from filters.players import PlayerIter
# Listeners
from listeners import OnLevelInit
from listeners import OnLevelShutdown
# Messages
from messages import SayText2
# Paths
from paths import PLUGIN_DATA_PATH
# Weapons
from weapons.manager import weapon_manager


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')

# Hold any found items in the file in this list
restrictions = defaultdict(set)


# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with restrictions_txt.open() as file_object:

# Store its contents
contents = file_object.read().replace('\r', '').split('\n')

# Loop through each line
for line in filter(lambda x: weapon_manager.prefix in x and x.startswith('@'), contents):

# Get the team and the weapon
team, weapon = line[1:].split(' ')

# Append the weapon
restrictions[team].add(weapon)


# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelInit
def level_init(map_name):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()


@OnLevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()


# =============================================================================
# >> CLIENT COMMANDS
# =============================================================================
@ClientCommand('buy')
def on_buy(command, index, team=None):
"""Hooks the 'buy' command and allows or restricts the purchase of a weapon."""
# Get the full weapon name
weapon = weapon_manager.prefix + command[1]

# Loop through the restrictions
for team, restricted in restrictions.items():

# Ignore this team if the weapon is not restricted
if weapon not in restricted:
continue

# Is the player purchasing the weapon on a team where the weapon is restricted?
if index in map(lambda x: x.index, PlayerIter(is_filters=team)):

SayText2('{0}SP-Restrict: {1}{2} is restricted.'.format(
RED, WHITE, command[1]
)).send(index)

# If yes, return False to block the purchase
return False

# Return True to allow the purchase
return True


# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()


def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()


I'm using a ClientCommand for hooking the buy command now, the old school way :D

Posted: Sat Feb 13, 2016 1:30 pm
by Jerome69
Wonderfull, it works fine.

Thanks :)

Posted: Mon Feb 15, 2016 10:57 am
by Jerome69
I have found a bug :)

I can't buy restricted weapons, but bots can do it...

In the restrictions.txt file, I have this :
@all weapon_awp
@all weapon_sg550
@all weapon_scout
@all weapon_g3sg1

Posted: Mon Feb 15, 2016 1:24 pm
by satoon101
That is probably because he only restricts the buy command instead of using a buy_internal hook. I have been working on SP's weapon restriction and am close to pushing some changes to a new test branch. Once that is available, this sort of thing will be much easier to implement.

Posted: Mon Feb 29, 2016 8:31 am
by BackRaw
satoon101 wrote:That is probably because he only restricts the buy command instead of using a buy_internal hook. I have been working on SP's weapon restriction and am close to pushing some changes to a new test branch. Once that is available, this sort of thing will be much easier to implement.


Yeah, the hooking didn't work for me, so I figured bots are handled through it.

Jerome69: Just wait for satoon's restriction plugin for that or until he's pushed the changes to SP. :)

Posted: Mon Feb 29, 2016 9:13 am
by Jerome69
ok no problem I will wait.

Thanks :)

Posted: Mon Feb 29, 2016 12:58 pm
by BackRaw
I worked it out, hopefully. Try this:

Syntax: Select all

# ../sprestrict/sprestrict.py

"""Simple weapon restriction plugin."""


# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
# Collections
from collections import defaultdict

# Souce.Python Imports
# Colors
from colors import RED
from colors import WHITE
# Entities
from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
# Listeners
from listeners import OnLevelInit
from listeners import OnLevelShutdown
# Memory
from memory import make_object
# Messages
from messages import SayText2
# Paths
from paths import PLUGIN_DATA_PATH
# Players
from players.entity import Player
# Weapons
from weapons.manager import weapon_manager


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# The path to the file
# ../addons/source-python/data/plugins/sprestrict/restrictions.txt
restrictions_txt = PLUGIN_DATA_PATH.joinpath('sprestrict', 'restrictions.txt')

# Hold any found items in the file in a list
restrictions = defaultdict(set)

# Hold team names and their values in a dict
teams = {
'ct': 3,
't': 2
}


# =============================================================================
# >> FUNCTIONS
# =============================================================================
def load_restrictions():
"""Load the items from the txt file into memory."""
# Open the restrictions file
with restrictions_txt.open() as file_object:

# Store its contents
contents = file_object.read().replace('\r', '').split('\n')

# Loop through each line
for line in filter(lambda x: weapon_manager.prefix in x and x.startswith('@'), contents):

# Get the team and the weapon
team, weapon = line[1:].split(' ')

# Append the weapon
restrictions[team].add(weapon)


# =============================================================================
# >> LISTENERS
# =============================================================================
@OnLevelInit
def level_init(map_name):
"""Called when the map is about to be changed."""
# Load the restrictions into memory
load_restrictions()


@OnLevelShutdown
def level_shutdown():
"""Called when the map is shut down."""
# Clear the restrictions dict
restrictions.clear()


# =============================================================================
# >> CLIENT COMMANDS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'buy_internal')
def pre_buy(args):
"""Hooks the 'buy' command and allows or restricts the purchase of a weapon."""
# Get the player entity
player = make_object(Player, args[0])

# Get the full weapon name
weapon = weapon_manager.prefix + args[1]

# Loop through the restrictions
for team, restricted in restrictions.items():

# Ignore this team if the weapon is not restricted
if weapon not in restricted:
continue

if team == 'all' or teams[team] == player.team:
SayText2('{0}SP-Restrict: {1}{2} is restricted.'.format(
RED, WHITE, args[1]
)).send(player.index)

return False


# =============================================================================
# >> LOAD & UNLOAD
# =============================================================================
def load():
"""Called when this plugin is loaded."""
# Load the restrictions into memory
load_restrictions()


def unload():
"""Called when this plugin is unloaded."""
# Clear the restrictions dict
restrictions.clear()