In what war was Ptolemy defeated?