-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-storage-utils.js
178 lines (119 loc) · 3.61 KB
/
local-storage-utils.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const PLAYERS = 'PLAYERS';
const WINNINGSCORE = 'WINNINGSCORE';
export function setPlayers(players) { // takes player array and sets them in local storage
const stringyPlayers = JSON.stringify(players);
localStorage.setItem(PLAYERS, stringyPlayers);
}
export function getPlayers() { // gets local storage and returns array
const stringyPlayers = localStorage.getItem(PLAYERS);
const parsedPlayers = JSON.parse(stringyPlayers);
return parsedPlayers;
}
export function setWinningScore(score) {
const stringyScore = JSON.stringify(score);
localStorage.setItem(WINNINGSCORE, stringyScore);
}
export function getWinningScore() { // gets local storage and returns array
const stringyScore = localStorage.getItem(WINNINGSCORE);
const parsedScore = JSON.parse(stringyScore);
return parsedScore;
}
export function createPlayers(nameOne, nameTwo) {
const playerOne = {
name: nameOne,
roundScore: 0,
score: 0,
zilches: 0,
turnCount: 0,
diceroll: 0,
turn: true,
zilchRun: 0,
bank: 0
};
const playerTwo = {
name: nameTwo,
roundScore: 0,
score: 0,
zilches: 0,
turnCount: 0,
diceroll: 0,
turn: false,
zilchRun: 0,
bank: 0
};
const players = [playerOne, playerTwo];
setPlayers(players);
}
export function updateScore(playerScore) {
const players = getPlayers();
const playerName = getCurrentPlayer().name; //thanks tis
if (players[0].name === playerName) {
players[0].score += playerScore;
players[0].roundScore = playerScore;
players[0].turnCount += 1;
}
if (players[1].name === playerName) {
players[1].score += playerScore;
players[1].roundScore = playerScore;
players[1].turnCount += 1;
}
setPlayers(players);
}
export function clearZilchRun() {
const players = getPlayers();
const playerName = getCurrentPlayer().name;
if (players[0].name === playerName) {
players[0].zilchRun = 0;
}
if (players[1].name === playerName) {
players[1].zilchRun = 0;
}
setPlayers(players);
}
export function updateZilch() {
const players = getPlayers();
const playerName = getCurrentPlayer().name;
if (players[0].name === playerName) {
players[0].zilches = players[0].zilches + 1;
players[0].zilchRun = players[0].zilchRun + 1;
}
if (players[1].name === playerName) {
players[1].zilches = players[1].zilches + 1;
players[1].zilchRun = players[1].zilchRun + 1;
}
setPlayers(players);
}
export function changeCurrrentPlayer() {
const players = getPlayers();
players[0].turn = !players[0].turn;
players[1].turn = !players[1].turn;
setPlayers(players);
}
export function getCurrentPlayer() {
const players = getPlayers();
for (let player of players) {
if (player.turn === true) return player;
}
}
export function setBankZero() {
const players = getPlayers();
const playerName = getCurrentPlayer().name;
if (players[0].name === playerName) {
players[0].bank = 0;
}
if (players[1].name === playerName) {
players[1].bank = 0;
}
setPlayers(players);
}
export function increaseBank(score) {
const players = getPlayers();
const playerName = getCurrentPlayer().name; //thanks tis
if (players[0].name === playerName) {
players[0].bank = players[0].bank + score;
}
if (players[1].name === playerName) {
players[1].bank = players[1].bank + score;
}
setPlayers(players);
}