refactor end battle logic