|
You last visited: Today at 23:05
Advertisement
the better method
Discussion on the better method within the CO2 Private Server forum part of the Conquer Online 2 category.
12/09/2014, 22:24
|
#1
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
the better method
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
Here is the code !
Code:
using System;
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 static string EventName = "TeamsWar";
public static const int BlueTN = 1000
, RedTN = 1001
, BlackTN = 1002
, GreenTN = 1003;
public static int BlueTeamKills, RedTeamKills, BlackTeamKills, GreenTeamKills;
public static int MapID = 11123;
public static int BlueTC = 0
, RedTC = 0
, BlackTC = 0
, GreenTC = 0;
#region TeamChoice2
public static bool isBlueC = false
, isRedC = false
, isBlackC = false
, isGreenC = false;
#endregion
public static void killingFromEnemies(GameClient killer, GameClient Killed)
{
Message msg;
msg = new Message("", System.Drawing.Color.Red, Message.FirstRightCorner);
msg = new Message("BlueTeamKills Kills ! : " + BlueTeamKills, System.Drawing.Color.Red, Message.ContinueRightCorner);
msg = new Message("RedTeamKills Kills ! : " + RedTeamKills, System.Drawing.Color.Red, Message.ContinueRightCorner);
msg = new Message("BlackTeamKills Kills ! : " + BlackTeamKills, System.Drawing.Color.Red, Message.ContinueRightCorner);
msg = new Message("GreenTeamKills Kills ! : " + GreenTeamKills, System.Drawing.Color.Red, Message.ContinueRightCorner);
// Effect place !
Kernel.SendWorldMessage(msg, Program.GamePool, (ushort)MapID);
}
public static void TeamChoice()
{
int choice = 0;
Client.GameClient client = new GameClient(null);
if (BlueTC > RedTC && BlueTC > BlackTC && BlueTC > GreenTC)
choice = Kernel.RandFromGivingNums(RedTN, BlackTN, GreenTN);
else if (RedTC > BlueTC && RedTC > BlackTC && RedTC > GreenTC)
choice = Kernel.RandFromGivingNums(BlueTN, BlackTN, GreenTN);
else if (BlackTC > BlueTC &&
BlackTC > RedTC &&
BlackTC > GreenTC)
{
choice = Kernel.RandFromGivingNums(BlueTN, RedTN, GreenTN);
}
else if (GreenTC > BlueTC &&
GreenTC > BlackTC &&
GreenTC > RedTC)
{
choice = Kernel.RandFromGivingNums(BlueTN, RedTN, BlackTN);
}
else
{
choice = Kernel.RandFromGivingNums(BlueTN, RedTN, BlackTN, GreenTN);
}
switch (choice)
{
case BlueTN:
{
BlueTC++;
client.Entity.isBlueTeam = true;
break;
}
case RedTN:
{
RedTC++;
client.Entity.isRedTeam = true;
break;
}
case BlackTN:
{
BlackTC++;
client.Entity.isBlackTeam = true;
break;
}
case GreenTN:
{
GreenTC++;
client.Entity.isBlackTeam = true;
break;
}
break;
}
}
public static void TeamChoice2()
{
Client.GameClient client = new GameClient(null);
if (isBlueC == true && isRedC == true && isBlackC == true && isGreenC == true)
{
isBlueC = false;
isRedC = false;
isBlackC = false;
isGreenC = false;
}
if (isBlueC == false && isRedC == false && isBlackC == false && isGreenC == false)
{
isBlueC = true;
client.Entity.isBlueTeam = true;
BlueTC++;
}
else if (isBlueC == true)
{
isRedC = true;
client.Entity.isRedTeam = true;
RedTC++;
}
else if (isRedC == true)
{
isBlackC = true;
client.Entity.isBlackTeam = true;
BlackTC++;
}
else if (isBlackC == true)
{
isGreenC = true;
client.Entity.isGreenTeam = true;
GreenTC++;
}
}
}
}
|
|
|
12/09/2014, 23:01
|
#2
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
Nothing in that example will work.
-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)
|
|
|
12/09/2014, 23:17
|
#3
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
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
|
|
|
12/09/2014, 23:29
|
#4
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
Code:
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);
}
|
|
|
12/09/2014, 23:38
|
#5
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
but this will work for one team ?
or I can use this method in as many teams as I want
|
|
|
12/09/2014, 23:48
|
#6
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
You can use it for as many teams as you want.
The idea is you create an array of teams of any length and then you assign players to each team.
want 10 teams? Change 0 lines of code and it'll work the same as 2 teams.
|
|
|
12/09/2014, 23:56
|
#7
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
thats what I did
enum Teams { Blue, Red, Black, Green };
public static void TeamSorting()
{
var teamCounter = 0;
var teamCounts = 4;
foreach (var client in TeamsWar.MapID)
{
teamCounter++;
if (teamCounter > teamCounts)
teamCounter = 0;
Teams[teamCounter].AddMember(client);
}
}
and I got this error
Sorry man, I am just a noob
|
|
|
12/10/2014, 00:33
|
#8
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
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.
|
|
|
12/10/2014, 01:03
|
#9
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
Wow just downloaded albetros source and looked in the TDM wow ! it is amazing !
|
|
|
12/10/2014, 01:06
|
#10
|
elite*gold: 130
Join Date: Oct 2007
Posts: 1,655
Received Thanks: 706
|
Updated your code a bit (can be done better, but you will atleast get the idea):
Code:
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);
}
}
}
}
|
|
|
12/10/2014, 01:27
|
#11
|
elite*gold: 0
Join Date: Sep 2011
Posts: 105
Received Thanks: 17
|
Quote:
Originally Posted by turk55
Updated your code a bit (can be done better, but you will atleast get the idea):
Code:
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);
}
}
}
}
|
ahhahahah that's not a bit 
thanks
|
|
|
All times are GMT +1. The time now is 23:05.
|
|