Page 1 of 1

GiveCash

Posted: Mon Sep 28, 2015 9:15 pm
by Jerome69
Hello,

Another plugin request :)

I would like a plugin which give money each round. If a file can store the amount to give and a sentence, it will be better

Thanks you

Posted: Mon Sep 28, 2015 9:55 pm
by satoon101
Are there any criteria for which players to give money to or is it just give all players the same amount every round? Do you want to set every player's cash to the value or just give them this amount extra each round? By sentence, you mean that it will print a message in chat every round to let each player know they were given cash?

The following is based off of the idea that every player gets the same amount every round, the answer to the second question is 'add cash', and that the answer to the last question is 'yes'.

Syntax: Select all

# ../addons/source-python/plugins/<plugin_name>/<plugin_name>.py

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Config
from config.manager import ConfigManager
# Cvars
from cvars.flags import ConVarFlags
# Events
from events import Event
# Filters
from filters.players import PlayerIter
# Messages
from messages import SayText2
# Translations
from translations.strings import LangStrings


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Get the translation strings
config_strings = LangStrings('<plugin_name>')

# Get the message to send
message = SayText2(message=config_strings['chat'])


# =============================================================================
# >> CONFIGURATION
# =============================================================================
# Create the config file
with ConfigManager('<plugin_name>') as config:

# Create the cash amount convar
cash_amount = config.cvar(
'round_cash_amount', 100, ConVarFlags.NONE,
'Amount of cash to add to each player at the start of each round.')


# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event('round_start')
def give_cash(game_event):
# Get the cash amount
amount = cash_amount.get_int()

# Loop through all living players
for player in PlayerIter('alive', return_types='player'):

# Give the current player the cash amount
player.cash += amount

# Send the message to all players
message.send(amount=amount)


Syntax: Select all

# ../resource/source-python/translations/<plugin_name>.ini

[chat]
en = "You were given $$$amount."


The location of the 2 files are in the top line comment of each syntax block. Replace <plugin_name> with whatever you name the plugin. Make sure to replace that actual value in the ConfigManager and LangStrings lines so that it creates the config file properly and gets the proper translation file. Then, once you have loaded the plugin once, you can change the value in the ../cfg/source-python/<plugin_name>.cfg file which will take effect when loading the plugin. You can also use the round_cash_amount cvar anytime in the server console or with rcon to change its value live.

Posted: Mon Sep 28, 2015 10:37 pm
by Jerome69
Yes, thank you it works fine.

But I thought the amount max was $16.000, but when the plugin give money, I have $35.000 or more.

I tried this to have always $16.000:

amount = 16000 - player.cash
player.cash += amount


It doesn't work. Any idea?

Posted: Mon Sep 28, 2015 11:12 pm
by satoon101
Do you just want to always give every player 16000 every round? Or do you want to add a specific amount to each player with the maximum cash being 16000?

Posted: Tue Sep 29, 2015 5:05 am
by Jerome69
I would like a specific amount, with maximum cash being $16.000.

And yes, for every player.

Thanks

Posted: Tue Sep 29, 2015 8:20 am
by Predz
Change:

Syntax: Select all

player.cash += amount


To:

Syntax: Select all

player.cash = min(player.cash+amount, 16000)


Will make it so no matter the situation, when cash is given that the player's cash will never exceed $16000.

Posted: Tue Sep 29, 2015 11:24 am
by Jerome69
Predz wrote:Change:

Syntax: Select all

player.cash += amount


To:

Syntax: Select all

player.cash = min(player.cash+amount, 16000)


Will make it so no matter the situation, when cash is given that the player's cash will never exceed $16000.


I tried it but it gives me $17.000...

Posted: Tue Sep 29, 2015 11:30 am
by Ayuto
That's impossible. Please show us the code you are using now.

Posted: Tue Sep 29, 2015 11:54 am
by Jerome69
This is the code :

Syntax: Select all

# ../addons/source-python/plugins/GiveCash/GiveCash.py

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Config
from config.manager import ConfigManager
# Cvars
from cvars.flags import ConVarFlags
# Events
from events import Event
# Filters
from filters.players import PlayerIter
# Messages
from messages import SayText2
# Translations
from translations.strings import LangStrings


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Get the translation strings
#config_strings = LangStrings('GiveCash')

# Get the message to send
#message = SayText2(message=config_strings['chat'])


