Scoring System Approach

So my final decision was to make the networking system of my game non authoritative, but have the server check and correct if a player is acting weird (is cheating)…still don’t know how will I go for the checking, but I digress. The reason I’m writing the thread is that I need some ideas for a damaging and fragging system.

Currently my players can shoot projectiles, they instantiate them with Network.Instantiate and the one, who has instantiated a projectile sends an RPC to it with his name. When the projectile hits an enemy it sends an RPC to it with the name of the player, that has earlier been asigned via RPC to the projectile and the damage the projectile does. Then it sends an RPC to the server’s version of the projectile, telling it to Network.Destroy the projectile. That way the enemies receive damage and also the name of the last player to damage them, so it’s pretty easy to tell who has killed them. Problem is, there could be players with identical names…if there are players with identical names I don’t care if the players will know who is the one, that has done the damage or the damage has been done to (at least not at this time). What I’m concerned about is the fragging system. First how would I indicate which player is the one, that has killed the other? I definitely can’t use the name, do I use the viewID or what? And where would it be best to keep the information about all the frags? Do I keep it on the server or individually for each player on each player’s machine?

Hey, I’m still stuck here! I got some ideas, but they are not complete. I think the score/frags should be calculated on the server’s machine and sent to all the clients. I guess I’ll have to keep an array of all the players on the server… I don’t even know where to start. I want to do something similar to this:
http://www.pantheongaming.co.za/files/borg_ragefinalend.jpg

I was searching in google for the workflow for creating such thing, but I couldn’t find useful information, guess I don’t know what to search for.

OK, I know that not everyone is dealing with networking so let’s say that this is not an online frag list and the game is running offline and the other players are just AI. How can I keep a list of everyone in the game and their frags ordered in descending order. This is the first thing I need to know. I find this really tricky, because players enter and exit the game during the game itself and the array of player would have to be modified.

Has anyone done such thing? Is there an already written code for this somewhere, I would like to take a look at it. Thanks and sorry for that double bump, but I still haven’t figured out how to do this and it’s a really important part of my game.

You can do this using the List generic container and a custom class that implements IComparable. The comparator will be used when sorting giving you easy access to a correctly compiled scoreboard list.

Here’s an example implementation. Note it doesn’t deal with your unique identifier problem, but you can add an identifier easily once you figure out what you want to use for it.

public class PlayerScore : System.IComparable
{
    public PlayerScore( string name )
    {
        name_ = name;
        frags_ = 0;
        deaths_ = 0;
    }

    public int CompareTo( object other )
    {
        PlayerScore otherScore = other as PlayerScore;
        if ( otherScore == null )
        {
            throw new System.ArgumentException( "Object is not a PlayerScore" );
        }
        // Sort by frags first.
        if ( otherScore.Frags > otherScore.Frags )
        {
            return 1;
        }
        else if ( otherScore.Frags < otherScore.Frags )
        {
            return -1;
        }
        // If frags are equal, sort by deaths.
        if ( otherScore.Deaths < otherScore.Deaths )
        {
            return 1;
        }
        else if ( otherScore.Deaths > otherScore.Deaths )
        {
            return -1;
        }
        // If deaths are equal, sort by name.
        return this.Name.CompareTo( otherScore.Name );
    }

    public string Name
    {
        get { return name_; }
    }

    public int Frags
    {
        get { return frags_; }
        set { frags_ = value; }
    }

    public int Deaths
    {
        get { return deaths_; }
        set { deaths_ = value; }
    }

    string name_;
    int frags_;
    int deaths_;
}

Now to use it in a List is simple:

List<PlayerScore> myList = new List<PlayerScore>();
PlayerScore player1Score = new PlayerScore( "Player 1" );
player1Score.Frags = 5;
player1Score.Deaths = 5;
PlayerScore player2Score = new PlayerScore( "Player 2" );
player1Score.Frags = 5;
player1Score.Deaths = 2;
myList.Add( player1Score );
myList.Add( player2Score );
myList.Sort();

Now the list is sorted such that the first entry in the list will have the highest frags or tied high frags with lowest deaths, or tied for both and the highest sorting name.

Thanks for your reply, I’ve been waiting for one for a long time. To be honest I got a little confused. If I have understood correct, the first peace of code compares two players and their deaths and kills and returns an integer which can be later used to reorder players, right? Even if it’s not that, I got a very nice idea for ordering using return -1, 1 and 0

This code works with only two players, what if there are 8 players in the game? I also couldn’t understand this line:

and why it gets compared with itself:

I know it’s something pretty basic, but I’m still a lerner

I’m working with JavaScript and this is a really, really stupid question, but how is that implemented in Javascript (oh god :smile: ) ?

That’s what the CompareTo function does, yes. The -1, 1, 0 meanings are spelled out here (also linked above). By implementing CompareTo, you can utilize built-in sorting algorithms so you don’t need to worry about any of that (see the example at the end of my first post)

Whoops, that’s my bad! It should be:

if ( otherScore.Frags > Frags )

We are checking if the otherScore has more Frags than our current object’s score. Same change needs to be made to the deaths comparison.

I only know how to do the most basic things in JavaScript, so I’m not sure of all the syntax changes. If you search for “Javascript generics” on the forums and Unity documentation you should be able to figure out the differences.

Alright, I’m still a little bit lost. I’m having troubble building the class:

var name : String;
var frags : int = 0;

static class FragList extends System.IComparable {
	
	static function PlayerScore(playerName : String) {
	name = playerName;
	frags = 0;
	}
	
	static function CompareTo(obj : object) : int{
	
	}
	
}

This thing outputs this error in the editor:

And if I replace “extends” with “implements” it will tell me that the name “object” does not donate a valid type. What is that object type anyway?
I read about IComparable and I understand it requires that object type, but Unity says that it’s not a valid type.

OK, I managed the JavaScript syntax. Here’s how this looks like in javascript:

import System;
import System.Collections;


public class SortFrags implements System.IComparable {
	var frags : int = 0;
	private var playerName : String = "";
	
	function PlayerScore(pn : String){
	playerName = pn;
	}
		
	function CompareTo(obj: Object) : int { 
		var other : SortFrags = obj;
		if(other.frags < frags) return 1;
		if(other.frags > frags) return -1;
		if(other.frags == frags) return 0;
}


}

However I can’t seem to understand the idea of all this. I asked my C++ teacher, she shed some light on that, but still it’s not really clear.

I couldn’t understand what does this do and why it has to be there:

 public string Name
    {
        get { return name_; }
    }

    public int Frags
    {
        get { return frags_; }
        set { frags_ = value; }
    }