Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Architecture et Application Web -- 2022-2023 -- TP - RAJOHNSON
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
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
Architecture Web
Architecture et Application Web -- 2022-2023 -- TP - RAJOHNSON
Commits
a0e5de2c
Commit
a0e5de2c
authored
2 years ago
by
narindra.rajohnso
Browse files
Options
Downloads
Patches
Plain Diff
add begin game and send random question
parent
d9bf196c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
API/src/socket.io/ServerIO.ts
+73
-5
73 additions, 5 deletions
API/src/socket.io/ServerIO.ts
with
73 additions
and
5 deletions
API/src/socket.io/ServerIO.ts
+
73
−
5
View file @
a0e5de2c
...
...
@@ -3,6 +3,8 @@ import logger from '../logging/WinstonLogger';
import
http
from
'
http
'
;
import
Server
,
{
SocketIoInfo
}
from
"
../express/Server
"
;
import
{
UserInfo
}
from
"
./UserInfo
"
import
{
Database
}
from
"
../database/Database
"
;
import
{
Question
}
from
"
../database/models/Question
"
;
//TODO: In this file you can add/edit all things about socket.io
...
...
@@ -11,6 +13,11 @@ import {UserInfo} from "./UserInfo"
class
ServerIO
extends
IO
.
Server
{
players
:
{
[
key
:
string
]:
UserInfo
}
=
{};
playersReady
:
{
[
key
:
string
]:
boolean
}
=
{};
questions
!
:
Question
[];
currentQuestion
:
Question
;
nbGamer
=
0
;
constructor
(
server
:
http
.
Server
)
{
super
(
server
,
{
cors
:
{
...
...
@@ -19,6 +26,7 @@ class ServerIO extends IO.Server {
});
this
.
on
(
'
connection
'
,
(
socket
:
SocketIoInfo
)
=>
{
logger
.
info
(
`Nouveau socket vers
${
socket
.
client
.
conn
.
remoteAddress
}
`
);
console
.
log
(
`Socket info:
${
socket
.
user
.
username
}
//
${
socket
.
user
.
firstname
}
//
${
socket
.
user
.
lastname
}
`
);
...
...
@@ -34,12 +42,11 @@ class ServerIO extends IO.Server {
socket
.
user
.
firstname
,
socket
.
user
.
lastname
);
this
.
playersReady
[
playerKey
]
=
false
;
console
.
log
(
`Player
${
playerKey
}
is connected.`
);
}
this
.
testNumberofPlayer
();
this
.
registerEventsOnSocket
(
socket
);
});
...
...
@@ -47,6 +54,33 @@ class ServerIO extends IO.Server {
}
private
registerEventsOnSocket
(
socket
:
SocketIoInfo
)
{
const
playerKey
=
socket
.
user
.
username
;
socket
.
on
(
"
player-ready
"
,
()
=>
{
if
(
this
.
playersReady
[
playerKey
]
===
true
){
console
.
log
(
`player
${
playerKey
}
already ready`
)
}
else
{
logger
.
info
(
`player
${
playerKey
}
ready`
);
this
.
playersReady
[
playerKey
]
=
true
;
this
.
emit
(
"
ready-player
"
,
playerKey
);
}
this
.
testNumberOfReady
();
});
socket
.
on
(
"
player-not-ready
"
,
()
=>
{
if
(
this
.
playersReady
[
playerKey
]
===
false
){
console
.
log
(
`player
${
playerKey
}
already not ready`
)
}
else
{
logger
.
info
(
`player
${
playerKey
}
not ready`
);
this
.
playersReady
[
playerKey
]
=
false
;
this
.
emit
(
"
not-ready-player
"
,
playerKey
);
}
})
socket
.
on
(
"
on-game
"
,
async
()
=>
{
this
.
nbGamer
++
;
if
(
this
.
nbGamer
===
3
)
{
let
randomQuestion
=
await
this
.
getRandomQuestion
();
this
.
emit
(
"
question
"
,
randomQuestion
);
}
})
socket
.
on
(
'
disconnect
'
,
()
=>
{
logger
.
info
(
`Deconnexion du socket
${
socket
.
client
.
conn
.
remoteAddress
}
`
)
;
...
...
@@ -58,6 +92,7 @@ class ServerIO extends IO.Server {
if
(
this
.
players
[
playerKey
])
{
// Le joueur est connecté, retirez-le du dictionnaire
delete
this
.
players
[
playerKey
];
delete
this
.
playersReady
[
playerKey
];
console
.
log
(
`Player
${
playerKey
}
is disconnected.`
);
}
else
{
// Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal
...
...
@@ -69,9 +104,12 @@ class ServerIO extends IO.Server {
}
private
testNumberofPlayer
(){
this
.
emit
(
"
players
"
,
Object
.
values
(
this
.
players
));
if
(
Object
.
keys
(
this
.
players
).
length
<
3
){
this
.
emit
(
"
players-left
"
,
3
-
Object
.
keys
(
this
.
players
).
length
)
console
.
log
(
Object
.
keys
(
this
.
players
).
length
)
console
.
log
(
Object
.
keys
(
this
.
players
).
length
)
;
}
if
(
Object
.
keys
(
this
.
players
).
length
===
3
)
{
...
...
@@ -79,6 +117,36 @@ class ServerIO extends IO.Server {
this
.
emit
(
'
start-game
'
);
}
}
// Méthode pour récupérer une question aléatoire
async
getRandomQuestion
()
{
if
(
!
this
.
questions
||
this
.
questions
.
length
===
0
)
{
this
.
questions
=
await
Database
.
getAllQuestions
();
this
.
questions
.
forEach
(
q
=>
{
q
.
possibleResponse
=
JSON
.
parse
(
q
.
possibleResponse
);
q
.
correctResponse
=
parseInt
(
String
(
q
.
correctResponse
));
});
}
const
randomIndex
=
Math
.
floor
(
Math
.
random
()
*
this
.
questions
.
length
);
this
.
currentQuestion
=
this
.
questions
[
randomIndex
];
this
.
questions
.
splice
(
randomIndex
,
1
);
return
this
.
currentQuestion
;
}
private
testNumberOfReady
()
{
let
allPlayerReady
=
true
;
Object
.
values
(
this
.
playersReady
).
forEach
(
playerReady
=>
{
if
(
!
playerReady
)
{
allPlayerReady
=
false
;
}
});
if
(
allPlayerReady
)
{
this
.
emit
(
"
game-ready-start
"
);
}
}
}
...
...
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