UNPKG

@colyseus/redis-driver

Version:

<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly

77 lines (76 loc) 2.12 kB
// packages/drivers/redis-driver/src/Query.ts var Query = class { constructor(rooms, conditions) { this.order = /* @__PURE__ */ new Map(); this.conditions = conditions; this.rooms = rooms; } sort(options) { this.order.clear(); const fields = Object.entries(options); if (fields.length) { for (const [field, direction] of fields) { if (direction === 1 || direction === "asc" || direction === "ascending") { this.order.set(field, 1); } else { this.order.set(field, -1); } } } return this; } filterRooms(rooms) { const conditions = Object.entries(this.conditions); const withConditions = conditions.length > 0; return rooms.filter((room) => { if (withConditions) { for (let [field, value] of conditions) { if (room.hasOwnProperty(field)) { if (room[field] !== value) { return false; } } else if (room.metadata?.hasOwnProperty(field)) { if (room.metadata[field] !== value) { return false; } } else { return false; } } } return true; }); } sortRooms(rooms) { if (this.order.size) { rooms.sort((room1, room2) => { for (const [field, direction] of this.order) { const val1 = room1.hasOwnProperty(field) ? room1[field] : room1.metadata?.[field]; const val2 = room2.hasOwnProperty(field) ? room2[field] : room2.metadata?.[field]; if (direction === 1) { if (val1 > val2) return 1; if (val1 < val2) return -1; } else { if (val1 > val2) return -1; if (val1 < val2) return 1; } } return 0; }); } return rooms; } all() { return this.rooms.then((rooms) => { return this.sortRooms(this.filterRooms(rooms)); }); } then(resolve, reject) { return this.rooms.then((rooms) => { return this.sortRooms(this.filterRooms(rooms))[0]; }).then(resolve, reject); } }; export { Query };