Trying to search and sort a collection by comparing the objects to each other
I have this query:
$locs_with_units = DB::table('units')
->join('locations', 'locations.id', '=', 'units.location_id')
->join('castles', 'castles.id', '=', 'units.castle_id')
->join('unit_types', 'unit_types.id', '=', 'units.unit_type_id')
->select('units.location_id',
'units.previous_location_id',
'units.id as unit_id',
'units.castle_id',
'castles.guild_id',
'units.unit_type_id',
'units.current_health',
'unit_types.damage',
'unit_types.range')
->get();
echo response()->json($locs_with_units);
which results in some data like this:
[
{
"location_id": 1,
"previous_location_id": 1,
"unit_id": 1,
"castle_id": 1,
"guild_id": 1,
"unit_type_id": 1,
"current_health": 100,
"damage": 10,
"range": 1
},
{
"location_id": 2,
"previous_location_id": 2,
"unit_id": 2,
"castle_id": 2,
"guild_id": null,
"unit_type_id": 1,
"current_health": 100,
"damage": 10,
"range": 1
},
{
"location_id": 1,
"previous_location_id": 3,
"unit_id": 3,
"castle_id": 3,
"guild_id": 1,
"unit_type_id": 1,
"current_health": 100,
"damage": 10,
"range": 1
},
{
"location_id": 2,
"previous_location_id": 3,
"unit_id": 4,
"castle_id": 3,
"guild_id": 1,
"unit_type_id": 1,
"current_health": 100,
"damage": 10,
"range": 1
},
{
"location_id": 2,
"previous_location_id": 2,
"unit_id": 5,
"castle_id": 2,
"guild_id": null,
"unit_type_id": 1,
"current_health": 100,
"damage": 10,
"range": 1
}
]
I'm completely stuck as to how I can go about it, but in pseudo code I need to:
-
Find each
location_idwhere there are Units from differentcastle_idand differentguild_id(except in cases where bothguild_idarenull) -
Then group the units on that
location_idby "defenders" and "attackers".- Defenders will have
previous_location_idequal tolocation_idand attackers will not. - Any Units with a
previous_location_idthat's not equal to thelocation_id(an "attacker") BUT has the sameguild_idas any of the defenders should be considered a defender as well.
- Defenders will have
Once I have the defenders and attackers for a given location_id identified I'll apply the combat logic.
If there are no more attackers left after combat then it will end there and move on to the next location_id and repeat the above process again.
-
If there are no more defenders left and only one attacker
castle_idthen they will win ownership of thatlocation_id -
If there are no more defenders left and more than one attacker left of different
castle_idbut the sameguild_idthe ownership of thatlocation_idwill go to thecastle_idof whoever has the largest amount of units left. -
If there are no more defenders left and the remaining attackers have different
guild_idthen find the two largest groups of units from eachguild_idand apply combat logic to only those two groups, all others will be sent away from thelocation_id
Please or to participate in this conversation.