{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.GameLift.ClaimGameServer
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- __This operation is used with the GameLift FleetIQ solution and game
-- server groups.__
--
-- Locates an available game server and temporarily reserves it to host
-- gameplay and players. This operation is called from a game client or
-- client service (such as a matchmaker) to request hosting resources for a
-- new game session. In response, GameLift FleetIQ locates an available
-- game server, places it in @CLAIMED@ status for 60 seconds, and returns
-- connection information that players can use to connect to the game
-- server.
--
-- To claim a game server, identify a game server group. You can also
-- specify a game server ID, although this approach bypasses GameLift
-- FleetIQ placement optimization. Optionally, include game data to pass to
-- the game server at the start of a game session, such as a game map or
-- player information.
--
-- When a game server is successfully claimed, connection information is
-- returned. A claimed game server\'s utilization status remains
-- @AVAILABLE@ while the claim status is set to @CLAIMED@ for up to 60
-- seconds. This time period gives the game server time to update its
-- status to @UTILIZED@ after players join. If the game server\'s status is
-- not updated within 60 seconds, the game server reverts to unclaimed
-- status and is available to be claimed by another request. The claim time
-- period is a fixed value and is not configurable.
--
-- If you try to claim a specific game server, this request will fail in
-- the following cases:
--
-- -   If the game server utilization status is @UTILIZED@.
--
-- -   If the game server claim status is @CLAIMED@.
--
-- When claiming a specific game server, this request will succeed even if
-- the game server is running on an instance in @DRAINING@ status. To avoid
-- this, first check the instance status by calling
-- <https://docs.aws.amazon.com/gamelift/latest/apireference/API_DescribeGameServerInstances.html DescribeGameServerInstances>
-- .
--
-- __Learn more__
--
-- <https://docs.aws.amazon.com/gamelift/latest/fleetiqguide/gsg-intro.html GameLift FleetIQ Guide>
module Amazonka.GameLift.ClaimGameServer
  ( -- * Creating a Request
    ClaimGameServer (..),
    newClaimGameServer,

    -- * Request Lenses
    claimGameServer_gameServerData,
    claimGameServer_gameServerId,
    claimGameServer_gameServerGroupName,

    -- * Destructuring the Response
    ClaimGameServerResponse (..),
    newClaimGameServerResponse,

    -- * Response Lenses
    claimGameServerResponse_gameServer,
    claimGameServerResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.GameLift.Types
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newClaimGameServer' smart constructor.
data ClaimGameServer = ClaimGameServer'
  { -- | A set of custom game server properties, formatted as a single string
    -- value. This data is passed to a game client or service when it requests
    -- information on game servers.
    ClaimGameServer -> Maybe Text
gameServerData :: Prelude.Maybe Prelude.Text,
    -- | A custom string that uniquely identifies the game server to claim. If
    -- this parameter is left empty, GameLift FleetIQ searches for an available
    -- game server in the specified game server group.
    ClaimGameServer -> Maybe Text
gameServerId :: Prelude.Maybe Prelude.Text,
    -- | A unique identifier for the game server group where the game server is
    -- running. If you are not specifying a game server to claim, this value
    -- identifies where you want GameLift FleetIQ to look for an available game
    -- server to claim.
    ClaimGameServer -> Text
gameServerGroupName :: Prelude.Text
  }
  deriving (ClaimGameServer -> ClaimGameServer -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ClaimGameServer -> ClaimGameServer -> Bool
$c/= :: ClaimGameServer -> ClaimGameServer -> Bool
== :: ClaimGameServer -> ClaimGameServer -> Bool
$c== :: ClaimGameServer -> ClaimGameServer -> Bool
Prelude.Eq, ReadPrec [ClaimGameServer]
ReadPrec ClaimGameServer
Int -> ReadS ClaimGameServer
ReadS [ClaimGameServer]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ClaimGameServer]
$creadListPrec :: ReadPrec [ClaimGameServer]
readPrec :: ReadPrec ClaimGameServer
$creadPrec :: ReadPrec ClaimGameServer
readList :: ReadS [ClaimGameServer]
$creadList :: ReadS [ClaimGameServer]
readsPrec :: Int -> ReadS ClaimGameServer
$creadsPrec :: Int -> ReadS ClaimGameServer
Prelude.Read, Int -> ClaimGameServer -> ShowS
[ClaimGameServer] -> ShowS
ClaimGameServer -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ClaimGameServer] -> ShowS
$cshowList :: [ClaimGameServer] -> ShowS
show :: ClaimGameServer -> String
$cshow :: ClaimGameServer -> String
showsPrec :: Int -> ClaimGameServer -> ShowS
$cshowsPrec :: Int -> ClaimGameServer -> ShowS
Prelude.Show, forall x. Rep ClaimGameServer x -> ClaimGameServer
forall x. ClaimGameServer -> Rep ClaimGameServer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ClaimGameServer x -> ClaimGameServer
$cfrom :: forall x. ClaimGameServer -> Rep ClaimGameServer x
Prelude.Generic)

