|
| 1 | +import { createObjectCsvWriter } from "csv-writer"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import { sortBy, sumBy } from "lodash"; |
| 4 | +import * as process from "process"; |
| 5 | +import Engine, { Booster, Command, Player, roundScorings } from "../../engine"; |
| 6 | +import { boosterNames } from "../../viewer/src/data/boosters"; |
| 7 | +import { advancedTechTileNames, baseTechTileNames } from "../../viewer/src/data/tech-tiles"; |
| 8 | +import { chartFactory, families } from "../../viewer/src/logic/chart-factory"; |
| 9 | +import { parsedMove } from "../../viewer/src/logic/recent"; |
| 10 | +import { Game, GameDocument, PlayerInfo } from "./game"; |
| 11 | +import { connectMongo, shouldReplay } from "./util"; |
| 12 | + |
| 13 | +const fam = families().filter((f) => f != "Final Scoring Conditions" && f != "Federations"); |
| 14 | + |
| 15 | +function getDetailStats(commonProps: any, data: Engine, pl: Player) { |
| 16 | + const newDetailRow = (round: any) => { |
| 17 | + const d = { |
| 18 | + round: round, |
| 19 | + }; |
| 20 | + Object.assign(d, commonProps); |
| 21 | + return d; |
| 22 | + }; |
| 23 | + |
| 24 | + const rows: any[] = []; |
| 25 | + |
| 26 | + for (let family of fam) { |
| 27 | + const f = chartFactory(family); |
| 28 | + const sources = f.sources(family); |
| 29 | + |
| 30 | + const details = f.newDetails(data, pl.player, sources, "except-final", family, false); |
| 31 | + for (let detail of details) { |
| 32 | + const dataPoints = detail.getDataPoints(); |
| 33 | + if (rows.length == 0) { |
| 34 | + for (let round = 0; round < dataPoints.length; round++) { |
| 35 | + rows.push(newDetailRow(round)); |
| 36 | + } |
| 37 | + rows.push(newDetailRow("total")); |
| 38 | + } |
| 39 | + const key = `${family} - ${detail.label}`; |
| 40 | + let last = 0; |
| 41 | + dataPoints.forEach((value, index) => { |
| 42 | + rows[index][key] = value - last; |
| 43 | + last = value; |
| 44 | + }); |
| 45 | + rows[dataPoints.length][key] = dataPoints[dataPoints.length - 1]; |
| 46 | + } |
| 47 | + } |
| 48 | + return rows; |
| 49 | +} |
| 50 | + |
| 51 | +function getGameStats(pl: Player, outerPlayer: PlayerInfo<any>, data: Engine, game: GameDocument, commonProps: any) { |
| 52 | + const playerProp = (key: string, def: any) => (key in outerPlayer ? outerPlayer[key] ?? def : def); |
| 53 | + const rank = sortBy(data.players, (p: Player) => -p.data.victoryPoints); |
| 54 | + const rankWithoutBid = sortBy(data.players, (p: Player) => -(p.data.victoryPoints + (p.data.bid ?? 0))); // bid is positive |
| 55 | + |
| 56 | + const row = { |
| 57 | + initialTurnOrder: pl.player + 1, |
| 58 | + version: data.version ?? "1.0.0", |
| 59 | + players: data.players.length, |
| 60 | + started: game.createdAt?.toISOString(), |
| 61 | + ended: game.lastMove?.toISOString(), |
| 62 | + variant: data.options.factionVariant, |
| 63 | + auction: data.options.auction, |
| 64 | + layout: data.options.layout, |
| 65 | + randomFactions: data.options.randomFactions ?? false, |
| 66 | + rotateSectors: !data.options.advancedRules, |
| 67 | + rank: rank.indexOf(pl) + 1, |
| 68 | + rankWithoutBid: rankWithoutBid.indexOf(pl) + 1, |
| 69 | + playerDropped: playerProp("dropped", false), |
| 70 | + playerQuit: playerProp("quit", false), |
| 71 | + }; |
| 72 | + |
| 73 | + Object.assign(row, commonProps); |
| 74 | + |
| 75 | + for (let pos in data.tiles.techs) { |
| 76 | + if (pos === "move") { |
| 77 | + continue; |
| 78 | + } |
| 79 | + const tech = data.tiles.techs[pos].tile; |
| 80 | + row[`tech-${pos}`] = advancedTechTileNames[tech] ?? baseTechTileNames[tech].name; |
| 81 | + } |
| 82 | + |
| 83 | + row["finalA"] = data.tiles.scorings.final[0]; |
| 84 | + row["finalB"] = data.tiles.scorings.final[1]; |
| 85 | + |
| 86 | + data.tiles.scorings.round.forEach((tile, index) => { |
| 87 | + row[`roundScoring${index + 1}`] = roundScorings[tile][0]; |
| 88 | + }); |
| 89 | + |
| 90 | + for (let booster of Booster.values()) { |
| 91 | + row[`booster ${boosterNames[booster].name}`] = data.tiles.boosters[booster] ? 1 : 0; |
| 92 | + } |
| 93 | + |
| 94 | + let i = 1; |
| 95 | + for (const move of data.moveHistory) { |
| 96 | + const command = parsedMove(move).commands[0]; |
| 97 | + if (command.command == Command.Build && command.faction === pl.faction) { |
| 98 | + const hex = data.map.getS(command.args[1]); |
| 99 | + // data.map.distance() |
| 100 | + row[`startPosition${i}`] = hex.toString(); |
| 101 | + row[`startPositionDistance${i}`] = (Math.abs(hex.q) + Math.abs(hex.r) + Math.abs(-hex.q - hex.r)) / 2; |
| 102 | + i++; |
| 103 | + } else if (command.command == Command.ChooseRoundBooster) { |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (; i < 4; i++) { |
| 109 | + //so that all columns are filled to get the correct headers |
| 110 | + row[`startPosition${i}`] = ""; |
| 111 | + row[`startPositionDistance${i}`] = ""; |
| 112 | + } |
| 113 | + |
| 114 | + return row; |
| 115 | +} |
| 116 | + |
| 117 | +function getStats(game: GameDocument, data: Engine): { game: any[]; detail: any[] } { |
| 118 | + const avgElo = |
| 119 | + sumBy(game.players, (p: PlayerInfo) => (p.elo?.initial ?? 0) + (p.elo?.delta ?? 0)) / game.players.length; |
| 120 | + |
| 121 | + return data.players |
| 122 | + .flatMap((pl) => { |
| 123 | + const outerPlayer = game.players.find((p) => p.faction === pl.faction); |
| 124 | + |
| 125 | + const commonProps = { |
| 126 | + id: game._id, |
| 127 | + player: outerPlayer.name, |
| 128 | + faction: pl.faction, |
| 129 | + score: outerPlayer.score, |
| 130 | + scoreWithoutBid: outerPlayer.score + (pl.data.bid ?? 0), // bid is positive |
| 131 | + eloInitial: outerPlayer.elo?.initial, |
| 132 | + eloDelta: outerPlayer.elo?.delta, |
| 133 | + averageElo: avgElo, |
| 134 | + }; |
| 135 | + const gameRow = getGameStats(pl, outerPlayer, data, game, commonProps); |
| 136 | + return { game: [gameRow], detail: getDetailStats(commonProps, data, pl) }; |
| 137 | + }) |
| 138 | + .reduce((a, b) => { |
| 139 | + a.game.push(...b.game); |
| 140 | + a.detail.push(...b.detail); |
| 141 | + return a; |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +async function main() { |
| 146 | + let success = 0; |
| 147 | + let errors = 0; |
| 148 | + let skipReplay = 0; |
| 149 | + let cancelled = 0; |
| 150 | + let active = 0; |
| 151 | + let expansion = 0; |
| 152 | + |
| 153 | + let progress = 0; |
| 154 | + |
| 155 | + const outcomes = () => ({ |
| 156 | + success, |
| 157 | + errors, |
| 158 | + skipReplay, |
| 159 | + cancelled, |
| 160 | + active, |
| 161 | + expansion, |
| 162 | + }); |
| 163 | + |
| 164 | + let gameWriter = null; |
| 165 | + let detailWriter = null; |
| 166 | + |
| 167 | + async function process(game: GameDocument) { |
| 168 | + progress++; |
| 169 | + if (progress % 10 == 0) { |
| 170 | + console.log("progress", progress); |
| 171 | + } |
| 172 | + |
| 173 | + if (game.cancelled) { |
| 174 | + cancelled++; |
| 175 | + return; |
| 176 | + } |
| 177 | + if (game.status !== "ended") { |
| 178 | + active++; |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + if (game.game.expansions.length > 0) { |
| 183 | + expansion++; |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + let data: Engine; |
| 188 | + |
| 189 | + if (shouldReplay(game)) { |
| 190 | + const file = `replay/${game._id}.json`; |
| 191 | + if (fs.existsSync(file)) { |
| 192 | + data = Engine.fromData(JSON.parse(fs.readFileSync(file, { encoding: "utf8" }))); |
| 193 | + } else { |
| 194 | + skipReplay++; |
| 195 | + return; |
| 196 | + } |
| 197 | + } else { |
| 198 | + data = Engine.fromData(game.data); |
| 199 | + } |
| 200 | + |
| 201 | + const stats = getStats(game, data); |
| 202 | + |
| 203 | + if (gameWriter == null) { |
| 204 | + gameWriter = createObjectCsvWriter({ |
| 205 | + path: "gaia-stats-game.csv", |
| 206 | + header: Object.keys(stats.game[0]).map((k) => ({ id: k, title: k })), |
| 207 | + }); |
| 208 | + detailWriter = createObjectCsvWriter({ |
| 209 | + path: "gaia-stats-turns.csv", |
| 210 | + header: Object.keys(stats.detail[0]).map((k) => ({ id: k, title: k })), |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + await gameWriter.writeRecords(stats.game); |
| 215 | + await detailWriter.writeRecords(stats.detail); |
| 216 | + success++; |
| 217 | + } |
| 218 | + |
| 219 | + connectMongo(); |
| 220 | + |
| 221 | + for await (const game of Game.find().where("game.name").equals("gaia-project")) { |
| 222 | + try { |
| 223 | + await process(game); |
| 224 | + } catch (e) { |
| 225 | + console.log(game._id); |
| 226 | + // console.log(JSON.stringify(game)); |
| 227 | + console.log(e); |
| 228 | + errors++; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + console.log(outcomes()); |
| 233 | +} |
| 234 | + |
| 235 | +const start = new Date(); |
| 236 | +main().then(() => { |
| 237 | + console.log("done"); |
| 238 | + console.log(new Date().getTime() - start.getTime()); |
| 239 | + process.exit(0); |
| 240 | +}); |
0 commit comments