mirror of
https://github.com/Melledy/Nebula.git
synced 2026-09-20 01:29:54 +02:00
Initial Commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package emu.nebula.game.formation;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import emu.nebula.proto.Public.FormationInfo;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Entity(useDiscriminator = false)
|
||||
public class Formation {
|
||||
private int num;
|
||||
private int[] charIds;
|
||||
private int[] discIds;
|
||||
|
||||
@Deprecated
|
||||
public Formation() {
|
||||
|
||||
}
|
||||
|
||||
public Formation(int num) {
|
||||
this.num = num;
|
||||
this.charIds = new int[3];
|
||||
this.discIds = new int[6];
|
||||
}
|
||||
|
||||
public Formation(FormationInfo formation) {
|
||||
this.num = formation.getNumber();
|
||||
this.charIds = formation.getCharIds().toArray();
|
||||
this.discIds = formation.getDiscIds().toArray();
|
||||
}
|
||||
|
||||
public int getCharIdAt(int i) {
|
||||
if (i < 0 || i >= this.charIds.length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return this.charIds[i];
|
||||
}
|
||||
|
||||
public int getDiscIdAt(int i) {
|
||||
if (i < 0 || i >= this.discIds.length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return this.discIds[i];
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public FormationInfo toProto() {
|
||||
var proto = FormationInfo.newInstance()
|
||||
.setNumber(this.getNum())
|
||||
.addAllCharIds(this.getCharIds())
|
||||
.addAllDiscIds(this.getDiscIds());
|
||||
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package emu.nebula.game.formation;
|
||||
|
||||
import emu.nebula.game.player.PlayerManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import emu.nebula.GameConstants;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.proto.Public.FormationInfo;
|
||||
import emu.nebula.proto.Public.TowerFormation;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Entity(value = "formations", useDiscriminator = false)
|
||||
public class FormationManager extends PlayerManager implements GameDatabaseObject {
|
||||
@Id
|
||||
private int uid;
|
||||
|
||||
private Map<Integer, Formation> formations;
|
||||
|
||||
@Deprecated // Morphia only
|
||||
public FormationManager() {
|
||||
|
||||
}
|
||||
|
||||
public FormationManager(Player player) {
|
||||
super(player);
|
||||
this.uid = player.getUid();
|
||||
this.formations = new HashMap<>();
|
||||
|
||||
this.save();
|
||||
}
|
||||
|
||||
public Formation getFormationById(int num) {
|
||||
return this.formations.get(num);
|
||||
}
|
||||
|
||||
public boolean updateFormation(FormationInfo info) {
|
||||
// Sanity check
|
||||
if (info.getNumber() < 1 || info.getNumber() > GameConstants.MAX_FORMATIONS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// More sanity
|
||||
if (info.getCharIds().length() < 1 || info.getCharIds().length() > 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (info.getDiscIds().length() < 3 || info.getDiscIds().length() > 6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate formation to make sure we have all the chars and discs
|
||||
// TODO
|
||||
|
||||
// Create formation
|
||||
var formation = new Formation(info);
|
||||
|
||||
// Add to formations map
|
||||
this.formations.put(formation.getNum(), formation);
|
||||
|
||||
// Save to db
|
||||
Nebula.getGameDatabase().update(this, this.getPlayerUid(), "formations." + formation.getNum(), formation, true);
|
||||
|
||||
// Success
|
||||
return true;
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public TowerFormation toProto() {
|
||||
var proto = TowerFormation.newInstance();
|
||||
|
||||
return proto;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user