-- |
-- Create a value of 'ClaimGameServer' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'gameServerData', 'claimGameServer_gameServerData' - A set of custom game server properties, formatted as a single string
-- value. This data is passed to a game client or service when it requests
-- information on game servers.
--
-- 'gameServerId', 'claimGameServer_gameServerId' - A custom string that uniquely identifies the game server to claim. If
-- this parameter is left empty, GameLift FleetIQ searches for an available
-- game server in the specified game server group.
--
-- 'gameServerGroupName', 'claimGameServer_gameServerGroupName' - A unique identifier for the game server group where the game server is
-- running. If you are not specifying a game server to claim, this value
-- identifies where you want GameLift FleetIQ to look for an available game
-- server to claim.
newClaimGameServer ::
  -- | 'gameServerGroupName'
  Prelude.Text ->
  ClaimGameServer
newClaimGameServer :: Text -> ClaimGameServer
newClaimGameServer Text
pGameServerGroupName_ =
  ClaimGameServer'
    { $sel:gameServerData:ClaimGameServer' :: Maybe Text
gameServerData = forall a. Maybe a
Prelude.Nothing,
      $sel:gameServerId:ClaimGameServer' :: Maybe Text
gameServerId = forall a. Maybe a
Prelude.Nothing,
      $sel:gameServerGroupName:ClaimGameServer' :: Text
gameServerGroupName = Text
pGameServerGroupName_
    }

-- | A set of custom game server properties, formatted as a single string
-- value. This data is passed to a game client or service when it requests
-- information on game servers.
claimGameServer_gameServerData :: Lens.Lens' ClaimGameServer (Prelude.Maybe Prelude.Text)
claimGameServer_gameServerData :: Lens' ClaimGameServer (Maybe Text)
claimGameServer_gameServerData = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ClaimGameServer' {Maybe Text
gameServerData :: Maybe Text
$sel:gameServerData:ClaimGameServer' :: ClaimGameServer -> Maybe Text
gameServerData} -> Maybe Text
gameServerData) (\s :: ClaimGameServer
s@ClaimGameServer' {} Maybe Text
a -> ClaimGameServer
s {$sel:gameServerData:ClaimGameServer' :: Maybe Text
gameServerData = Maybe Text
a} :: ClaimGameServer)

-- | A custom string that uniquely identifies the game server to claim. If
-- this parameter is left empty, GameLift FleetIQ searches for an available
-- game server in the specified game server group.
claimGameServer_gameServerId :: Lens.Lens' ClaimGameServer (Prelude.Maybe Prelude.Text)
claimGameServer_gameServerId :: Lens' ClaimGameServer (Maybe Text)
claimGameServer_gameServerId = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ClaimGameServer' {Maybe Text
gameServerId :: Maybe Text
$sel:gameServerId:ClaimGameServer' :: ClaimGameServer -> Maybe Text
gameServerId} -> Maybe Text
gameServerId) (\s :: ClaimGameServer
s@ClaimGameServer' {} Maybe Text
a -> ClaimGameServer
s {$sel:gameServerId:ClaimGameServer' :: Maybe Text
gameServerId = Maybe Text
a} :: ClaimGameServer)

