Full typescript and mvvm implementation #3
@ -30,10 +30,17 @@ export class CpuRepository {
|
|||||||
return __classPrivateFieldGet(this, _CpuRepository_nextCPU, "f");
|
return __classPrivateFieldGet(this, _CpuRepository_nextCPU, "f");
|
||||||
}
|
}
|
||||||
getRandomCpu() {
|
getRandomCpu() {
|
||||||
let randomIndex = __classPrivateFieldGet(this, _CpuRepository_instances, "m", _CpuRepository_getRandomInt).call(this, 0, __classPrivateFieldGet(this, _CpuRepository_cpuList, "f").length);
|
let randomIndex;
|
||||||
|
do {
|
||||||
|
randomIndex = __classPrivateFieldGet(this, _CpuRepository_instances, "m", _CpuRepository_getRandomInt).call(this, 0, __classPrivateFieldGet(this, _CpuRepository_cpuList, "f").length);
|
||||||
|
} while (__classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["value"] == null || __classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["type"] == null);
|
||||||
__classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["name"] = __classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["name"].split('@')[0];
|
__classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["name"] = __classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex]["name"].split('@')[0];
|
||||||
return __classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex];
|
return __classPrivateFieldGet(this, _CpuRepository_cpuList, "f")[randomIndex];
|
||||||
}
|
}
|
||||||
|
nextRound() {
|
||||||
|
__classPrivateFieldSet(this, _CpuRepository_currentCPU, __classPrivateFieldGet(this, _CpuRepository_nextCPU, "f"), "f");
|
||||||
|
__classPrivateFieldSet(this, _CpuRepository_nextCPU, this.getRandomCpu(), "f");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_CpuRepository_cpuList = new WeakMap(), _CpuRepository_currentCPU = new WeakMap(), _CpuRepository_nextCPU = new WeakMap(), _CpuRepository_instances = new WeakSet(), _CpuRepository_getRandomInt = function _CpuRepository_getRandomInt(min, max) {
|
_CpuRepository_cpuList = new WeakMap(), _CpuRepository_currentCPU = new WeakMap(), _CpuRepository_nextCPU = new WeakMap(), _CpuRepository_instances = new WeakSet(), _CpuRepository_getRandomInt = function _CpuRepository_getRandomInt(min, max) {
|
||||||
min = Math.ceil(min);
|
min = Math.ceil(min);
|
||||||
|
44
js/game.js
44
js/game.js
@ -2,52 +2,36 @@
|
|||||||
import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0/countUp.min.js";
|
import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0/countUp.min.js";
|
||||||
import { Stats } from "./statistics.js";
|
import { Stats } from "./statistics.js";
|
||||||
import { CpuRepository } from "./cpuRepository.js";
|
import { CpuRepository } from "./cpuRepository.js";
|
||||||
let cpuList;
|
|
||||||
let currentCpu;
|
|
||||||
let nextCpu;
|
|
||||||
var repo;
|
var repo;
|
||||||
var localStats;
|
var localStats;
|
||||||
export async function main() {
|
export async function main() {
|
||||||
localStats = new Stats("highScore_cpu");
|
localStats = new Stats("highScore_cpu");
|
||||||
repo = new CpuRepository();
|
repo = new CpuRepository();
|
||||||
await repo.init();
|
await repo.init();
|
||||||
await (fetch('./js/data.json')
|
|
||||||
.then((response) => response.json())
|
|
||||||
.then((json) => cpuList = json));
|
|
||||||
currentCpu = getRandomCpu();
|
|
||||||
nextCpu = getRandomCpu();
|
|
||||||
updateLayout();
|
updateLayout();
|
||||||
}
|
}
|
||||||
function getRandomCpu() {
|
|
||||||
let randomIndex = getRandomInt(0, cpuList.length);
|
|
||||||
return {
|
|
||||||
name: cpuList[randomIndex]["name"].split('@')[0],
|
|
||||||
score: cpuList[randomIndex]["cpuScore"]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
export function btnLowerClick() {
|
export function btnLowerClick() {
|
||||||
nextCpu.score < currentCpu.score ? showResult(true) : showResult(false);
|
repo.nextCpu.cpuScore < repo.currentCpu.cpuScore ? showResult(true) : showResult(false);
|
||||||
}
|
}
|
||||||
export function btnHigherClick() {
|
export function btnHigherClick() {
|
||||||
nextCpu.score > currentCpu.score ? showResult(true) : showResult(false);
|
repo.nextCpu.cpuScore > repo.currentCpu.cpuScore ? showResult(true) : showResult(false);
|
||||||
}
|
}
|
||||||
function showResult(isCorrect) {
|
function showResult(isCorrect) {
|
||||||
document.getElementById("btnHigher").setAttribute("disabled", "");
|
document.getElementById("btnHigher").setAttribute("disabled", "");
|
||||||
document.getElementById("btnLower").setAttribute("disabled", "");
|
document.getElementById("btnLower").setAttribute("disabled", "");
|
||||||
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
||||||
isCorrect ? localStats.incrementScore() : localStats.resetScore();
|
isCorrect ? localStats.incrementScore() : localStats.resetScore();
|
||||||
document.getElementById("highScore").innerText = localStats.highScore;
|
document.getElementById("highScore").innerText = localStats.highScore.toString();
|
||||||
document.getElementById("score").innerText = localStats.score;
|
document.getElementById("score").innerText = localStats.score.toString();
|
||||||
countUp();
|
countUp();
|
||||||
}
|
}
|
||||||
// updates view based on the cpu objects
|
// updates view based on the cpu objects
|
||||||
function updateLayout() {
|
function updateLayout() {
|
||||||
document.getElementById("highScore").innerText = localStats.highScore;
|
document.getElementById("highScore").innerText = localStats.highScore.toString();
|
||||||
;
|
document.getElementById("currentCpuTitle").innerText = repo.currentCpu.name;
|
||||||
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
|
||||||
// add "." to large numbers
|
// add "." to large numbers
|
||||||
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(currentCpu.score);
|
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(repo.currentCpu.cpuScore);
|
||||||
document.getElementById("nextCpuTitle").innerText = nextCpu.name;
|
document.getElementById("nextCpuTitle").innerText = repo.nextCpu.name;
|
||||||
document.getElementById("nextCpuScore").innerText = "?";
|
document.getElementById("nextCpuScore").innerText = "?";
|
||||||
document.getElementById("col2").style.backgroundColor = "";
|
document.getElementById("col2").style.backgroundColor = "";
|
||||||
}
|
}
|
||||||
@ -56,12 +40,12 @@ function delay(time) {
|
|||||||
}
|
}
|
||||||
async function countUp() {
|
async function countUp() {
|
||||||
const options = {
|
const options = {
|
||||||
startVal: nextCpu.score / 2,
|
startVal: repo.nextCpu.cpuScore / 2,
|
||||||
separator: '.',
|
separator: '.',
|
||||||
decimal: ',',
|
decimal: ',',
|
||||||
duration: 2
|
duration: 2
|
||||||
};
|
};
|
||||||
let counter = new CountUp('nextCpuScore', nextCpu.score, options);
|
let counter = new CountUp('nextCpuScore', repo.nextCpu.cpuScore, options);
|
||||||
if (!counter.error) {
|
if (!counter.error) {
|
||||||
counter.start();
|
counter.start();
|
||||||
}
|
}
|
||||||
@ -74,12 +58,6 @@ async function countUp() {
|
|||||||
document.getElementById("btnLower").removeAttribute("disabled");
|
document.getElementById("btnLower").removeAttribute("disabled");
|
||||||
}
|
}
|
||||||
function nextRound() {
|
function nextRound() {
|
||||||
currentCpu = nextCpu;
|
repo.nextRound();
|
||||||
nextCpu = getRandomCpu();
|
|
||||||
updateLayout();
|
updateLayout();
|
||||||
}
|
}
|
||||||
function getRandomInt(min, max) {
|
|
||||||
min = Math.ceil(min);
|
|
||||||
max = Math.floor(max);
|
|
||||||
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
|
|
||||||
}
|
|
||||||
|
@ -12,20 +12,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|||||||
var _Stats_score, _Stats_highScore, _Stats_highScoreStorageKey;
|
var _Stats_score, _Stats_highScore, _Stats_highScoreStorageKey;
|
||||||
export class Stats {
|
export class Stats {
|
||||||
constructor(highScoreStorageKey) {
|
constructor(highScoreStorageKey) {
|
||||||
// used as key for localStorage
|
|
||||||
var _a;
|
var _a;
|
||||||
_Stats_score.set(this, void 0);
|
_Stats_score.set(this, void 0);
|
||||||
_Stats_highScore.set(this, void 0);
|
_Stats_highScore.set(this, void 0);
|
||||||
_Stats_highScoreStorageKey.set(this, void 0);
|
_Stats_highScoreStorageKey.set(this, void 0);
|
||||||
|
// used as key for localStorage
|
||||||
__classPrivateFieldSet(this, _Stats_highScoreStorageKey, highScoreStorageKey, "f");
|
__classPrivateFieldSet(this, _Stats_highScoreStorageKey, highScoreStorageKey, "f");
|
||||||
__classPrivateFieldSet(this, _Stats_score, 0, "f");
|
__classPrivateFieldSet(this, _Stats_score, 0, "f");
|
||||||
__classPrivateFieldSet(this, _Stats_highScore, (_a = localStorage.getItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f"))) !== null && _a !== void 0 ? _a : 0, "f");
|
__classPrivateFieldSet(this, _Stats_highScore, (_a = Number(localStorage.getItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f")))) !== null && _a !== void 0 ? _a : 0, "f");
|
||||||
}
|
}
|
||||||
incrementScore(value = 1) {
|
incrementScore(value = 1) {
|
||||||
__classPrivateFieldSet(this, _Stats_score, __classPrivateFieldGet(this, _Stats_score, "f") + value, "f");
|
__classPrivateFieldSet(this, _Stats_score, __classPrivateFieldGet(this, _Stats_score, "f") + value, "f");
|
||||||
if (__classPrivateFieldGet(this, _Stats_highScore, "f") < __classPrivateFieldGet(this, _Stats_score, "f")) {
|
if (__classPrivateFieldGet(this, _Stats_highScore, "f") < __classPrivateFieldGet(this, _Stats_score, "f")) {
|
||||||
__classPrivateFieldSet(this, _Stats_highScore, __classPrivateFieldGet(this, _Stats_score, "f"), "f");
|
__classPrivateFieldSet(this, _Stats_highScore, __classPrivateFieldGet(this, _Stats_score, "f"), "f");
|
||||||
localStorage.setItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f"), __classPrivateFieldGet(this, _Stats_highScore, "f"));
|
localStorage.setItem(__classPrivateFieldGet(this, _Stats_highScoreStorageKey, "f"), __classPrivateFieldGet(this, _Stats_highScore, "f").toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resetScore() {
|
resetScore() {
|
||||||
|
52
src/game.ts
52
src/game.ts
@ -3,12 +3,8 @@ import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0
|
|||||||
import { Stats } from "./statistics.js";
|
import { Stats } from "./statistics.js";
|
||||||
import { CpuRepository } from "./cpuRepository.js"
|
import { CpuRepository } from "./cpuRepository.js"
|
||||||
|
|
||||||
let cpuList;
|
var repo: CpuRepository;
|
||||||
let currentCpu;
|
var localStats: Stats;
|
||||||
let nextCpu;
|
|
||||||
|
|
||||||
var repo;
|
|
||||||
var localStats;
|
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
localStats = new Stats("highScore_cpu");
|
localStats = new Stats("highScore_cpu");
|
||||||
@ -16,30 +12,15 @@ export async function main() {
|
|||||||
repo = new CpuRepository();
|
repo = new CpuRepository();
|
||||||
await repo.init();
|
await repo.init();
|
||||||
|
|
||||||
await (fetch('./js/data.json')
|
|
||||||
.then((response) => response.json())
|
|
||||||
.then((json) => cpuList = json));
|
|
||||||
|
|
||||||
currentCpu = getRandomCpu();
|
|
||||||
nextCpu = getRandomCpu();
|
|
||||||
|
|
||||||
updateLayout();
|
updateLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRandomCpu() {
|
|
||||||
let randomIndex = getRandomInt(0, cpuList.length)
|
|
||||||
return {
|
|
||||||
name: cpuList[randomIndex]["name"].split('@')[0],
|
|
||||||
score: cpuList[randomIndex]["cpuScore"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function btnLowerClick() {
|
export function btnLowerClick() {
|
||||||
nextCpu.score < currentCpu.score ? showResult(true) : showResult(false);
|
repo.nextCpu.cpuScore < repo.currentCpu.cpuScore ? showResult(true) : showResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function btnHigherClick() {
|
export function btnHigherClick() {
|
||||||
nextCpu.score > currentCpu.score ? showResult(true) : showResult(false);
|
repo.nextCpu.cpuScore > repo.currentCpu.cpuScore ? showResult(true) : showResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function showResult(isCorrect) {
|
function showResult(isCorrect) {
|
||||||
@ -50,20 +31,20 @@ function showResult(isCorrect) {
|
|||||||
|
|
||||||
isCorrect ? localStats.incrementScore() : localStats.resetScore();
|
isCorrect ? localStats.incrementScore() : localStats.resetScore();
|
||||||
|
|
||||||
document.getElementById("highScore").innerText = localStats.highScore;
|
document.getElementById("highScore").innerText = localStats.highScore.toString();
|
||||||
document.getElementById("score").innerText = localStats.score;
|
document.getElementById("score").innerText = localStats.score.toString();
|
||||||
|
|
||||||
countUp();
|
countUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
// updates view based on the cpu objects
|
// updates view based on the cpu objects
|
||||||
function updateLayout() {
|
function updateLayout() {
|
||||||
document.getElementById("highScore").innerText = localStats.highScore;;
|
document.getElementById("highScore").innerText = localStats.highScore.toString();
|
||||||
|
|
||||||
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
document.getElementById("currentCpuTitle").innerText = repo.currentCpu.name;
|
||||||
// add "." to large numbers
|
// add "." to large numbers
|
||||||
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(currentCpu.score)
|
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(repo.currentCpu.cpuScore)
|
||||||
document.getElementById("nextCpuTitle").innerText = nextCpu.name;
|
document.getElementById("nextCpuTitle").innerText = repo.nextCpu.name;
|
||||||
|
|
||||||
document.getElementById("nextCpuScore").innerText = "?";
|
document.getElementById("nextCpuScore").innerText = "?";
|
||||||
|
|
||||||
@ -76,12 +57,12 @@ function delay(time) {
|
|||||||
|
|
||||||
async function countUp() {
|
async function countUp() {
|
||||||
const options = {
|
const options = {
|
||||||
startVal: nextCpu.score / 2,
|
startVal: repo.nextCpu.cpuScore / 2,
|
||||||
separator: '.',
|
separator: '.',
|
||||||
decimal: ',',
|
decimal: ',',
|
||||||
duration: 2
|
duration: 2
|
||||||
};
|
};
|
||||||
let counter = new CountUp('nextCpuScore', nextCpu.score, options);
|
let counter = new CountUp('nextCpuScore', repo.nextCpu.cpuScore, options);
|
||||||
if (!counter.error) {
|
if (!counter.error) {
|
||||||
counter.start();
|
counter.start();
|
||||||
} else {
|
} else {
|
||||||
@ -96,14 +77,7 @@ async function countUp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function nextRound() {
|
function nextRound() {
|
||||||
currentCpu = nextCpu;
|
repo.nextRound();
|
||||||
nextCpu = getRandomCpu();
|
|
||||||
|
|
||||||
updateLayout();
|
updateLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRandomInt(min, max) {
|
|
||||||
min = Math.ceil(min);
|
|
||||||
max = Math.floor(max);
|
|
||||||
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user