Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
2doku
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kes444
2doku
Commits
71500449
Commit
71500449
authored
Sep 2, 2023
by
AceZephyr
Browse files
Options
Downloads
Patches
Plain Diff
Refactored end of game logic
parent
fa0a300c
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/login.js
+3
-2
3 additions, 2 deletions
app/login.js
app/semipublic/game.html
+74
-68
74 additions, 68 deletions
app/semipublic/game.html
app/server.js
+29
-14
29 additions, 14 deletions
app/server.js
with
106 additions
and
84 deletions
app/login.js
+
3
−
2
View file @
71500449
...
@@ -53,6 +53,7 @@ function writeUser(user, hash) {
...
@@ -53,6 +53,7 @@ function writeUser(user, hash) {
}
}
function
writeRecord
(
user
,
score
)
{
function
writeRecord
(
user
,
score
)
{
console
.
log
(
user
,
score
)
db
.
run
(
"
INSERT INTO leaderboard (username, score) VALUES (?, ?);
"
,
[
user
,
score
]);
db
.
run
(
"
INSERT INTO leaderboard (username, score) VALUES (?, ?);
"
,
[
user
,
score
]);
const
data
=
db
.
export
();
const
data
=
db
.
export
();
const
buffer
=
Buffer
.
from
(
data
);
const
buffer
=
Buffer
.
from
(
data
);
...
@@ -76,8 +77,8 @@ async function signup(user, pass) {
...
@@ -76,8 +77,8 @@ async function signup(user, pass) {
async
function
login
(
user
,
pass
)
{
async
function
login
(
user
,
pass
)
{
const
row
=
queryUser
(
user
);
const
row
=
queryUser
(
user
);
if
(
!
row
)
if
(
!
row
[
'
username
'
]
)
return
{
"
code
"
:
40
4
,
"
error
"
:
"
Username
does not exis
t
"
};
return
{
"
code
"
:
40
0
,
"
error
"
:
"
Username
/password combination is incorrec
t
"
};
try
{
try
{
if
(
!
await
argon2
.
verify
(
row
[
'
pwhash
'
],
pass
))
if
(
!
await
argon2
.
verify
(
row
[
'
pwhash
'
],
pass
))
return
{
"
code
"
:
400
,
"
error
"
:
"
Username/password combination is incorrect
"
};
return
{
"
code
"
:
400
,
"
error
"
:
"
Username/password combination is incorrect
"
};
...
...
This diff is collapsed.
Click to expand it.
app/semipublic/game.html
+
74
−
68
View file @
71500449
...
@@ -45,13 +45,19 @@
...
@@ -45,13 +45,19 @@
BOARD1
.
editable
=
args
.
editable
;
BOARD1
.
editable
=
args
.
editable
;
displayPlayerScores
(
args
.
playerScores
);
displayPlayerScores
(
args
.
playerScores
);
hideJoinButton
();
hideJoinButton
();
socket
.
emit
(
"
verify solution
"
,
{
board
:
args
.
boardDisplay
,
playerScores
:
args
.
playerScores
});
});
});
socket
.
on
(
"
game stop
"
,
()
=>
{
socket
.
on
(
"
game stop
"
,
()
=>
{
window
.
location
.
href
=
"
/
"
;
window
.
location
.
href
=
"
/
"
;
});
});
socket
.
on
(
"
game complete
"
,
args
=>
{
const
bestScore
=
Object
.
values
(
args
.
playerScores
).
reduce
((
a
,
b
)
=>
a
>
b
?
a
:
b
);
const
winners
=
Object
.
keys
(
args
.
playerScores
).
filter
(
u
=>
args
.
playerScores
[
u
]
===
bestScore
).
reduce
((
a
,
b
)
=>
`
${
a
}
,
${
b
}
`
);
window
.
alert
(
`Game complete!\nWinner:
${
winners
}
with
${
bestScore
}
points`
);
window
.
location
.
href
=
"
/
"
;
})
socket
.
on
(
"
please log in
"
,
()
=>
{
socket
.
on
(
"
please log in
"
,
()
=>
{
window
.
alert
(
"
Please log in to join a game.
"
);
window
.
alert
(
"
Please log in to join a game.
"
);
window
.
location
.
href
=
"
/login
"
;
window
.
location
.
href
=
"
/login
"
;
...
...
This diff is collapsed.
Click to expand it.
app/server.js
+
29
−
14
View file @
71500449
...
@@ -18,6 +18,8 @@ const hostname = "localhost";
...
@@ -18,6 +18,8 @@ const hostname = "localhost";
const
GAMES
=
new
Map
();
// maps game id -> game
const
GAMES
=
new
Map
();
// maps game id -> game
const
DEBUG_FLAG
=
false
;
const
ROOM_OPEN
=
0
;
const
ROOM_OPEN
=
0
;
const
ROOM_PLAYING
=
1
;
const
ROOM_PLAYING
=
1
;
const
ROOM_TERMINATED
=
-
1
;
const
ROOM_TERMINATED
=
-
1
;
...
@@ -53,6 +55,10 @@ class Game {
...
@@ -53,6 +55,10 @@ class Game {
initGame
()
{
initGame
()
{
const
gen
=
generateBoard
(
this
.
difficulty
);
const
gen
=
generateBoard
(
this
.
difficulty
);
this
.
boardDisplay
=
gen
[
0
];
this
.
boardDisplay
=
gen
[
0
];
if
(
DEBUG_FLAG
)
{
this
.
boardDisplay
=
JSON
.
parse
(
JSON
.
stringify
(
gen
[
1
]));
this
.
boardDisplay
[
0
][
0
]
=
0
;
}
this
.
boardSolution
=
gen
[
1
];
this
.
boardSolution
=
gen
[
1
];
this
.
boardLock
=
ARR9
.
map
(
y
=>
ARR9
.
map
(
x
=>
this
.
boardDisplay
[
y
][
x
]
?
1
:
0
));
this
.
boardLock
=
ARR9
.
map
(
y
=>
ARR9
.
map
(
x
=>
this
.
boardDisplay
[
y
][
x
]
?
1
:
0
));
this
.
boardColors
=
ARR9
.
map
(
y
=>
ARR9
.
map
(
x
=>
this
.
boardDisplay
[
y
][
x
]
?
1
:
0
));
this
.
boardColors
=
ARR9
.
map
(
y
=>
ARR9
.
map
(
x
=>
this
.
boardDisplay
[
y
][
x
]
?
1
:
0
));
...
@@ -79,6 +85,24 @@ class Game {
...
@@ -79,6 +85,24 @@ class Game {
}
}
}
}
isSolved
()
{
for
(
let
y
=
0
;
y
<
9
;
y
++
)
{
for
(
let
x
=
0
;
x
<
9
;
x
++
)
{
if
(
this
.
boardDisplay
[
y
][
x
]
!==
this
.
boardSolution
[
y
][
x
])
{
return
false
;
}
}
}
return
true
;
}
endGame
()
{
this
.
sendAll
(
"
game complete
"
,
this
.
packageBoardState
(
false
));
for
(
let
user
in
this
.
playerScores
)
{
login
.
writeRecord
(
user
,
this
.
playerScores
[
user
]);
}
}
input
(
client
,
user
,
x
,
y
,
val
)
{
input
(
client
,
user
,
x
,
y
,
val
)
{
let
dPoints
=
0
;
let
dPoints
=
0
;
if
(
!
(
typeof
x
===
"
number
"
&&
typeof
y
===
"
number
"
&&
typeof
val
===
"
number
"
&&
0
<=
x
&&
x
<=
8
&&
0
<=
y
&&
y
<=
8
&&
1
<=
val
&&
val
<=
9
))
if
(
!
(
typeof
x
===
"
number
"
&&
typeof
y
===
"
number
"
&&
typeof
val
===
"
number
"
&&
0
<=
x
&&
x
<=
8
&&
0
<=
y
&&
y
<=
8
&&
1
<=
val
&&
val
<=
9
))
...
@@ -97,6 +121,11 @@ class Game {
...
@@ -97,6 +121,11 @@ class Game {
}
}
this
.
boardDisplay
[
y
][
x
]
=
val
;
this
.
boardDisplay
[
y
][
x
]
=
val
;
this
.
playerScores
[
user
]
+=
dPoints
;
this
.
playerScores
[
user
]
+=
dPoints
;
if
(
this
.
isSolved
())
{
this
.
endGame
();
}
return
true
;
return
true
;
}
}
...
@@ -327,20 +356,6 @@ io.on("connection", (socket) => {
...
@@ -327,20 +356,6 @@ io.on("connection", (socket) => {
}
}
});
});
socket
.
on
(
"
verify solution
"
,
args
=>
{
const
{
board
,
playerScores
}
=
args
;
// Check for missing/incorrect answers
for
(
let
y
=
0
;
y
<
9
;
y
++
)
{
for
(
let
x
=
0
;
x
<
9
;
x
++
)
{
if
(
board
[
y
][
x
]
!==
game
.
boardSolution
[
y
][
x
])
{
return
;
}
}
}
// We solved the sudoku!
login
.
writeRecord
(
user
,
playerScores
[
user
]);
});
socket
.
on
(
"
disconnect
"
,
()
=>
{
socket
.
on
(
"
disconnect
"
,
()
=>
{
if
(
game
)
{
if
(
game
)
{
game
.
leave
(
socketId
,
user
);
game
.
leave
(
socketId
,
user
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment