protected override Player MakePlayer(GamePeer peer, PlayerInfo info) { //Get Info from webserver BlackjackPlayer newPlayer = new BlackjackPlayer(peer, info); return newPlayer; }
public static PlayerKey MakeFromPeer(GamePeer peer) { PlayerKey key; key.ID = peer.ConnectionId; return(key); }
public void OnBroadcastMessage(GamePeer peer, EventData eventData, SendParameters sendParameters) { if (peer != this) { this.SendEvent(eventData, sendParameters); } }
public virtual void HandleOperationRequest(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { switch (operationRequest.OperationCode) { case CommonOperationCode.Join: HandleJoinOperation(peer, operationRequest, sendParameters); break; case CommonOperationCode.Exit: HandleExitOperation(peer, operationRequest, sendParameters); break; case CommonOperationCode.Chat: HandleChatOperation(peer, operationRequest, sendParameters); break; case CommonOperationCode.GetRooms: SendAllRoomStatus(peer, sendParameters); break; case CommonOperationCode.ConfirmJoin: var task = HandleConfirmJoinOperation(peer, operationRequest, sendParameters); task.Wait(); break; } }
public async Task HandleConfirmJoinOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var joinRequest = new ConfirmJoinRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(joinRequest, sendParameters)) { return; } //string playerInfoStr = await WebHelper.RequestPlayerInfo(joinRequest.UserKey); string playerInfoStr = "{ \"username\": \"test\", \"game_money\": 2000 }"; PlayerInfo info = JsonConvert.DeserializeObject <PlayerInfo>(playerInfoStr); PeerManager.Instance.OnPeerJoin(peer, new PeerInfo(peer, joinRequest.RoomID, joinRequest.UserKey)); var room = FindRoom(joinRequest.RoomID); if (room != null) { room.ExecutionFiber.Enqueue(() => room.Join(peer, joinRequest, sendParameters, info)); } else { var response = new OperationResponse(CommonOperationCode.ConfirmJoin, new Dictionary <byte, object> { { (byte)CommonParameterKey.Success, false } }); peer.SendOperationResponse(response, sendParameters); } }
public async Task HandleConfirmJoinOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var joinRequest = new ConfirmJoinRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(joinRequest, sendParameters)) { return; } //string playerInfoStr = await WebHelper.RequestPlayerInfo(joinRequest.UserKey); string playerInfoStr = "{ \"username\": \"test\", \"game_money\": 2000 }"; PlayerInfo info = JsonConvert.DeserializeObject<PlayerInfo>(playerInfoStr); PeerManager.Instance.OnPeerJoin(peer, new PeerInfo(peer, joinRequest.RoomID, joinRequest.UserKey)); var room = FindRoom(joinRequest.RoomID); if (room != null) { room.ExecutionFiber.Enqueue(() => room.Join(peer, joinRequest, sendParameters, info)); } else { var response = new OperationResponse(CommonOperationCode.ConfirmJoin, new Dictionary<byte, object> { { (byte)CommonParameterKey.Success, false } }); peer.SendOperationResponse(response, sendParameters); } }
public void SendAllRoomStatus(GamePeer peer, SendParameters sendParameters) { GetRoomsResponse response = new GetRoomsResponse(); response.RoomProperties = PacketHelper.Serialize <RoomPropertyList>(GetRoomProperties()); peer.SendOperationResponse(new OperationResponse(CommonOperationCode.GetRooms, response), sendParameters); }
public BaccaratPlayer(GamePeer peer, PlayerInfo info) : base(peer, info) { money = info.game_money; win = 0; lose = 0; }
public BlackjackPlayer(GamePeer peer, PlayerInfo info) : base(peer, info) { money = info.game_money; win = 0; lose = 0; status = PlayerStatus.Waiting; }
public void RemovePlayer(GamePeer peer) { lock (playerLock) { PlayerKey pKey = PlayerKey.MakeFromPeer(peer); playersDic.Remove(pKey); } }
public Player AddPlayer(GamePeer peer, PlayerInfo info) { lock (playerLock) { Player newPlayer = MakePlayer(peer, info); playersDic.Add(newPlayer.key, newPlayer); return(newPlayer); } }
public void OnPeerJoin(GamePeer peer, PeerInfo info) { ClearPeerInfo(peer); peersDic.Add(peer.ConnectionId, peer); peerInfosDic.Add(peer.ConnectionId, info); peer.OnLeaveHandler += OnPeerLeave; }
protected void BroadcastMessage(GamePeer peer, EventData eventData, SendParameters sendParameters) { if (BroadcastMessageHandler == null) { return; } BroadcastMessageHandler(peer, eventData, sendParameters); }
public override void RemovePlayer(GamePeer peer, ExitRequest exitReq, SendParameters sendParameters) { BaccaratPlayer player = playerManager.GetPlayer(peer) as BaccaratPlayer; playersBetDic.Remove(player); seatsDic.Remove(player.seat); base.RemovePlayer(peer, exitReq, sendParameters); CheckAllBet(); }
public Player GetPlayer(GamePeer peer) { PlayerKey key = PlayerKey.MakeFromPeer(peer); if (playersDic.ContainsKey(key)) { return(playersDic[key]); } return(null); }
private void HandleBetRequest(GamePeer gamePeer, OperationRequest operationRequest, SendParameters sendParameters) { var room = FindPeerRoom(gamePeer) as BaccaratGameRoom; var sendBetRequest = new SendBetRequest(gamePeer.Protocol, operationRequest); if (room != null) { room.Bet(gamePeer, sendBetRequest); } }
private void HandleBetBroadcastRequest(GamePeer gamePeer, OperationRequest operationRequest, SendParameters sendParameters) { var room = FindPeerRoom(gamePeer) as BlackjackGameRoom; var betBroadcast = new BroadcastBetRequest(gamePeer.Protocol, operationRequest); if (room != null) { room.BroadcastBet(gamePeer, betBroadcast); } }
private void HandleActionRequest(GamePeer gamePeer, OperationRequest operationRequest, SendParameters sendParameters) { var room = FindPeerRoom(gamePeer) as BlackjackGameRoom; var actionRequest = new ActionRequest(gamePeer.Protocol, operationRequest); if (room != null) { room.OnPlayerAction(gamePeer, actionRequest); } }
private void ClearPeerInfo(GamePeer peer) { if (peersDic.ContainsKey(peer.ConnectionId)) { peersDic.Remove(peer.ConnectionId); } if (peerInfosDic.ContainsKey(peer.ConnectionId)) { peerInfosDic.Remove(peer.ConnectionId); } }
protected GameRoom FindPeerRoom(GamePeer peer) { foreach (var room in roomsDic.Values) { if (room.HasPlayer(peer)) { return(room); } } return(null); }
public bool HasPlayer(GamePeer peer) { PlayerKey pKey; pKey.ID = peer.ConnectionId; if (playersDic.ContainsKey(pKey)) { return(true); } return(false); }
public void Bet(GamePeer peer, SendBetRequest sendBet) { BaccaratPlayer player = playerManager.GetPlayer(peer) as BaccaratPlayer; var bet = new BaccaratBet(sendBet.BankerBet, sendBet.PlayerBet, sendBet.TieBet); if (HasBet(player) == false && CanBet(player, bet)) { playersBetDic.Add(player, bet); } CheckAllBet(); }
public void BroadcastBet(GamePeer peer, BroadcastBetRequest broadcastBet) { broadcastBet.Actor = playerManager.GetPlayer(peer).key.ID; var eventData = new EventData(EventCode.BaccaratBroadcastBet) { Parameters = broadcastBet.OperationRequest.Parameters }; lock (syncRoot) { BroadcastMessage(peer, eventData, new SendParameters()); } var response = new OperationResponse(CommonOperationCode.BaccaratBroadcastBet, new Dictionary<byte, object> { { (byte)CommonParameterKey.Success, true } }); peer.SendOperationResponse(response, new SendParameters()); }
public bool CanJoin(GamePeer peer) { if (playerManager.HasPlayer(peer)) { return false; //already joined. } if (playerManager.isFull()) { return false; //exceed max player count. } return true; }
public bool CanJoin(GamePeer peer) { if (playerManager.HasPlayer(peer)) { return(false); //already joined. } if (playerManager.isFull()) { return(false); //exceed max player count. } return(true); }
public override void HandleOperationRequest(GamePeer gamePeer, OperationRequest operationRequest, SendParameters sendParameters) { base.HandleOperationRequest(gamePeer, operationRequest, sendParameters); //nothing switch(operationRequest.OperationCode) { case CommonOperationCode.BaccaratBet: HandleBetRequest(gamePeer, operationRequest, sendParameters); break; case CommonOperationCode.BaccaratBroadcastBet: HandleBetBroadcastRequest(gamePeer, operationRequest, sendParameters); break; } }
public void HandleChatOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var chatRequest = new ChatRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(chatRequest, sendParameters)) { return; } var room = FindPeerRoom(peer); if (room != null) { room.Chat(peer, chatRequest, sendParameters); } }
public void Chat(GamePeer peer, ChatRequest chatReq, SendParameters sendParameters) { var eventData = new EventData(EventCode.Chat) { Parameters = chatReq.OperationRequest.Parameters }; lock (syncRoot) { BroadcastMessage(peer, eventData, sendParameters); } var response = new OperationResponse(CommonOperationCode.Chat); peer.SendOperationResponse(response, sendParameters); }
public void HandleExitOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var exitRequest = new ExitRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(exitRequest, sendParameters)) { return; } var room = FindPeerRoom(peer); if (room != null) { room.ExecutionFiber.Enqueue(() => room.Leave(peer)); } peer.Leave(); }
public void HandleJoinOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var joinRequest = new JoinRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(joinRequest, sendParameters)) { return; } var room = FindRoom(joinRequest.RoomID); var response = new OperationResponse(CommonOperationCode.Join, new Dictionary<byte, object> { { (byte)CommonParameterKey.Success, false } }); response.Parameters[(byte)JoinParameterKey.RoomID] = joinRequest.RoomID; if (room != null && room.CanJoin(peer)) { response.Parameters[(byte)CommonParameterKey.Success] = true; } peer.SendOperationResponse(response, sendParameters); }
public void Join(GamePeer peer, ConfirmJoinRequest joinReq, SendParameters sendParameters, PlayerInfo info) { if (CanJoin(peer)) { Player newPlayer = playerManager.AddPlayer(peer, info); BroadcastMessageHandler += peer.OnBroadcastMessage; peer.OnLeaveHandler += OnPeerLeave; OnJoin(peer); } else { var response = new OperationResponse(CommonOperationCode.ConfirmJoin, new Dictionary <byte, object> { { (byte)CommonParameterKey.Success, false } }); peer.SendOperationResponse(response, sendParameters); } }
public virtual void RemovePlayer(GamePeer peer, ExitRequest exitReq, SendParameters sendParameters) { lock (syncRoot) { if (playerManager.HasPlayer(peer) == false) { return; //already removed. } BroadcastMessageHandler -= peer.OnBroadcastMessage; peer.OnLeaveHandler -= OnPeerLeave; PlayerLeaveEvent leaveEvent = new PlayerLeaveEvent(); leaveEvent.Actor = playerManager.GetPlayer(peer).key.ID; EventData eventData = new EventData(EventCode.PlayerLeave, leaveEvent); BroadcastMessage(peer, eventData, sendParameters); playerManager.RemovePlayer(peer); } }
public void HandleJoinOperation(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { var joinRequest = new JoinRequest(peer.Protocol, operationRequest); if (!peer.ValidateOperation(joinRequest, sendParameters)) { return; } var room = FindRoom(joinRequest.RoomID); var response = new OperationResponse(CommonOperationCode.Join, new Dictionary <byte, object> { { (byte)CommonParameterKey.Success, false } }); response.Parameters[(byte)JoinParameterKey.RoomID] = joinRequest.RoomID; if (room != null && room.CanJoin(peer)) { response.Parameters[(byte)CommonParameterKey.Success] = true; } peer.SendOperationResponse(response, sendParameters); }
public void Join(GamePeer peer, ConfirmJoinRequest joinReq, SendParameters sendParameters, PlayerInfo info) { if (CanJoin(peer)) { Player newPlayer = playerManager.AddPlayer(peer, info); BroadcastMessageHandler += peer.OnBroadcastMessage; peer.OnLeaveHandler += OnPeerLeave; OnJoin(peer); } else { var response = new OperationResponse(CommonOperationCode.ConfirmJoin, new Dictionary<byte, object> { { (byte)CommonParameterKey.Success, false } }); peer.SendOperationResponse(response, sendParameters); } }
public void Leave(GamePeer peer) { OnPeerLeave(peer); }
protected abstract void OnJoin(GamePeer peer);
public bool HasPlayer(GamePeer peer) { return playerManager.HasPlayer(peer); }
public void HandleOperationRequest(GamePeer peer, OperationRequest operationRequest, SendParameters sendParameters) { gameCore.HandleOperationRequest(peer, operationRequest, sendParameters); }
public PeerInfo GetPeerInfo(GamePeer peer) { return(peerInfosDic.ContainsKey(peer.ConnectionId) ? peerInfosDic[peer.ConnectionId] : null); }
public PeerInfo(GamePeer peer, int roomID, string userKey) { this.connectionID = peer.ConnectionId; this.roomID = roomID; this.userKey = userKey; }
protected GameRoom FindPeerRoom(GamePeer peer) { foreach (var room in roomsDic.Values) { if (room.HasPlayer(peer)) { return room; } } return null; }
private void OnPeerLeave(GamePeer peer) { RemovePlayer(peer, null, new SendParameters()); }
protected void BroadcastMessage(GamePeer peer, EventData eventData, SendParameters sendParameters) { if (BroadcastMessageHandler == null) return; BroadcastMessageHandler(peer, eventData, sendParameters); }
protected abstract Player MakePlayer(GamePeer peer, PlayerInfo info);
public PeerInfo GetPeerInfo(GamePeer peer) { return peerInfosDic.ContainsKey(peer.ConnectionId) ? peerInfosDic[peer.ConnectionId] : null; }
public void HandleDisconnect(GamePeer peer) { peer.Leave(); }
public void OnPeerLeave(GamePeer peer) { ClearPeerInfo(peer); }
public Player(GamePeer peer, PlayerInfo info) { this.peer = peer; this.key = PlayerKey.MakeFromPeer(peer); this.name = info.username; }
public bool HasPlayer(GamePeer peer) { return(playerManager.HasPlayer(peer)); }