Commit 0e905591 authored by nanahira's avatar nanahira

Merge branch 'database' into databasen

parents e142379a 3aa26a21
...@@ -76,8 +76,17 @@ class DataManager { ...@@ -76,8 +76,17 @@ class DataManager {
} }
async getRandomCloudReplay() { async getRandomCloudReplay() {
try { try {
const [minQuery, maxQuery] = await Promise.all(["min", "max"].map(minOrMax => this.db.createQueryBuilder()
.select(`${minOrMax}(id)`, "value")
.from(CloudReplay_1.CloudReplay, "replay")
.getRawOne()));
if (!minQuery || maxQuery) {
return null;
}
const targetId = Math.floor((maxQuery.value - minQuery.value) * Math.random()) + minQuery.value;
return await this.db.createQueryBuilder(CloudReplay_1.CloudReplay, "replay") return await this.db.createQueryBuilder(CloudReplay_1.CloudReplay, "replay")
.orderBy("rand()") .where("replay.id >= :targetId", { targetId })
.orderBy("replay.id", "ASC")
.limit(4) //there may be 4 players .limit(4) //there may be 4 players
.leftJoinAndSelect("replay.players", "player") .leftJoinAndSelect("replay.players", "player")
.getOne(); .getOne();
...@@ -331,7 +340,7 @@ class DataManager { ...@@ -331,7 +340,7 @@ class DataManager {
return user ? user.chatColor : null; return user ? user.chatColor : null;
} }
async setUserChatColor(key, color) { async setUserChatColor(key, color) {
const user = await this.getOrCreateUser(key); let user = await this.getOrCreateUser(key);
user.chatColor = color; user.chatColor = color;
return await this.saveUser(user); return await this.saveUser(user);
} }
......
...@@ -98,8 +98,18 @@ export class DataManager { ...@@ -98,8 +98,18 @@ export class DataManager {
async getRandomCloudReplay() { async getRandomCloudReplay() {
try { try {
const [minQuery, maxQuery] = await Promise.all(["min", "max"].map(minOrMax => this.db.createQueryBuilder()
.select(`${minOrMax}(id)`, "value")
.from(CloudReplay, "replay")
.getRawOne()
));
if(!minQuery || maxQuery) {
return null;
}
const targetId = Math.floor((maxQuery.value - minQuery.value) * Math.random()) + minQuery.value;
return await this.db.createQueryBuilder(CloudReplay, "replay") return await this.db.createQueryBuilder(CloudReplay, "replay")
.orderBy("rand()") .where("replay.id >= :targetId", {targetId})
.orderBy("replay.id", "ASC")
.limit(4) //there may be 4 players .limit(4) //there may be 4 players
.leftJoinAndSelect("replay.players", "player") .leftJoinAndSelect("replay.players", "player")
.getOne(); .getOne();
...@@ -355,7 +365,7 @@ export class DataManager { ...@@ -355,7 +365,7 @@ export class DataManager {
return user ? user.chatColor : null; return user ? user.chatColor : null;
} }
async setUserChatColor(key: string, color: string) { async setUserChatColor(key: string, color: string) {
const user = await this.getOrCreateUser(key); let user = await this.getOrCreateUser(key);
user.chatColor = color; user.chatColor = color;
return await this.saveUser(user); return await this.saveUser(user);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment