put winning configuration server side
This commit is contained in:
parent
02c05a8d2e
commit
af8c6a2159
2 changed files with 31 additions and 10 deletions
|
@ -16,7 +16,7 @@ export const Minesweeper = function() {
|
||||||
const storageService = new StorageService();
|
const storageService = new StorageService();
|
||||||
const timerService = new TimerService();
|
const timerService = new TimerService();
|
||||||
const loggerService = new LoggerService();
|
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');
|
let grid = document.createElement('table');
|
||||||
grid.setAttribute('id', 'grid');
|
grid.setAttribute('id', 'grid');
|
||||||
|
@ -307,6 +307,7 @@ export const Minesweeper = function() {
|
||||||
cell.addEventListener('touchend', ontouchend);
|
cell.addEventListener('touchend', ontouchend);
|
||||||
|
|
||||||
let ontouchstart = function(e) {
|
let ontouchstart = function(e) {
|
||||||
|
isMobile = true;
|
||||||
if (!isBusy && typeof e === 'object') {
|
if (!isBusy && typeof e === 'object') {
|
||||||
startTouchTimer(this);
|
startTouchTimer(this);
|
||||||
}
|
}
|
||||||
|
@ -327,8 +328,12 @@ export const Minesweeper = function() {
|
||||||
document.onkeydown = function(e) {
|
document.onkeydown = function(e) {
|
||||||
if (e.keyCode == 32) {
|
if (e.keyCode == 32) {
|
||||||
generateGrid();
|
generateGrid();
|
||||||
|
if ('preventDefault' in e) {
|
||||||
|
e.preventDefault();
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
resetMouseEventFlags();
|
resetMouseEventFlags();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,6 +349,7 @@ export const Minesweeper = function() {
|
||||||
resetMouseEventFlags();
|
resetMouseEventFlags();
|
||||||
}
|
}
|
||||||
document.onmousedown = function(e) {
|
document.onmousedown = function(e) {
|
||||||
|
isMobile = false;
|
||||||
switch (e.button) {
|
switch (e.button) {
|
||||||
case 0: pressed = 'left'; isLeft = true; break;
|
case 0: pressed = 'left'; isLeft = true; break;
|
||||||
case 1: pressed = 'middle'; break;
|
case 1: pressed = 'middle'; break;
|
||||||
|
@ -516,7 +522,9 @@ export const Minesweeper = function() {
|
||||||
const game = {
|
const game = {
|
||||||
time,
|
time,
|
||||||
status: win ? 'win' : 'loss',
|
status: win ? 'win' : 'loss',
|
||||||
level: setting.name
|
level: setting.name,
|
||||||
|
time_stamp: new Date(),
|
||||||
|
isMobile
|
||||||
}
|
}
|
||||||
leaderBoard.send(game, 'time');
|
leaderBoard.send(game, 'time');
|
||||||
// send google analytics event
|
// send google analytics event
|
||||||
|
|
|
@ -2,19 +2,29 @@ import { DatabaseService } from '../database/db';
|
||||||
import { TimerService } from '../timer/timer';
|
import { TimerService } from '../timer/timer';
|
||||||
import { UserService } from '../user/user';
|
import { UserService } from '../user/user';
|
||||||
import { LoadingService } from '../loading/loading';
|
import { LoadingService } from '../loading/loading';
|
||||||
|
import { LoggerService } from '../logger/logger';
|
||||||
|
|
||||||
const dbService = new DatabaseService();
|
const dbService = new DatabaseService();
|
||||||
const timerService = new TimerService();
|
const timerService = new TimerService();
|
||||||
const loadingService = new LoadingService();
|
const loadingService = new LoadingService();
|
||||||
|
const loggerService = new LoggerService();
|
||||||
const db = dbService.store;
|
const db = dbService.store;
|
||||||
const user = new UserService();
|
const user = new UserService();
|
||||||
let previousLevel;
|
let previousLevel;
|
||||||
|
|
||||||
|
|
||||||
export class LeaderBoardService {
|
export class LeaderBoardService {
|
||||||
constructor(leaders, all) {
|
constructor(leaders, all, configuration) {
|
||||||
this.leaders = db.collection(leaders);
|
this.leaders = db.collection(leaders);
|
||||||
this.all = db.collection(all);
|
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() {
|
updateTimeStampsLeaders() {
|
||||||
|
@ -141,15 +151,12 @@ export class LeaderBoardService {
|
||||||
const sessionId = new Date().toDateString().replace(/\s/g, '_');
|
const sessionId = new Date().toDateString().replace(/\s/g, '_');
|
||||||
const gameId = new Date().toTimeString().replace(/\s/g, '_');
|
const gameId = new Date().toTimeString().replace(/\s/g, '_');
|
||||||
const data = {};
|
const data = {};
|
||||||
game = {
|
|
||||||
time_stamp: new Date(),
|
|
||||||
...game
|
|
||||||
}
|
|
||||||
data[gameId] = game;
|
data[gameId] = game;
|
||||||
this.all.doc(user.browserId).collection('games').doc(sessionId).set(data, {merge: true});
|
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) {
|
if (!name) {
|
||||||
name = 'Anonymous';
|
name = 'Anonymous';
|
||||||
}
|
}
|
||||||
|
@ -163,4 +170,10 @@ export class LeaderBoardService {
|
||||||
this.leaders.doc(game.level).collection('games').add(newGame);
|
this.leaders.doc(game.level).collection('games').add(newGame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configurationPromt() {
|
||||||
|
if (!this.configuration) {
|
||||||
|
loggerService.debug('Failed to fetch server configuration. Please contact your developer.');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue