diff --git a/docs/ErrorCodes.pod b/docs/ErrorCodes.pod index d74a3fe1..cceb0940 100644 --- a/docs/ErrorCodes.pod +++ b/docs/ErrorCodes.pod @@ -66,5 +66,15 @@ A captcha was required for this request and it was not valid. The data portion w The function you were attempting to access is not valid for sitter accounts. + +=head2 1100 Empire not founded. + +You're trying to log in to an empire that is part way through the creation process. The data portion will tell you the empire id so that they can update the species and found the empire. + +=head2 1101 Empire not founded, and you tried to create it, but had the wrong password. + +This message will only occur if a user tries to create an empire with a name that already exists, but has not yet been founded, and the user doesn't enter the same password as they did when they first created the empire. + + =cut diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index ee82ec1f..c8abc586 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -31,17 +31,31 @@ sub find { sub is_name_available { my ($self, $name) = @_; - Lacuna::Verify->new(content=>\$name, throws=>[1000,'Empire name not available.', 'name']) + $self->is_name_valid($name); + $self->is_name_unique($name); + return 1; +} + +sub is_name_valid { + my ($self, $name) = @_; + Lacuna::Verify->new(content=>\$name, throws=>[1000,'Empire name is invalid.', 'name']) ->length_lt(31) ->length_gt(2) ->not_empty ->no_padding ->no_restricted_chars - ->no_profanity - ->ok( !Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name=>$name})->count ); + ->no_profanity; return 1; } +sub is_name_unique { + my ($self, $name) = @_; + if (Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name=>$name})->count) { + confess [1000, 'Empire name is in use by another player.', 'name']; + } + return 1; +} + sub logout { my ($self, $session_id) = @_; $self->get_session($session_id)->end; @@ -58,7 +72,7 @@ sub login { confess [1002, 'Empire does not exist.', $name]; } if ($empire->stage eq 'new') { - confess [1010, "You can't log in to an empire that has not been founded."]; + confess [1100, "Your empire has not been completely created. You must complete it in order to play the game.", { empire_id => $empire->id } ]; } unless ($empire->is_password_valid($password)) { if ($password ne '' && $empire->sitter_password eq $password) { @@ -235,6 +249,24 @@ sub create { $params{password} = Lacuna::DB::Result::Empire->encrypt_password($account{password}); } + # verify username + eval { $self->is_name_unique($account{name}) }; + if ($@) { # maybe they're trying to finish an incomplete empire + my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name=>$account{name}})->next; + if (defined $empire) { + if ($empire->stage eq 'new') { + if ($empire->is_password_valid($account{password})) { + confess [1100, "Your empire has not been completely created. You must complete it in order to play the game.", { empire_id => $empire->id } ]; + } + else { + confess [1101, "Your empire has not been completed created, but you have also entered the wrong password."]; + } + } + } + } + $self->is_name_valid($account{name}); + $params{name} = $account{name}; + # verify email if (exists $account{email} && $account{email} ne '') { Lacuna::Verify->new(content=>\$account{email}, throws=>[1005,'The email address specified does not look valid.', 'email']) @@ -245,10 +277,6 @@ sub create { $params{email} = $account{email}; } - # verify username - $self->is_name_available($account{name}); - $params{name} = $account{name}; - # create account my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->new(\%params)->insert; Lacuna->cache->increment('empires_created', format_date(undef,'%F'), 1, 60 * 60 * 26); diff --git a/lib/Lacuna/Web/Facebook.pm b/lib/Lacuna/Web/Facebook.pm index 703069d7..90f1d1a5 100644 --- a/lib/Lacuna/Web/Facebook.pm +++ b/lib/Lacuna/Web/Facebook.pm @@ -32,12 +32,18 @@ sub www_postback { my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({facebook_uid => $user->{id} }, { rows => 1 })->single; my $uri = Lacuna->config->get('server_url'); - if (defined $empire) { + if (defined $empire && $empire->stage eq 'founded') { $empire->facebook_token($fb->access_token); $empire->update; $uri .= '#session_id=%s'; $uri = sprintf $uri, $empire->start_session({ api_key => 'facebook' })->id; } + elsif (defined $empire && $empire->stage ne 'founded') { + $empire->facebook_token($fb->access_token); + $empire->update; + $uri .= '#empire_id=%s'; + $uri = sprintf $uri, $empire->id; + } else { $uri .= '#facebook_uid=%s&facebook_token=%s&facebook_name=%s'; $uri = sprintf $uri, $user->{id}, $fb->access_token, $user->{name};