He takes advantage of the cloak of darkness to lure the giants (robbers, in other variants) to the castle to kill them. This type of narrative is classified as another tale type: AaTh 723*, "Hero Binds Midnight, Dawn, and Midday".
This narrative type is classified as another tale type: AaTh 723*, "Hero Binds Midnight, Dawn, and Midday", which involves a hero luring enemies to their doom.