Jump to content
crezzy

CastleWars rewrote 3 years later

Recommended Posts

Hey guys well i wrote a castleWars tutorial almost 3 years ago!
Original thread: [URL="http://www.****************/runescape-development/rs-503-client-server/tutorials/274631-614-80-castlewars-sort-custom-i-guess.html"]http://www.****************/runescape-development/rs-503-client-server/tutorials/274631-614-80-castlewars-sort-custom-i-guess.html[/URL]
I had a look at the other day and I got to say it was horrible lol

Its mostly just the game play but some things will need to be added and a few more checks will have to be made
I would say this is about 50% complete.
Anyway so I re-did the class which I'm giving out but I will not be giving you all the objects, well all small parts. You will have to figure out how to make it work ;)
It's easy enough all you're doing is calling the methods in the class.

Example:
Leaving of a team via portal:
You would just type this code in the object id:
[code]CastleWars wars = new CastleWars();
wars.leaveTeamLobby(p);[/code]

What you get so far with this class:
You join the lobby Via portal.
When there is enough players to start a game, the game starts.
The in-game timer is set to 10 mins.
Kill a player in-game to add points to your team.
When timer hits 0 or zammy team/sara team player count hits 0 the game will end.
Depending on if your team won(I.e Team having the most kills), lost or drew depends on how many points you will received.

CastleWars.java
[code]
package dragonkk.rs2rsps.model.minigames;

import java.util.ArrayList;
import java.util.List;

import org.world.event.Event;
import org.world.event.EventManager;

import com.google.common.math.DoubleMath;

import dragonkk.rs2rsps.Server;
import dragonkk.rs2rsps.events.Task;
import dragonkk.rs2rsps.model.Item;
import dragonkk.rs2rsps.model.World;
import dragonkk.rs2rsps.model.player.Equipment;
import dragonkk.rs2rsps.model.player.Mask;
import dragonkk.rs2rsps.model.player.Player;
import dragonkk.rs2rsps.net.Frames;
import dragonkk.rs2rsps.rsobjects.RSObjectsRegion;
import dragonkk.rs2rsps.util.Misc;
import dragonkk.rs2rsps.util.RSObject;
import dragonkk.rs2rsps.util.RSTile;

