# ../addons/eventscripts/gungame51/scripts/custom/gg_assist/gg_assist.py
# ============================================================================
# >> IMPORTS
# ============================================================================
# Python Imports
from random import randint
# Eventscripts Imports
import es
import cmdlib
import popuplib
from playerlib import getPlayer
from playerlib import getPlayerList
# GunGame Imports
from gungame51.core.addons.shortcuts import AddonInfo
from gungame51.core.players.shortcuts import Player
from gungame51.core.players.shortcuts import setAttribute
from gungame51.core.players.shortcuts import deleteAttribute
from gungame51.core.weapons.shortcuts import get_total_levels
# ============================================================================
# >> ADDON REGISTRATION/INFORMATION
# ============================================================================
info = AddonInfo()
info.name = 'gg_assist'
info.title = 'GG Assist'
info.author = 'XE_ManUp'
info.version = '1.5'
info.translations = ['gg_assist']
# ============================================================================
# >> GLOBAL VARIABLES
# ============================================================================
# Server Vars
gg_assist_advertise = es.ServerVar("gg_assist_advertise")
gg_assist_award_alive_only = es.ServerVar("gg_assist_award_alive_only")
gg_assist_percent = es.ServerVar("gg_assist_percent")
gg_assist_redeem_win = es.ServerVar("gg_assist_redeem_win")
gg_assist_skip_knife = es.ServerVar("gg_assist_skip_knife")
gg_assist_skip_nade = es.ServerVar("gg_assist_skip_nade")
gg_assist_sound = es.ServerVar("gg_assist_sound")
# ============================================================================
# >> CLASSES
# ============================================================================
class AdvertCounter:
count = 0
def __init__(self):
self.__class__.count += 1
# ============================================================================
# >> LOAD & UNLOAD
# ============================================================================
def load():
es.dbgmsg(0, "Loaded: gg_assist")
# Set up attributes for all existing players
setAttribute("#all", "damageTaken", {})
setAttribute("#all", "assistPoints", 0)
setAttribute("#all", "assistNotification", 0)
# Register the say command (!assist)
cmdlib.registerSayCommand('!assist', display_menu,
'Opens the GG Assist menu.')
# Register the say command (!redeem)
cmdlib.registerSayCommand('!redeem', redeem_points_cmd,
'Redeems assist points.')
# Make the assist sound downloadable
add_sound()
def unload():
es.dbgmsg(0, "Unloaded: gg_assist")
# Delete assist attributes from all existing players
deleteAttribute("#all", "damageTaken")
deleteAttribute("#all", "assistPoints")
deleteAttribute("#all", "assistNotification")
# Unregister the say command (!assist)
cmdlib.unregisterSayCommand('!assist')
# Unregister the say command (!assist)
cmdlib.unregisterSayCommand('!redeem')
# ============================================================================
# >> GAME EVENTS
# ============================================================================
def es_map_start(event_var):
# Reset attributes for all existing players
setAttribute("#all", "damageTaken", {})
setAttribute("#all", "assistPoints", 0)
setAttribute("#all", "assistNotification", 0)
# Make the assist sound downloadable
add_sound()
# Reset the Advert Counter
AdvertCounter.count = 0
def gg_start(event_var):
# Reset attributes for all existing players
setAttribute("#all", "damageTaken", {})
setAttribute("#all", "assistPoints", 0)
setAttribute("#all", "assistNotification", 0)
def player_activate(event_var):
userid = int(event_var["userid"])
# Set up attributes
setAttribute(userid, "damageTaken", {})
setAttribute(userid, "assistPoints", 0)
setAttribute(userid, "assistNotification", 0)
def round_start(event_var):
# Reset all damage taken attributes - this is a new round
setAttribute("#all", "damageTaken", {})
def player_spawn(event_var):
# Make sure that the player that is spawning is on a team
if not int(event_var["es_userteam"]) > 1:
return
# We will only advertise every 30 spawns (returns if there is a remainder)
if AdvertCounter().count % 30:
return
# Select a random integer 1 through 4
randomNumber = randint(1, 4)
randomNumber = 4
# Message the advertisement
if randomNumber < 4:
for userid in getPlayerList("#human"):
Player(userid).msg("gg_assist_advert_%s" %randomNumber,
prefix=True)
else:
for userid in getPlayerList("#human"):
Player(userid).msg("gg_assist_advert_%s" %randomNumber,
{'percent':int(gg_assist_percent)}, prefix=True)
def player_hurt(event_var):
userid = int(event_var["userid"])
attacker = int(event_var["attacker"])
# Return if hurt by themselves
if userid == attacker:
return
# Return if hurt by world
if not attacker:
return
# Return if hurt by a teammate
if event_var["es_userteam"] == event_var["es_attackerteam"]:
return
# Create or update damage done to the player
if not Player(userid).damageTaken.has_key(attacker):
Player(userid).damageTaken[attacker] = int(event_var["dmg_health"])
else:
Player(userid).damageTaken[attacker] += int(event_var["dmg_health"])
def player_death(event_var):
userid = int(event_var["userid"])
attacker = int(event_var["attacker"])
# Loop through all players that did damage to this player
for playerid in Player(userid).damageTaken:
# We do not award the player that killed the player
if playerid == attacker:
continue
# See if we are allowed to award the player
if int(gg_assist_award_alive_only) and getPlayer(playerid).isdead:
# Continue if the player is dead while gg_assist_award_alive_only
# is active
continue
# Award points to the player
award_points(playerid, userid, Player(userid).damageTaken[playerid])
# Clear the player's damage taken dictionary
Player(userid).damageTaken.clear()
# ============================================================================
# >> CUSTOM/HELPER FUNCTIONS
# ============================================================================
def award_points(userid, victim, points):
# Make sure this player is still on the server; if not, return
if not es.exists('userid', userid):
return
# Calculate the points based on the percent set up
points = points * (int(gg_assist_percent) / 100.0)
# Add points
Player(userid).assistPoints += points
if int(gg_assist_advertise):
Player(userid).saytext2(Player(victim).index, 'gg_assist_earn_points',
{'earned_points': points,
'victim': es.getplayername(victim),
'total_points': Player(userid).assistPoints
},
prefix=True,
)
# Do not continue with the player does not have 100 assist points
if Player(userid).assistPoints < 100:
return
# Calculate the levels that can be gained if all assist points are redeemed
assistLevelsAvailable = int(Player(userid).assistPoints / 100)
# Return if the player has already been notified by sound and message
if assistLevelsAvailable == Player(userid).assistNotification:
return
# Send a message
if assistLevelsAvailable == 1:
Player(userid).msg("gg_assist_notify_singular",
{'points': Player(userid).assistPoints,
'levels': assistLevelsAvailable
},
prefix=True
)
else:
Player(userid).msg("gg_assist_notify_plural",
{'points': Player(userid).assistPoints,
'levels': assistLevelsAvailable
},
prefix=True
)
# Make sure there is a sound to play
if str(gg_assist_sound) and str(gg_assist_sound) != "0":
# es.playsound(userid, str(gg_assist_sound), 1.0)
pass
# Increment the assist notification count
Player(userid).assistNotification += 1
def redeem_points(userid, choice=None, popup=None):
"""Only redeems 100 points at a time. The player must type something each
time they want to redeem a level for 100 points.
"""
# Make sure the player has enough points to redeem
if Player(userid).assistPoints < 100:
# Error message to the player
Player(userid).msg("gg_assist_not_enough_points", prefix=True)
# Player error sound to the player
# es.playsound(userid, "buttons/weapon_cant_buy.wav", 1.0)
return
# Knife check
if Player(userid).weapon == "knife" and not int(gg_assist_skip_knife):
# Error message to the player
Player(userid).msg("gg_assist_deny_knife", prefix=True)
# Player error sound to the player
# es.playsound(userid, "buttons/weapon_cant_buy.wav", 1.0)
return
# HEGrenade check
if Player(userid).weapon == "hegrenade" and not int(gg_assist_skip_nade):
# Error message to the player
Player(userid).msg("gg_assist_deny_nade", prefix=True)
# Player error sound to the player
# es.playsound(userid, "buttons/weapon_cant_buy.wav", 1.0)
return
# Do we allow the player to redeem assist points to win?
if Player(userid).level == get_total_levels():
if not int(gg_assist_redeem_win):
# Error message to the player
Player(userid).msg("gg_assist_deny_win", prefix=True)
# Player error sound to the player
# es.playsound(userid, "buttons/weapon_cant_buy.wav", 1.0)
return
# Level the player up
if Player(userid).levelup(1, 0, "gg_assist"):
# Remove the points
Player(userid).assistPoints -= 100
# Decrement the assist notification count
Player(userid).assistNotification -= 1
# Message the remaining balance
Player(userid).msg("gg_assist_redeemed",
{'points': Player(userid).assistPoints},
prefix=True
)
# Play the levelup sound
Player(userid).playsound("levelup")
def display_menu(userid, args):
# Attempt to delete the popup
try:
popuplib.delete("gg_assist_popup")
except:
pass
# Create the popuplib menu for !assist
assistPopup = popuplib.create("gg_assist_popup")
# Add lines
assistPopup.addline(info.title)
assistPopup.addline("="*10)
assistPopup.addline(Player(userid).langstring("gg_assist_popup_points",
{'points': Player(userid).assistPoints}))
assistPopup.addline("="*10)
assistPopup.addline(" ")
assistPopup.addline(Player(userid).langstring("gg_assist_popup_redeem"))
assistPopup.addline(" ")
assistPopup.addline("-"*10)
assistPopup.addline(Player(userid).langstring("gg_assist_popup_exit"))
# Set the redeem points function to option 1
assistPopup.select(1, redeem_points)
# Send the menu
assistPopup.send(userid)
def redeem_points_cmd(userid, args):
# Redirect to redeem_points()
redeem_points(userid)
def add_sound():
# Make sure that a sound is listed
if str(gg_assist_sound) and str(gg_assist_sound) != "0":
# Make the sound downloadable
es.stringtable("downloadable", "sound/%s" %str(gg_assist_sound))