# =============================================================================
# >> CONFIGURATION
# =============================================================================
# Create the config file
with ConfigManager('GiveCash') as config:

# Create the cash amount convar
cash_amount = config.cvar(
'round_cash_amount', 100, ConVarFlags.NONE,
'Amount of cash to add to each player at the start of each round.')


# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event('round_start')
def give_cash(game_event):
# Get the cash amount
amount = cash_amount.get_int()

# Loop through all living players
for player in PlayerIter('alive', return_types='player'):

# Give the current player the cash amount
player.cash += min(player.cash+amount, 16000)

# Send the message to all players
message.send(amount=amount)

Posted: Tue Sep 29, 2015 12:24 pm
by Ayuto
Take a closer look at the code posted by Predz. This is what he suggested:

Syntax: Select all

player.cash = min(player.cash+amount, 16000)
And this is what you have done:

Syntax: Select all

player.cash += min(player.cash+amount, 16000)
There is a little difference between both versions.

Posted: Tue Sep 29, 2015 3:48 pm
by Jerome69
This is the new code, with the correction :

Syntax: Select all

# ../addons/source-python/plugins/GiveCash/GiveCash.py

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Config
from config.manager import ConfigManager
# Cvars
from cvars.flags import ConVarFlags
# Events
from events import Event
# Filters
from filters.players import PlayerIter
# Messages
from messages import SayText2
# Translations
from translations.strings import LangStrings


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Get the translation strings
config_strings = LangStrings('GiveCash')

# Get the message to send
message = SayText2(message=config_strings['chat'])


# =============================================================================
# >> CONFIGURATION
# =============================================================================
# Create the config file
with ConfigManager('GiveCash') as config:

# Create the cash amount convar
cash_amount = config.cvar(
'round_cash_amount', 100, ConVarFlags.NONE,
'Amount of cash to add to each player at the start of each round.')


# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event('round_start')
def give_cash(game_event):
# Get the cash amount
amount = cash_amount.get_int()

# Loop through all living players
for player in PlayerIter('alive', return_types='player'):

# Give the current player the cash amount
player.cash = min(player.cash+amount, 16000)

# Send the message to all players
message.send(amount=amount)


And it doesn't work anymore. Someone has tested the plugin? Or it's just me??

Posted: Tue Sep 29, 2015 8:08 pm
by Ayuto
Yep, it's working for me.

Posted: Tue Sep 29, 2015 8:31 pm
by satoon101
I would recommend changing the message so that it shows the actual amount the player gains (or that they are already maxed out). Other than that, it looks like it should work fine to me.

Try this:

Syntax: Select all

# ../addons/source-python/plugins/GiveCash/GiveCash.py

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Config
from config.manager import ConfigManager
# Cvars
from cvars.flags import ConVarFlags
# Events
from events import Event
# Filters
from filters.players import PlayerIter
# Messages
from messages import SayText2
# Translations
from translations.strings import LangStrings


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Get the translation strings
message_strings = LangStrings('GiveCash')

# Store the give message instance
give_message = SayText2(message=message_strings['Give'])

# Store the max cash message instance
max_message = SayText2(message=message_strings['Max'])


# =============================================================================
# >> CONFIGURATION
# =============================================================================
# Create the config file
with ConfigManager('GiveCash') as config:

# Create the cash amount convar
cash_amount = config.cvar(
'round_cash_amount', 100, ConVarFlags.NONE,
'Amount of cash to add to each player at the start of each round.')


# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event('round_start')
def give_cash(game_event):
# Get the cash amount
amount = cash_amount.get_int()

# Loop through all living players
for player in PlayerIter('alive', return_types='player'):

# Get the amount to add to the current player
give_amount = min(player.cash + amount, 16000) - player.cash

# Does the player need any cash?
if give_amount:

# Give the current player the cash amount
player.cash += give_amount

# Send the give message to the player
give_message.send(player.index, amount=give_amount)

# Does the player already have max cash?
else:

# Send the max cash message to the player
max_message.send(player.index)


Syntax: Select all

# ../resource/source-python/translations/GiveCash.ini

[Give]
en = "You were given $$$amount."

[Max]
en = "You are already at max cash, none given."

Posted: Tue Sep 29, 2015 8:37 pm
by Jerome69
Ok, I have found : in the configuration file, I have to put "16000" min.

I tried to put "100" as default, but it gives me only 100.

Works fine for me too now.

Thanks everybody :)