-- | A unique identifier for the game server group where the game server is
-- running. If you are not specifying a game server to claim, this value
-- identifies where you want GameLift FleetIQ to look for an available game
-- server to claim.
claimGameServer_gameServerGroupName :: Lens.Lens' ClaimGameServer Prelude.Text
claimGameServer_gameServerGroupName :: Lens' ClaimGameServer Text
claimGameServer_gameServerGroupName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ClaimGameServer' {Text
gameServerGroupName :: Text
$sel:gameServerGroupName:ClaimGameServer' :: ClaimGameServer -> Text
gameServerGroupName} -> Text
gameServerGroupName) (\s :: ClaimGameServer
s@ClaimGameServer' {} Text
a -> ClaimGameServer
s {$sel:gameServerGroupName:ClaimGameServer' :: Text
gameServerGroupName = Text
a} :: ClaimGameServer)

instance Core.AWSRequest ClaimGameServer where
  type
    AWSResponse ClaimGameServer =
      ClaimGameServerResponse
  request :: (Service -> Service) -> ClaimGameServer -> Request ClaimGameServer
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy ClaimGameServer
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse ClaimGameServer)))
response =
    forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe GameServer -> Int -> ClaimGameServerResponse
ClaimGameServerResponse'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"GameServer")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable ClaimGameServer where
  hashWithSalt :: Int -> ClaimGameServer -> Int
hashWithSalt Int
_salt ClaimGameServer' {Maybe Text
Text
gameServerGroupName :: Text
gameServerId :: Maybe Text
gameServerData :: Maybe Text
$sel:gameServerGroupName:ClaimGameServer' :: ClaimGameServer -> Text
$sel:gameServerId:ClaimGameServer' :: ClaimGameServer -> Maybe Text
$sel:gameServerData:ClaimGameServer' :: ClaimGameServer -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
gameServerData
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
gameServerId
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
gameServerGroupName

instance Prelude.NFData ClaimGameServer where
  rnf :: ClaimGameServer -> ()
rnf ClaimGameServer' {Maybe Text
Text
gameServerGroupName :: Text
gameServerId :: Maybe Text
gameServerData :: Maybe Text
$sel:gameServerGroupName:ClaimGameServer' :: ClaimGameServer -> Text
$sel:gameServerId:ClaimGameServer' :: ClaimGameServer -> Maybe Text
$sel:gameServerData:ClaimGameServer' :: ClaimGameServer -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
gameServerData
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
gameServerId
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
gameServerGroupName

instance Data.ToHeaders ClaimGameServer where
  toHeaders :: ClaimGameServer -> ResponseHeaders
toHeaders =
    forall a b. a -> b -> a
Prelude.const
      ( forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# (ByteString
"GameLift.ClaimGameServer" :: Prelude.ByteString),
            HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON ClaimGameServer where
  toJSON :: ClaimGameServer -> Value
toJSON ClaimGameServer' {Maybe Text
Text
gameServerGroupName :: Text
gameServerId :: Maybe Text
gameServerData :: Maybe Text
$sel:gameServerGroupName:ClaimGameServer' :: ClaimGameServer -> Text
$sel:gameServerId:ClaimGameServer' :: ClaimGameServer -> Maybe Text
$sel:gameServerData:ClaimGameServer' :: ClaimGameServer -> Maybe Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"GameServerData" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
gameServerData,
            (Key
"GameServerId" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
gameServerId,
            forall a. a -> Maybe a
Prelude.Just
              (Key
"GameServerGroupName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
gameServerGroupName)
          ]
      )

instance Data.ToPath ClaimGameServer where
  toPath :: ClaimGameServer -> ByteString
toPath = forall a b. a -> b -> a
Prelude.const ByteString
"/"

instance Data.ToQuery ClaimGameServer where
  toQuery :: ClaimGameServer -> QueryString
