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
86488c1b
Commit
86488c1b
authored
2 years ago
by
narindra.rajohnso
Browse files
Options
Downloads
Patches
Plain Diff
10 questions and restart logic add
parent
aaf4b226
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
+43
-32
43 additions, 32 deletions
API/src/socket.io/ServerIO.ts
with
43 additions
and
32 deletions
API/src/socket.io/ServerIO.ts
+
43
−
32
View file @
86488c1b
import
*
as
IO
from
'
socket.io
'
;
import
*
as
IO
from
'
socket.io
'
;
import
logger
from
'
../logging/WinstonLogger
'
;
import
logger
from
'
../logging/WinstonLogger
'
;
import
http
from
'
http
'
;
import
http
from
'
http
'
;
import
Server
,
{
SocketIoInfo
}
from
"
../express/Server
"
;
import
{
SocketIoInfo
}
from
"
../express/Server
"
;
import
{
UserInfo
}
from
"
./UserInfo
"
import
{
UserInfo
}
from
"
./UserInfo
"
import
{
Database
}
from
"
../database/Database
"
;
import
{
Database
}
from
"
../database/Database
"
;
import
{
Question
}
from
"
../database/models/Question
"
;
import
{
Question
}
from
"
../database/models/Question
"
;
import
{
response
}
from
"
express
"
;
//TODO: In this file you can add/edit all things about socket.io
//TODO: In this file you can add/edit all things about socket.io
...
@@ -33,6 +32,15 @@ class ServerIO extends IO.Server {
...
@@ -33,6 +32,15 @@ class ServerIO extends IO.Server {
this
.
on
(
'
connection
'
,
(
socket
:
SocketIoInfo
)
=>
{
this
.
on
(
'
connection
'
,
(
socket
:
SocketIoInfo
)
=>
{
logger
.
info
(
`Nouveau socket vers
${
socket
.
client
.
conn
.
remoteAddress
}
`
);
logger
.
info
(
`Nouveau socket vers
${
socket
.
client
.
conn
.
remoteAddress
}
`
);
console
.
log
(
`Socket info:
${
socket
.
user
.
username
}
//
${
socket
.
user
.
firstname
}
//
${
socket
.
user
.
lastname
}
`
);
console
.
log
(
`Socket info:
${
socket
.
user
.
username
}
//
${
socket
.
user
.
firstname
}
//
${
socket
.
user
.
lastname
}
`
);
this
.
initializeGame
(
socket
);
this
.
testNumberofPlayer
();
this
.
registerEventsOnSocket
(
socket
);
});
}
initializeGame
(
socket
:
SocketIoInfo
){
const
playerKey
=
socket
.
user
.
username
;
const
playerKey
=
socket
.
user
.
username
;
// Vérifiez si le joueur existe déjà dans le dictionnaire
// Vérifiez si le joueur existe déjà dans le dictionnaire
if
(
this
.
players
[
playerKey
])
{
if
(
this
.
players
[
playerKey
])
{
...
@@ -45,19 +53,16 @@ class ServerIO extends IO.Server {
...
@@ -45,19 +53,16 @@ class ServerIO extends IO.Server {
socket
.
user
.
firstname
,
socket
.
user
.
firstname
,
socket
.
user
.
lastname
socket
.
user
.
lastname
);
);
if
(
!
this
.
playersReady
.
hasOwnProperty
(
playerKey
)){
this
.
playersReady
[
playerKey
]
=
false
;
this
.
playersReady
[
playerKey
]
=
false
;
}
if
(
!
this
.
playersScore
.
hasOwnProperty
(
playerKey
))
{
this
.
playersScore
[
playerKey
]
=
0
;
this
.
playersScore
[
playerKey
]
=
0
;
}
this
.
nbQuestion
=
0
;
this
.
nbQuestion
=
0
;
console
.
log
(
`Player
${
playerKey
}
is connected.`
);
console
.
log
(
`Player
${
playerKey
}
is connected.`
);
}
}
this
.
testNumberofPlayer
();
this
.
registerEventsOnSocket
(
socket
);
});
}
}
private
registerEventsOnSocket
(
socket
:
SocketIoInfo
)
{
private
registerEventsOnSocket
(
socket
:
SocketIoInfo
)
{
const
playerKey
=
socket
.
user
.
username
;
const
playerKey
=
socket
.
user
.
username
;
socket
.
on
(
"
player-ready
"
,
()
=>
{
socket
.
on
(
"
player-ready
"
,
()
=>
{
...
@@ -91,26 +96,27 @@ class ServerIO extends IO.Server {
...
@@ -91,26 +96,27 @@ class ServerIO extends IO.Server {
}
}
else
{
else
{
let
question
=
{...
this
.
currentQuestion
};
let
question
=
{...
this
.
currentQuestion
};
question
=
question
.
dataValues
as
Question
;
question
.
correctResponse
=-
1
;
randomQuestion
=
question
;
randomQuestion
=
question
;
}
}
console
.
log
(
"
send question
"
,
randomQuestion
);
this
.
emit
(
"
question
"
,
randomQuestion
);
this
.
emit
(
"
question
"
,
randomQuestion
);
this
.
nbQuestion
++
;
}
}
});
});
socket
.
on
(
"
validate-question
"
,
async
(
responseSelected
)
=>
{
socket
.
on
(
"
validate-question
"
,
async
(
responseSelected
)
=>
{
let
randomQuestion
=
await
this
.
getRandom
Question
(
);
console
.
log
(
"
current question:
"
,
this
.
current
Question
);
if
(
responseSelected
===
this
.
currentQuestion
.
correctResponse
)
{
if
(
responseSelected
===
this
.
currentQuestion
.
correctResponse
)
{
// le joueur gagne 1 point
// le joueur gagne 1 point
console
.
log
(
`joueur
${
playerKey
}
gagne 1 point`
)
this
.
playersScore
[
playerKey
]
+=
1
;
this
.
playersScore
[
playerKey
]
+=
1
;
}
else
{
}
else
{
console
.
log
(
`joueur
${
playerKey
}
perd 2 point`
)
// le joueur perd deux points
// le joueur perd deux points
this
.
playersScore
[
playerKey
]
-=
2
;
this
.
playersScore
[
playerKey
]
-=
2
;
}
}
this
.
nbQuestion
++
;
console
.
log
(
"
playerScore:
"
,
this
.
playersScore
);
if
(
this
.
nbQuestion
<
10
){
let
randomQuestion
=
await
this
.
getRandomQuestion
();
if
(
this
.
nbQuestion
<=
10
){
this
.
emit
(
"
question
"
,
randomQuestion
);
this
.
emit
(
"
question
"
,
randomQuestion
);
}
else
{
}
else
{
const
playersScoreFormatted
=
Object
.
keys
(
this
.
playersScore
).
reduce
((
formatted
:
any
,
key
)
=>
{
const
playersScoreFormatted
=
Object
.
keys
(
this
.
playersScore
).
reduce
((
formatted
:
any
,
key
)
=>
{
...
@@ -119,7 +125,13 @@ class ServerIO extends IO.Server {
...
@@ -119,7 +125,13 @@ class ServerIO extends IO.Server {
},
{});
},
{});
this
.
emit
(
"
game-finished
"
,
playersScoreFormatted
);
this
.
emit
(
"
game-finished
"
,
playersScoreFormatted
);
}
}
this
.
nbQuestion
++
;
});
socket
.
on
(
"
restart-game
"
,
()
=>
{
if
(
Object
.
keys
(
this
.
players
).
length
===
3
)
this
.
players
=
{};
this
.
initializeGame
(
socket
);
})
})
socket
.
on
(
'
disconnect
'
,
()
=>
{
socket
.
on
(
'
disconnect
'
,
()
=>
{
...
@@ -133,6 +145,7 @@ class ServerIO extends IO.Server {
...
@@ -133,6 +145,7 @@ class ServerIO extends IO.Server {
// Le joueur est connecté, retirez-le du dictionnaire
// Le joueur est connecté, retirez-le du dictionnaire
delete
this
.
players
[
playerKey
];
delete
this
.
players
[
playerKey
];
delete
this
.
playersReady
[
playerKey
];
delete
this
.
playersReady
[
playerKey
];
delete
this
.
playersScore
[
playerKey
];
console
.
log
(
`Player
${
playerKey
}
is disconnected.`
);
console
.
log
(
`Player
${
playerKey
}
is disconnected.`
);
}
else
{
}
else
{
// Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal
// Le joueur n'est pas trouvé dans le dictionnaire, cela peut être un cas anormal
...
@@ -170,12 +183,10 @@ class ServerIO extends IO.Server {
...
@@ -170,12 +183,10 @@ class ServerIO extends IO.Server {
}
}
const
randomIndex
=
Math
.
floor
(
Math
.
random
()
*
this
.
questions
.
length
);
const
randomIndex
=
Math
.
floor
(
Math
.
random
()
*
this
.
questions
.
length
);
this
.
currentQuestion
=
this
.
questions
[
randomIndex
];
this
.
currentQuestion
=
this
.
questions
[
randomIndex
]
.
dataValues
;
this
.
questions
.
splice
(
randomIndex
,
1
);
this
.
questions
.
splice
(
randomIndex
,
1
);
// pour ne pas envoyer au frontend la reponse attendue
// pour ne pas envoyer au frontend la reponse attendue
let
randomQuestion
=
{...
this
.
currentQuestion
};
return
{...
this
.
currentQuestion
};
randomQuestion
=
randomQuestion
.
dataValues
as
Question
;
return
randomQuestion
;
}
}
private
testNumberOfReady
()
{
private
testNumberOfReady
()
{
...
...
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