[HL2:DM] Save Scores

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
User avatar
Painkiller
Senior Member
Posts: 330
Joined: Sun Mar 01, 2015 8:09 am
Contact:

[HL2:DM] Save Scores

Postby Painkiller » Mon Apr 11, 2016 1:29 pm

Could someone please make for SourcePython?

https://forums.alliedmods.net/showthread.php?p=660327
Image
User avatar
satoon101
Project Leader
Posts: 2463
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Apr 11, 2016 2:48 pm

Try this (untested):

Syntax: Select all

from events import Event
from listeners import OnClientDisconnect
from listeners import OnLevelShutdown
from players.entity import Player
from players.helpers import index_from_userid

scores = dict()

@OnLevelShutdown
def clear_scores():
scores.clear()

@OnClientDisconnect
def save_player_score(index):
player = Player(index)
scores[player.uniqueid] = {
'kills': player.kills,
'deaths': player.deaths,
}

@Event('player_activate')
def reset_player_score(game_event):
player = Player(index_from_userid(game_event['userid']))
if player.uniqueid not in scores:
return

player_scores = scores.pop(player.uniqueid)

player.kills = player_scores['kills']
player.deaths = player_scores['deaths']
Image
User avatar
Painkiller
Senior Member
Posts: 330
Joined: Sun Mar 01, 2015 8:09 am
Contact:

Postby Painkiller » Mon Apr 11, 2016 3:37 pm

Delete
Image
User avatar
satoon101
Project Leader
Posts: 2463
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Apr 11, 2016 3:46 pm

It shouldn't matter if there are bots. Are you trying to store the scores for bots, too?

I just tested, and it works fine, at least for human players.

It should also work for bots, as 'uniqueid' uses the name for bots. I guess it might not work if bots don't call OnClientDisconnect or event player_activate.
Image
User avatar
Painkiller
Senior Member
Posts: 330
Joined: Sun Mar 01, 2015 8:09 am
Contact:

Postby Painkiller » Mon Apr 11, 2016 3:47 pm

Aie it lacked a line apologize.

It works flawlessly
Image
User avatar
Painkiller
Senior Member
Posts: 330
Joined: Sun Mar 01, 2015 8:09 am
Contact:

Re: [HL2:DM] Save Scores

Postby Painkiller » Mon Feb 20, 2017 8:51 pm

Code: Select all

2017-02-20 19:31:45 - sp   -   EXCEPTION   
[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/plugins/savescore/savescore.py", line 15, in save_player_score
    player = Player(index)
  File "../addons/source-python/packages/source-python/players/_base.py", line 86, in __init__
    super().__init__(index)
  File "../addons/source-python/packages/source-python/entities/_base.py", line 94, in __init__
    super().__init__(index)

ValueError: Conversion from "Index" (1) to "BaseEntity" failed.
User avatar
iPlayer
Developer
Posts: 588
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [HL2:DM] Save Scores

Postby iPlayer » Mon Oct 02, 2017 7:04 pm

The error is not critical. It doesn't affect anything. It occurs only when a player disconnects before activating. But such player doesn't have any score to save anyways. You can safely ignore it.

But here's the fix:

Syntax: Select all

from events import Event
from listeners import OnClientDisconnect, OnLevelShutdown
from players.entity import Player


scores = dict()


@OnLevelShutdown
def clear_scores():
scores.clear()


@OnClientDisconnect
def save_player_score(index):
try:
player = Player(index)
except ValueError:
return

scores[player.uniqueid] = {
'kills': player.kills,
'deaths': player.deaths,
}


@Event('player_activate')
def reset_player_score(game_event):
player = Player.from_userid(game_event['userid'])
if player.uniqueid not in scores:
return

player_scores = scores.pop(player.uniqueid)

player.kills = player_scores['kills']
player.deaths = player_scores['deaths']
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Painkiller
Senior Member
Posts: 330
Joined: Sun Mar 01, 2015 8:09 am
Contact:

Re: [HL2:DM] Save Scores

Postby Painkiller » Tue Oct 03, 2017 2:21 pm

Thanks it worksfine for me.

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 1 guest