Skip to content

Commit

Permalink
fixed the resolution of the battle simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pismice committed Jul 15, 2024
1 parent 410c0a0 commit 05c1478
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/entities/battle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub fn persist(self: *Battle, db: *sqlite.Db) !void {
);
}

// TODO bizzare d avoir mis self en const
pub fn resolve(self: *Battle, db: *sqlite.Db, allocator: std.mem.Allocator) !void {
var attacking_army = try Army.initArmyById(db, allocator, self.army_attacker_id);
var defending_army = try Army.initArmyById(db, allocator, self.army_defender_id);
Expand All @@ -75,14 +74,16 @@ pub fn resolve(self: *Battle, db: *sqlite.Db, allocator: std.mem.Allocator) !voi
// Check if the army was defending a village
const eventual_defending_place = try defending_army.getDefendingPlace(db, allocator);
if (eventual_defending_place) |defending_village| {
// Remove golds from the defending village
self.gold_stolen = defending_village.gold;
defending_village.name = "JE SUIS PAUVRE";
defending_village.gold = 0;
try defending_village.persist(db);
var attacker_village = try Village.initVillageByPlayerId(db, allocator, defending_village.player_id);
attacker_village.name = "JE SUIS RICHE";

// Give those golds to the attacker
var attacker_village = try Village.initVillageByPlayerId(db, allocator, attacking_army.player_id);
attacker_village.gold += self.gold_stolen;
try attacker_village.persist(db);

// Troops bilan
self.defender_lost_units = defending_army.nb_cavalry + defending_army.nb_infantry + defending_army.nb_ranged;
try defending_army.destory(db);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ fn eventsPolling(db: *sqlite.Db) !void {
const allocator = arena.allocator();
const allBattles = try Battle.getAllBattlesInOrder(db, allocator);
var testTODO = try Village.initVillageById(db, allocator, 31);
testTODO.name = "test";
try testTODO.persist(db);
for (allBattles) |*battle| {
// If the battle is over
if (battle.duration + battle.time_start < std.time.timestamp()) {
if (battle.resolved == false) {
// Resolve the battle
try battle.resolve(db, allocator); // FIXME cant do because somehow battle is const
try battle.resolve(db, allocator);
}
} else {
break; // this battle is not over so the next ones wont be either
Expand Down

0 comments on commit 05c1478

Please sign in to comment.