-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (133 loc) · 4.5 KB
/
app.js
File metadata and controls
148 lines (133 loc) · 4.5 KB
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
'use strict';
// Pull Storage and Push storage functions are probably you data problems
Player.all = [];
var startGame = document.getElementById('start_game');
function Player(name) {
this.name = name;
this.wins = 0;
this.losses = 0;
this.totalMatches = 0;
this.winRatio = 0;
this.match = [];
this.nemesis;
Player.all.push(this);
}
function Match(winner,loser) {
this.winner = winner;
this.loser = loser;
this.winnerScore = 9;
this.loserScore = 0;
this.opponent = '';
}
function updatePlayerArray(playerOneOrTwo) {
for (var i = 0; i < Player.all.length; i++) {
if (playerOneOrTwo.name === Player.all[i].name) {
console.log('Found player to update');
Player.all[i] = playerOneOrTwo;
}
}
}
var teamRePongTheme = new Audio('img/RePongTheme.m4a');
function go(event) {
event.preventDefault();
document.getElementById('game').style.display = 'block';
document.getElementById('start').style.display = 'none';
// document.getElementById('second').style.display = 'none';
if (localStorage.length > 0) {
pullStorage();
}
var getpone = event.target.playerone.value;
var getptwo = event.target.playertwo.value;
Player.playerOne = initiatePlayers(getpone);
Player.playerTwo = initiatePlayers(getptwo);
// Start the game execution
game.p1.score = 0;
game.p2.score = 0;
//Used code from Stackexchange:
//https://stackoverflow.com/questions/8916620/disable-arrow-key-scrolling-in-users-browser
window.addEventListener('keydown', function(e) {
// space and arrow keys
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
}, false);
//////////////////////////////////////////
MainLoop();
teamRePongTheme.play();
}
// return new Player(value);
//
function endResults() {
console.log('this score of p1 is' + game.p1.score);
console.log('this score of p2 is' + game.p2.score);
Player.playerOne.totalMatches++;
Player.playerTwo.totalMatches++;
if(game.p1.score > game.p2.score) {
console.log('P1 score is > p2 score');
Player.playerOne.wins++;
Player.playerTwo.losses++;
matchMaker(Player.playerOne.name, Player.playerTwo.name, game.p2.score);
} else {
Player.playerTwo.wins++;
Player.playerOne.losses++;
matchMaker(Player.playerTwo.name, Player.playerOne.name, game.p1.score);
}
Player.playerOne.winRatio = (Player.playerOne.wins / Player.playerOne.totalMatches);
Player.playerTwo.winRatio = (Player.playerTwo.wins / Player.playerTwo.totalMatches);
}
function matchMaker(winner,loser, loserScore) {
var matchP1 = new Match(winner, loser);
matchP1.opponent = Player.playerTwo.name;
matchP1.loserScore = loserScore;
Player.playerOne.match.push(matchP1);
var matchP2 = new Match(winner, loser);
matchP2.opponent = Player.playerOne.name;
matchP2.loserScore = loserScore;
Player.playerTwo.match.push(matchP2);
lastMatch(Player.playerOne);
}
function pullStorage() {
var storeAll = localStorage.getItem('all');
Player.all = JSON.parse(storeAll);
}
function checkPlayers(array, value) {
for (var i = 0; i < array.length; i++) {
if (value === array[i].name) {
console.log('Found match');
alert('Welcome back ' + value + '.');
return array[i];
}
}
return new Player(value);
}
function initiatePlayers(val) {
var playerInit;
var playerMatch = val;
if (Player.all.length > 0) {
playerInit = checkPlayers(Player.all, playerMatch);
console.log('I found a existing player ' + playerInit.name + ' and local storage exists!');
} else {
playerInit = new Player(playerMatch);
console.log('No local storage. Created a new player.');
}
return playerInit;
}
function pushStorage() {
console.log('push');
var storeAll = JSON.stringify(Player.all);
localStorage.setItem('all', storeAll);
if (localStorage.all) {
Player.all = storeAll;
}
}
function lastMatch(player) {
var i = player.match.length - 1;
playerCard('start', 'div', player.name + i);
playerCard(player.name + i, 'h1', '', player.match[i].winner + ' VS ' + player.match[i].loser);
playerCard(player.name + i, 'ul', player.name + i + '_stats');
playerCard(player.name + i + '_stats', 'li', '', 'Winner: ' + player.match[i].winner);
playerCard(player.name + i + '_stats', 'li', '', 'Loser: ' + player.match[i].loser);
playerCard(player.name + i + '_stats', 'li', '', player.match[i].winner + ': ' + player.match[i].winnerScore);
playerCard(player.name + i + '_stats', 'li', '', player.match[i].loser + ': ' + player.match[i].loserScore);
}
startGame.addEventListener('submit', go);