Skip to content

Instantly share code, notes, and snippets.

@litvil
Created January 13, 2016 17:09
Show Gist options
  • Save litvil/53b149574ac90fadf171 to your computer and use it in GitHub Desktop.
Save litvil/53b149574ac90fadf171 to your computer and use it in GitHub Desktop.
class StartPackController < CommonPromoController
CONTROLLER_MANIFEST = {
:common_methods => { # common methods, that controller responds to
:on_first_request => :on_first_request,
:give_product => :give_start_pack_rewards,
:check_product => :check_product,
:to_client_data => :to_client_data
},
:commands => {},
:category => 'start_pack'
}
def on_first_request
if is_active?
update_info
end
self
end
def update_info
if self.data.size == 0
create_new_promotion
else
refresh_promotion
end
self
end
def create_new_promotion
info = {}
group = get_user_group
info['offer_time_end'] = @user.current_time.to_i + self.class.config['period_length'].to_i
info['offers'] = self.class.config['products_by_user_group'][group]
self.data = info
end
def get_user_group
ABTestController.get_user_group(@user, 'start_pack_test')
end
def refresh_promotion
return if self.data['finished']
old_info = self.data
if old_info['offer_time_end'] > 0 && old_info['offer_time_end'] < @user.current_time.to_i
old_info['finished'] = true
self.data = old_info
end
end
def check_product(product_id, product)
return false unless product['type'] == self.class.config['product_type']
return false if self.data.size == 0
raise DZError.new('Promotion has finished', "User id: #{@user.user_id}, level: #{@user.level}") if self.data['finished']
raise DZNotFoundError.new('Product', 'Product id', product_id) if product.nil?
raise DZError.new('Wrong product offer_key', "Product id: #{product_id}, Key: #{product['offer_key']}") unless product['offer_key'].present? && self.data['offers'].include?( product['offer_key'] )
end
# вызов идет с другого сервера, в отличии от того где может играть игрок
# так как колбек о покупке может идти с опозданием на час или два, не надо проверять даты для акции и тп вещи
# просто выдаем продукт если параметры сходятся
def give_start_pack_rewards(product_id, product, money, comment = nil, test_mode = false, data = nil)
return false unless product['type'] == self.class.config['product_type']
info = self.data
return false if info.size == 0
rewards = product['items'] || {}
if rewards.present?
response_name = 'check_balance'
case @user.last_client_type
when 'android'
response_name = 'check_android_balance'
when 'ios'
response_name = 'check_itunes_balance'
end
drop = {'items' => rewards}
@user.inventory_controller.without_storage_check {
@user.with_stubbed_random {
@user.reward_controller.with_reward({}, drop, 'items', {'transaction_type' => UserTransaction::START_PACK}){}
}
}
response_items = Calculations.read_rewards_by(drop, 'items')
@user.responses_controller.add_response(response_name, nil, response_items)
end
info['finished'] = true
self.data = info
true
end
def to_client_data
return nil unless self.data.present?
if check_category == :time
return nil
end
self.data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment