abemaca Posted March 30, 2010 Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted March 30, 2010 Share Posted March 30, 2010 you could use modulus $mod = $id % 3; if ($mod == 1) { ... } else if ($mod == 2) { ... } else if ($mod == 0){ ... } Quote Link to comment Share on other sites More sharing options...
ignace Posted March 30, 2010 Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
abemaca Posted March 30, 2010 Author Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
Zane Posted March 30, 2010 Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 30, 2010 Share Posted March 30, 2010 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';"); Quote Link to comment Share on other sites More sharing options...
abemaca Posted March 31, 2010 Author Share Posted March 31, 2010 thanks alot my fellow freaks ill try them out now. ill reply my results asap , thanks again Quote Link to comment Share on other sites More sharing options...
ignace Posted March 31, 2010 Share Posted March 31, 2010 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 ... Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 31, 2010 Share Posted March 31, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.