Skip to content

Commit

Permalink
Implemented /api/yggdrasil/api/profiles/minecraft, fixed #10
Browse files Browse the repository at this point in the history
  • Loading branch information
daidr committed Mar 7, 2021
1 parent 223bd67 commit 4d53d3e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
45 changes: 45 additions & 0 deletions controller/api/yggdrasil.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,51 @@ module.exports = {
}
ctx.body = data;
},
api: {
profiles: {
minecraft: async (ctx, next) => {
const data = ctx.request.body;

// 校验是否传入了数组
if (!data instanceof Array) {
ctx.set("Content-Type", "application/json");
ctx.body = [];
return;
}

// 去除重复及无效数据
data.filter(function (item, index, arr) {
return arr.indexOf(item, 0) === index && typeof item == "string";
});

// 一次最多查询3个玩家的数据
if (data.length >= 3) {
ctx.set("Content-Type", "application/json");
ctx.body = [];
return;
}

let playerList = [];

// 遍历玩家列表
for (let i = 0; i < data.length; i++) {

// 搜索玩家信息
let userData = await suser.searchUserInfoByPlayerName(data[i]).then(result => { return result; });

// 如果存在则生成玩家Profile,并加入到playerList
// 无需提供详细角色属性,第二个参数设置为false
if (userData) {
playerList.push(suser.genUserProfile(userData, false));
}
}

// 返回数据
ctx.set("Content-Type", "application/json");
ctx.body = playerList;
}
}
},
authserver: {
authenticate: async (ctx, next) => {
const data = ctx.request.body;
Expand Down
4 changes: 4 additions & 0 deletions routers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ router.get('/yggdrasil/sessionserver/session/minecraft/hasJoined', yggdrasilCont
// Yggdrasil角色查询接口
router.get('/yggdrasil/sessionserver/session/minecraft/profile/:uuid', yggdrasilController.sessionserver.session.minecraft.profile);

// POST ./api/yggdrasil/api/profiles/minecraft
// Yggdrasil按名称批量查询角色接口
router.post('/yggdrasil/api/profiles/minecraft', yggdrasilController.api.profiles.minecraft);

/* ---------- ROUTES END ---------- */

module.exports = router;
64 changes: 43 additions & 21 deletions service/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ module.exports = {
});
})
},
searchUserInfoByPlayerName: (playername) => {
return new Promise((resolve, reject) => {
User.findOne({ 'playername': playername }, '', { lean: true }, function (err, user) {
if (!err && user) {
resolve(user);
} else {
resolve(false);
}
});
})
},
letUserLoggedIn: (ctx, email) => {
return new Promise((resolve, reject) => {
User.findOne({ 'email': email }, 'email playername password uuid tokens skin isBanned ip time', function (err, user) {
Expand Down Expand Up @@ -256,32 +267,43 @@ module.exports = {
});
})
},
genUserProfile: (userData) => {
let textureData = {
timestamp: Date.now(),
profileId: userData.uuid.replace(/-/g, ""),
profileName: userData.playername,
textures: {
SKIN: {
url: `${config.common.url}/textures/${userData.skin.hash}`,
metadata: {
model: userData.skin.type == 0 ? "default" : "slim"
genUserProfile: (userData, isPropertiesContained = true) => {
if (isPropertiesContained) {
let textureData = {
timestamp: Date.now(),
profileId: userData.uuid.replace(/-/g, ""),
profileName: userData.playername,
textures: {
SKIN: {
url: `${config.common.url}/textures/${userData.skin.hash}`,
metadata: {
model: userData.skin.type == 0 ? "default" : "slim"
}
}
}
}
textureData = Buffer.from(JSON.stringify(textureData)).toString("base64");
}
textureData = Buffer.from(JSON.stringify(textureData)).toString("base64");
let data = {
id: userData.uuid.replace(/-/g, ""),
name: userData.playername,
properties: [
{
"name": "textures",
"value": textureData,
"signature": utils.genSignedData(textureData)
}
]
let data;
if (isPropertiesContained) {
data = {
id: userData.uuid.replace(/-/g, ""),
name: userData.playername,
properties: [
{
"name": "textures",
"value": textureData,
"signature": utils.genSignedData(textureData)
}
]
}
} else {
data = {
id: userData.uuid.replace(/-/g, ""),
name: userData.playername
}
}

return data;
},
genUserList: (filter, currectPage, pageSize) => {
Expand Down

0 comments on commit 4d53d3e

Please sign in to comment.