Now guys I am doing a TeamsWar and I don't know how to do the automatic team choice for every player that sign up !
so I made two methods may you please tell me if they will work properly.
and tell me what method to choose from, BTW I am not a good coder I am still a beginner so just don't come here to make fun of me. if you have a better way to do it may you please explain how
-You're creating 'new' clients... you cannot do that...
-You're over complicating things
There's no reason to have bools for isTeam in the player.
#1: Create a 'team' enum.
#2: Create an 'event' class. Assign starting/ending events to sort players into teams
#3: Create a 'team' class. Have it store team score and members
When starting an event you take a collection of all signed up players (players on waiting map or add them in via npc/command/etc) and sort them into teams. You can sort them by level/battlepower to try to balance teams if needed or simply randomize the collection.
Something like
var teamCounter = 0;//Starting team index
var teamCounts = 3;//4 possible teams
foreach(var player in awaitingPlayers)
{
teamCounter++;
if(teamCounter > teamCounts)
teamCounter = 0;
Teams[teamCounter].AddMember(player);
}
Again, there's lots of ways to simplify and make your events system easier to build on.
This way you can check in your combat handler if you are part of an event with something like
if(character.Event != null)
You can modify their team's score and perform any needed actions (freezing, changing teams, etc)
character.Team.ScorePoint(target)
Just boils down to coding the base event systems in a way that you can have them support any event you can imagine. I know in albetros I wrote a BASIC system (it's messy but it could give you a basic idea of how to code an event system)
well that's true that I am always complicating thing up.
all of that is just good.
but my question was on how to sort the teams, how can I do that
and on your answer here you just explained every thing beside
sorting the teams, I know you said "You can sort them by level/battlepower to try to balance teams if needed or simply randomize the collection. "
but what I want is to random every one into a team of the 4 but all the teams should have equal players or a nearly equal players.
thanks for your answer and time
but what I want is to random every one into a team of the 4 but all the teams should have equal players or a nearly equal players.
yes, that's the example I gave you. You need to get a collection of ALL players signed up. Query the map of the event or a 'waiting room' for the event or build it as people sign up. Either way you have a collection of all players.
Sort that collection based on criteria (level for example)
var awaitingPlayers = registeredPlayers.OrderBy(I=>I.Level);
Then loop through all elements and cycle them into teams evendly (first element = team 1, second element = team 2, etc)
var teamCounter = 0;//Starting team index
var teamCounts = 3;//4 possible teams
foreach(var player in awaitingPlayers)
{
teamCounter++;
if(teamCounter > teamCounts)
teamCounter = 0;
Teams[teamCounter].AddMember(player);
}
Enums is just making text out of a number. You need a collection (list, array, dictionary, etcetc) containing the teams themselves (a separate class)
It's not handled all that well but I really do suggest looking through the albetros source as it will at least demonstrate an idea of how you could write an event system.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conquer_Online_Server.Client;
using Conquer_Online_Server.Network.GamePackets;
namespace Conquer_Online_Server.Game.Features.Tournaments
{
class TeamsWar
{
public const string EVENTNAME = "TeamsWar";
public enum TeamName
{
Blue,
Red,
Black,
Green
}
private ConcurrentDictionary<TeamName, int> Teams = new ConcurrentDictionary<string, int>();
private ConcurrentDictionary<GameClient, TeamName> Players = new ConcurrentDictionary<GameClient, string>();
private List<GameClient> RegisteredPlayers = new List<GameClient>();
public TeamsWar() {
// Add the teams
Teams.TryAdd(TeamName.Blue, 0);
Teams.TryAdd(TeamName.Red, 0);
Teams.TryAdd(TeamName.Black, 0);
Teams.TryAdd(TeamName.Green, 0);
}
public void AddPlayer(GameClient player) {
// Add the player to the registered players
if(!RegisteredPlayers.Contains(player)) {
RegisteredPlayers.Add(player);
}
}
public void SortPlayers() {
// Sort the players based on level
List<GameClient> awaitingPlayers = RegisteredPlayers.OrderBy(I => I.Level);
int index = 0;
// Loop through the players that are registered and add them to the team
foreach(GameClient ap in awaitingPlayers) {
if(index > Teams.Count) {
index = 0;
}
Players.TryAdd(ap, Teams.ElementAt(index));
index++;
}
}
public void KillPlayer(GameClient killer, GameClient killed) {
// Check if the players are in the event
if(Players.ContainsKey(killer) && Players.ContainsKey(killed)) {
TeamName teamName;
Players.TryGetValue(killer, out teamName);
int score = 0;
Teams.TryGetValue(teamName, out score);
Teams.TryUpdate(teamName, score + 1, score);
}
}
}
}
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conquer_Online_Server.Client;
using Conquer_Online_Server.Network.GamePackets;
namespace Conquer_Online_Server.Game.Features.Tournaments
{
class TeamsWar
{
public const string EVENTNAME = "TeamsWar";
public enum TeamName
{
Blue,
Red,
Black,
Green
}
private ConcurrentDictionary<TeamName, int> Teams = new ConcurrentDictionary<string, int>();
private ConcurrentDictionary<GameClient, TeamName> Players = new ConcurrentDictionary<GameClient, string>();
private List<GameClient> RegisteredPlayers = new List<GameClient>();
public TeamsWar() {
// Add the teams
Teams.TryAdd(TeamName.Blue, 0);
Teams.TryAdd(TeamName.Red, 0);
Teams.TryAdd(TeamName.Black, 0);
Teams.TryAdd(TeamName.Green, 0);
}
public void AddPlayer(GameClient player) {
// Add the player to the registered players
if(!RegisteredPlayers.Contains(player)) {
RegisteredPlayers.Add(player);
}
}
public void SortPlayers() {
// Sort the players based on level
List<GameClient> awaitingPlayers = RegisteredPlayers.OrderBy(I => I.Level);
int index = 0;
// Loop through the players that are registered and add them to the team
foreach(GameClient ap in awaitingPlayers) {
if(index > Teams.Count) {
index = 0;
}
Players.TryAdd(ap, Teams.ElementAt(index));
index++;
}
}
public void KillPlayer(GameClient killer, GameClient killed) {
// Check if the players are in the event
if(Players.ContainsKey(killer) && Players.ContainsKey(killed)) {
TeamName teamName;
Players.TryGetValue(killer, out teamName);
int score = 0;
Teams.TryGetValue(teamName, out score);
Teams.TryUpdate(teamName, score + 1, score);
}
}
}
}