projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Complete GameStorage.update. TODO: move transmission
[vchess.git]
/
client
/
src
/
utils
/
storage.js
diff --git
a/client/src/utils/storage.js
b/client/src/utils/storage.js
index
2c2d4be
..
f5c3b25
100644
(file)
--- a/
client/src/utils/storage.js
+++ b/
client/src/utils/storage.js
@@
-1,5
+1,4
@@
import { extractTime } from "@/utils/timeControl";
import { extractTime } from "@/utils/timeControl";
-import { shuffle } from "@/utils/alea";
// TODO: show game structure
//const newItem = [
// TODO: show game structure
//const newItem = [
@@
-79,10
+78,6
@@
export const GameStorage =
// localStorage:
init: function(o)
{
// localStorage:
init: function(o)
{
- // NOTE: when >= 3 players, better use an array + shuffle for mycolor
- const mycolor = (Math.random() < 0.5 ? "w" : "b");
- // Shuffle players order (white then black then other colors).
- const players = shuffle(o.players);
// Extract times (in [milli]seconds), set clocks, store in localStorage
const tc = extractTime(o.timeControl);
// Extract times (in [milli]seconds), set clocks, store in localStorage
const tc = extractTime(o.timeControl);
@@
-91,9
+86,8
@@
export const GameStorage =
{
gameId: o.gameId,
vname: o.vname,
{
gameId: o.gameId,
vname: o.vname,
- mycolor: mycolor,
fenStart: o.fenStart,
fenStart: o.fenStart,
- players: players,
+ players:
o.
players,
timeControl: o.timeControl,
increment: tc.increment,
mode: "live", //function for live games only
timeControl: o.timeControl,
increment: tc.increment,
mode: "live", //function for live games only
@@
-113,31
+107,37
@@
export const GameStorage =
localStorage.setItem("gameState", JSON.stringify(gameState));
},
localStorage.setItem("gameState", JSON.stringify(gameState));
},
+ getInitime: function()
+ {
+ const gameState = JSON.parse(localStorage.getItem("gameState"));
+ return gameState.initime;
+ },
+
// localStorage:
// localStorage:
- // TODO: also option to takeback a move ?
Is fen included in move ?
+ // TODO: also option to takeback a move ?
// NOTE: for live games only (all on server for corr)
// NOTE: for live games only (all on server for corr)
- update: function(
fen, moves, clocks, started, score)
+ update: function(
o) //colorIdx, move, fen, elapsed, increment, initime, score
{
let gameState = JSON.parse(localStorage.getItem("gameState"));
{
let gameState = JSON.parse(localStorage.getItem("gameState"));
- if (!!
fen
)
+ if (!!
o.move
)
{
{
- gameState.moves
= moves
;
- gameState.fen = fen;
- gameState.clocks
= clocks
;
+ gameState.moves
.push(o.move)
;
+ gameState.fen =
o.
fen;
+ gameState.clocks
[o.colorIdx] += (o.increment - o.elapsed)
;
}
}
- if (!!
started
)
- gameState.
started = started
;
- if (!!score)
- gameState.score = score;
+ if (!!
o.initime) //just a flag (true
)
+ gameState.
initime = Date.now()
;
+ if (!!
o.
score)
+ gameState.score =
o.
score;
localStorage.setItem("gameState", JSON.stringify(gameState));
localStorage.setItem("gameState", JSON.stringify(gameState));
- if (!!
score &&
score != "*")
+ if (!!
o.score && o.
score != "*")
transferToDb(); //game is over
},
// indexedDB:
// Since DB requests are asynchronous, require a callback using the result
// TODO: option for remote retrieval (third arg, or just "gameRef")
transferToDb(); //game is over
},
// indexedDB:
// Since DB requests are asynchronous, require a callback using the result
// TODO: option for remote retrieval (third arg, or just "gameRef")
- getLocal: function(
callback, gameId
)
+ getLocal: function(
gameId, callback
)
{
let games = [];
dbOperation((db) => {
{
let games = [];
dbOperation((db) => {
@@
-145,7
+145,7
@@
export const GameStorage =
let objectStore = db.transaction('games').objectStore('games');
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
let objectStore = db.transaction('games').objectStore('games');
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
- // if there is still another cursor to go, keep runing this code
+ // if there is still another cursor to go, keep run
n
ing this code
if (cursor)
{
games.push(cursor.value);
if (cursor)
{
games.push(cursor.value);
@@
-200,6
+200,6
@@
export const GameStorage =
}
// Game is local and not running
}
// Game is local and not running
-
getLocal(callback, gid
);
+
GameStorage.getLocal(gid, callback
);
},
};
},
};