How to quickly write a stranger referral system

Source: Internet
Author: User

How to quickly write a stranger referral system

In social games, in addition to interacting with your friends, you often design a game session where strangers interact. The following two pictures are QQ margin and the people Farm Strangers recommended interface.

QQ Margin Stranger interface

Universal Farm Stranger Interface

So what does a stranger referral system usually do? The following is an example of the Strangers recommendation system for the people's farm to explain how to quickly build a stranger referral system, because the use of the boost::multi_index Library, the entire recommendation system code in the line around, very concise.

First, let's briefly introduce the rules of the Strangers recommendation system for the National Farm:

1. Similar levels

2. Urban Proximity

3. Gender Reverse

4. Active Users

An easy way to do this is to use the list to store all recent logged-in players ' data, to iterate over each player when querying, and filter. However, this approach is inefficient, and the number of players who need to traverse the average of ten players is:

10 * ( assume 100 ) * ( Span style= "Font-weight:bolder" > assume 50 Span style= "Font-weight:bolder" > City ) * 2 ( unisex half )

= 100000

Obviously simply list way query inefficient, so we have to do index, here we have to mention multi_index Library, The introduction to this library is only one sentence: multi_index is a container that simulates the relational database index structure ( the concept of Multi-indexing over the same collection of elements are borrowed from relational database terminology and allows for the SP Ecification of complex data structures in the spirit of multiply indexed relational tables where simple sets and maps is Not enough ), its internal implementation is equivalent to multiple std::map combination, but more efficient and more convenient than std::map combination.

Given the small value of the gender field, we don't have to index it. We index the rank, the city, and the last login time, consider a higher priority than the city, we decided to < , city > do composite index ( you heard wrong, Is the composite index in the relational database ). Based on the above considerations, the data structure of our stranger referral system is as follows:

struct STRANGER_INFO

{

pod_open_id_t open_id;

pod_city_t City;

uint32_t ts;

uint32_t level;

uint32_t gender;

};

typedef bmi::multi_index_container<

Stranger_info,

bmi::indexed_by<

Bmi::ordered_non_unique<bmi::tag<tag_level_city>,

Bmi::composite_key<stranger_info,

Bmi::member<stranger_info, uint32_t, &stranger_info::level>

Bmi::member<stranger_info, pod_city_t, &stranger_info::city> >

,

Bmi::ordered_non_unique<bmi::tag<tag_ts>, Bmi::member<stranger_info, uint32_t, &stranger_info::ts >,

Bmi::ordered_unique<bmi::tag<tag_open_id>, Bmi::member<stranger_info, pod_open_id_t, &stranger_ info::open_id> >

,

stranger_info_allocator_t

> stranger_info_db_t;

The code above defines two data structures stranger_info and stranger_info_db_t. Stranger_info describes the player's information, stranger_info_db_t looks more complex, in fact, it is very simple, it defines a stranger_info container, the container of Stranger_info structure open_id, The city, TS, and level fields are indexed.

With the index, the rules of the Stranger referral system are easily met:

1. Similar levels

2. Urban Proximity

Press tag_level_city to find the most matched player, then use the player as the benchmark, forward / back traversal, to find the recommended strangers.

3. Gender Reverse

In the tag_level_city traversal process, the gender is not satisfied with the filter out

4. Active Users

Periodically press Tag_ts to traverse from the beginning, removing the TS too small node.

Since then, the Stranger Referral system is introduced here, nearby is a stranger referral system implementation files. It is important to note that in the real system,Stranger_info has a handler field, which is equivalent to a subscript, the client requests the last handler passed over, the server from the Handler back to the data to achieve paging function.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to quickly write a stranger referral system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.