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
Merge requests
!21
Error handling
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Error handling
error-handling
into
Development
Overview
0
Commits
26
Pipelines
0
Changes
2
Merged
Ghost User
requested to merge
error-handling
into
Development
5 years ago
Overview
0
Commits
26
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Viewing commit
acbf2b5e
Prev
Next
Show latest version
2 files
+
166
−
197
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
acbf2b5e
removed try catches
· acbf2b5e
L4168
authored
5 years ago
src/game/game.service.ts
+
125
−
151
Options
@@ -38,98 +38,88 @@ export class GameService {
// create a new game
async
createNewGame
(
personId
:
PersonEntity
,
gameData
:
GameDTO
)
{
try
{
// checks if a game with the same name exists already
const
{
name
}
=
gameData
;
if
(
await
this
.
gameRepository
.
findOne
({
where
:
{
name
}
}))
{
throw
new
HttpException
(
'
Game already exists
'
,
HttpStatus
.
BAD_REQUEST
);
}
// else add the game to the database
const
game
=
await
this
.
gameRepository
.
create
({
...
gameData
,
factions
:
gameData
.
factions
,
});
await
this
.
gameRepository
.
insert
(
game
);
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
faction
:
null
,
game
:
game
,
person
:
personId
,
});
gamePerson
[
'
role
'
]
=
'
admin
'
;
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
message
:
'
New game added
'
,
};
}
catch
(
error
)
{
return
error
.
message
;
// checks if a game with the same name exists already
const
{
name
}
=
gameData
;
if
(
await
this
.
gameRepository
.
findOne
({
where
:
{
name
}
}))
{
throw
new
HttpException
(
'
Game already exists
'
,
HttpStatus
.
BAD_REQUEST
);
}
// else add the game to the database
const
game
=
await
this
.
gameRepository
.
create
({
...
gameData
,
factions
:
gameData
.
factions
,
});
await
this
.
gameRepository
.
insert
(
game
);
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
faction
:
null
,
game
:
game
,
person
:
personId
,
});
gamePerson
[
'
role
'
]
=
'
admin
'
;
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
message
:
'
New game added
'
,
};
}
// edit already created game
async
editGame
(
id
:
string
,
gameData
:
GameDTO
)
{
try
{
// checks if a game with the same name exists already
const
{
name
}
=
gameData
;
if
(
await
this
.
gameRepository
.
findOne
({
name
:
name
,
id
:
Not
(
id
)
}))
{
throw
new
HttpException
(
'
Game already exists
'
,
HttpStatus
.
BAD_REQUEST
);
}
// update game entry in db
const
updatedGame
=
await
this
.
gameRepository
.
create
({
...
gameData
,
factions
:
null
,
objective_points
:
null
,
// checks if a game with the same name exists already
const
{
name
}
=
gameData
;
if
(
await
this
.
gameRepository
.
findOne
({
name
:
name
,
id
:
Not
(
id
)
}))
{
throw
new
HttpException
(
'
Game already exists
'
,
HttpStatus
.
BAD_REQUEST
);
}
// update game entry in db
const
updatedGame
=
await
this
.
gameRepository
.
create
({
...
gameData
,
factions
:
null
,
objective_points
:
null
,
});
updatedGame
[
'
id
'
]
=
id
;
const
gameId
=
await
this
.
gameRepository
.
save
(
updatedGame
);
// get all the factions that are associated with the game to deny duplicate entries
const
factions
=
await
this
.
factionRepository
.
find
(
gameId
);
const
factionNames
=
factions
.
map
(({
factionName
})
=>
factionName
);
// add the factions to db
if
(
gameData
.
factions
)
{
gameData
.
factions
.
map
(
async
faction
=>
{
if
(
!
Object
.
values
(
factionNames
).
includes
(
faction
.
factionName
))
{
let
name
=
await
this
.
factionRepository
.
create
({
...
faction
,
gameId
:
gameId
,
});
await
this
.
factionRepository
.
insert
(
name
);
}
});
updatedGame
[
'
id
'
]
=
id
;
const
gameId
=
await
this
.
gameRepository
.
save
(
updatedGame
);
// get all the factions that are associated with the game to deny duplicate entries
const
factions
=
await
this
.
factionRepository
.
find
(
gameId
);
const
factionNames
=
factions
.
map
(({
factionName
})
=>
factionName
);
// add the factions to db
if
(
gameData
.
factions
)
{
gameData
.
factions
.
map
(
async
faction
=>
{
if
(
!
Object
.
values
(
factionNames
).
includes
(
faction
.
factionName
))
{
let
name
=
await
this
.
factionRepository
.
create
({
...
faction
,
gameId
:
gameId
,
});
await
this
.
factionRepository
.
insert
(
name
);
}
});
}
}
// get old flagboxes to deny duplicate entries
const
flagboxes
=
await
this
.
objectivePointRepository
.
find
({
game
:
gameId
,
// get old flagboxes to deny duplicate entries
const
flagboxes
=
await
this
.
objectivePointRepository
.
find
({
game
:
gameId
,
});
const
flagboxIds
=
flagboxes
.
map
(
({
objectivePointDescription
})
=>
objectivePointDescription
,
);
// insert the flagboxes to db
if
(
gameData
.
objective_points
)
{
gameData
.
objective_points
.
map
(
async
flagbox
=>
{
if
(
!
Object
.
values
(
flagboxIds
).
includes
(
flagbox
.
objectivePointDescription
)
)
{
let
newFlagbox
=
await
this
.
objectivePointRepository
.
create
({
...
flagbox
,
game
:
gameId
,
});
await
this
.
objectivePointRepository
.
insert
(
newFlagbox
);
}
});
const
flagboxIds
=
flagboxes
.
map
(
({
objectivePointDescription
})
=>
objectivePointDescription
,
);
// insert the flagboxes to db
if
(
gameData
.
objective_points
)
{
gameData
.
objective_points
.
map
(
async
flagbox
=>
{
if
(
!
Object
.
values
(
flagboxIds
).
includes
(
flagbox
.
objectivePointDescription
,
)
)
{
let
newFlagbox
=
await
this
.
objectivePointRepository
.
create
({
...
flagbox
,
game
:
gameId
,
});
await
this
.
objectivePointRepository
.
insert
(
newFlagbox
);
}
});
}
}
// TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY
// TO DO: ADD FLAGBOX LOCATION TO MAPDRAWING ENTITY
return
{
message
:
'
Game updated
'
,
};
}
catch
(
error
)
{
return
error
;
}
return
{
message
:
'
Game updated
'
,
};
}
async
deleteGame
(
id
)
{
@@ -142,87 +132,71 @@ export class GameService {
// checks the password, creates an entry in GamePerson table with associated role&faction
async
createGroup
(
person
,
gameId
,
groupData
)
{
try
{
// check if the person already is in a group in this game
const
checkDuplicate
=
await
this
.
game_PersonRepository
.
findOne
({
person
:
person
,
});
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
,
game
:
gameId
,
});
const
gameGroup
=
await
this
.
game_GroupRepository
.
insert
(
group
);
// check if the person already is in a group in this game
const
checkDuplicate
=
await
this
.
game_PersonRepository
.
findOne
({
person
:
person
,
});
if
(
checkDuplicate
)
{
throw
new
HttpException
(
'
You already belong to a group!
'
,
HttpStatus
.
BAD_REQUEST
,
);
}
// 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
);
// create a group entry and insert it to db
const
group
=
await
this
.
game_GroupRepository
.
create
({
...
groupData
,
game
:
gameId
,
});
const
gameGroup
=
await
this
.
game_GroupRepository
.
insert
(
group
);
// 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
'
,
};
}
catch
(
e
)
{
return
e
;
}
return
{
message
:
'
created new group
'
,
};
}
async
showGroups
()
{
try
{
return
await
this
.
game_GroupRepository
.
find
({
relations
:
[
'
leader
'
,
'
players
'
,
'
game
'
],
});
}
catch
(
e
)
{
return
e
;
}
return
await
this
.
game_GroupRepository
.
find
({
relations
:
[
'
leader
'
,
'
players
'
,
'
game
'
],
});
}
async
joinGroup
(
person
,
groupId
)
{
try
{
const
gameData
=
await
this
.
game_GroupRepository
.
findOne
({
where
:
{
id
:
groupId
},
relations
:
[
'
players
'
,
'
game
'
],
});
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
role
:
'
soldier
'
,
faction
:
null
,
game
:
gameData
.
game
,
person
:
person
,
leaderGroup
:
null
,
group
:
groupId
,
});
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
message
:
'
Joined group
'
,
};
}
catch
(
e
)
{
return
e
;
}
const
gameData
=
await
this
.
game_GroupRepository
.
findOne
({
where
:
{
id
:
groupId
},
relations
:
[
'
players
'
,
'
game
'
],
});
const
gamePerson
=
await
this
.
game_PersonRepository
.
create
({
role
:
'
soldier
'
,
faction
:
null
,
game
:
gameData
.
game
,
person
:
person
,
leaderGroup
:
null
,
group
:
groupId
,
});
await
this
.
game_PersonRepository
.
insert
(
gamePerson
);
return
{
message
:
'
Joined group
'
,
};
}
// returns name and id of each game
async
listGames
()
{
try
{
const
games
=
await
this
.
gameRepository
.
find
();
return
games
.
map
(
game
=>
{
return
game
.
gameObject
();
});
}
catch
(
error
)
{
return
error
;
}
const
games
=
await
this
.
gameRepository
.
find
();
return
games
.
map
(
game
=>
{
return
game
.
gameObject
();
});
}
// returns information about a game identified by id
Loading