Jump to content

Recommended Posts

if my user log's into a game it will check the ID and place them into 1 of 3 different groups. at the moment im using a very basic code to determine which group they are placed. like so ....

 


if (($id == '1') || ($id == '4') || ($id == '7') || ($id == '10'))
{  mysql_query("UPDATE $tab[user] SET group=1 WHERE id='$id';");}

if (($id == '2') || ($id == '5') || ($id == '8') || ($id == '11'))
{  mysql_query("UPDATE $tab[user] SET group=2 WHERE id='$id';");}

if (($id == '3') || ($id == '6') || ($id == '9') || ($id == '12'))
{  mysql_query("UPDATE $tab[user] SET group=2 WHERE id='$id';");}

 

this way it places them in groups in order so all groups stay the same size almost.

 

My issue is im now having hundreds join so is there an easier way to place them into 3 groups doing the same as the code above or do i just need to keep adding , thanks

Link to comment
https://forums.phpfreaks.com/topic/197010-how-can-i-split-into-3s-or-4s/
Share on other sites

You basically want to create a game-balancer?

 

SELECT g.id, count(p.*) AS player_count
FROM groups g
LEFT JOIN players ON g.id = p.group_id
ORDER BY player_count
LIMIT 1

 

This should give you the ID of the group that has the lowest number of players

hmmmm let me explain step by step ......

 

CURRENTLY THE CODE DOES ...... a user chooses to join a game that is running , its a team game with 3 teams.

 

when he join he is auto assigned an ID# for that game...... he click join it call him ID1 and he i placed on the 1team....... Next players joins the same game so is ID2 .... placed into 2team .... ID3 into 3team .... then it starts from 1team again. ....... ID4 goes into 1team ... etc etc

 

 

It sounds like you're wanting to keep a value into a variable... forever.  Incrementing to 3 and repeat.

 

You oughta make a file... called something like currentteam.inc or just currentteam.  It doesn't really matter about the file extension just so long as it's "text application" works on.. notepad or wordpad, etc.

 

Then you can pull the file everytime the page loads and run your scripts on it to check and see what the current team is.  incrementing it and setting it to 1 on == 3

 

OR..

 

since I see you're already using a database.. you can just compare the new team to the last added team member.

SELECT teamid FROM onlineplayers ORDER by id DESC LIMIT 1

 

As mikesta707 already showed, you can achieve this with the modulus operator. here's a bit more elaborate example:

 

//Define max number of groups
$groups = 3;

//Determine user's group based upon id
$user_group = ($groups%$id!=0) ? $groups%$id : $groups;

mysql_query("UPDATE $tab['user'] SET group=$user_group WHERE id='$id';");

My code would do the same:

 

The second number is the number of players in each group

 

Group 1, 0

Group 2, 0

Group 3, 0

 

Group 1, 1

Group 2, 0

Group 3, 0

 

Group 1, 1

Group 2, 1

Group 3, 0

 

Group 1, 1

Group 2, 1

Group 3, 1

 

Group 1, 2

Group 2, 1

Group 3, 1

 

...

My code would do the same:

 

Not exactly. Your code would set the group according to number of players currently assigned to each group. That may actually be preferred. But, the original request was to set the group according to the value of the user ID. It's a subtle difference, but one which the OP did not address. If a player "drops", if that is the option, the outcome would be different. But, if the goal is to simply set the group according to the order in which users are created, then using modulus would be much more efficient. Although, a db solution would be more exacting.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.