Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS375_TriviaGame
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Team7
CS375_TriviaGame
Commits
ffe407d9
Commit
ffe407d9
authored
6 months ago
by
ea654
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
714a4798
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.js
+118
-0
118 additions, 0 deletions
server.js
with
118 additions
and
0 deletions
server.js
0 → 100644
+
118
−
0
View file @
ffe407d9
const
express
=
require
(
'
express
'
);
const
http
=
require
(
'
http
'
);
const
socketIo
=
require
(
'
socket.io
'
);
const
axios
=
require
(
'
axios
'
);
const
app
=
express
();
const
server
=
http
.
createServer
(
app
);
const
io
=
socketIo
(
server
);
const
PORT
=
process
.
env
.
PORT
||
3000
;
const
rooms
=
{};
app
.
use
(
express
.
static
(
'
public
'
));
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
console
.
log
(
'
A user connected:
'
,
socket
.
id
);
socket
.
on
(
'
createRoom
'
,
()
=>
{
const
roomCode
=
Math
.
random
().
toString
(
36
).
substring
(
2
,
7
).
toUpperCase
();
rooms
[
roomCode
]
=
{
players
:
[],
questions
:
[]
};
socket
.
join
(
roomCode
);
socket
.
emit
(
'
roomCreated
'
,
roomCode
);
console
.
log
(
`Room created with code
${
roomCode
}
`
);
});
socket
.
on
(
'
joinRoom
'
,
(
roomCode
)
=>
{
if
(
rooms
[
roomCode
])
{
socket
.
join
(
roomCode
);
socket
.
emit
(
'
roomJoined
'
);
console
.
log
(
`User
${
socket
.
id
}
joined room
${
roomCode
}
`
);
}
else
{
socket
.
emit
(
'
error
'
,
'
Room not found
'
);
}
});
socket
.
on
(
'
startGame
'
,
({
roomCode
,
category
,
difficulty
})
=>
{
fetchQuestions
(
category
,
difficulty
).
then
((
questions
)
=>
{
if
(
!
questions
||
questions
.
length
===
0
)
{
socket
.
emit
(
'
error
'
,
'
Failed to load questions
'
);
return
;
}
rooms
[
roomCode
].
questions
=
questions
;
io
.
in
(
roomCode
).
emit
(
'
newQuestion
'
,
questions
[
0
]);
});
});
socket
.
on
(
'
submitAnswer
'
,
(
data
)
=>
{
const
room
=
rooms
[
data
.
roomCode
];
if
(
!
room
||
!
room
.
questions
||
room
.
questions
.
length
===
0
)
{
socket
.
emit
(
'
error
'
,
'
No questions available
'
);
return
;
}
const
question
=
room
.
questions
[
0
];
const
correct
=
question
.
correctAnswer
===
data
.
answer
;
socket
.
emit
(
'
answerResult
'
,
{
correct
,
correctAnswer
:
question
.
correctAnswer
});
});
socket
.
on
(
'
nextQuestion
'
,
(
roomCode
)
=>
{
const
room
=
rooms
[
roomCode
];
if
(
room
&&
room
.
questions
.
length
>
0
)
{
room
.
questions
.
shift
();
if
(
room
.
questions
.
length
>
0
)
{
io
.
in
(
roomCode
).
emit
(
'
newQuestion
'
,
room
.
questions
[
0
]);
}
else
{
io
.
in
(
roomCode
).
emit
(
'
gameOver
'
);
}
}
else
{
io
.
in
(
roomCode
).
emit
(
'
gameOver
'
);
}
});
socket
.
on
(
'
disconnect
'
,
()
=>
{
console
.
log
(
'
User disconnected:
'
,
socket
.
id
);
});
});
function
fetchQuestions
(
category
,
difficulty
)
{
return
axios
.
get
(
`https://opentdb.com/api.php`
,
{
params
:
{
amount
:
5
,
category
,
difficulty
,
type
:
'
multiple
'
,
},
})
.
then
((
response
)
=>
{
return
response
.
data
.
results
.
map
((
q
)
=>
{
let
options
=
[
q
.
correct_answer
];
for
(
let
incorrect
of
q
.
incorrect_answers
)
{
options
.
push
(
incorrect
);
}
options
=
shuffleArray
(
options
);
return
{
question
:
q
.
question
,
options
:
options
,
correctAnswer
:
q
.
correct_answer
,
};
});
})
.
catch
((
error
)
=>
{
console
.
error
(
'
Error fetching questions:
'
,
error
);
return
[];
});
}
function
shuffleArray
(
array
)
{
for
(
let
i
=
array
.
length
-
1
;
i
>
0
;
i
--
)
{
const
j
=
Math
.
floor
(
Math
.
random
()
*
(
i
+
1
));
[
array
[
i
],
array
[
j
]]
=
[
array
[
j
],
array
[
i
]];
}
return
array
;
}
server
.
listen
(
PORT
,
()
=>
console
.
log
(
`Server is running on http://localhost:
${
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
register
or
sign in
to comment