Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ehasa-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
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
WIMMA Lab 2019
Overflow
ehasa-backend
Commits
4616442a
Commit
4616442a
authored
5 years ago
by
L4168
Browse files
Options
Downloads
Patches
Plain Diff
cleaning up joinGroup and createGroup functions
parent
54ba2839
No related branches found
No related tags found
2 merge requests
!59
Development to master
,
!18
Join game
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/game/game.service.ts
+34
-50
34 additions, 50 deletions
src/game/game.service.ts
with
34 additions
and
50 deletions
src/game/game.service.ts
+
34
−
50
View file @
4616442a
import
{
Injectable
,
HttpException
,
HttpStatus
}
from
'
@nestjs/common
'
;
import
{
InjectRepository
}
from
'
@nestjs/typeorm
'
;
import
{
Repository
,
Not
,
In
}
from
'
typeorm
'
;
import
{
Repository
,
Not
}
from
'
typeorm
'
;
import
{
GameEntity
,
FactionEntity
,
Game_PersonEntity
}
from
'
./game.entity
'
;
import
{
GameEntity
,
Game_PersonEntity
}
from
'
./game.entity
'
;
import
{
GameDTO
}
from
'
./game.dto
'
;
import
{
PersonEntity
}
from
'
../user/user.entity
'
;
import
{
GameGroupEntity
}
from
'
./group.entity
'
;
import
{
FactionEntity
}
from
'
./faction.entity
'
;
@
Injectable
()
export
class
GameService
{
...
...
@@ -68,10 +69,10 @@ export class GameService {
// get all the factions that are associated with the game to deny duplicate entries
const
factions
=
await
this
.
factionRepository
.
find
({
select
:
[
'
n
ame
'
],
select
:
[
'
factionN
ame
'
],
where
:
{
game
:
gameId
},
});
const
factionNames
=
factions
.
map
(({
n
ame
})
=>
n
ame
);
const
factionNames
=
factions
.
map
(({
factionN
ame
})
=>
factionN
ame
);
// add the factions to db
if
(
gameData
.
factions
)
{
...
...
@@ -94,34 +95,37 @@ export class GameService {
// checks the password, creates an entry in GamePerson table with associated role&faction
async
createGroup
(
person
,
gameId
,
groupData
)
{
try
{
// create a partial game_Person entry and insert it to db
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
role
:
'
soldier
'
,
faction
:
null
,
game
:
gameId
,
// check if the person already is in a group in this game
const
checkDuplicate
=
await
this
.
game_PersonRepository
.
findOne
({
person
:
person
,
leaderGroup
:
null
,
group
:
null
,
});
const
gamePersonId
=
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
if
(
checkDuplicate
)
{
throw
new
HttpException
(
'
You already belong to a group!
'
,
HttpStatus
.
BAD_REQUEST
,
);
}
// create a group entry and insert it to db
const
group
=
await
this
.
game_GroupRepository
.
create
({
...
groupData
,
leader
:
gamePerson
.
gamepersonId
,
players
:
gamePerson
.
gamepersonId
,
game
:
gameId
,
});
const
gameGroup
=
await
this
.
game_GroupRepository
.
insert
(
group
);
// add the missing information to game_Person and update the db entry
gamePerson
[
'
gamepersonId
'
]
=
gamePersonId
.
identifiers
[
0
][
'
gamepersonId
'
];
gamePerson
[
'
leaderGroup
'
]
=
gameGroup
.
identifiers
[
0
][
'
id
'
];
gamePerson
[
'
group
'
]
=
gameGroup
.
identifiers
[
0
][
'
id
'
];
await
this
.
game_PersonRepository
.
save
(
gamePerson
);
// create game_Person entry and insert it to db
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
role
:
'
soldier
'
,
faction
:
null
,
game
:
gameId
,
person
:
person
,
leaderGroup
:
gameGroup
.
identifiers
[
0
][
'
id
'
],
group
:
gameGroup
.
identifiers
[
0
][
'
id
'
],
});
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
"
message
"
:
"
created new group
"
message
:
'
created new group
'
,
};
}
catch
(
e
)
{
return
e
;
...
...
@@ -140,45 +144,25 @@ export class GameService {
async
joinGroup
(
person
,
groupId
)
{
try
{
// get the old group data to see earlier players
const
oldData
=
await
this
.
game_GroupRepository
.
findOne
({
const
gameData
=
await
this
.
game_GroupRepository
.
findOne
({
where
:
{
id
:
groupId
},
relations
:
[
'
players
'
,
'
game
'
],
});
// create game_Person entry for the joining player and insert it
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
role
:
'
soldier
'
,
faction
:
null
,
game
:
old
Data
.
game
,
game
:
game
Data
.
game
,
person
:
person
,
leaderGroup
:
null
,
group
:
null
,
group
:
groupId
,
});
console
.
log
(
oldData
)
console
.
log
(
gamePerson
)
const
gamePersonId
=
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
// map the players from old data to array and push the new one
const
oldPlayers
=
oldData
.
players
.
map
(
({
gamepersonId
})
=>
gamepersonId
,
);
oldPlayers
.
push
(
person
);
// string[] to Game_PersonEntity[]
const
newPlayers
=
await
this
.
game_PersonRepository
.
find
({
gamepersonId
:
In
(
oldPlayers
)})
// create group entity and update the entry in db
const
group
=
await
this
.
game_GroupRepository
.
create
({
id
:
groupId
,
players
:
newPlayers
,
});
const
gameGroup
=
await
this
.
game_GroupRepository
.
save
(
group
);
// add the missing information to game_Person and update the db entry
gamePerson
[
'
gamepersonId
'
]
=
gamePersonId
.
identifiers
[
0
][
'
gamepersonId
'
];
gamePerson
[
'
leaderGroup
'
]
=
gameGroup
;
gamePerson
[
'
group
'
]
=
gameGroup
;
await
this
.
game_PersonRepository
.
save
(
gamePerson
);
return
{
"
message
"
:
"
Joined group
"
}
}
catch
(
e
)
{
return
e
}
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
message
:
'
Joined group
'
,
};
}
catch
(
e
)
{
return
e
;
}
}
// returns name and id of each game
...
...
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