/*
* CastleWars
*
* [MENTION=52317]auth[/MENTION]or Crezzy 100%
*
*/
public class CastleWars { // rewards interface id : 60

private int[] startCoords = { 2427 , 3076 , 2371 , 3130 };
private static int[] timer = { 0 , 0 };
private static byte[] kills = { 0 , 0 };
private static boolean gameStarted = false;
private boolean timerStarted = false;
private boolean zamHighSara = zamTeam.size () > saraTeam.size ();
private boolean saraHighZam = saraTeam.size () > zamTeam.size ();
private Item zamFlagId = new Item ( 4039 );
private Item saraFlagId = new Item ( 4037 );
public static List <Player> saraTeam = new ArrayList <Player> ();
public static List <Player> zamTeam = new ArrayList <Player> ();
public static List <Player> inLobby = new ArrayList <Player> ();
public static List <Player> inGame = new ArrayList <Player> ();

private int setTimeInGame() {
switch ( inLobby.size () ) {
case 2:
return 300;
case 3:
case 4:
return 400;
case 5:
case 6:
return 500;
default:
return 600;
}
}

private boolean enoughPlayers() {
return saraTeam.size () >= 1 & zamTeam.size () >= 1;
}

private boolean canEndGame() {
return timer[1] == 0 || zamTeam.size () <= 0 || saraTeam.size () <= 0 && gameStarted == true;
}

private boolean atCw( Player p ) {
return p.getLocation ().getX () >= 2368 && p.getLocation ().getX () <= 2431 && p.getLocation ().getY () >= 3069
&& p.getLocation ().getY () <= 3138;
}

private void removeAddList( Player p1, boolean saraList, boolean adding ) {
synchronized ( inLobby ) {
synchronized ( saraTeam ) {
synchronized ( zamTeam ) {
switch ( add ) {
String add = adding ? "true" : "false";
case "true":
inLobby.add ( p1 );
if ( saraList )
saraTeam.add ( p1 );
else
zamTeam.add ( p1 );
break;
case "false":
inLobby.remove ( p1 );
if ( saraList )
saraTeam.remove ( p1 );
else
zamTeam.remove ( p1 ) ;
break;
}
}
}
}
}

private void addRemovePlayersToInGameList( boolean adding,boolean removingAll ) {
synchronized ( inGame ) {
if ( adding ) {
inGame.addAll ( zamTeam );
inGame.addAll ( saraTeam );
} else {
if(removingAll){
inLobby.removeAll ( inGame );
zamTeam.clear ();
saraTeam.clear ();
inGame.clear ();
} else {
inLobby.removeAll( inGame);
}
}
}
}

public void leaveTeamLobby( Player p ) {
handleTeamCape ( p, false );
p.getMask ().getRegion ().teleport ( 2441, 3090, 0, 0 );
p.getFrames ().sendCloseOverlay ();
p.getFrames ().sendChatMessage ( 0,
saraTeam.contains ( p ) ? "You leave the Saradomin team." : "You leave the Zamarok team." );
removeAddList ( p, saraTeam.contains ( p ) ? true : false, false );
}

public void joinTeamLobby( Player p, int object ) {
boolean bool = object == ( 4387 );
if ( zamHighSara && !bool ) {
p.getFrames ().sendChatMessage ( 0, "To many players in the Zamorak Team. Please join Saradomin Team!" );
return;
}
if ( saraHighZam && bool ) {
p.getFrames ().sendChatMessage ( 0, "To many players in the Saradomin Team. Please join Zamorak Team!" );
return;
}
p.getFrames ().sendOverlay ( 57 );
p.getMask ().getRegion ().teleport ( bool ? 2376 : 2424, bool ? 9488 : 9527, 0, 0 );
removeAddList ( p, bool ? true : false, true );
handleTeamCape ( p, true );
p.getFrames ().sendChatMessage ( 0, "You join the" + ( bool ? "Saradomin team" : "Zamorak team!" ) );
processLobbyTimer ();
}

public void handleTeamCape( Player p, boolean adding ) {
Item saraCloakId = new Item ( 4041 );
Item zamCloakId = new Item ( 4042 );
p.getEquipment ();
if ( adding ) {
p.getEquipment ().removeSlot ( Equipment.SLOT_CAPE );
p.getEquipment ().removeSlot ( Equipment.SLOT_HAT );
p.getEquipment ().set ( Equipment.SLOT_CAPE, saraTeam.contains ( p ) ? saraCloakId : zamCloakId );
} else {
p.getEquipment ().removeSlot ( Equipment.SLOT_CAPE );
p.getEquipment ().removeSlot ( Equipment.SLOT_HAT );
p.getInventory ().deleteItem ( saraTeam.contains ( p ) ? 4041 : 4042, 10 );
}
}

public void randomTeamPortal( Player p ) {
removeAddList ( p, saraHighZam ? true : false, true );
handleTeamCape ( p, true );
p.getFrames ().sendOverlay ( 57 );
p.getMask ().getRegion ().teleport ( saraHighZam ? 2376 : 2424, saraHighZam ? 9488 : 9527, 0, 0 );
p.getFrames ().sendChatMessage ( 0, "You join the" + ( saraHighZam ? "Saradomin team" : "Zamorak team" + "!" ) );
processLobbyTimer ();
}

public void loginLogout( Player p, boolean logginIn ) {
if ( atCw ( p ) || inLobby.contains ( p ) )
if ( logginIn == true ) {
removeFlag ( p, false );
p.getMask ().getRegion ().teleport ( 2441, 3090, 0, 0 );
p.getFrames ().sendChatMessage ( 0, "You logged out so you auto left the game!" );
} else
removeAddList ( p, saraTeam.contains ( p ) ? true : false, false );
}

private void cwPlay() {
EventManager.submit ( new Event ( 1000 ) {
@ Override
public void execute() {
if ( timer[1] >= 1 ) {
timer[1]--;
for ( Player p : inGame ) {
if(inLobby.contains(p))
addRemovePlayersToInGameList(false, false);
p.getFrames ().sendString ( "" + timer[1] / 60 + " Mins left", 58, 6 );
if ( !atCw ( p ) ) {
removeAddList ( p, saraTeam.contains ( p ) ? true : false, false );
continue;
}
if ( canEndGame () ) {
gameEnd ();
this.stop ();
}
}
}
}
} );
}

private void processLobbyTimer() {
if ( enoughPlayers () ) {
if ( !timerStarted )
timer[0] = 60;
EventManager.submit ( new Event ( 1000 ) {
@ Override
public void execute() {
for ( final Player pl : inLobby ) {
pl.getFrames ().sendString (
gameStarted ? "Currently game underway. Time remaining = " + timer[1] / 60 + " Mins left"
: "Waiting for players to join the other team.", 57, 0 );
continue;
}
if ( timer[0] >= 1 && gameStarted == false ) {
timer[0]--;
timerStarted = true;
for ( final Player pl : inLobby )
pl.getFrames ().sendString (
zamTeam.size () == 0 || saraTeam.size () == 0 ? "Waiting for players to join the other team."
: "Time untill next game = " + timer[0], 57, 0 );
}
if ( timer[0] <= 1 )
if ( zamTeam.size () == 0 || saraTeam.size () == 0 ) {
timerStarted = false;
this.stop ();
} else {
gameStart ();
this.stop ();
}
}
} );
}
}

private void gameStart() {
addRemovePlayersToInGameList ( true , false);
cwPlay ();
gameStarted = true;
timerStarted = false;
timer[1] = setTimeInGame ();
for ( Player pl : inLobby ) {
boolean bool = saraTeam.contains ( pl );
pl.getFrames ().sendOverlay ( 58 );
pl.getFrames ().sendChatMessage ( 0, "Game has started. Good luck" + ( bool ? "Saradomin" : "Zamorak" + "!" ) );
pl.getMask ().getRegion ().teleport ( startCoords[bool ? 0 : 2], startCoords[bool ? 1 : 3], 1, 0 );
}
}

public void cwDeath( final Player p ) {
if ( atCw ( p ) ) {
boolean saraDeath = saraTeam.contains ( p );
p.getMask ().getRegion ().teleport ( startCoords[( saraDeath ? 0 : 2 )], startCoords[( saraDeath ? 1 : 3 )], 1, 0 );
kills[saraDeath ? 1 : 0]++;
for ( Player pl : inGame ) {
pl.getFrames ().sendString ( "Saradomin deaths = " + kills[0], 58, 0 );
pl.getFrames ().sendString ( "Zamarok deaths = " + kills[1], 58, 1 );
}
if ( p.getEquipment ().contains ( zamFlagId ) || p.getEquipment ().contains ( saraFlagId ) )
removeFlag ( p, false );
}
}

public void gameEnd() {
gameStarted = false;
handleWinner ();
for ( Player pl : inGame ) {
pl.getMask ().getRegion ().teleport ( 2441 + Misc.random ( 5 ), 3090 + Misc.random ( 5 ), 0, 0 );
pl.getFrames ().sendCloseOverlay ();
handleTeamCape ( pl, false );
pl.getFrames ().sendChatMessage ( 1, "Game Has Ended." );
}
addRemovePlayersToInGameList ( false , true);
kills[0] = 0;
kills[1] = 0;
}

private void handleWinner() {
for ( Player pl : inGame ) {
if ( kills[0] > kills[1] ) {
pl.getFrames ()
.sendChatMessage (
1,
saraTeam.contains ( pl ) ? "Congratz your team wins the game, You have received get 5 points for winning!"
: "To bad Zamorak, your team loses the game. You have received 2 points for your effort." );

pl.cwpoints += saraTeam.contains ( pl ) ? 5 : 2;
} else if ( kills[1] > kills[0] ) {
pl.getFrames ()
.sendChatMessage (
1,
zamTeam.contains ( pl ) ? "Congratz Zamorak! Your team wins the game. You have received 5 points for winning!"
: "To bad Saradomin, your team loses the game. You have received 2 points for your effort." );
pl.cwpoints += zamTeam.contains ( pl ) ? 5 : 2;
} else if ( kills[0] == kills[1] ) {
pl.getFrames ().sendChatMessage ( 1, "No points were scored during the game. " );
pl.getFrames ().sendChatMessage ( 1, "As a result no CastleWars points will be rewarded." );
} else {
pl.getFrames ().sendChatMessage ( 1, "The game was a tie. Both teams received 3 Point each for the effort." );
pl.cwpoints += 3;
}
}
}

public void handleTakeFlag( Player p, int object ) {
boolean zamFlag = object == 4903;
RSTile flagTile = new RSTile ( zamFlag ? (short) 2370 : 2429, zamFlag ? (short) 3133 : 3074, (byte) 3, 0 );
if ( zamFlag && zamTeam.contains ( p ) && p.getLocation ().getDistance ( flagTile ) <= 2 ) {
p.getFrames ().sendChatMessage ( 1, "You can't take your own team Flag!" );
return;
}
if ( !zamFlag && saraTeam.contains ( p ) && p.getLocation ().getDistance ( flagTile ) <= 2 ) {
p.getFrames ().sendChatMessage ( 1, "You can't take your own team Flag!" );
return;
}
p.getEquipment ();
p.getEquipment ().removeSlot ( Equipment.SLOT_WEAPON );
p.getEquipment ().removeSlot ( Equipment.SLOT_SHIELD );
p.getEquipment ().set ( Equipment.SLOT_WEAPON, zamFlag ? zamFlagId : saraFlagId );
for ( Player pl : inGame ) {
pl.getFrames ().removeMapObject ( flagTile );
// pl.getHinticonmanager().addHintIcon(p, 1, -1, true);
}
}

public void removeFlag( Player p, boolean capped ) {
if ( atCw ( p ) ) {
boolean zamFlag = p.getEquipment ().getWeaponId () == 4039;
p.getEquipment ().deleteItem ( zamFlag ? 4039 : 4037, 1 );
if ( !capped ) {
for ( Player pl : inGame ) {
pl.getFrames ().sendChatMessage (
1,
( zamFlag ? "Zamarok" : "Saradomin" )
+ " flag has been dropped and is no longer able to be captured." );
}
}
}
}

public void flagCap( Player p ) {
if ( p.getEquipment ().getWeaponId () == 4039 || p.getEquipment ().getWeaponId () == 4037 ) {
for ( Player pl : inGame ) {
pl.getFrames ().sendChatMessage ( 1, "" + p.getUsername ().toString () + " has just captured the "+
(p.getEquipment ().getWeaponId () == 4039 ? "Zamorak " : "Saradomin ")+"flag!" );
pl.getFrames ().sendChatMessage (
1,
"10 Kills have been awarded to the "
+ ( p.getEquipment ().getWeaponId () == 4039 ? "Zamorak team!" : "Saradomin Team!" ) );
}
kills[p.getEquipment ().getWeaponId () == 4039 ? 1 : 0] += 10;
removeFlag ( p, true );
}
}

public void resetCw() {
for ( Player pl : inGame ) {
pl.getMask ().getRegion ().teleport ( 2441, 3090, 0, 0 );
pl.getFrames ().sendChatMessage ( 1, "CastleWars reset by an admin." );
pl.getFrames ().sendCloseOverlay ();
}
for ( Player pl1 : inLobby ) {
pl1.getMask ().getRegion ().teleport ( 2441, 3090, 0, 0 );
pl1.getFrames ().sendChatMessage ( 1, "CastleWars reset by an admin." );
pl1.getFrames ().sendCloseOverlay ();
}
gameStarted = false;
zamTeam.clear ();
saraTeam.clear ();
inGame.clear ();
inLobby.clear ();
kills[0] = 0;
kills[1] = 0;
timer[0] = 0;
timer[1] = 0;
}
}
[/code]

