package gtanks.battles; import gtanks.battles.BattlefieldModel; import gtanks.battles.BattlefieldPlayerController; import gtanks.battles.bonuses.BonusesSpawnService; import gtanks.battles.ctf.CTFModel; import gtanks.battles.ctf.flags.FlagServer; import gtanks.battles.effects.EffectType; import gtanks.battles.tanks.Tank; import gtanks.battles.tanks.colormaps.Colormap; import gtanks.battles.tanks.data.DamageTankData; import gtanks.battles.tanks.hulls.Hull; import gtanks.battles.tanks.math.Vector3; import gtanks.battles.tanks.statistic.PlayerStatistic; import gtanks.battles.tanks.statistic.PlayersStatisticModel; import gtanks.battles.tanks.statistic.prizes.BattlePrizeCalculate; import gtanks.battles.tanks.weapons.EntityType; import gtanks.battles.tanks.weapons.IEntity; import gtanks.battles.tanks.weapons.IWeapon; import gtanks.battles.tanks.weapons.WeaponUtils; import gtanks.collections.FastHashMap; import gtanks.commands.Type; import gtanks.json.JSONUtils; import gtanks.lobby.LobbyManager; import gtanks.lobby.battles.BattleInfo; import gtanks.services.TanksServices; import gtanks.services.annotations.ServicesInject; import gtanks.system.destroy.Destroyable; import gtanks.system.quartz.QuartzJob; import gtanks.system.quartz.QuartzService; import gtanks.system.quartz.TimeType; import gtanks.system.quartz.impl.QuartzServiceImpl; import gtanks.users.User; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Set; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; public class TankKillModel implements Destroyable { private static final String QUARTZ_GROUP = TankKillModel.class.getName(); private final String QUARTZ_NAME; private static final int EXPERIENCE_FOR_KILL = 15; private static final int TIME_TO_RESTART_BATTLE = 10000; private BattlefieldModel bfModel; @ServicesInject(target=TanksServices.class) private TanksServices tanksServices = TanksServices.getInstance(); @ServicesInject(target=QuartzService.class) private QuartzService quartzService = QuartzServiceImpl.inject(); private double battleFund; private BattleInfo battleInfo; public TankKillModel(BattlefieldModel bfModel) { this.bfModel = bfModel; this.battleInfo = bfModel.battleInfo; this.QUARTZ_NAME = "TankKillModel " + this.hashCode() + " battle=" + this.battleInfo.battleId; } public synchronized void damageTank(BattlefieldPlayerController controller, BattlefieldPlayerController damager, float damage, boolean considerDD) { if (controller == null || damager == null) { return; } Tank tank = controller.tank; if (tank.state.equals("newcome") || tank.state.equals("suicide")) { return; } if (this.bfModel.battleInfo.team && controller != damager && controller.playerTeamType.equals(damager.playerTeamType) && !this.bfModel.battleInfo.friendlyFire) { return; } Integer resistance = controller.tank.getColormap().getResistance(damager.tank.getWeapon().getEntity().getType()); damage = WeaponUtils.calculateDamageWithResistance(damage, resistance == null ? 0 : resistance); if (tank.isUsedEffect(EffectType.ARMOR)) { damage /= 2.0f; } if (damager.tank.isUsedEffect(EffectType.DAMAGE) && considerDD) { damage *= 2.0f; } DamageTankData lastDamage = tank.lastDamagers.get(damager); DamageTankData damageData = new DamageTankData(); damageData.damage = damage; damageData.timeDamage = System.currentTimeMillis(); damageData.damager = damager; if (lastDamage != null) { damageData.damage += lastDamage.damage; } if (damager.tank.isUsedEffect(EffectType.DAMAGE) && considerDD) { damageData.damage /= 2.0f; } if (controller != damager) { tank.lastDamagers.put(damager, damageData); } tank.health -= WeaponUtils.calculateHealth(tank, damage); this.changeHealth(tank, tank.health); if (tank.health <= 0) { tank.health = 0; this.killTank(controller, damager); } } public boolean healPlayer(BattlefieldPlayerController healer, BattlefieldPlayerController target, float addHeal) { Tank targetTank = target.tank; if (targetTank.state.equals("newcome") || targetTank.state.equals("suicide")) { return false; } if (targetTank.health >= 10000) { return false; } targetTank.health += WeaponUtils.calculateHealth(targetTank, addHeal); if (targetTank.health >= 10000) { targetTank.health = 10000; } this.changeHealth(targetTank, targetTank.health); return true; } public void changeHealth(Tank tank, int value) { if (tank != null) { tank.health = value; this.bfModel.sendToAllPlayers(Type.BATTLE, "change_health", tank.id, String.valueOf(tank.health)); } } public synchronized void killTank(BattlefieldPlayerController controller, BattlefieldPlayerController killer) { Tank tank = controller.tank; tank.state = "suicide"; tank.getWeapon().stopFire(); controller.clearEffects(); controller.send(Type.BATTLE, "local_user_killed"); if (killer == null) { this.bfModel.sendToAllPlayers(Type.BATTLE, "kill_tank", tank.id, "suicide"); } else { this.bfModel.sendToAllPlayers(Type.BATTLE, "kill_tank", tank.id, "killed", killer.tank.id); if (controller == killer) { controller.statistic.addDeaths(); this.bfModel.statistics.changeStatistic(controller); } else { if (this.bfModel.battleInfo.team) { if (controller.playerTeamType.equals(killer.playerTeamType)) { if (this.bfModel.battleInfo.friendlyFire) { controller.statistic.addDeaths(); this.bfModel.statistics.changeStatistic(controller); } } else { killer.statistic.addKills(false); controller.statistic.addDeaths(); if (killer.playerTeamType.equals("BLUE")) { if (!this.battleInfo.battleType.equals("CTF")) { ++this.bfModel.battleInfo.scoreBlue; } this.bfModel.sendToAllPlayers(Type.BATTLE, "change_team_scores", "BLUE", String.valueOf(this.bfModel.battleInfo.scoreBlue)); } else if (killer.playerTeamType.equals("RED")) { if (!this.battleInfo.battleType.equals("CTF")) { ++this.bfModel.battleInfo.scoreRed; } this.bfModel.sendToAllPlayers(Type.BATTLE, "change_team_scores", "RED", String.valueOf(this.bfModel.battleInfo.scoreRed)); } this.bfModel.statistics.changeStatistic(killer); this.bfModel.statistics.changeStatistic(controller); } } else { killer.statistic.addKills(true); controller.statistic.addDeaths(); this.bfModel.statistics.changeStatistic(killer); this.bfModel.statistics.changeStatistic(controller); } if (this.bfModel.battleInfo.team) { LinkedHashMap<BattlefieldPlayerController, DamageTankData> lastDamagers = controller.tank.lastDamagers; if (lastDamagers.size() <= 1) { this.tanksServices.addScore(killer.parentLobby, 10); if (this.bfModel.battleInfo.team) { killer.statistic.addScore(10); this.bfModel.statistics.changeStatistic(killer); } } else { DamageTankData damager1 = (DamageTankData)((HashMap)lastDamagers).get(((HashMap)lastDamagers).keySet().toArray()[lastDamagers.size() - 1]); DamageTankData damager2 = (DamageTankData)((HashMap)lastDamagers).get(((HashMap)lastDamagers).keySet().toArray()[lastDamagers.size() - 2]); long currentTime = System.currentTimeMillis(); if (currentTime - damager1.timeDamage <= 10000L && currentTime - damager2.timeDamage <= 10000L) { int score1 = 0; int score2 = 0; if (damager1.damage > damager2.damage) { score1 = (int)(0.15 * (double)(100.0f / (controller.tank.getHull().hp / damager1.damage))); score2 = 15 - score1; } else if (damager2.damage > damager1.damage) { score2 = (int)(0.15 * (double)(100.0f / (controller.tank.getHull().hp / damager2.damage))); score1 = 15 - score2; } else { score1 = 7; score2 = 7; } score1 = Math.abs(score1); score2 = Math.abs(score2); if (this.bfModel.battleInfo.team) { damager1.damager.statistic.addScore(score1); damager2.damager.statistic.addScore(score2); this.bfModel.statistics.changeStatistic(damager1.damager); this.bfModel.statistics.changeStatistic(damager2.damager); } this.tanksServices.addScore(damager1.damager.parentLobby, score1); this.tanksServices.addScore(damager2.damager.parentLobby, score2); } else { killer.statistic.addScore(10); this.bfModel.statistics.changeStatistic(killer); this.tanksServices.addScore(killer.parentLobby, 10); } } } else { int killScore = 15; if (controller.flag != null) { killScore *= 2; } this.tanksServices.addScore(killer.parentLobby, killScore); } this.addFund(0.064 * (double)(killer.getUser().getRang() + 1) + 0.01); } if (this.battleInfo.numKills > 0 && killer.statistic.getKills() >= (long)this.battleInfo.numKills) { this.restartBattle(false); } if (controller.flag != null) { this.bfModel.ctfModel.dropFlag(controller, controller.tank.position); } } this.bfModel.statistics.changeStatistic(controller); this.bfModel.respawnPlayer(controller, false); controller.tank.lastDamagers.clear(); } public void restartBattle(boolean timeLimitFinish) { if (!timeLimitFinish && this.battleInfo.time > 0) { this.quartzService.deleteJob(this.bfModel.QUARTZ_NAME, BattlefieldModel.QUARTZ_GROUP); } this.calculatePrizes(); this.bfModel.battleFinish(); this.bfModel.sendToAllPlayers(Type.BATTLE, "battle_finish", JSONUtils.parseFishishBattle(this.bfModel.players, 10000)); this.quartzService.addJob(this.QUARTZ_NAME, QUARTZ_GROUP, e -> this.bfModel.battleRestart(), TimeType.MS, 10000L); } private void calculatePrizes() { if (this.bfModel == null || this.bfModel.players == null || this.bfModel.players.size() <= 0) { return; } ArrayList<BattlefieldPlayerController> users = new ArrayList<BattlefieldPlayerController>(this.bfModel.players.values()); if (!this.bfModel.battleInfo.team) { Collections.sort(users); BattlePrizeCalculate.calc(users, (int)this.battleFund); } else { ArrayList<BattlefieldPlayerController> redTeam = new ArrayList<BattlefieldPlayerController>(); ArrayList<BattlefieldPlayerController> blueTeam = new ArrayList<BattlefieldPlayerController>(); for (BattlefieldPlayerController player : users) { if (player.playerTeamType.equals("RED")) { redTeam.add(player); continue; } if (!player.playerTeamType.equals("BLUE")) continue; blueTeam.add(player); } BattlePrizeCalculate.calculateForTeam(redTeam, blueTeam, this.bfModel.battleInfo.scoreRed, this.bfModel.battleInfo.scoreBlue, 0.25, (int)this.battleFund); } } public void addFund(double value) { this.battleFund += value; this.bfModel.sendToAllPlayers(Type.BATTLE, "change_fund", String.valueOf((int)this.battleFund)); this.bfModel.bonusesSpawnService.updatedFund(); } public double getBattleFund() { return this.battleFund; } public void setBattleFund(int value) { this.battleFund = value; } @Override public void destroy() { this.quartzService.deleteJob(this.QUARTZ_NAME, QUARTZ_GROUP); } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |