Example #1
0
 /**
  * creates a city and the belonging models (BuildingSlot, Resource, HumanResource)
  * saves the city in the map
  *
  * @param User   $user     The user to whom the city belongs
  * @param bool   $capital  Is the city a capital
  * @param int    $hex_id   the id of the hex on which the city is located
  * @param string $name     the name of the city
  */
 public static function create(array $attributes)
 {
     /** @var City $city */
     $city = parent::create($attributes);
     /** @var BuildingSlot $slot */
     $slot = BuildingSlot::create(['city_id' => $city->id]);
     Resource::create(['city_id' => $city->id]);
     HumanResource::create(['city_id' => $city->id]);
     $city->building_slot = $slot->id;
     $city->save();
     /** @var Building $wall */
     $wall = Building::create(['city_id' => $city->id, 'slot' => $slot->id, 'nation' => $attributes['nation'], 'type' => 9, 'finished_at' => Carbon::now()]);
     $slot->wall = $wall->id;
     $slot->save();
     /** @var Grid $hex */
     $hex = Grid::find($hex_id);
     $hex->update(['owner_id' => $attributes['owner'], 'city_id' => $city->id]);
     $hex->setNeighborsOwner($attributes['owner']);
 }
Example #2
0
 public function getArmyData(Request $request)
 {
     TaskController::checkTasks();
     $army = Army::where('id', $request->input('army_id'))->first()->toArray();
     $hex = Grid::find($army['current_hex_id']);
     // if there's a city on the current hex
     if ($hex->city > 0) {
         $city = City::find($hex->city);
         $army['city_nation'] = intval($city->nation);
         $army['city_name'] = $city->name;
     }
     $army['hex_type'] = $hex->type;
     $army['hex_owner'] = User::find($hex->owner)->name;
     if (Auth::user()->id === $army['user_id']) {
         return $army;
     } else {
         $units = ['unit1' => 0, 'unit2' => 0, 'unit3' => 0, 'unit4' => 0, 'unit5' => 0, 'unit6' => 0, 'unit7' => 0];
         $army = array_diff_key($army, $units);
         $user = User::find($army['user_id']);
         $army['army_owner'] = $user->name;
         $army['nation'] = intval($user->nation);
         return $army;
     }
 }