100% credits me aka Crezzy
Feedback and constructive criticism will be thanked. Edited by crezzy

Share this post


Link to post
Share on other sites
1. Do you have any idea what the keyword static is for?
2. Have you ever heard of OOP?
3. Have you ever heard of a constant?
4. Static lists. Never, let me repeat, NEVER use static lists (or any sort of collection for that matter unless you know what it's for).
5. Why are you using the synchronized keyword? You could of just created a synchronized list and avoided those tedious blocks.
6. [code]String add = adding ? "true" : "false";
switch (add) {
case "true":
inLobby.add(p1);
if (saraList) {
saraTeam.add(p1);
} else {
zamTeam.add(p1);
}
break;
case "false":
inLobby.remove(p1);
if (saraList) {
saraTeam.remove(p1);
} else {
zamTeam.remove(p1);
}
break;
}
}[/code]
You literally could of had written this 10 times better, and please explain that string in the middle of it. You're already giving the team as a parameter in the form of a boolean.
7. Booleans work this way aswell, no need to make things complicated. [code]private boolean saraWon() {
return kills[0] > kills[1];
}[/code]
8. [code]saraTeam.size() >= 1 & zamTeam.size() >= 1;[/code]
& is not the same as &&. You are using the bitwise AND operator here.
9. For no apparent reason, some of your fields are static and some are not.
10. [code]public void joinTeamLobby(Player p, int object) {
boolean bool = object == 4387;[/code] why didn't you give the boolean as a parameter? The object id is not relevant, you just want to know if the player clicked X or not.

I give up.

Share this post


Link to post
Share on other sites
[MENTION=444]Stacx[/MENTION] oh shit son.

Share this post


Link to post
Share on other sites
[quote name='Stacx']1. Do you have any idea what the keyword static is for?
2. Have you ever heard of OOP?
3. Have you ever heard of a constant?
4. Static lists. Never, let me repeat, NEVER use static lists (or any sort of collection for that matter unless you know what it's for).
5. Why are you using the synchronized keyword? You could of just created a synchronized list and avoided those tedious blocks.
6. [code]String add = adding ? "true" : "false";
switch (add) {
case "true":
inLobby.add(p1);
if (saraList) {
saraTeam.add(p1);
} else {
zamTeam.add(p1);
}
break;
case "false":
inLobby.remove(p1);
if (saraList) {
saraTeam.remove(p1);
} else {
zamTeam.remove(p1);
}
break;
}
}[/code]
You literally could of had written this 10 times better, and please explain that string in the middle of it. You're already giving the team as a parameter in the form of a boolean.
7. Booleans work this way aswell, no need to make things complicated. [code]private boolean saraWon() {
return kills[0] > kills[1];
}[/code]
8. [code]saraTeam.size() >= 1 & zamTeam.size() >= 1;[/code]
& is not the same as &&. You are using the bitwise AND operator here.
9. For no apparent reason, some of your fields are static and some are not.
10. [code]public void joinTeamLobby(Player p, int object) {
boolean bool = object == 4387;[/code] why didn't you give the boolean as a parameter? The object id is not relevant, you just want to know if the player clicked X or not.

I give up.[/QUOTE]

I agree this class is horrible. that's not the completed version lol

I use static so there can only be 1 list. I.e when the list was not static every time I tried to add a player it would create a new list.
I try my best not to use static methods when ever possible unless they are needed. I find its better just to create a new instance instead. The only time I really use static is if its going to be used allot by different classes.
And how is this not OOP lol? Uh screw it il post my other Castlewars class this one is missing lots lol.

Share this post


Link to post
Share on other sites
[MENTION=444]Stacx[/MENTION] There's 5 static objects in the whole class.
Like I said I try my hardest not to use static methods unless they get used allot which castle-wars is not going to be used that often really. So I don't see the point having i loaded constantly but for the List they need to be static for when I'm doing world checks to make sure anything hasnt gone wrong like there not in cw but there in list est So I keep the list loaded and when the list was not static every time I tried to adding a player to it, it would create a new list. I know there's prob a way around this like adding the list to the constructor but I just settled for that. But could you explain why not to use static lists?

Also this String add = adding ? "true" : "false"; is there because there are 4 lists and its being handled in 1 method. Read the code lol if the string is add it will add them to the ingame list and remove from lobby list... Edited by crezzy

Share this post


Link to post
Share on other sites
[quote name='crezzy']I agree this class is horrible. that's not the completed version lol

I use static so there can only be 1 list. I.e when the list was not static every time I tried to add a player it would create a new list.
I try my best not to use static methods when ever possible unless they are needed. I find its better just to create a new instance instead. The only time I really use static is if its going to be used allot by different classes.
And how is this not OOP lol? Uh screw it il post my other Castlewars class this one is missing lots lol.[/QUOTE]

There's nothing wrong using static, but because you don't know when to use it, I pointed it out.

This is anti-OOP. You're using primitive types for everything, like your coord array (which should be a constant, by the way) when you could of had used an object that represents a location. You've stacked everything in a single class when you should have clustered the different components and structures in their own classes, this lacks a real design. You should probably have a class that you can instance as the castle wars game that holds all the players and stats etc, a class that represents a team and then stack the rest in a misc. cw class.

[quote name='crezzy'][MENTION=444]Stacx[/MENTION] There's 5 static objects in the whole class.
Like I said I try my hardest not to use static methods unless they get used allot which castle-wars is not going to be used that often really. So I don't see the point having i loaded constantly but for the List they need to be static for when I'm doing world checks to make sure anything hasnt gone wrong like there not in cw but there in list est So I keep the list loaded and when the list was not static every time I tried to adding a player to it, it would create a new list. I know there's prob a way around this like adding the list to the constructor but I just settled for that. But could you explain why not to use static lists?

Also this String add = adding ? "true" : "false"; is there because there are 4 lists and its being handled in 1 method. Read the code lol if the string is add it will add them to the ingame list and remove from lobby list...[/QUOTE]

The reason you ended up with static collections is because there is no design behind this code.

And why the fuck would you have a string when you have a boolean that serves the [I]exact [/I]same purpose here? The 'fixes' you did to the original post are not what I meant by any of what I had said earlier. Edited by Stacx

Share this post


Link to post
Share on other sites
[quote name='Stacx']There's nothing wrong using static, but because you don't know when to use it, I pointed it out.

This is anti-OOP. You're using primitive types for everything, like your coord array (which should be a constant, by the way) when you could of had used an object that represents a location. You've stacked everything in a single class when you should have clustered the different components and structures in their own classes, this lacks a real design. You should probably have a class that you can instance as the castle wars game that holds all the players and stats etc, a class that represents a team and then stack the rest in a misc. cw class.



The reason you ended up with static collections is because there is no design behind this code.

And why the fuck would you have a string when you have a boolean that serves the [I]exact [/I]same purpose here? The 'fixes' you did to the original post are not what I meant by any of what I had said earlier.[/QUOTE]

I don't like coding things in different classes, especially when its not exactly a big class. Fair enough if it was as big as the Player class but making more classes would = more code'
I do know when static methods/variables are needed I just prefer not to use them if possible I only use them when a variable is a common value associated with the type rather than an individual instance of the type, that is when it should be a static variable. I really don't see much of this code being used that often apart from the timer.
I do agree with you however this code is not done as well as it could be but that's why I'm giving it out. People can use it as a base, its not even complete and I doubt I will ever complete it tbh.

Still you have not gave me a valid reply to why you should never use statsic Lists? Edited by crezzy

Share this post


Link to post
Share on other sites
[quote name='crezzy']I don't like coding things in different classes, especially when its not exactly a big class. Fair enough if it was as big as the Player class but making more classes would = more code'
I do know when static methods/variables are needed I just prefer not to use them if possible I only use them when a variable is a common value associated with the type rather than an individual instance of the type, that is when it should be a static variable. I really don't see much of this code being used that often apart from the timer.
I do agree with you however this code is not done as well as it could be but that's why I'm giving it out. People can use it as a base, its not even complete and I doubt I will ever complete it tbh.

Still you have not gave me a valid reply to why you should never use statsic Lists?[/QUOTE]

You defeat the purpose of OOP here. You don't exactly get it. Each time you click an object, you'll create a new instance of the CastleWars class. That is complete waste and it happens for no reason, when you should instead have a class with static methods to handle the clicking and shit, and the actual CW game clustered into different structures with a real design. You have minimal control over a static collection and it'll quickly get out of hands. As I see it, you will never have to use a static collection. Look it up on google if you want a quote about it.

The player class is not big if you know what the player class is supposed to do, which is to represent a player, not to hold 30,000 useless fields. The way minigames should generally work is to have a flexible system that supports any type of minigame, not a bunch of half assed stacked up classes. Your excuse does not work very well; even if it's something small, it doesn't give you an excuse to write half assed code.

I wrote up a rough example of what I'm talking about, this is how you could represent the players and teams individually by clustering them.
[code]
...
...
public class CastleWarsGame {

private CastleWarsTeam saradominTeam, zamorakTeam;

...

public CastleWarsGame(List<Player> zamorakPlayers, List<Player> saradominPlayers) {
this.zamorakTeam = new CastleWarsTeam("Zamorak", zamorakPlayers);
this.saradominTeam = new CastleWarsTeam("Saradomin", saradominPlayers);
}

...

public CastleWarsTeam getSaradominTeam() {
return saradominTeam;
}

public CastleWarsTeam getZamorakTeam() {
return zamorakTeam;
}
}

...
...
public class CastleWarsTeam {

private List<Player> players;
private String name; // should actually be done in an enum, as in Alliance.ZAMORAK, Alliance.SARADOMIN
private int score;

public CastleWarsTeam(String name) {
this(name, new ArrayList<Player>());
}

public CastleWarsTeam(String name, List<Player> players) {
this.name = name;
this.players = players;
this.score = 0;
}

public boolean add(Player player) {
return players.add(player);
}

public boolean remove(Player player) {
return players.remove(player);
}

public List<Player> getPlayers() {
return players;
}

public void incrementScore() {
score++;
}

public int getScore() {
return score;
}

public String getName() {
return name;
}
}
...
...[/code]

You'd essentially only have to instance the CastleWarsGame once the lobby timer ends and the lobbies are merged in the lists we're giving as a parameter in the constructor, and that instance would represent the whole game and everything in it. Edited by Stacx

Share this post


Link to post
Share on other sites
[quote name='Stacx']You defeat the purpose of OOP here. You don't exactly get it. Each time you click an object, you'll create a new instance of the CastleWars class. That is complete waste and it happens for no reason, when you should instead have a class with static methods to handle the clicking and shit, and the actual CW game clustered into different structures with a real design. You have minimal control over a static collection and it'll quickly get out of hands. As I see it, you will never have to use a static collection. Look it up on google if you want a quote about it.

The player class is not big if you know what the player class is supposed to do, which is to represent a player, not to hold 30,000 useless fields. The way minigames should generally work is to have a flexible system that supports any type of minigame, not a bunch of half assed stacked up classes. Your excuse does not work very well; even if it's something small, it doesn't give you an excuse to write half assed code.

I wrote up a rough example of what I'm talking about, this is how you could represent the players and teams individually by clustering them.
[code]
...
...
public class CastleWarsGame {

private CastleWarsTeam saradominTeam, zamorakTeam;

...

public CastleWarsGame(List<Player> zamorakPlayers, List<Player> saradominPlayers) {
this.zamorakTeam = new CastleWarsTeam("Zamorak", zamorakPlayers);
this.saradominTeam = new CastleWarsTeam("Saradomin", saradominPlayers);
}

...

public CastleWarsTeam getSaradominTeam() {
return saradominTeam;
}

public CastleWarsTeam getZamorakTeam() {
return zamorakTeam;
}
}

...
...
public class CastleWarsTeam {

private List<Player> players;
private String name; // should actually be done in an enum, as in Alliance.ZAMORAK, Alliance.SARADOMIN
private int score;

public CastleWarsTeam(String name) {
this(name, new ArrayList<Player>());
}

public CastleWarsTeam(String name, List<Player> players) {
this.name = name;
this.players = players;
this.score = 0;
}

public boolean add(Player player) {
return players.add(player);
}

public boolean remove(Player player) {
return players.remove(player);
}

public List<Player> getPlayers() {
return players;
}

public void incrementScore() {
score++;
}

public int getScore() {
return score;
}

public String getName() {
return name;
}
}
...
...[/code]

You'd essentially only have to instance the CastleWarsGame once the lobby timer ends and the lobbies are merged in the lists we're giving as a parameter in the constructor, and that instance would represent the whole game and everything in it.[/QUOTE]

Ok I get your point now but still I prefer doing it in one class as it seems easier to manage.
However if I do it your way would it cause less stress on the server? Or would it be exactly the same?

Ps I did say I could of made the list via the class constructor like you have done here:
[QUOTE]public CastleWarsTeam(String name) {
this(name, new ArrayList<Player>());
}[/QUOTE] Edited by crezzy

Share this post


Link to post
Share on other sites
[quote name='Stacx']
You'd essentially only have to instance the CastleWarsGame once the lobby timer ends and the lobbies are merged in the lists we're giving as a parameter in the constructor, and that instance would represent the whole game and everything in it.[/QUOTE]
Are you saying he cannot create instances of castlewars games meaning that if one game is going, and another one starts then it breaks? I haven't looked through the code.

Share this post


Link to post
Share on other sites

×