Skip to content

Commit

Permalink
Merge pull request #544 from lenguyenthanh/code-golf/2
Browse files Browse the repository at this point in the history
Use mapN instead of multiple zip
  • Loading branch information
ornicar authored Mar 10, 2024
2 parents 2cade50 + 1e8aa87 commit 02dc12a
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions src/main/scala/Controller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,31 @@ final class Controller(

def roundWatch(id: Game.Id, header: RequestHeader) =
WebSocket(header): req =>
mongo
.gameExists(id)
.zip(mongo.troll.is(req.user))
.zip(roundFrom(id.into(Game.AnyId), req))
.map:
case ((true, isTroll), from) =>
val userTv = UserTv.from(header.queryParameter("userTv"))
endpoint(
name = "round/watch",
behavior = emit =>
RoundClientActor
.start(RoomActor.State(id.into(RoomId), isTroll), None, userTv, from):
Deps(emit, req, services)
,
header,
credits = 50,
interval = 20.seconds
)
case _ => notFound
(
mongo.gameExists(id),
mongo.troll.is(req.user),
roundFrom(id.into(Game.AnyId), req)
).mapN:
case (true, isTroll, from) =>
val userTv = UserTv.from(header.queryParameter("userTv"))
endpoint(
name = "round/watch",
behavior = emit =>
RoundClientActor
.start(RoomActor.State(id.into(RoomId), isTroll), None, userTv, from):
Deps(emit, req, services)
,
header,
credits = 50,
interval = 20.seconds
)
case _ => notFound

def roundPlay(id: Game.FullId, header: RequestHeader) =
WebSocket(header): req =>
mongo
.player(id, req.user)
.zip(mongo.troll.is(req.user))
.zip(roundFrom(id.into(Game.AnyId), req))
.map:
case ((Some(player), isTroll), from) =>
(mongo.player(id, req.user), mongo.troll.is(req.user), roundFrom(id.into(Game.AnyId), req))
.mapN:
case (Some(player), isTroll, from) =>
endpoint(
name = "round/play",
behavior = emit =>
Expand Down Expand Up @@ -182,20 +179,22 @@ final class Controller(

def team(id: Team.Id, header: RequestHeader) =
WebSocket(header): req =>
mongo.teamView(id, req.user).zip(mongo.troll.is(req.user)).map { (view, isTroll) =>
if view.exists(_.yes) then
endpoint(
name = "team",
behavior = emit =>
TeamClientActor.start(RoomActor.State(id.into(RoomId), isTroll), fromVersion(header)):
Deps(emit, req, services)
,
header,
credits = 30,
interval = 20.seconds
)
else siteEndpoint(req)
}
mongo
.teamView(id, req.user)
.zip(mongo.troll.is(req.user))
.map: (view, isTroll) =>
if view.exists(_.yes) then
endpoint(
name = "team",
behavior = emit =>
TeamClientActor.start(RoomActor.State(id.into(RoomId), isTroll), fromVersion(header)):
Deps(emit, req, services)
,
header,
credits = 30,
interval = 20.seconds
)
else siteEndpoint(req)

def swiss(id: Swiss.Id, header: RequestHeader) =
WebSocket(header): req =>
Expand Down

0 comments on commit 02dc12a

Please sign in to comment.