public Subscription AddSubscription(MasterClientPeer peer, int gameCount) { if (log.IsDebugEnabled) { log.DebugFormat("New Subscription: pid={0}, gc={1}, props={2}", peer.ConnectionId, gameCount, this.propertyString); } if (gameCount < 0) { gameCount = 0; } var subscription = new Subscription(this, peer, gameCount); HashSet <PeerBase> hashSet; if (this.subscriptions.TryGetValue(gameCount, out hashSet) == false) { if (log.IsDebugEnabled) { log.DebugFormat("Creating new hashset for game count = {0}", gameCount); } hashSet = new HashSet <PeerBase>(); this.subscriptions.Add(gameCount, hashSet); } hashSet.Add(peer); return(subscription); }
protected virtual void OnRemovePeer(MasterClientPeer peer) { if (log.IsDebugEnabled) { log.DebugFormat("peer removed from lobby:l:'{0}',p:'{1}',u:'{2}'", this, peer, peer.UserId); } }
protected virtual OperationResponse HandleJoinLobby(MasterClientPeer peer, OperationRequest operationRequest, SendParameters sendParameters) { // validate operation var operation = new JoinLobbyRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // special handling for game properties send by AS3/Flash (Amf3 protocol) clients if (peer.Protocol.ProtocolType == ProtocolType.Amf3V151 || peer.Protocol.ProtocolType == ProtocolType.Amf3V152) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } peer.GameChannelSubscription = null; var subscription = this.GameList.AddSubscription(peer, operation.GameProperties, operation.GameListCount); peer.GameChannelSubscription = subscription; peer.SendOperationResponse(new OperationResponse(operationRequest.OperationCode), sendParameters); // publish game list to peer after the response has been sent var gameList = subscription.GetGameList(); var e = new GameListEvent { Data = gameList }; var eventData = new EventData((byte)EventCode.GameList, e); peer.SendEvent(eventData, new SendParameters()); return(null); }
protected virtual OperationResponse HandleJoinLobby(MasterClientPeer peer, JoinLobbyRequest operation, SendParameters sendParameters) { try { // special handling for game properties send by AS3/Flash (Amf3 protocol) clients if (peer.Protocol.ProtocolType == ProtocolType.Amf3V152 || peer.Protocol.ProtocolType == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } peer.GameChannelSubscription = null; var subscription = this.GameList.AddSubscription(peer, operation.GameProperties, operation.GameListCount); peer.GameChannelSubscription = subscription; peer.SendOperationResponse(new OperationResponse(operation.OperationRequest.OperationCode), sendParameters); // publish game list to peer after the response has been sent var gameList = subscription.GetGameList(); var e = new GameListEvent { Data = gameList }; var eventData = new EventData((byte)EventCode.GameList, e); peer.SendEvent(eventData, new SendParameters()); return(null); } catch (Exception ex) { log.Error(ex); return(null); } }
protected virtual GetGameListResponse GetGetGameListResponse(MasterClientPeer peer, Hashtable gameList) { return(new GetGameListResponse { GameList = gameList }); }
public virtual IGameListSubscription AddSubscription(MasterClientPeer peer, Hashtable gamePropertyFilter, int maxGameCount) { var subscribtion = new Subscribtion(this, maxGameCount, peer); this.peers.Add(peer); return(subscribtion); }
protected virtual OperationResponse HandleDebugGame(MasterClientPeer peer, OperationRequest operationRequest) { var operation = new DebugGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } GameState gameState; if (this.GameList.TryGetGame(operation.GameId, out gameState) == false) { return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)ErrorCode.GameIdNotExists, DebugMessage = LBErrorMessages.GameIdDoesNotExist }); } var debugGameResponse = this.GetDebugGameResponse(peer, gameState); log.InfoFormat("DebugGame: {0}", debugGameResponse.Info); return(new OperationResponse(operationRequest.OperationCode, debugGameResponse)); }
protected virtual DebugGameResponse GetDebugGameResponse(MasterClientPeer peer, GameState gameState) { return(new DebugGameResponse { Address = gameState.GetServerAddress(peer), Info = gameState.ToString() }); }
//don't send game list public override IGameListSubscription AddSubscription(MasterClientPeer peer, Hashtable gamePropertyFilter, int maxGameCount) { if (_useLegacyLobbies) { return(base.AddSubscription(peer, gamePropertyFilter, maxGameCount)); } return(null); }
protected virtual OperationResponse HandleCreateGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request OperationResponse response; var operation = new CreateGameRequest(peer.Protocol, operationRequest); if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // special handling for game properties send by AS3/Flash (Amf3 protocol) or JSON clients var protocol = peer.Protocol.ProtocolType; if (protocol == ProtocolType.Amf3V151 || protocol == ProtocolType.Amf3V152 || protocol == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); // special treatment for game properties sent by AS3/Flash } // if no gameId is specified by the client generate a unique id if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); } else { // check if a game with the specified id already exists if (this.GameList.ContainsGameId(operation.GameId)) { return(new OperationResponse(operationRequest.OperationCode) { ReturnCode = (int)ErrorCode.GameIdAlreadyExists, DebugMessage = "A game with the specified id already exist." }); } } // try to create game GameState gameState; if (!this.TryCreateGame(operation, operation.GameId, operation.GameProperties, out gameState, out response)) { return(response); } // add peer to game gameState.AddPeer(peer); this.ScheduleCheckJoinTimeOuts(); // publish operation response object createGameResponse = this.GetCreateGameResponse(peer, gameState); return(new OperationResponse(operationRequest.OperationCode, createGameResponse)); }
public static void SendUpdatePlayerProfileEvent(this MasterClientPeer peer) { // Update profile event var currentPlayer = peer.GetCurrentPlayer(); var profileEvent = new UpdateProfileEvent { ProfileData = currentPlayer.ToBson() }; var profileEventData = new EventData((byte)EventCode.PlayerProfile, profileEvent); peer.SendEvent(profileEventData, new SendParameters()); }
protected virtual OperationResponse HandleJoinLobby(MasterClientPeer peer, JoinLobbyRequest operation, SendParameters sendParameters) { try { peer.GameChannelSubscription = null; if (operation.GameListCount > 0 && this.gameListLimit > 0) { if (operation.GameListCount > this.gameListLimit) { operation.GameListCount = this.gameListLimit; } } var subscription = this.GameList.AddSubscription(peer, operation.GameProperties, operation.GameListCount); peer.GameChannelSubscription = subscription; peer.SendOperationResponse(new OperationResponse(operation.OperationRequest.OperationCode), sendParameters); if (subscription != null) { // publish game list to peer after the response has been sent var gameList = subscription.GetGameList(); if (gameList.Count != 0) { var sb = new StringBuilder(); foreach (var game in gameList.Keys) { sb.AppendFormat("{0};", game); } if (log.IsDebugEnabled) { log.DebugFormat("Game list is: {0}", sb.ToString()); } } var e = new GameListEvent { Data = gameList }; var eventData = new EventData((byte)EventCode.GameList, e); peer.SendEvent(eventData, new SendParameters()); } this.peers.Add(peer); return(null); } catch (Exception ex) { log.Error(ex); return(null); } }
protected virtual void ExecuteOperation(MasterClientPeer peer, OperationRequest operationRequest, SendParameters sendParameters) { try { OperationResponse response; switch ((OperationCode)operationRequest.OperationCode) { default: response = new OperationResponse(operationRequest.OperationCode) { ReturnCode = -1, DebugMessage = "Unknown operation code" }; break; case OperationCode.JoinLobby: var joinLobbyRequest = new JoinLobbyRequest(peer.Protocol, operationRequest); if (OperationHelper.ValidateOperation(joinLobbyRequest, log, out response)) { response = this.HandleJoinLobby(peer, joinLobbyRequest, sendParameters); } break; case OperationCode.LeaveLobby: response = this.HandleLeaveLobby(peer, operationRequest); break; case OperationCode.CreateGame: response = this.HandleCreateGame(peer, operationRequest); break; case OperationCode.JoinGame: response = this.HandleJoinGame(peer, operationRequest); break; case OperationCode.JoinRandomGame: response = this.HandleJoinRandomGame(peer, operationRequest); break; case OperationCode.DebugGame: response = this.HandleDebugGame(peer, operationRequest); break; } if (response != null) { peer.SendOperationResponse(response, sendParameters); } } catch (Exception ex) { log.Error(ex); } }
protected virtual OperationResponse HandleCreateGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request OperationResponse response; var operation = new CreateGameRequest(peer.Protocol, operationRequest, peer.UserId); if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // if no gameId is specified by the client generate a unique id if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); } // try to create game GameState gameState; bool gameCreated; if (!this.TryCreateGame(operation, peer.ExpectedProtocol, false, out gameCreated, out gameState, out response, peer.AuthCookie)) { return(response); } string errMsg; if (!gameState.CheckSlots(peer.UserId, operation.AddUsers, out errMsg)) { if (gameCreated) { this.GameList.RemoveGameStateByName(operation.GameId); } return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)ErrorCode.SlotError, DebugMessage = errMsg }); } // add peer to game gameState.AddPeer(peer); gameState.AddSlots(operation); this.ScheduleCheckJoinTimeOuts(); // publish operation response var createGameResponse = this.GetCreateGameResponse(peer, gameState); return(new OperationResponse(operationRequest.OperationCode, createGameResponse)); }
public static void SendDomainConfigurationEvent(this MasterClientPeer peer) { var domainConfigBytes = MasterApplication.DomainConfiguration.ToBson(); LogManager.GetCurrentClassLogger().Warn($"DomainConfigBytes: {domainConfigBytes.Length}"); var e = new UpdateDomainConfigurationEvent { DomainConfiguration = domainConfigBytes }; var eventData = new EventData((byte)EventCode.UpdateDomainConfiguration, e); peer.SendEvent(eventData, new SendParameters()); }
private OperationResponse AddPlayerToGameAndGenerateResponse(MasterClientPeer peer, JoinGameRequest operation, GameState gameState) { gameState.AddPeer(peer); gameState.AddSlots(operation); this.ScheduleCheckJoinTimeOuts(); // publish operation response var joinResponse = this.GetJoinGameResponse(peer, gameState); return(new OperationResponse(operation.OperationCode, joinResponse)); }
private void RemovePeer(MasterClientPeer peer) { try { this.peers.Remove(peer); this.OnRemovePeer(peer); } catch (Exception ex) { log.Error(string.Format("Exception in AppLobby:{0}, Exception Msg:{1}", this, ex.Message), ex); } }
private void HandleRemovePeer(MasterClientPeer peer) { try { peer.GameChannelSubscription = null; this.peers.Remove(peer); this.OnRemovePeer(peer); } catch (Exception ex) { log.Error(ex); } }
protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // special handling for game properties send by AS3/Flash (Amf3 protocol) clients if (peer.Protocol.ProtocolType == ProtocolType.Amf3V152 || peer.Protocol.ProtocolType == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } // try to find a match GameState game; string errorMessage; var result = this.GameList.TryGetRandomGame((JoinRandomType)operation.JoinRandomType, peer, operation.GameProperties, operation.QueryData, out game, out errorMessage); if (result != ErrorCode.Ok) { if (string.IsNullOrEmpty(errorMessage)) { errorMessage = "No match found"; } response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage }; return(response); } // match found, add peer to game and notify the peer game.AddPeer(peer); if (log.IsDebugEnabled) { log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id); } this.ScheduleCheckJoinTimeOuts(); object joinResponse = this.GetJoinRandomGameResponse(peer, game); return(new OperationResponse(operationRequest.OperationCode, joinResponse)); }
protected virtual OperationResponse HandleLeaveLobby(MasterClientPeer peer, OperationRequest operationRequest) { peer.GameChannelSubscription = null; if (this.peers.Remove(peer)) { return(new OperationResponse { OperationCode = operationRequest.OperationCode }); } return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = 0, DebugMessage = "lobby not joined" }); }
public override IGameListSubscription AddSubscription(MasterClientPeer peer, Hashtable gamePropertyFilter, int maxGameCount) { //could be unified: if(_useLegacyLobbies || _limitGameList > 0 || _limitGameListUpdate > 0) if (_useLegacyLobbies) { return(base.AddSubscription(peer, gamePropertyFilter, maxGameCount)); } //no gamelist and no updates if (_limitGameList == 0 && _limitGameListUpdate == 0) { return(null); } return(base.AddSubscription(peer, gamePropertyFilter, maxGameCount)); }
public static void SendUpdateCurrency(this MasterClientPeer peer) { // Update profile event var currentPlayer = peer.GetCurrentPlayer(); var changeCurrencyEvent = new UpdateCurrencyEvent { Gold = currentPlayer.Gold, Crystal = currentPlayer.Crystals, Keys = currentPlayer.Keys, HealBox = currentPlayer.HealBox }; var currencyEvenData = new EventData((byte)EventCode.UpdateCurrency, changeCurrencyEvent); peer.SendEvent(currencyEvenData, new SendParameters()); }
public override IGameListSubscription AddSubscription(MasterClientPeer peer, Hashtable gamePropertyFilter, int maxGameCount) { if (gamePropertyFilter == null) { gamePropertyFilter = new Hashtable(0); } GameChannel gameChannel; var key = new GameChannelKey(gamePropertyFilter); if (!this.GameChannels.TryGetValue(key, out gameChannel)) { gameChannel = new GameChannel(this, key); this.GameChannels.Add(key, gameChannel); } return(gameChannel.AddSubscription(peer, maxGameCount)); }
protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // try to find a match GameState game; string errorMessage; var result = this.GameList.TryGetRandomGame(operation, peer, out game, out errorMessage); if (result != ErrorCode.Ok) { if (string.IsNullOrEmpty(errorMessage)) { errorMessage = "No match found"; } response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage }; return(response); } // match found, add peer to game and notify the peer game.AddPeer(peer); if (log.IsDebugEnabled) { log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id); } this.ScheduleCheckJoinTimeOuts(); object joinResponse = this.GetJoinRandomGameResponse(peer, game); return(new OperationResponse(operationRequest.OperationCode, joinResponse)); }
protected virtual OperationResponse HandleLeaveLobby(MasterClientPeer peer, OperationRequest operationRequest) { peer.GameChannelSubscription = null; this.GameList.RemoveSubscription(peer); if (this.peers.Remove(peer)) { return(new OperationResponse { OperationCode = operationRequest.OperationCode }); } return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)ErrorCode.Ok, DebugMessage = LBErrorMessages.LobbyNotJoined, }); }
protected virtual OperationResponse HandleGetGameList(MasterClientPeer peer, OperationRequest operationRequest) { var operation = new GetGameListRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } //check is already done in MasterClientPeer if (this.LobbyType != AppLobbyType.SqlLobby) { return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)ErrorCode.OperationInvalid, DebugMessage = string.Format("Invalid lobby type: {0}", this.LobbyType) }); } var gameList = (SqlGameList)GameList; ErrorCode errorCode; string message; var games = gameList.GetGameList(operation.QueryData, out errorCode, out message); if (errorCode != ErrorCode.Ok) { return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)errorCode, DebugMessage = message }); } var getGameListResponse = this.GetGetGameListResponse(peer, games); return(new OperationResponse(operationRequest.OperationCode, getGameListResponse)); }
protected virtual OperationResponse HandleCreateGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request OperationResponse response; var operation = new CreateGameRequest(peer.Protocol, operationRequest); if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // if no gameId is specified by the client generate a unique id if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); } // try to create game GameState gameState; bool gameCreated; if (!this.TryCreateGame(operation, operation.GameId, false, operation.GameProperties, out gameCreated, out gameState, out response)) { return(response); } // add peer to game gameState.AddPeer(peer); this.ScheduleCheckJoinTimeOuts(); // publish operation response var createGameResponse = this.GetCreateGameResponse(peer, gameState); return(new OperationResponse(operationRequest.OperationCode, createGameResponse)); }
protected virtual OperationResponse HandleLeaveLobby(MasterClientPeer peer, OperationRequest operationRequest) { if (this.peers.Remove(peer)) { return new OperationResponse { OperationCode = operationRequest.OperationCode }; } return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.OperationDenied, DebugMessage = "lobby not joined" }; }
protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return(response); } // try to find a match GameState game; string errorMessage; var result = this.GameList.TryGetRandomGame(operation, peer, out game, out errorMessage); //create game if (result == ErrorCode.NoMatchFound && operation.CreateIfNotExists) { //random GameId if not set if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); operationRequest.Parameters[(byte)ParameterKey.GameId] = operation.GameId; } //replace GameProperties (used for filter) with values for game creation if (operation.Properties != null) { operationRequest.Parameters[(byte)ParameterKey.GameProperties] = operation.Properties; } //create - we try to use current HandleCreateGame implementation var createGameResponse = HandleCreateGame(peer, operationRequest); if (createGameResponse.ReturnCode != 0) { return(new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = createGameResponse.ReturnCode, DebugMessage = createGameResponse.DebugMessage }); } var joinRandomGameResponse = new JoinRandomGameResponse { GameId = operation.GameId, Address = (string)createGameResponse.Parameters[(byte)ParameterKey.Address], }; //NodeId is not used anywhere atm if (createGameResponse.Parameters.ContainsKey((byte)ParameterKey.NodeId)) { joinRandomGameResponse.NodeId = (byte)createGameResponse.Parameters[(byte)ParameterKey.NodeId]; } return(new OperationResponse(operationRequest.OperationCode, joinRandomGameResponse) { //TODO other ReturnCode if game was created ReturnCode = (byte)ErrorCode.Ok }); } if (result != ErrorCode.Ok) { if (string.IsNullOrEmpty(errorMessage)) { errorMessage = "No match found"; } response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage }; return(response); } // match found, add peer to game and notify the peer game.AddPeer(peer); if (log.IsDebugEnabled) { log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id); } this.ScheduleCheckJoinTimeOuts(); var joinResponse = this.GetJoinRandomGameResponse(peer, game); return(new OperationResponse(operationRequest.OperationCode, joinResponse)); }
private void HandleRemovePeer(MasterClientPeer peer) { try { this.peers.Remove(peer); this.OnRemovePeer(peer); } catch (Exception ex) { log.Error(ex); } }
protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } // special handling for game properties send by AS3/Flash (Amf3 protocol) clients if (peer.Protocol.ProtocolType == ProtocolType.Amf3V152 || peer.Protocol.ProtocolType == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } // try to find a match GameState game; string errorMessage; var result = this.GameList.TryGetRandomGame((JoinRandomType)operation.JoinRandomType, peer, operation.GameProperties, operation.QueryData, out game, out errorMessage); if (result != ErrorCode.Ok) { if (string.IsNullOrEmpty(errorMessage)) { errorMessage = "No match found"; } response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage}; return response; } // match found, add peer to game and notify the peer game.AddPeer(peer); if (log.IsDebugEnabled) { log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id); } this.ScheduleCheckJoinTimeOuts(); object joinResponse = this.GetJoinRandomGameResponse(peer, game); return new OperationResponse(operationRequest.OperationCode, joinResponse); }
public void EnqueueOperation(MasterClientPeer peer, OperationRequest operationRequest, SendParameters sendParameters) { this.ExecutionFiber.Enqueue(() => this.ExecuteOperation(peer, operationRequest, sendParameters)); }
protected virtual OperationResponse HandleJoinGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate operation var operation = new JoinGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } GameState gameState; // try to find game by id if (this.GameList.TryGetGame(operation.GameId, out gameState) == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameIdNotExists, DebugMessage = "Game does not exist" }; } // check if max players of the game is already reached if (gameState.MaxPlayer > 0 && gameState.PlayerCount >= gameState.MaxPlayer) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameFull, DebugMessage = "Game full" }; } // check if the game is open if (gameState.IsOpen == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameClosed, DebugMessage = "Game closed" }; } // check if user is blocked by peers allready in the game if (gameState.IsBlocked(peer)) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.UserBlocked, DebugMessage = "User is blocked" }; } // add peer to game if (gameState.TryAddPeer(peer) == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.UserBlocked, DebugMessage = "Already joined the specified game." }; } // publish operation response object joinResponse = this.GetJoinGameResponse(peer, gameState); return new OperationResponse(operationRequest.OperationCode, joinResponse); }
protected virtual OperationResponse HandleCancelJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.OperationDenied, DebugMessage = "Invalid operation" }; }
public void AddPlayerToWaitList(MasterClientPeer peer, JoinGameRequest operation) { this.waitList.Add(new DeferredUser { Peer = peer, JoinRequest = operation }); }
protected virtual object GetJoinRandomGameResponse(MasterClientPeer peer, GameState gameState) { return new JoinRandomGameResponse { GameId = gameState.Id, Address = gameState.GetServerAddress(peer) }; }
protected virtual OperationResponse HandleJoinGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate operation var operation = new JoinGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } // special handling for game properties send by AS3/Flash (Amf3 protocol) or JSON clients var protocol = peer.Protocol.ProtocolType; if (protocol == ProtocolType.Amf3V152 || protocol == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } // try to find game by id GameState gameState; bool gameCreated = false; if (operation.CreateIfNotExists == false) { // The client does not want to create the game if it does not exists. // In this case the game must have been created on the game server before it can be joined. if (this.GameList.TryGetGame(operation.GameId, out gameState) == false || gameState.IsCreatedOnGameServer == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameIdNotExists, DebugMessage = "Game does not exist" }; } } else { // The client will create the game if it does not exists already. if (!this.GameList.TryGetGame(operation.GameId, out gameState)) { if (!this.TryCreateGame(operation, operation.GameId, true, operation.GameProperties, out gameCreated, out gameState, out response)) { return response; } } } // game properties have only be to checked if the game was not created by the client if (gameCreated == false) { // check if max players of the game is already reached if (gameState.MaxPlayer > 0 && gameState.PlayerCount >= gameState.MaxPlayer) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameFull, DebugMessage = "Game full" }; } // check if the game is open if (gameState.IsOpen == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameClosed, DebugMessage = "Game closed" }; } } // add peer to game gameState.AddPeer(peer); this.ScheduleCheckJoinTimeOuts(); // publish operation response object joinResponse = this.GetJoinGameResponse(peer, gameState); return new OperationResponse(operationRequest.OperationCode, joinResponse); }
protected virtual void OnRemovePeer(MasterClientPeer peer) { }
protected virtual OperationResponse HandleDebugGame(MasterClientPeer peer, OperationRequest operationRequest) { var operation = new DebugGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } GameState gameState; if (this.GameList.TryGetGame(operation.GameId, out gameState) == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.GameIdNotExists, DebugMessage = "Game does not exist" }; } var debugGameResponse = this.GetDebugGameResponse(peer, gameState); log.InfoFormat("DebugGame: {0}", debugGameResponse.Info); return new OperationResponse(operationRequest.OperationCode, debugGameResponse); }
protected virtual OperationResponse HandleLeaveLobby(MasterClientPeer peer, OperationRequest operationRequest) { peer.GameChannelSubscription = null; if (this.peers.Remove(peer)) { return new OperationResponse { OperationCode = operationRequest.OperationCode }; } return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = 0, DebugMessage = "lobby not joined" }; }
protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest); OperationResponse response; if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } // Check if peer is a flash (amf3) client because flash clients does not support byte keys in a hastable. // If a flash client likes to match a game with a specific 'MaxPlayer' value 'MaxPlayer' will be sent // with the string key "255" and the max player value as int. if (peer.Protocol.ProtocolType == ProtocolType.Amf3V151 || peer.Protocol.ProtocolType == ProtocolType.Amf3V152) { if (operation.GameProperties != null && operation.GameProperties.Count > 0) { if (operation.GameProperties.ContainsKey("255")) { var maxPlayer = (byte)(int)operation.GameProperties["255"]; operation.GameProperties.Remove("255"); operation.GameProperties.Add((byte)GameParameter.MaxPlayer, maxPlayer); } } } // try to find a match GameState game; if (this.GameList.TryGetRandomGame(peer, operation.GameProperties, out game) == false) { response = new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.NoMatchFound, DebugMessage = "No match found" }; return response; } // match found, add peer to game and notify the peer game.AddPeer(peer); if (log.IsDebugEnabled) { log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id); } object joinResponse = this.GetJoinRandomGameResponse(peer, game); return new OperationResponse(operationRequest.OperationCode, joinResponse); }
protected virtual OperationResponse HandleJoinLobby(MasterClientPeer peer, OperationRequest operationRequest) { // check if peer already joined if (this.peers.Add(peer) == false) { return new OperationResponse { OperationCode = operationRequest.OperationCode, ReturnCode = (int)ErrorCode.OperationDenied, DebugMessage = "Peer already joined the lobby" }; } // publish game list to peer after the response has been sent this.ExecutionFiber.Enqueue(() => this.PublishGameList(peer)); return new OperationResponse(operationRequest.OperationCode); }
public void JoinLobby(MasterClientPeer peer, JoinLobbyRequest joinLobbyrequest, SendParameters sendParameters) { this.ExecutionFiber.Enqueue(() => this.HandleJoinLobby(peer, joinLobbyrequest, sendParameters)); }
protected virtual OperationResponse HandleCreateGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate operation OperationResponse response; var operation = new CreateGameRequest(peer.Protocol, operationRequest); if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } // if no gameId is specified by the client generate a unique id if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); } else { // check if a game with the specified id already exists if (this.GameList.ContainsGameId(operation.GameId)) { return new OperationResponse(operationRequest.OperationCode) { ReturnCode = (int)ErrorCode.GameIdAlreadyExists, DebugMessage = "A game with the specified id already exist." }; } } // try to create game GameState gameState; if (!this.TryCreateGame(operation, operation.GameId, operation.GameProperties, out gameState, out response)) { return response; } // add peer to game gameState.AddPeer(peer); // publish operation response object createGameResponse = this.GetCreateGameResponse(peer, gameState); return new OperationResponse(operationRequest.OperationCode, createGameResponse); }
public void RemovePeer(MasterClientPeer serverPeer) { this.ExecutionFiber.Enqueue(() => this.HandleRemovePeer(serverPeer)); }
protected virtual DebugGameResponse GetDebugGameResponse(MasterClientPeer peer, GameState gameState) { return new DebugGameResponse { Address = gameState.GetServerAddress(peer), Info = gameState.ToString() }; }
protected virtual OperationResponse HandleJoinLobby(MasterClientPeer peer, JoinLobbyRequest operation, SendParameters sendParameters) { try { // special handling for game properties send by AS3/Flash (Amf3 protocol) clients if (peer.Protocol.ProtocolType == ProtocolType.Amf3V152 || peer.Protocol.ProtocolType == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); } peer.GameChannelSubscription = null; var subscription = this.GameList.AddSubscription(peer, operation.GameProperties, operation.GameListCount); peer.GameChannelSubscription = subscription; peer.SendOperationResponse(new OperationResponse(operation.OperationRequest.OperationCode), sendParameters); // publish game list to peer after the response has been sent var gameList = subscription.GetGameList(); var e = new GameListEvent { Data = gameList }; var eventData = new EventData((byte)EventCode.GameList, e); peer.SendEvent(eventData, new SendParameters()); return null; } catch (Exception ex) { log.Error(ex); return null; } }
public override IGameListSubscription AddSubscription(MasterClientPeer peer, Hashtable gamePropertyFilter, int maxGameCount) { return(null); }
protected virtual OperationResponse HandleCreateGame(MasterClientPeer peer, OperationRequest operationRequest) { // validate the operation request OperationResponse response; var operation = new CreateGameRequest(peer.Protocol, operationRequest); if (OperationHelper.ValidateOperation(operation, log, out response) == false) { return response; } // special handling for game properties send by AS3/Flash (Amf3 protocol) or JSON clients var protocol = peer.Protocol.ProtocolType; if (protocol == ProtocolType.Amf3V152 || protocol == ProtocolType.Json) { Utilities.ConvertAs3WellKnownPropertyKeys(operation.GameProperties, null); // special treatment for game properties sent by AS3/Flash } // if no gameId is specified by the client generate a unique id if (string.IsNullOrEmpty(operation.GameId)) { operation.GameId = Guid.NewGuid().ToString(); } // try to create game GameState gameState; bool gameCreated; if (!this.TryCreateGame(operation, operation.GameId, false, operation.GameProperties, out gameCreated, out gameState, out response)) { return response; } // add peer to game gameState.AddPeer(peer); this.ScheduleCheckJoinTimeOuts(); // publish operation response object createGameResponse = this.GetCreateGameResponse(peer, gameState); return new OperationResponse(operationRequest.OperationCode, createGameResponse); }