Jump to content

how can i split into 3's or 4's ???


abemaca

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
Share on other sites

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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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';");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.