|
| 1 | +const Service = require("moleculer").Service; |
| 2 | +const DbService = require("moleculer-db"); |
| 3 | +const { ValidationError } = require("moleculer").Errors; |
| 4 | +const _ = require('lodash'); |
| 5 | +const getDbAdapterConfig = require("../lib/getDbAdapterConfig.js"); |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +class LeagueService extends Service { |
| 10 | + |
| 11 | + constructor(broker) { |
| 12 | + super(broker); |
| 13 | + |
| 14 | + let adapterConfig = getDbAdapterConfig(broker.serviceConfig.data, 'league') |
| 15 | + this.parseServiceSchema({ |
| 16 | + ...adapterConfig, |
| 17 | + name: "league", |
| 18 | + mixins: [DbService], |
| 19 | + settings: { |
| 20 | + idField: 'id', |
| 21 | + fields: [ |
| 22 | + "id", |
| 23 | + "joinedAt", |
| 24 | + "ownerId", |
| 25 | + "ownerName", |
| 26 | + "scriptId", |
| 27 | + "scriptName", |
| 28 | + "rank", |
| 29 | + "fights_total", |
| 30 | + "fights_win", |
| 31 | + "fights_lose", |
| 32 | + "fights_error", |
| 33 | + "score", |
| 34 | + "code" |
| 35 | + ] |
| 36 | + }, |
| 37 | + entityValidator: { |
| 38 | + joinedAt: "date", |
| 39 | + ownerId: "string", |
| 40 | + ownerName: { type: "string", min: 1, max: 255 }, |
| 41 | + scriptId: "string", |
| 42 | + scriptName: { type: "string", min: 1, max: 255 }, |
| 43 | + rank: "number", |
| 44 | + fights_total: "number", |
| 45 | + fights_win: "number", |
| 46 | + fights_lose: "number", |
| 47 | + fights_error: "number", |
| 48 | + score: "number", |
| 49 | + code: { type: "string", min: 0, max: 65536 }, |
| 50 | + }, |
| 51 | + dependencies: ['scriptStore'], |
| 52 | + actions: { |
| 53 | + seedLeague: this.seedLeague, |
| 54 | + getUserSubmission: this.getUserSubmission, |
| 55 | + joinLeague: this.joinLeague, |
| 56 | + leaveLeague: this.leaveLeague, |
| 57 | + getLeagueSummary: this.getLeagueSummary |
| 58 | + }, |
| 59 | + hooks: { |
| 60 | + before: { |
| 61 | + create: [ |
| 62 | + function addDefaults(ctx) { |
| 63 | + ctx.params.joinedAt = new Date(); |
| 64 | + ctx.params.rank = 0; |
| 65 | + ctx.params.fights_total = 0; |
| 66 | + ctx.params.fights_win = 0; |
| 67 | + ctx.params.fights_lose = 0; |
| 68 | + ctx.params.fights_error = 0; |
| 69 | + ctx.params.score = 0; |
| 70 | + ctx.params = _.omit(ctx.params, ['id']); |
| 71 | + return ctx; |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + }, |
| 76 | + events: { |
| 77 | + "app.seed": async (ctx) => { |
| 78 | + await ctx.call('league.seedLeague', {}) |
| 79 | + } |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + async seedLeague(ctx) { |
| 85 | + const seedPath = path.resolve(__dirname, 'league', 'seed'); |
| 86 | + const seedFiles = fs.readdirSync(seedPath) |
| 87 | + .map((filename) => ({ |
| 88 | + ownerId: 0, |
| 89 | + ownerName: 'jsbattle', |
| 90 | + scriptId: 0, |
| 91 | + scriptName: filename.replace(/\.tank$/, ''), |
| 92 | + code: fs.readFileSync(path.resolve(seedPath, filename), 'utf8') |
| 93 | + })) |
| 94 | + .map((entry) => new Promise(async (resolve) => { |
| 95 | + let existingEntry = await ctx.call('league.find', { |
| 96 | + query: { |
| 97 | + ownerName: 'jsbattle', |
| 98 | + scriptName: entry.scriptName, |
| 99 | + } |
| 100 | + }); |
| 101 | + if(existingEntry.length == 0) { |
| 102 | + await ctx.call('league.create', entry); |
| 103 | + } |
| 104 | + resolve(); |
| 105 | + })); |
| 106 | + |
| 107 | + await Promise.all(seedFiles); |
| 108 | + } |
| 109 | + |
| 110 | + async getUserSubmission(ctx) { |
| 111 | + const userId = ctx.meta.user ? ctx.meta.user.id : null; |
| 112 | + if(!userId) { |
| 113 | + throw new ValidationError('Not Authorized!', 401); |
| 114 | + } |
| 115 | + |
| 116 | + let response = await ctx.call('league.find', { |
| 117 | + query: { |
| 118 | + ownerId: userId |
| 119 | + }, |
| 120 | + limit: 1 |
| 121 | + }); |
| 122 | + |
| 123 | + if(response.length === 0) { |
| 124 | + return {} |
| 125 | + } |
| 126 | + return response[0] |
| 127 | + } |
| 128 | + |
| 129 | + async joinLeague(ctx) { |
| 130 | + const userId = ctx.meta.user ? ctx.meta.user.id : null; |
| 131 | + if(!userId) { |
| 132 | + throw new ValidationError('Not Authorized!', 401); |
| 133 | + } |
| 134 | + |
| 135 | + let script = await ctx.call('scriptStore.getUserScript', { id: ctx.params.scriptId }); |
| 136 | + if(script.ownerId != userId) { |
| 137 | + throw new ValidationError('Not Authorized!', 401); |
| 138 | + } |
| 139 | + |
| 140 | + await this.leaveLeague(ctx); |
| 141 | + |
| 142 | + await ctx.call('league.create', { |
| 143 | + ownerId: script.ownerId, |
| 144 | + ownerName: script.ownerName, |
| 145 | + scriptId: script.id, |
| 146 | + scriptName: script.scriptName, |
| 147 | + code: script.code |
| 148 | + }); |
| 149 | + |
| 150 | + return this.getLeagueSummary(ctx); |
| 151 | + } |
| 152 | + |
| 153 | + async leaveLeague(ctx) { |
| 154 | + const userId = ctx.meta.user ? ctx.meta.user.id : null; |
| 155 | + if(!userId) { |
| 156 | + throw new ValidationError('Not Authorized!', 401); |
| 157 | + } |
| 158 | + |
| 159 | + let submissions = await ctx.call('league.find', { |
| 160 | + query: { |
| 161 | + ownerId: userId |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + let removals = submissions.map((submission) => ctx.call('league.remove', { |
| 166 | + id: submission.id |
| 167 | + })); |
| 168 | + |
| 169 | + await Promise.all(removals); |
| 170 | + |
| 171 | + return this.getLeagueSummary(ctx); |
| 172 | + } |
| 173 | + |
| 174 | + async getLeagueSummary(ctx) { |
| 175 | + const userId = ctx.meta.user ? ctx.meta.user.id : null; |
| 176 | + if(!userId) { |
| 177 | + throw new ValidationError('Not Authorized!', 401); |
| 178 | + } |
| 179 | + |
| 180 | + let ranktable = await ctx.call('league.find', { |
| 181 | + sort: 'rank' |
| 182 | + }); |
| 183 | + ranktable = ranktable.map((item) => _.pick(item, [ |
| 184 | + "rank", |
| 185 | + "ownerId", |
| 186 | + "ownerName", |
| 187 | + "scriptId", |
| 188 | + "scriptName", |
| 189 | + "joinedAt", |
| 190 | + "fights_total", |
| 191 | + "fights_win", |
| 192 | + "fights_lose", |
| 193 | + "fights_error", |
| 194 | + "score" |
| 195 | + ])); |
| 196 | + |
| 197 | + let submission = await this.getUserSubmission(ctx) |
| 198 | + submission = _.pick(submission, [ |
| 199 | + "rank", |
| 200 | + "ownerId", |
| 201 | + "ownerName", |
| 202 | + "scriptId", |
| 203 | + "scriptName", |
| 204 | + "joinedAt", |
| 205 | + "fights_total", |
| 206 | + "fights_win", |
| 207 | + "fights_lose", |
| 208 | + "fights_error", |
| 209 | + "score" |
| 210 | + ]); |
| 211 | + |
| 212 | + return { |
| 213 | + submission, |
| 214 | + ranktable |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | +} |
| 219 | + |
| 220 | +module.exports = LeagueService; |
0 commit comments