From a831d99fef3d11cbd7cea492efde3827dd5979af Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 30 Sep 2010 17:03:25 -0500 Subject: [PATCH] new invitation stuff --- docs/Empire.pod | 6 +- lib/Lacuna.pm | 2 +- lib/Lacuna/DB/Result/Empire.pm | 134 ++++++++++++++---- lib/Lacuna/DB/Result/Invite.pm | 1 + lib/Lacuna/RPC/Empire.pm | 11 +- var/messages/friend_joined.txt | 7 + .../thank_you_for_inviting_friends.txt | 4 +- var/upgrades/2.2500.sql | 1 + 8 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 var/messages/friend_joined.txt create mode 100644 var/upgrades/2.2500.sql diff --git a/docs/Empire.pod b/docs/Empire.pod index 64d6043b..ce03f770 100644 --- a/docs/Empire.pod +++ b/docs/Empire.pod @@ -117,6 +117,10 @@ A Facebook user id passed in through Lacuna's Facebook integration system. Optio A Facebook access token passed in through Lacuna's Facebook integration system. Optional, but required with the use of C. +=head4 invite_code + +A 36 character code that was sent to the user by a friend. It is usable only once, and will ensure that their friend gets a home planet that is in relatively close proximity to their home planet. Can be passed in here, or in C, whichever works best for the UI. + @@ -142,7 +146,7 @@ Your client's unique API key, identifiying it from all other clients. See L, whichever works best for the UI. diff --git a/lib/Lacuna.pm b/lib/Lacuna.pm index fc03aace..ed6a11bd 100644 --- a/lib/Lacuna.pm +++ b/lib/Lacuna.pm @@ -7,7 +7,7 @@ use Config::JSON; useall __PACKAGE__; -our $VERSION = 2.2400; +our $VERSION = 2.2500; my $config = Config::JSON->new('/data/Lacuna-Server/etc/lacuna.conf'); my $db = Lacuna::DB->connect($config->get('db/dsn'),$config->get('db/username'),$config->get('db/password')); diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 69c9303d..cbbf2013 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -250,14 +250,108 @@ sub encrypt_password { return Digest::SHA::sha256_base64($password); } + +sub attach_invite_code { + my ($self, $invite_code) = @_; + my $invites = Lacuna->db->resultset('Lacuna::DB::Result::Invite'); + if (defined $invite_code && $invite_code ne '') { + my $invite = $invites->search( + {code => $invite_code, invitee_id => undef }, + {rows => 1} + )->single; + if (defined $invite) { + $invite->invitee_id($self->id); + $invite->update; + Lacuna->cache->increment('friends_accepted', format_date(undef,'%F'), 1, 60 * 60 * 26); + my $inviter = $invite->inviter; + if (defined $inviter) { # they may have deleted + my $accepts = $invites->search({inviter_id => $invite->inviter_id, invitee_id => {'>' => 0}})->count; + if ($accepts == 3) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Crater',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a crater plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 4) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::RockyOutcrop',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a rocky outcropping plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 5) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Lake',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a lake plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 6) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Sand',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a sand plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 7) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Grove',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a grove of trees plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 8) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Lagoon',1); + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a lagoon plan'], + from => $self->lacuna_expanse_corp, + ); + } + elsif ($accepts == 10) { + for my $i (1..13) { + $inviter->home_planet->add_plan('Lacuna::DB::Result::Building::Permanent::Beach'.$i,1); + } + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'thank_you_for_inviting_friends.txt', + params => [$invite->email, $self->id, $self->name, $accepts, 'a complete set of beach plans'], + from => $self->lacuna_expanse_corp, + ); + } + else { + $inviter->send_predefined_message( + tags => ['Correspondence'], + filename => 'friend_joined.txt', + params => [$invite->email, $self->id, $self->name], + from => $self->lacuna_expanse_corp, + ); + } + } + } + } +} + sub found { - my ($self, $home_planet, $invite_code) = @_; + my ($self, $home_planet) = @_; # lock empire $self->update({stage=>'finding home planet'}); # found home planet - $home_planet ||= $self->find_home_planet($invite_code); + $home_planet ||= $self->find_home_planet; $self->tutorial_scratch($home_planet->name); $self->home_planet_id($home_planet->id); $self->stage('founded'); @@ -272,41 +366,19 @@ sub found { return Lacuna::Tutorial->new(empire=>$self)->start('explore_the_ui'); } + sub find_home_planet { - my ($self, $invite_code) = @_; + my ($self) = @_; my $planets = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body'); my %search = ( usable_as_starter => {'>', 0}, orbit => { between => [ $self->min_orbit, $self->max_orbit] }, ); - my $invite; - my $invites = Lacuna->db->resultset('Lacuna::DB::Result::Invite'); - if (defined $invite_code && $invite_code ne '') { - $invite = $invites->search( - {code => $invite_code, invitee_id => undef }, - {rows => 1} - )->single; - } - # determine search area + my $invite = Lacuna->db->resultset('Lacuna::DB::Result::Invite')->search({invitee_id => $self->id},{rows=>1})->single; if (defined $invite) { - $invite->invitee_id($self->id); - $invite->update; - Lacuna->cache->increment('friends_accepted', format_date(undef,'%F'), 1, 60 * 60 * 26); - my $inviter = $invite->inviter; - my $inviter_home = $inviter->home_planet; - if ($invites->search({inviter_id => $invite->inviter_id, invitee_id => {'>' => 0}})->count == 10) { # got 10 friends - for my $i (1..13) { - $inviter_home->add_plan('Lacuna::DB::Result::Building::Permanent::Beach'.$i,1); - } - $inviter->send_predefined_message( - tags => ['Correspondence'], - filename => 'thank_you_for_inviting_friends.txt', - from => $self->lacuna_expanse_corp, - ); - } - $search{zone} = $inviter_home->zone; + $search{zone} = $invite->zone; # other possible solution # (SQRT( POW(5-x,2) + POW(8-y,2) )) as distance # then order by distance @@ -341,6 +413,9 @@ sub find_home_planet { unless (defined $home_planet) { # unlock $self->update({stage => 'new'}); + if (defined $invite) { + $invite->update({invitee_id => undef}); + } confess [1002, 'Could not find a home planet. Try again in a few moments.']; } @@ -361,14 +436,15 @@ sub invite_friend { $invites->new({ inviter_id => $self->id, code => $code, + zone => $self->home_planet->zone, email => $email, })->insert; + Lacuna->cache->increment('friends_invited', format_date(undef,'%F'), 1, 60 * 60 * 26); my $message = sprintf "%s\n\nMy name in the game is %s. Use the code below when you register and you'll be placed near me.\n\n%s\n\n%s", $custom_message, $self->name, $code, Lacuna->config->get('server_url'); - Lacuna->cache->increment('friends_invited', format_date(undef,'%F'), 1, 60 * 60 * 26); Email::Stuff->from($self->email) ->to($email) ->subject('Come Play With Me') diff --git a/lib/Lacuna/DB/Result/Invite.pm b/lib/Lacuna/DB/Result/Invite.pm index 8601e163..382edaf3 100644 --- a/lib/Lacuna/DB/Result/Invite.pm +++ b/lib/Lacuna/DB/Result/Invite.pm @@ -7,6 +7,7 @@ extends 'Lacuna::DB::Result'; __PACKAGE__->table('invite'); __PACKAGE__->add_columns( inviter_id => { data_type => 'int', is_nullable => 0 }, + zone => { data_type => 'varchar', size => 16, is_nullable => 0 }, invitee_id => { data_type => 'int', is_nullable => 1 }, email => { data_type => 'varchar', size => 255, is_nullable => 0 }, code => { data_type => 'varchar', size => 36, is_nullable => 0 }, diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index c8abc586..ae76c311 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -221,7 +221,6 @@ sub reset_password { return { session_id => $empire->start_session({ api_key => $api_key, request => $plack_request })->id, status => $self->format_status($empire) }; } - sub create { my ($self, $plack_request, %account) = @_; my %params = ( @@ -280,6 +279,9 @@ sub create { # 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); + + # handle invitation + $self->attach_invite_code($account{invite_code}); return $empire->id; } @@ -315,7 +317,11 @@ sub found { unless ($empire->stage eq 'new') { confess [1010, "This empire cannot be founded again.", $empire_id]; } - my $welcome = $empire->found(undef, $invite_code); + + # handle invitation + $self->attach_invite_code($invite_code); + + my $welcome = $empire->found; return { session_id => $empire->start_session({ api_key => $api_key, request => $plack_request })->id, status => $self->format_status($empire), @@ -657,7 +663,6 @@ sub invite_friend { return { status => $self->format_status($empire) }; } - sub update_species { my ($self, $empire_id, $me) = @_; diff --git a/var/messages/friend_joined.txt b/var/messages/friend_joined.txt new file mode 100644 index 00000000..e36b61ab --- /dev/null +++ b/var/messages/friend_joined.txt @@ -0,0 +1,7 @@ +Friend Joined +~~~ +Your friend %s has just joined the Lacuna Expanse with an empire name of {Empire %s %s}. Thanks for inviting your friends to play. + +Warmest Regards, + +The Lacuna Expanse Team diff --git a/var/messages/thank_you_for_inviting_friends.txt b/var/messages/thank_you_for_inviting_friends.txt index 87f4b82c..dbe2c3de 100644 --- a/var/messages/thank_you_for_inviting_friends.txt +++ b/var/messages/thank_you_for_inviting_friends.txt @@ -1,6 +1,8 @@ Thank You ~~~ -You have invited many friends to the Lacuna Expanse, and 10 of them have taken you up on your invitation. To say thank you, we have just awarded you a complete set of beach plans on your home planet. You can use them to either decorate your planet, or trade them to other players for something you really want. Thanks again for being a valued player. +Your friend %s has just joined the Lacuna Expanse with an empire name of {Empire %s %s}. + +You have invited many friends to the Lacuna Expanse, and %s of them have taken you up on your invitation. To say thank you, we have just awarded you %s on your home planet. You can use this gift to decorate your planet, or trade it to other players for something you really want. Thanks again for being a valued player. Warmest Regards, diff --git a/var/upgrades/2.2500.sql b/var/upgrades/2.2500.sql new file mode 100644 index 00000000..9f7f065d --- /dev/null +++ b/var/upgrades/2.2500.sql @@ -0,0 +1 @@ +alter table invite add column `zone` varchar(16); -- 2.51.2