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
b34f93c4
Commit
b34f93c4
authored
Aug 30, 2023
by
Patrick
Browse files
Options
Downloads
Patches
Plain Diff
Added code to write to leaderboard if the board is solved.
parent
37449ad1
Branches
Branches containing commit
No related tags found
1 merge request
!1
Update 1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/login.js
+9
-1
9 additions, 1 deletion
app/login.js
app/semipublic/game.html
+2
-1
2 additions, 1 deletion
app/semipublic/game.html
app/server.js
+22
-0
22 additions, 0 deletions
app/server.js
with
33 additions
and
2 deletions
app/login.js
+
9
−
1
View file @
b34f93c4
...
...
@@ -16,6 +16,7 @@ initSqlJs().then(function (SQL) {
const
filebuffer
=
fs
.
readFileSync
(
db_path
);
db
=
new
SQL
.
Database
(
filebuffer
);
db
.
run
(
"
CREATE TABLE IF NOT EXISTS users(username VARCHAR(20), pwhash VARCHAR(100));
"
);
db
.
run
(
"
CREATE TABLE IF NOT EXISTS leaderboard(username VARCHAR(20), score INT);
"
);
const
data
=
db
.
export
();
const
buffer
=
Buffer
.
from
(
data
);
fs
.
writeFileSync
(
env
[
"
db_path
"
],
buffer
);
...
...
@@ -51,6 +52,13 @@ function writeUser(user, hash) {
fs
.
writeFileSync
(
env
[
"
db_path
"
],
buffer
);
}
function
writeRecord
(
user
,
score
)
{
db
.
run
(
"
INSERT INTO leaderboard (username, score) VALUES (?, ?);
"
,
[
user
,
score
]);
const
data
=
db
.
export
();
const
buffer
=
Buffer
.
from
(
data
);
fs
.
writeFileSync
(
env
[
"
db_path
"
],
buffer
);
}
async
function
signup
(
user
,
pass
)
{
if
(
user
.
length
<
2
||
user
.
length
>
20
)
return
{
"
code
"
:
400
,
"
error
"
:
"
Username should be 2-20 characters
"
};
...
...
@@ -79,4 +87,4 @@ async function login(user, pass) {
}
}
module
.
exports
=
{
login
,
signup
,
getUserForToken
};
module
.
exports
=
{
login
,
signup
,
getUserForToken
,
writeRecord
};
This diff is collapsed.
Click to expand it.
app/semipublic/game.html
+
2
−
1
View file @
b34f93c4
...
...
@@ -37,7 +37,8 @@
BOARD1
.
updateBoard
(
args
.
boardDisplay
,
args
.
boardLock
,
args
.
boardColors
);
document
.
querySelector
(
"
#join-game
"
).
style
.
display
=
"
none
"
;
displayPlayerScores
(
args
.
playerScores
);
socket
.
emit
(
"
verify solution
"
,
args
.
boardDisplay
);
console
.
log
(
args
.
boardDisplay
);
socket
.
emit
(
"
verify solution
"
,
{
board
:
args
.
boardDisplay
,
playerScores
:
args
.
playerScores
});
});
socket
.
on
(
"
game stop
"
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
app/server.js
+
22
−
0
View file @
b34f93c4
...
...
@@ -200,6 +200,13 @@ function ensureAuth(req, res, fn) {
fn
(
user
);
}
function
writeUser
(
user
,
hash
)
{
db
.
run
(
"
INSERT INTO users (username, pwhash) VALUES (?, ?);
"
,
[
user
,
hash
]);
const
data
=
db
.
export
();
const
buffer
=
Buffer
.
from
(
data
);
fs
.
writeFileSync
(
env
[
"
db_path
"
],
buffer
);
}
app
.
get
(
"
/
"
,
(
req
,
res
)
=>
{
clearBadToken
(
req
,
res
);
return
res
.
sendFile
(
__dirname
+
"
/semipublic/index.html
"
);
...
...
@@ -305,6 +312,20 @@ 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
"
,
()
=>
{
if
(
game
)
{
game
.
userLeave
(
socketId
,
user
);
...
...
@@ -312,6 +333,7 @@ io.on("connection", (socket) => {
});
});
httpServer
.
listen
(
port
,
hostname
,
()
=>
{
console
.
log
(
`http://
${
hostname
}
:
${
port
}
`
);
});
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