toQuery = forall a b. a -> b -> a
Prelude.const forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newClaimGameServerResponse' smart constructor.
data ClaimGameServerResponse = ClaimGameServerResponse'
  { -- | Object that describes the newly claimed game server.
    ClaimGameServerResponse -> Maybe GameServer
gameServer :: Prelude.Maybe GameServer,
    -- | The response's http status code.
    ClaimGameServerResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (ClaimGameServerResponse -> ClaimGameServerResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ClaimGameServerResponse -> ClaimGameServerResponse -> Bool
$c/= :: ClaimGameServerResponse -> ClaimGameServerResponse -> Bool
== :: ClaimGameServerResponse -> ClaimGameServerResponse -> Bool
$c== :: ClaimGameServerResponse -> ClaimGameServerResponse -> Bool
Prelude.Eq, ReadPrec [ClaimGameServerResponse]
ReadPrec ClaimGameServerResponse
Int -> ReadS ClaimGameServerResponse
ReadS [ClaimGameServerResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ClaimGameServerResponse]
$creadListPrec :: ReadPrec [ClaimGameServerResponse]
readPrec :: ReadPrec ClaimGameServerResponse
$creadPrec :: ReadPrec ClaimGameServerResponse
readList :: ReadS [ClaimGameServerResponse]
$creadList :: ReadS [ClaimGameServerResponse]
readsPrec :: Int -> ReadS ClaimGameServerResponse
$creadsPrec :: Int -> ReadS ClaimGameServerResponse
Prelude.Read, Int -> ClaimGameServerResponse -> ShowS
[ClaimGameServerResponse] -> ShowS
ClaimGameServerResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ClaimGameServerResponse] -> ShowS
$cshowList :: [ClaimGameServerResponse] -> ShowS
show :: ClaimGameServerResponse -> String
$cshow :: ClaimGameServerResponse -> String
showsPrec :: Int -> ClaimGameServerResponse -> ShowS
$cshowsPrec :: Int -> ClaimGameServerResponse -> ShowS
Prelude.Show, forall x. Rep ClaimGameServerResponse x -> ClaimGameServerResponse
forall x. ClaimGameServerResponse -> Rep ClaimGameServerResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ClaimGameServerResponse x -> ClaimGameServerResponse
$cfrom :: forall x. ClaimGameServerResponse -> Rep ClaimGameServerResponse x
Prelude.Generic)

-- |
-- Create a value of 'ClaimGameServerResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'gameServer', 'claimGameServerResponse_gameServer' - Object that describes the newly claimed game server.
--
-- 'httpStatus', 'claimGameServerResponse_httpStatus' - The response's http status code.
newClaimGameServerResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  ClaimGameServerResponse
newClaimGameServerResponse :: Int -> ClaimGameServerResponse
newClaimGameServerResponse Int
pHttpStatus_ =
  ClaimGameServerResponse'
    { $sel:gameServer:ClaimGameServerResponse' :: Maybe GameServer
gameServer =
        forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:ClaimGameServerResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | Object that describes the newly claimed game server.
claimGameServerResponse_gameServer :: Lens.Lens' ClaimGameServerResponse (Prelude.Maybe GameServer)
claimGameServerResponse_gameServer :: Lens' ClaimGameServerResponse (Maybe GameServer)
claimGameServerResponse_gameServer = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ClaimGameServerResponse' {Maybe GameServer
gameServer :: Maybe GameServer
$sel:gameServer:ClaimGameServerResponse' :: ClaimGameServerResponse -> Maybe GameServer
gameServer} -> Maybe GameServer
gameServer) (\s :: ClaimGameServerResponse
s@ClaimGameServerResponse' {} Maybe GameServer
a -> ClaimGameServerResponse
s {$sel:gameServer:ClaimGameServerResponse' :: Maybe GameServer
gameServer = Maybe GameServer
a} :: ClaimGameServerResponse)

-- | The response's http status code.
claimGameServerResponse_httpStatus :: Lens.Lens' ClaimGameServerResponse Prelude.Int
claimGameServerResponse_httpStatus :: Lens' ClaimGameServerResponse Int
claimGameServerResponse_httpStatus = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ClaimGameServerResponse' {Int
httpStatus :: Int
$sel:httpStatus:ClaimGameServerResponse' :: ClaimGameServerResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: ClaimGameServerResponse
s@ClaimGameServerResponse' {} Int
a -> ClaimGameServerResponse
s {$sel:httpStatus:ClaimGameServerResponse' :: Int
httpStatus = Int
a} :: ClaimGameServerResponse)

instance Prelude.NFData ClaimGameServerResponse where
  rnf :: ClaimGameServerResponse -> ()
rnf ClaimGameServerResponse' {Int
Maybe GameServer
httpStatus :: Int
gameServer :: Maybe GameServer
$sel:httpStatus:ClaimGameServerResponse' :: ClaimGameServerResponse -> Int
$sel:gameServer:ClaimGameServerResponse' :: ClaimGameServerResponse -> Maybe GameServer
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe GameServer
gameServer
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus