[Cs:s] Zombie Riot potion

Please post any questions about developing your plugin here. Please use the search function before posting!
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

[Cs:s] Zombie Riot potion

Postby cssbestrpg » Thu Aug 19, 2021 4:15 pm

Hi, i just noticed weird error on my potion code for my zombie riot plugin.
Sometimes when player buys infinity bullets potions it randomly gives this error.

The error:

Code: Select all

[2021-08-19 19:09:10]: [SP] Caught an Exception:[2021-08-19 19:09:10]: Traceback (most recent call last):[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/commands/auth.py", line 44, in __call__[2021-08-19 19:09:10]: return self.callback(*args)[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/menus/queue.py", line 267, in _menuselect_callback[2021-08-19 19:09:10]: _radio_queues[index]._select(choice)[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/menus/queue.py", line 126, in _select[2021-08-19 19:09:10]: next_menu = active_menu._select(self._index, choice)[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/menus/radio.py", line 113, in _select[2021-08-19 19:09:10]: self._player_pages[player_index].options[choice_index])[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/menus/base.py", line 126, in _select[2021-08-19 19:09:10]: return self.select_callback(self, player_index, choice_index)[2021-08-19 19:09:10]: File "../addons/source-python/plugins/zr/modules/potion/__init__.py", line 98, in potion_menu_callback[2021-08-19 19:09:10]: player.cash -= 16000[2021-08-19 19:09:10]: File "../addons/source-python/packages/source-python/entities/_base.py", line 240, in __setattr__[2021-08-19 19:09:10]: object.__setattr__(self, attr, value)[2021-08-19 19:09:10]: OverflowError: can't convert negative value to unsigned int


The code:

Syntax: Select all

from events import Event
from players.entity import Player
from players.helpers import index_from_userid, userid_from_index
from menus import SimpleMenu, SimpleOption, Text
from weapons.manager import weapon_manager
from zr.modules import message
from zr import zr
from rpg import rpg

def potion_market_menu(userid):
menu = SimpleMenu()
if zr.is_queued(menu, index_from_userid(userid)):
return
player = Player(index_from_userid(userid))
menu.append(Text('Market\nSection: Potions'))
menu.append(Text('-' * 30))
menu.append(SimpleOption(1, 'Full Health[12000$]', 'full_health', player.cash >= 12000 and zr.isAlive(userid), player.cash >= 12000 and zr.isAlive(userid)))
menu.append(SimpleOption(2, 'Half Health[6000$]', 'half_health', player.cash >= 6000 and zr.isAlive(userid), player.cash >= 6000 and zr.isAlive(userid)))
menu.append(SimpleOption(3, '25% Of Health[3000$]', '25_health', player.cash >= 3000 and zr.isAlive(userid), player.cash >= 3000 and zr.isAlive(userid)))
menu.append(SimpleOption(4, 'Full Speed[12000$]', 'full_speed', player.cash >= 12000 and zr.isAlive(userid), player.cash >= 12000 and zr.isAlive(userid)))
menu.append(SimpleOption(5, 'Half Speed[6000$]', 'half_speed', player.cash >= 6000 and zr.isAlive(userid), player.cash >= 6000 and zr.isAlive(userid)))
menu.append(SimpleOption(6, '25% Of Speed[3000$]', '25_speed', player.cash >= 3000 and zr.isAlive(userid), player.cash >= 3000 and zr.isAlive(userid)))
menu.append(SimpleOption(7, 'Full Bullets[14000$]', 'full_bullets', player.cash >= 14000 and zr.isAlive(userid), player.cash >= 14000 and zr.isAlive(userid)))
menu.append(SimpleOption(8, 'Infinity Bullets[16000$]', 'infi_bullets', player.cash >= 16000 and zr.isAlive(userid), player.cash >= 16000 and zr.isAlive(userid)))
menu.append(SimpleOption(9, 'Respawn[6000$]', 'respawn', player.cash >= 6000 and not zr.isAlive(userid) and zr._humans > 0, player.cash >= 6000 and not zr.isAlive(userid) and zr._humans > 0))
menu.append(Text('-' * 30))
menu.append(SimpleOption(0, 'Close', None))
menu.select_callback = potion_menu_callback
menu.send(index_from_userid(userid))

def potion_menu_callback(_menu, _index, _option):
choice = _option.value
if choice:
userid = userid_from_index(_index)
player = Player(index_from_userid(userid))
if choice == 'full_health':
if zr.isAlive(userid):
player.health += rpg.players[userid]['maxHealth']
player.cash -= 12000
message.Potion.send(_index, type="full health", cost=12000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="health potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'half_health':
if zr.isAlive(userid):
bonus = int(rpg.players[userid]['maxHealth'] / 2)
player.health += bonus
player.cash -= 6000
message.Potion.send(_index, type="half health", cost=6000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="health potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == '25_health':
if zr.isAlive(userid):
bonus = int(rpg.players[userid]['maxHealth'] / 4)
player.health += bonus
player.cash -= 3000
message.Potion.send(_index, type="25% health", cost=3000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="health potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'full_bullets':
if zr.isAlive(userid):
if player.get_active_weapon().classname.replace('weapon_', '', 1) in zr.secondaries() + zr.rifles():
player.cash -= 14000
weapon = player.get_active_weapon()
weapon.clip = weapon_manager[weapon.classname].clip
weapon.ammo = weapon_manager[weapon.classname].maxammo
message.Potion.send(_index, type="full bullets", red=zr.red,cost=14000, green=zr.green,light_green=zr.light_green)
else:
message.Invalid.send(_index, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="fullbullets", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == '25_speed':
if zr.isAlive(userid):
bonus = rpg.players[userid]['maxSpeed'] / 4
player.speed += bonus
player.cash -= 3000
message.Potion.send(_index, type="25% speed", cost=3000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="speed potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'half_speed':
if zr.isAlive(userid):
bonus = rpg.players[userid]['maxSpeed'] / 2
player.speed += bonus
player.cash -= 6000
message.Potion.send(_index, type="half speed", cost=6000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="speed potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'full_speed':
if zr.isAlive(userid):
player.speed += rpg.players[userid]['maxSpeed']
player.cash -= 12000
message.Potion.send(_index, type="full speed", cost=12000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="speed potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'infi_bullets':
if zr.isAlive(userid):
if player.get_active_weapon().classname.replace('weapon_', '', 1) in zr.secondaries() + zr.rifles():
player.get_active_weapon().clip = weapon_manager[player.get_active_weapon().classname].clip
player.cash -= 16000
user = zr.ZombiePlayer.from_userid(userid)
user.infinity_bullets = True
message.Potion.send(_index, type="infinity bullets", cost=16000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Invalid.send(_index, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Alive.send(_index, type="infinity bullets potion", red=zr.red,green=zr.green,light_green=zr.light_green)
elif choice == 'respawn':
if not zr.isAlive(userid) and zr._humans > 0:
player.delay(0.1, zr.respawn, (userid,))
player.cash -= 6000
message.Potion.send(_index, type="respawn", cost=6000, red=zr.red,green=zr.green,light_green=zr.light_green)
else:
message.Dead.send(_index, green=zr.green,light_green=zr.light_green)

@Event('round_end')
def round_end(args):
for i in zr.getUseridList():
user = zr.ZombiePlayer.from_userid(i)
user.infinity_bullets = False

@Event('player_death')
def player_death(args):
victim = zr.ZombiePlayer.from_userid(args['userid'])
victim.infinity_bullets = False

@Event('weapon_fire')
def weapon_fire(args):
userid = args.get_int('userid')
user = zr.ZombiePlayer.from_userid(userid)
if user.infinity_bullets:
player = Player(index_from_userid(userid))
weapon = player.get_active_weapon()
if player.get_active_weapon().classname.replace('weapon_', '', 1) in zr.secondaries() + zr.rifles():
weapon.clip += 1
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Cs:s] Zombie Riot potion

Postby L'In20Cible » Thu Aug 19, 2021 5:55 pm

That means that by that point:

Syntax: Select all

player.cash -= 16000
The player has less than 16k resulting into a negative value being assigned. You really just have to ensure the player have enough cash before performing the purchase. For example:

Syntax: Select all

cash = player.cash
if cash >= 16000:
player.cash = cash - 16000
...
else:
# not enough cash


Otherwise, the amount of cash the players have can change from external sources by the time they receive the menu and actually perform a selection.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Zombie Riot potion

Postby cssbestrpg » Fri Aug 20, 2021 3:42 pm

L'In20Cible wrote:That means that by that point:

Syntax: Select all

player.cash -= 16000
The player has less than 16k resulting into a negative value being assigned. You really just have to ensure the player have enough cash before performing the purchase. For example:

Syntax: Select all

cash = player.cash
if cash >= 16000:
player.cash = cash - 16000
...
else:
# not enough cash


Otherwise, the amount of cash the players have can change from external sources by the time they receive the menu and actually perform a selection.


Like in this commit? https://github.com/srpg/Zombie-Riot/commit/aeb2d729e332060a83aa6de6239ac3033b7b6b98
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: [Cs:s] Zombie Riot potion

Postby Ayuto » Fri Aug 20, 2021 3:59 pm

Exactly! Btw. your "full health" options adds the maximum health to the current health. I think you meant player.health = player.max_health.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Zombie Riot potion

Postby cssbestrpg » Fri Aug 20, 2021 4:15 pm

I did make it purposely to add maximum health to current health, so player could have more health than maximum and in clan_tag module i have made a code that does increase player maximum health when kills a zombie till reach a limit for it. So i figured it would be easiest way check clan_tag users in potions option without adding any extra code for clan_tag.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 28 guests