added scoreboard

This commit is contained in:
Tom Tibble
2020-04-04 18:00:39 -05:00
parent a7683b519e
commit 33ed5ec6b0
2 changed files with 130 additions and 36 deletions

60
.idea/workspace.xml generated
View File

@@ -2,12 +2,7 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="d9dac41b-fc54-4ab0-93ea-73586523c969" name="Default" comment="">
<change afterPath="$PROJECT_DIR$/foreground_drawing.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/audio.js" beforeDir="false" afterPath="$PROJECT_DIR$/audio.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/background.js" beforeDir="false" afterPath="$PROJECT_DIR$/background.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/game.js" beforeDir="false" afterPath="$PROJECT_DIR$/game.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/index.html" beforeDir="false" afterPath="$PROJECT_DIR$/index.html" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/.tmp/" />
<ignored path="$PROJECT_DIR$/temp/" />
@@ -31,13 +26,13 @@
<usages-collector id="statistics.file.extensions.edit">
<counts>
<entry key="html" value="27" />
<entry key="js" value="1243" />
<entry key="js" value="2895" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.edit">
<counts>
<entry key="HTML" value="27" />
<entry key="JavaScript" value="1243" />
<entry key="JavaScript" value="2895" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.open">
@@ -75,8 +70,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/game.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="209">
<caret line="190" column="54" selection-start-line="190" selection-start-column="44" selection-end-line="190" selection-end-column="54" />
<state relative-caret-position="423">
<caret line="433" column="5" lean-forward="true" selection-start-line="433" selection-start-column="5" selection-end-line="433" selection-end-column="5" />
</state>
</provider>
</entry>
@@ -102,8 +97,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/background.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="11" column="28" selection-start-line="11" selection-start-column="28" selection-end-line="11" selection-end-column="28" />
<state relative-caret-position="-148">
<caret line="147" column="63" selection-start-line="147" selection-start-column="52" selection-end-line="147" selection-end-column="63" />
</state>
</provider>
</entry>
@@ -119,11 +114,6 @@
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>coun</find>
<find>horseheigh</find>
<find>ctx.</find>
<find>finalplaces</find>
<find>canvas</find>
<find>rect</find>
<find>prerace</find>
<find>horses</find>
@@ -149,6 +139,11 @@
<find>inithorses</find>
<find>restarting</find>
<find>coundown</find>
<find>finalplaces[</find>
<find>finalplaces</find>
<find>.place</find>
<find>addw</find>
<find>roundss</find>
</findStrings>
<replaceStrings>
<replace>cyclesRemaining</replace>
@@ -258,17 +253,18 @@
<workItem from="1563080090211" duration="25669000" />
<workItem from="1569291993530" duration="184000" />
<workItem from="1585964564085" duration="8983000" />
<workItem from="1586030603124" duration="6776000" />
<workItem from="1586030603124" duration="10458000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="79404000" />
<option name="totallyTimeSpent" value="83086000" />
</component>
<component name="ToolWindowManager">
<frame x="39" y="0" width="1701" height="1054" extended-state="0" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.16837041" />
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.16837041" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
@@ -279,7 +275,7 @@
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info active="true" anchor="bottom" id="Terminal" order="8" visible="true" weight="0.23260869" />
<window_info anchor="bottom" id="Terminal" order="8" visible="true" weight="0.23260869" />
<window_info anchor="bottom" id="Event Log" order="9" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="10" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
@@ -298,7 +294,7 @@
<breakpoints>
<line-breakpoint enabled="true" type="javascript">
<url>file://$PROJECT_DIR$/game.js</url>
<line>94</line>
<line>97</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
<line-breakpoint enabled="true" type="javascript">
@@ -343,17 +339,10 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/audio.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="680">
<caret line="40" column="7" lean-forward="true" selection-start-line="40" selection-start-column="7" selection-end-line="40" selection-end-column="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/background.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="11" column="28" selection-start-line="11" selection-start-column="28" selection-end-line="11" selection-end-column="28" />
<state relative-caret-position="-148">
<caret line="147" column="63" selection-start-line="147" selection-start-column="52" selection-end-line="147" selection-end-column="63" />
</state>
</provider>
</entry>
@@ -364,6 +353,13 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/audio.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="680">
<caret line="40" column="7" lean-forward="true" selection-start-line="40" selection-start-column="7" selection-end-line="40" selection-end-column="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
@@ -373,8 +369,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/game.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="209">
<caret line="190" column="54" selection-start-line="190" selection-start-column="44" selection-end-line="190" selection-end-column="54" />
<state relative-caret-position="423">
<caret line="433" column="5" lean-forward="true" selection-start-line="433" selection-start-column="5" selection-end-line="433" selection-end-column="5" />
</state>
</provider>
</entry>

106
game.js
View File

@@ -4,9 +4,6 @@
const GAME_CANVAS = document.getElementById("gamecanvas");
const BG_CANVAS = document.getElementById("bgcanvas");
@@ -30,6 +27,9 @@ let finalPlaces = [];
let finishLineScan = 0;
let fanfarePlayedFlag = false;
let placeCount = 1;
let roundScored = false;
let scoreboard = JSON.parse(window.localStorage.getItem("cd_scoreboard")) || {};
//UI event handlers
let clickHandlerLoaded = false;
@@ -43,6 +43,8 @@ Math.floor(Math.random() * (100)) + 300, // should be between 300 and 400
//Start game loop
window.onload = ( ) => {
console.log("-- Scoreboard Check --");
console.log(scoreboard);
initGame();
// The game loop should be a setInterval that way the screen size can be adjusted
setInterval(gameLoop, 20); // a touch <60 FPS
@@ -58,6 +60,7 @@ function initGame(){
fanfarePlayedFlag = false;
clickHandlerLoaded = false;
roundScored = false;
initBackground();
initHorses();
@@ -193,6 +196,11 @@ function runRace(ctx,bg_ctx){
}
// horses.splice(foundHorseIndex);
if (finalPlaces.length === 4){
if (!roundScored){
addWinnerToScoreboard(finalPlaces);
getAlltimeWinnerFromScoreboard();
roundScored = true;
}
stopGallop();
// give it a bit of time before setting gameState to finished
setTimeout(() => {
@@ -339,4 +347,94 @@ swayPatternFunctions = [
{x:(cycle)=>{ return Math.sin(cycle/3) * 10;}, y:(cycle)=>{ return Math.cos(cycle) * 10;}},
{x:(cycle)=>{ return Math.cos(cycle/4) * 10;}, y:(cycle)=>{ return Math.sin(cycle/2) * 10}},
{x:(cycle)=>{ return Math.sin(cycle/4) * 10;}, y:(cycle)=>{ return Math.cos(cycle/2) * 10}}
];
];
function addWinnerToScoreboard(finalPlaceArray){
let makeFirst = true;
let makeSecond = true;
let makeThird = true;
let makeFourth = true;
// check for existing horses in scoreboard
for( const horseScoreListing in scoreboard){
if (horseScoreListing === finalPlaceArray[0].name){
makeFirst = false;
scoreboard[horseScoreListing].totalRaces ++;
scoreboard[horseScoreListing].firstPlaces ++;
} else if (horseScoreListing === finalPlaceArray[1].name){
makeSecond = false;
scoreboard[horseScoreListing].totalRaces ++;
scoreboard[horseScoreListing].secondPlaces ++;
} else if (horseScoreListing === finalPlaceArray[2].name){
makeThird = false;
scoreboard[horseScoreListing].totalRaces ++;
scoreboard[horseScoreListing].thirdPlaces ++;
} else if (horseScoreListing === finalPlaceArray[3].name){
makeFourth = false;
scoreboard[horseScoreListing].totalRaces ++;
scoreboard[horseScoreListing].forthPlaces ++;
}
}
//
if (makeFirst){
scoreboard[finalPlaceArray[0].name] = {totalRaces: 1, firstPlaces: 1, secondPlaces: 0, thirdPlaces: 0, forthPlaces:0}
}
if (makeSecond){
scoreboard[finalPlaceArray[1].name] = {totalRaces: 1, firstPlaces: 0, secondPlaces: 1, thirdPlaces: 0, forthPlaces:0}
}
if (makeThird){
scoreboard[finalPlaceArray[2].name] = {totalRaces: 1, firstPlaces: 0, secondPlaces: 0, thirdPlaces: 1, forthPlaces:0}
}
if (makeFourth){
scoreboard[finalPlaceArray[3].name] = {totalRaces: 1, firstPlaces: 0, secondPlaces: 0, thirdPlaces: 0, forthPlaces:1}
}
// Save Scoreboard to local variable again.
window.localStorage.setItem("cd_scoreboard", JSON.stringify(scoreboard));
}
function getAlltimeWinnerFromScoreboard(){
console.log(scoreboard);
let mostwins = 0;
let winningHorse = "";
for( const horseScoreListing in scoreboard){
if (scoreboard[horseScoreListing].firstPlaces > mostwins) {
mostwins = scoreboard[horseScoreListing].firstPlaces;
winningHorse = horseScoreListing;
}
}
if (mostwins > 0 ){
console.log("The winningest horse of all time is " +winningHorse+ " with a total of " +mostwins+ " wins");
}
}
function deleteScoreboard() {
scoreboard = {};
window.localStorage.setItem("cd_scoreboard", JSON.stringify(scoreboard));
}
let deletecheck = false;
window.addEventListener("keypress", (e)=>{
if (e.key === 'd'){
if (deletecheck === false){
deletecheck = true;
console.log("Press again to delete scoreboard, press any other key to cancel");
}
else {
console.log("deleting scoreboard");
deleteScoreboard()
}
}else {
deletecheck = false
}
if(e.key ===" "){
document.body.click();
}
},false);