AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Dividing players (https://forums.alliedmods.net/showthread.php?t=155952)

reinert 04-28-2011 15:38

Dividing players
 
Hey, would someone help me, to do a plugin that divides users by their numbers for example, Like I've 10 players,

PHP Code:

PLAYER_NAME                  NUMBER
reinert1                         150
reinert2                         600
reinert3                         50
reinert4                         200
reinert5                         300
reinert6                         450
reinert7                         500
reinert8                         300
reinert9                         100
reinert10                       200 

I Would like to make that they will be divided to 2 parts ( 5 / 5 ), and the first part total number would be equal or similar to the second parts number.

Total NUMBER is 2850, so I'll divide them by 2, and then Part1 should be around 1425 and part2 aswell..

So part1 will be: reinert1 + reinert2 + reinert4 + reinert8 + reinert10 (total number 1450)
and part2 will be: reinert3 + reinert5 + reinert6 + reinert7 + reinert9 (1400)

Help help help !

SonicSonedit 04-28-2011 15:46

Re: Dividing players
 
For best result you should sort player by points, descending.
Some like Players[33], where player[1] will be userid of player who has most points.
And then use something like this:
PHP Code:

public separate_players()
{
    static 
players1[32], players2[32], playercount1playercount2pointcount1pointcount2
    
    playercount1
=0
    playercount2
=0
    pointcount1
=0
    pointcount2
=0

    
for (i=1;i<=g_maxplayers;i++)
    {
        if (!
is_user_connected(Players[i]))
            continue
        
        if (
pointcount1==pointcount2||pointcount2>pointcount1)
        {
            
players1[playercount1]=Players[i]
            
playercount1++
            
pointcount1+=points[Players[i]]
        }
        
        
        if (
pointcount1>pointcount2)
        {
            
players2[playercount2]=Players[i]
            
playercount2++
            
pointcount2+=points[Players[i]]
        }
    }



reinert 04-28-2011 15:51

Re: Dividing players
 
Could someone explain me it more detailed ? please. I still don't get it ;(

ConnorMcLeod 04-28-2011 16:17

Re: Dividing players
 
1. Add all values
2. Divide by number of values
Done :)

May be this example would help you to understand :

PHP Code:

SharePlayersMoney()
{
    new 
iTotalMoney
    
new iPlayers[32], iNumid
    get_players
(iPlayersiNum"h")
    for(new 
ii<iNumi++)
    {
        
iTotalMoney += cs_get_user_moneyiPlayers[i] )
    }
    new 
iSharedMoney iTotalMoney iNum
    
for(new ii<iNumi++)
    {
        
cs_set_user_moneyiPlayers[i] , iSharedMoney )
    }



reinert 04-28-2011 16:34

Re: Dividing players
 
Well it's not what I need ;( Lets say I've 10 players they have the different amount of points, I would like to divide them to 2 teams (CT and T), and the CT and T team would be with equal or similar amount of points. I don't want to change their points.

I thought of sorting them in descending order, and then select every other (1, 3, 5, 7, 9 || 2, 4, 6, 8, 10). But it's not the best way of sorting players to 2 groups. Because I will get a teams like: (600+450+300+200+100 = 1650) and the other team (500+300+200+150+50 = 1200) well it's not the rightest way :(

ConnorMcLeod 04-28-2011 16:38

Re: Dividing players
 
So you you to balance teams levels ?

reinert 04-28-2011 16:40

Re: Dividing players
 
Yes.

Sylwester 04-28-2011 18:12

Re: Dividing players
 
There was similiar request: http://forums.alliedmods.net/showthr...41#post1250441

The idea goes like this:
1. Sort all numbers in descending order.
2. Loop through all numbers. In each iteration you add player to group that has lower total amount of points and is not full.

SonicSonedit 04-29-2011 07:05

Re: Dividing players
 
Quote:

The idea goes like this:
1. Sort all numbers in descending order.
2. Loop through all numbers. In each iteration you add player to group that has lower total amount of points and is not full.
Quote:

Originally Posted by SonicSonedit (Post 1459691)
For best result you should sort player by points, descending.
Some like Players[32], where player[0] will be userid of player who has most points.
And then use something like this:
PHP Code:

public separate_players()
{
    static 
players1[32], players2[32], playercount1playercount2pointcount1pointcount2
    
    playercount1
=0
    playercount2
=0
    pointcount1
=0
    pointcount2
=0

    
for (i=0;i<32;i++)
    {
        if (!
is_user_connected(Players[i]))
            continue
        
        if (
pointcount1==pointcount2||pointcount2>pointcount1)
        {
            
players1[playercount1]=Players[i]
            
playercount1++
            
pointcount1+=points[Players[i]]
        }
        
        
        if (
pointcount1>pointcount2)
        {
            
players2[playercount2]=Players[i]
            
playercount2++
            
pointcount2+=points[Players[i]]
        }
    }




Sylwester 04-29-2011 09:04

Re: Dividing players
 
Oh, it looks like I didn't read your post before posting and also your code will fail if in any iteration pointcount 1 and 2 become equal.


All times are GMT -4. The time now is 04:24.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.