Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | import {
GameMode,
Settings,
SettingsJSON,
TimeInterval
} from "../types/settings"
import { DependencyContainer } from "../types/dependencycontainer"
import { SettingsManager } from "./SettingsManager"
import { APIHandler } from "./APIHandler"
import { UiUpdater } from "./UiUpdater"
import { UrlObserver } from "./UrlObserver"
import { StatsCalculator } from "./StatsCalculator"
import { ApiChessData } from "../types/apidata"
import { Stats } from "../types/stats"
import { UiWindow } from "../types/wrapper"
/**
* Class responsible for updating chess statistics and UI elements.
*/
export class StatsUpdater {
/**
* Dependencies for the StatsUpdater.
*/
private settingsJSON: SettingsJSON
private settingsManager: SettingsManager
private urlobserver: UrlObserver
private uiUpdater: UiUpdater
private apiHandler: APIHandler
private statsCalculator: StatsCalculator
private urlObserver: UrlObserver
private uiWindow: UiWindow
/**
* Creates an instance of StatsUpdater with dependencies injected through a container.
* Initializes event listeners for URL mutations to trigger statistics updates.
*
* @param {DependencyContainer} dependencies - Dependencies for the StatsUpdater.
*/
constructor(dependencies: DependencyContainer) {
// Initialize dependencies
this.settingsManager = dependencies.settingsManager
this.uiUpdater = dependencies.uiUpdater
this.apiHandler = dependencies.apiHandler
this.settingsJSON = dependencies.settingsJSON
this.statsCalculator = dependencies.statsCalculator
this.urlobserver = dependencies.urlObserver
this.uiWindow = dependencies.uiWindow
}
/**
* Initialize the StatsUpdater and start updating chess statistics.
*
* @param {boolean} attachEventListeners - Whether to attach event listeners for flip board and settings updates.
* @param {boolean} startObserving - Whether to start observing URL changes.
* @param {boolean} startUpdating - Whether to start updating statistics.
*/
initialize(
attachEventListeners: boolean = true,
startObserving: boolean = true,
startUpdating: boolean = true
): void {
// initial update
if (startUpdating) {
setTimeout(() => {
this.updateStatsForBothPlayers()
this.updateTitleForBothPlayers()
}, this.settingsJSON.LOAD_DELAY)
}
// Attach event listeners for flip board and settings updates
if (attachEventListeners) {
this.attachButtonClickEvent("board-controls-flip")
this.attachButtonClickEvent("board-controls-settings")
this.attachButtonClickEvent("board-controls-theatre")
this.attachButtonClickEvent("board-controls-focus")
this.attachSettingsUpdateListener()
}
if (!startObserving) return
// start observing url changes
this.urlobserver.startObserving()
// Listen for URL mutations to trigger stats updates
this.urlobserver.on("url-mutation", () => {
setTimeout(() => {
this.updateStatsForBothPlayers()
this.updateTitleForBothPlayers()
}, this.settingsJSON.LOAD_DELAY)
})
}
/**
* Update chess statistics for both players.
*
* @param {boolean} updateSettings - Whether to update settings from storage.
* @returns {Promise<void[]>} A Promise that resolves once the statistics are updated for both players.
*/
async updateStatsForBothPlayers(
updateSettings: boolean = false
): Promise<void[]> {
return Promise.all([
this.updateStatsForPlayer("top", updateSettings),
this.updateStatsForPlayer("bottom", updateSettings)
])
}
/**
* Update chess title for both players.
*
* @returns {void}
*/
updateTitleForBothPlayers(): void {
this.updateTitleForPlayer("top")
this.updateTitleForPlayer("bottom")
}
/**
* Update chess title for a specific player.
*
* @param {("top" | "bottom")} side - The player ("top" or "bottom") for whom to update title.
* @returns {void}
*/
updateTitleForPlayer(side: "top" | "bottom"): void {
const username = this.uiUpdater.getUsername(side)
if (!username) return
const title = this.uiUpdater
.getPlayerElement(side)
?.parentElement?.querySelector(".cci-custom-title")
if (title) title.remove()
// check if user has a special Title
const specialTitles = this.settingsJSON.specialTitles
if (!specialTitles) return
const specialTitle = specialTitles[username]
if (!specialTitle) return
// give player title if applicable
this.uiUpdater.updateTitleElement(side, specialTitle)
}
/**
* Update chess statistics for a specific player.
*
* @param {("top" | "bottom")} side - The player ("top" or "bottom") for whom to update statistics.
* @param {boolean} updateSettings - Whether to update settings from storage.
* @returns {Promise<void>} A Promise that resolves once the statistics are updated.
*/
async updateStatsForPlayer(
side: "top" | "bottom",
updateSettings: boolean = false
): Promise<void> {
const settings: Settings =
await this.settingsManager.getSettings(updateSettings)
const flag = this.uiUpdater.getFlagElement(side)
if (flag) flag.remove()
if (!settings.show_stats) return
if (settings.hide_own_stats && side === "bottom") return
const username = this.uiUpdater.getUsername(side)
if (!username) throw `No username found for side ${side}`
const stats: Stats = await this.getStats(
side,
username,
settings.game_modes,
settings.time_interval
)
this.uiUpdater.updateElement(
side,
stats,
settings.show_accuracy,
settings.color_highlighting,
settings.time_interval
)
}
/**
* Get update chess statistics for a specific player.
*
* @param {("top" | "bottom")} side - The player ("top" or "bottom") for whom to update statistics.
* @param {string} username - The username of the player.
* @param {GameMode[]} gameModes - The game modes to include in the statistics.
* @param {TimeInterval} timeInterval - The time interval to include in the statistics.
* @returns {Promise<Stats>} A Promise that resolves with the updated statistics.
*/
async getStats(
side: "top" | "bottom",
username: string,
gameModes: GameMode[],
timeInterval: TimeInterval
): Promise<Stats> {
try {
const data: ApiChessData = await this.apiHandler.getChessData(username)
return this.statsCalculator.calculateStats(
data.games,
gameModes,
timeInterval,
username
)
} catch {
this.uiUpdater.removeInfoElement(side)
throw `Could not retrieve chess data for ${username}`
}
}
/**
* Attach click event listeners to specified buttons.
*/
private attachButtonClickEvent(buttonId: string): void {
const button = this.uiWindow.getDocument().getElementById(buttonId)
if (!button) return
button.addEventListener("click", () => {
this.updateStatsForBothPlayers()
this.updateTitleForBothPlayers()
})
}
/**
* Attach settings update listener.
*/
private attachSettingsUpdateListener(): void {
chrome.runtime.onMessage.addListener(
async (request: { action: string }) => {
if (request.action !== "updated-settings") return
this.updateStatsForBothPlayers(true)
this.updateTitleForBothPlayers()
}
)
}
getUiUpdater(): UiUpdater {
return this.uiUpdater
}
getSettingsManager(): SettingsManager {
return this.settingsManager
}
getApiHandler(): APIHandler {
return this.apiHandler
}
getStatsCalculator(): StatsCalculator {
return this.statsCalculator
}
getSettingsJSON(): SettingsJSON {
return this.settingsJSON
}
}
|