//============================================================================== void GameServer::HandlePutItem_(const QVariantMap& request, QVariantMap& response) { if (!testingStageActive_) { WriteResult_(response, EFEMPResult::BAD_ACTION); return; } if (!request["x"].toFloat() || !request["y"].toFloat()) { WriteResult_(response, EFEMPResult::BAD_PLACING); return; } float x = request["x"].toFloat(); float y = request["y"].toFloat(); if (levelMap_.GetCell(x, y) != '.') { WriteResult_(response, EFEMPResult::BAD_PLACING); return; } Item* item = CreateActor_<Item>(); SetActorPosition_(item, Vector2(x, y)); SetItemDescription(request["item"].toMap(), item); response["id"] = item->GetId(); }
bool Filters::FilterText(const Item& item) const { std::vector<std::wstring> words; Split(text, L" ", words); RemoveEmptyStrings(words); std::vector<std::wstring> titles; GetAllTitles(item.GetId(), titles); const auto& genres = item.GetGenres(); for (const auto& word : words) { auto check_strings = [&word](const std::vector<std::wstring>& v) { for (const auto& str : v) { if (InStr(str, word, 0, true) > -1) return true; } return false; }; if (!check_strings(titles) && !check_strings(genres) && InStr(item.GetMyTags(), word, 0, true) == -1) return false; } return true; }
//============================================================================== void GameServer::GetItems(Creature* actor) { if (actor->GetType() == EActorType::PLAYER) { for(Item* item: dynamic_cast<Player*>(actor)->items_) { item->SetPosition(actor->GetPosition()); actors_.push_back(item); } } else { Item* item = CreateActor_<Item>(); SetActorPosition_(item, actor->GetPosition()); //storage_.GetItem(item, 22); storage_.GetItem(item, (item->GetId() % 31) + 1); } }
//============================================================================== void GameServer::HandlePutPlayer_(const QVariantMap& request, QVariantMap& response) { if (!testingStageActive_) { WriteResult_(response, EFEMPResult::BAD_ACTION); return; } if (!request["x"].toFloat() || !request["y"].toFloat()) { WriteResult_(response, EFEMPResult::BAD_PLACING); return; } float x = request["x"].toFloat(); float y = request["y"].toFloat(); Player* player = CreateActor_<Player>(); SetActorPosition_(player, Vector2(x, y)); if (IsPositionWrong(x, y, player)) { KillActor_(player); WriteResult_(response, EFEMPResult::BAD_PLACING); return; } auto inventory = request["inventory"].toList(); for (auto elem: inventory) { Item* item = CreateActor_<Item>(); SetItemDescription(elem.toMap(), item); item->SetOnTheGround(false); player->items_.push_back(item); actors_.erase(std::remove(actors_.begin(), actors_.end(), item), actors_.end()); //idToActor_.erase(item->GetId()); } auto stats = request["stats"].toMap(); for (auto s = stats.begin(); s != stats.end(); s++) { EStatConst stat = StringToStat[s.key()]; QVariant val = s.value(); player->SetStat(stat, val.toFloat()); } if (stats.size() == 0) { player->SetSpeed(playerVelocity_); } QVariantList items; for (auto& elem: player->items_) { QVariantMap item; item["id"] = elem->GetId(); item["name"] = elem->Getname(); item["type"] = elem->GetTypeItem(); item["class"] = elem->GetClass(); item["subtype"] = elem->GetSubtype(); item["weight"] = elem->GetWeight(); items << item; } response["inventory"] = items; auto slot = request["slots"].toMap(); QVariantMap id_slot; for (auto i = SlotToString.begin(); i != SlotToString.end(); i++) { if (slot.find(i.key()) != slot.end()) { Item* item = CreateActor_<Item>(); SetItemDescription(slot[i.key()].toMap(), item); player->items_.push_back(item); player->SetSlot(i.value(), item); player->SetStat(true, item); id_slot[i.key()] = item->GetId(); } } response["slots"] = id_slot; response["id"] = player->GetId(); QByteArray sid; do { QByteArray id = QString::number(qrand()).toLatin1(); sid = QCryptographicHash::hash(id, QCryptographicHash::Sha1); } while (sidToPlayer_.find(sid) != sidToPlayer_.end()); sid = sid.toHex(); sidToPlayer_.insert(sid, player); response["sid"] = sid; response["fistId"] = FistId_; }
//============================================================================== void GameServer::HandleEquip_(const QVariantMap& request, QVariantMap& response) { #define BAD_ID(COND)\ if (COND)\ {\ WriteResult_(response, EFEMPResult::BAD_ID);\ return;\ }\ BAD_ID(request.find("id") == request.end()); BAD_ID(!request["id"].toInt()); QString slot = request["slot"].toString(); if (SlotToString.find(slot) == SlotToString.end()) { WriteResult_(response, EFEMPResult::BAD_SLOT); return; } int id = request["id"].toInt(); auto sid = request["sid"].toByteArray(); Player* p = sidToPlayer_[sid]; BAD_ID((idToActor_.find(id) == idToActor_.end()) && !p->GetItemId(id)); if (p->GetItemId(id)) { //equip item from inventory for (auto& item: p->items_) { if (item->GetId() == id) { Item* i = p->GetSlot(SlotToString[slot]); if (i) { p->items_.push_back(i); } if (!p->SetSlot(SlotToString[slot], item)) { WriteResult_(response, EFEMPResult::BAD_SLOT); return; } p->SetStat(true, item); p->items_.erase(std::remove(p->items_.begin(), p->items_.end(), item), p->items_.end()); WriteResult_(response, EFEMPResult::OK); return; } } BAD_ID(true); } else { //item is on the ground Item* item = dynamic_cast<Item*>(idToActor_[id]); BAD_ID(!item); Vector2 player_pos = p->GetPosition(); Vector2 item_pos = item->GetPosition(); float distance2 = Sqr(player_pos.x - item_pos.x) + Sqr(player_pos.y - item_pos.y); BAD_ID(distance2 > Sqr(pickUpRadius_)) if (!p->SetSlot(SlotToString[slot], item)) { WriteResult_(response, EFEMPResult::BAD_SLOT); return; } p->SetStat(true, item); //KillActor_(item); ??? idToActor_.erase(item->GetId()); actors_.erase(std::remove(actors_.begin(), actors_.end(), item), actors_.end()); } #undef BAD_ID }
double StructuralStreamDriftDetector::TreeAutomaticThreshold( const ItemMap<unsigned>* lhs_frequency_table, const ItemMap<unsigned>* rhs_frequency_table) const { uint32_t path_count = 0; ItemMapCmp<unsigned> cmp_lhs(*lhs_frequency_table); ItemMapCmp<unsigned> cmp_rhs(*rhs_frequency_table); //ASSERT(cmp_lhs.freq.valid.size > cmp_rhs.freq.valid.size); // declare left and right list of item to be sorted and compare the index vector<Item> leftItems; vector<Item> rightItems; ItemMap<unsigned>::Iterator leftIter = lhs_frequency_table->GetIterator(); ItemMap<unsigned>::Iterator rightIter = rhs_frequency_table->GetIterator(); TreePathIterator itr(tree.get()); vector<Item> path; unsigned ignored; while (itr.GetNext(path, ignored)) { ++path_count; } //push item to a vector of leftItems uint32_t n_value = 0; uint32_t maxLeftItempID = 0; while (leftIter.HasNext()) { n_value++; Item leftItem = leftIter.GetKey(); if (maxLeftItempID <= leftItem.GetId()) { maxLeftItempID = leftItem.GetId(); } leftItems.push_back(leftItem); leftIter.Next(); } //push item to a vector of rightItems while (rightIter.HasNext()) { Item rightItem = rightIter.GetKey(); //Only add the the compare list if item appears in both trees if (lhs_frequency_table->Contains(rightItem)) { rightItems.push_back(rightItem); } rightIter.Next(); } // sort left and right array based on cmp_lhs and cmp_rhs sort(leftItems.begin(), leftItems.end(), cmp_lhs); sort(rightItems.begin(), rightItems.end(), cmp_rhs); // create ranking array and initialise them (only compared with left item list) vector<float> leftRankedFloat(leftItems.size()); vector<float> rightRankedFloat(leftItems.size()); // initialise float ranking, it starts with 1.0 2.0 3.0 4.0 5.0 .... for (int i = 0; i < leftItems.size(); ++i) { leftRankedFloat[i] = i + 1; rightRankedFloat[i] = i + 1; } // calculate the float ranking, it may end up with sthing like 1.0 2.0 2.5 2.5 5.0 ... sortAVGRanking(leftItems, leftRankedFloat, cmp_lhs) ; sortAVGRanking(rightItems, rightRankedFloat, cmp_rhs) ; // create ranking array and initialise them all to zero vector<float> leftItemsWithRanks(maxLeftItempID + 1); vector<float> rightItemsWithRanks(maxLeftItempID + 1); for (int i = 0; i < maxLeftItempID + 1; ++i) { leftItemsWithRanks[i] = 0; rightItemsWithRanks[i] = 0; } for (int i = 0; i < maxLeftItempID; ++i) { Item leftItem = leftItems.at(i); int mIDL = leftItem.GetId(); leftItemsWithRanks[mIDL] = leftRankedFloat[i]; Item rightItem = rightItems.at(i); int mIDR = rightItem.GetId(); rightItemsWithRanks[mIDR] = rightRankedFloat[i]; } // For calculating rho and e_cut double totalRankDifference = 0; for (int i = 1; i < maxLeftItempID + 1; ++i) { totalRankDifference = totalRankDifference + (leftItemsWithRanks[i] - rightItemsWithRanks[i]) * (leftItemsWithRanks[i] - rightItemsWithRanks[i]); } double alpha = 0.01; double rho = 0.0; double z = 2.24; if (alpha == 0.01) z = 2.24; if (alpha == 0.005) z = 2.41; if (alpha == 0.1) z = 1.29; if(alpha == 0.25) z = 0.69; if (alpha == 0.001) z = 2.96; if (alpha == 0.05) z = 1.64; rho = z/sqrt(double(n_value - 1)); //double rho = (6.0 * totalRankDifference / (n_value * (n_value * n_value - 1))); // e_cut = ((1-rho)(n-1)) / (6.|P|) // |P| = path_count double e_cut = (1.0 - rho) * (n_value * n_value - 1) / (6 * n_value * path_count); // ASSERT(e_cut >= 0); //Log(" Spearman's rank correlation coefficient rho = %f similarity (0->1)\n", (1 - rho)); //Log(" Statistically calculated e_cut = %f\n", fabs(e_cut)); return fabs(e_cut); }
TEST(StructuralStreamDriftDetector, Spearman) { Item::ResetBaseId(); vector<Item> testV1(ToItemVector("0,1,2,3,4,5")); vector<Item> testV2(ToItemVector("0,1,2,3,4,5")); int maxLeftItempID = 6; EXPECT_EQ(edit_distance(testV1, testV2), 0); ItemMap<unsigned>* tst_lhs_frequency_table = new ItemMap<unsigned>(); tst_lhs_frequency_table->Set(Item("0"), 1); tst_lhs_frequency_table->Set(Item("1"), 5); tst_lhs_frequency_table->Set(Item("2"), 6); tst_lhs_frequency_table->Set(Item("3"), 5); tst_lhs_frequency_table->Set(Item("4"), 5); tst_lhs_frequency_table->Set(Item("5"), 6); ItemMap<unsigned>* tst_rhs_frequency_table = new ItemMap<unsigned>(); tst_rhs_frequency_table->Set(Item("0"), 1); tst_rhs_frequency_table->Set(Item("1"), 9); tst_rhs_frequency_table->Set(Item("2"), 11); tst_rhs_frequency_table->Set(Item("3"), 8); tst_rhs_frequency_table->Set(Item("4"), 8); tst_rhs_frequency_table->Set(Item("5"), 10); ItemMapCmp<unsigned> cmp_lhs(*tst_lhs_frequency_table); ItemMapCmp<unsigned> cmp_rhs(*tst_rhs_frequency_table); std::sort(testV1.begin(), testV1.end(), cmp_lhs); std::sort(testV2.begin(), testV2.end(), cmp_rhs); vector<float> leftRankedFloat(testV1.size()); vector<float> rightRankedFloat(testV2.size()); for (int i = 0; i < testV1.size(); i++) { leftRankedFloat[i] = i + 1; rightRankedFloat[i] = i + 1; } sortAVGRanking(testV1, leftRankedFloat, cmp_lhs); sortAVGRanking(testV2, rightRankedFloat, cmp_rhs); // create ranking array and initialise them all to zero vector<float> leftItemsWithRanks(maxLeftItempID + 1); vector<float> rightItemsWithRanks(maxLeftItempID + 1); for (int i = 0; i < maxLeftItempID + 1; ++i) { leftItemsWithRanks[i] = 0; rightItemsWithRanks[i] = 0; } for (int i = 0; i < maxLeftItempID; ++i) { Item leftItem = testV1.at(i); int mIDL = leftItem.GetId(); leftItemsWithRanks[mIDL] = leftRankedFloat[i]; Item rightItem = testV2.at(i); int mIDR = rightItem.GetId(); rightItemsWithRanks[mIDR] = rightRankedFloat[i]; } for (int i = 1; i < maxLeftItempID + 1; ++i) { //Item item1 = testV1.at(i); printf("Left Item %d is ranked at %f \n", i, leftItemsWithRanks[i]); } printf("----\n"); for (int i = 1; i < maxLeftItempID + 1; ++i) { //Item item2 = testV2.at(i); printf("Right Item %d is ranked at %f \n", i, rightItemsWithRanks[i]); } // For calculating rho double totalRankDifference = 0; for (int i = 1; i < maxLeftItempID + 1; ++i) { totalRankDifference = totalRankDifference + (leftItemsWithRanks[i] - rightItemsWithRanks[i]) * (leftItemsWithRanks[i] - rightItemsWithRanks[i]); } #ifdef DEBUG int counter = 6; double rho = 1.0 - (6.0 * totalRankDifference / (counter * (counter * counter - 1))); //calculated in excel by hand, RHO = 0.942857 ASSERT(abs(rho - 0.942857) < 0.001); #endif }