put winning configuration server side

This commit is contained in:
Ayo 2019-12-25 19:29:08 +08:00
parent 02c05a8d2e
commit af8c6a2159
2 changed files with 31 additions and 10 deletions

View file

@ -16,7 +16,7 @@ export const Minesweeper = function() {
const storageService = new StorageService();
const timerService = new TimerService();
const loggerService = new LoggerService();
const leaderBoard = new LeaderBoardService('mw-leaders', 'mw-all');
const leaderBoard = new LeaderBoardService('mw-leaders', 'mw-all', 'mw-config');
let grid = document.createElement('table');
grid.setAttribute('id', 'grid');
@ -307,6 +307,7 @@ export const Minesweeper = function() {
cell.addEventListener('touchend', ontouchend);
let ontouchstart = function(e) {
isMobile = true;
if (!isBusy && typeof e === 'object') {
startTouchTimer(this);
}
@ -327,8 +328,12 @@ export const Minesweeper = function() {
document.onkeydown = function(e) {
if (e.keyCode == 32) {
generateGrid();
if ('preventDefault' in e) {
e.preventDefault();
} else {
return false;
}
}
resetMouseEventFlags();
}
@ -344,6 +349,7 @@ export const Minesweeper = function() {
resetMouseEventFlags();
}
document.onmousedown = function(e) {
isMobile = false;
switch (e.button) {
case 0: pressed = 'left'; isLeft = true; break;
case 1: pressed = 'middle'; break;
@ -516,7 +522,9 @@ export const Minesweeper = function() {
const game = {
time,
status: win ? 'win' : 'loss',
level: setting.name
level: setting.name,
time_stamp: new Date(),
isMobile
}
leaderBoard.send(game, 'time');
// send google analytics event

View file

@ -2,19 +2,29 @@ import { DatabaseService } from '../database/db';
import { TimerService } from '../timer/timer';
import { UserService } from '../user/user';
import { LoadingService } from '../loading/loading';
import { LoggerService } from '../logger/logger';
const dbService = new DatabaseService();
const timerService = new TimerService();
const loadingService = new LoadingService();
const loggerService = new LoggerService();
const db = dbService.store;
const user = new UserService();
let previousLevel;
export class LeaderBoardService {
constructor(leaders, all) {
constructor(leaders, all, configuration) {
this.leaders = db.collection(leaders);
this.all = db.collection(all);
db.collection(configuration)
.doc('configuration')
.get()
.then(res => {
this.configuration = res.data();
this.configurationPromt();
})
.catch(err => console.error(err));
}
updateTimeStampsLeaders() {
@ -141,15 +151,12 @@ export class LeaderBoardService {
const sessionId = new Date().toDateString().replace(/\s/g, '_');
const gameId = new Date().toTimeString().replace(/\s/g, '_');
const data = {};
game = {
time_stamp: new Date(),
...game
}
data[gameId] = game;
this.all.doc(user.browserId).collection('games').doc(sessionId).set(data, {merge: true});
if (game.status === 'win' && game[key] < this.lastPlace) {
let name = window.prompt('Top performance! Enter your name:');
if (this.configuration && game.status === this.configuration.passingStatus && game[key] < this.lastPlace) {
let name = window.prompt(this.configuration.message);
if (!name) {
name = 'Anonymous';
}
@ -163,4 +170,10 @@ export class LeaderBoardService {
this.leaders.doc(game.level).collection('games').add(newGame);
}
}
configurationPromt() {
if (!this.configuration) {
loggerService.debug('Failed to fetch server configuration. Please contact your developer.');
}
}
}