/// <summary> /// 创建房间 /// </summary> /// <param name="pID">P I.</param> /// <param name="msg">Message.</param> /// <param name="action">Action.</param> public void sendMsg(GameProtocol pID, msgReqCreateRoom msg, Action <Message> action) { JsonObject jsonMsg = new JsonObject(); jsonMsg.Add("game", (int)msg.game); jsonMsg.Add("roomType", (int)msg.roomType); jsonMsg.Add("baseScore", msg.baseScore); jsonMsg.Add("minScore", msg.minScore); jsonMsg.Add("maxScore", msg.maxScore); jsonMsg.Add("roomName", msg.roomName); jsonMsg.Add("roomPassword", msg.roomPassword); jsonMsg.Add("rule", msg.rule); sendMsg(pID, jsonMsg, action); }
void OnApplicationQuit() { if (GameOverPanel.activeSelf) { MessageRoomPacket packet = new MessageRoomPacket(GameProtocol.QuitAfterGameOver(), Singleton.Instance.RoomID, Singleton.Instance.Me.Username); Singleton.Instance.Connection.SendPacket(packet.getData()); Singleton.Instance.Connection.CloseSocket(); } else { MessageRoomPacket packet = new MessageRoomPacket(GameProtocol.QuitWhileInGame(), Singleton.Instance.RoomID, Singleton.Instance.Me.Username); Singleton.Instance.Connection.SendPacket(packet.getData()); Singleton.Instance.Connection.CloseSocket(); } }
private GameServer GetGameServer() { var protocol = new GameProtocol() { FullTypeName = "GhostPanel.Rcon.Steam.SteamQueryProtocol" }; var game = new Game() { GameProtocol = protocol }; return(new GameServer() { IpAddress = "192.168.1.1", QueryPort = 27015, }); }
public void opponentQuitWhileInGame() { if (CurrentRoom.FirstUser != null && CurrentRoom.FirstUser.Username == Message) { Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.FirstUser); CurrentRoom.SendGameOver(CurrentRoom.SecondUser, "You won!\n Your opponent got disconnected."); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.SecondUser.Username) { MessagePacket pack = new MessagePacket(GameProtocol.OpponentQuitWhileInGame(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, pack.getData()); continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.FirstUser = null; } else if (CurrentRoom.SecondUser != null && CurrentRoom.SecondUser.Username == Message) { CurrentRoom.SendGameOver(CurrentRoom.FirstUser, "You won!\n Your opponent got disconnected."); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.SecondUser); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.FirstUser.Username) { MessagePacket pack = new MessagePacket(GameProtocol.OpponentQuitWhileInGame(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, pack.getData()); continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.SecondUser = null; } if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } }
public void FightButton() { MessagePacket packet = new MessagePacket( GameProtocol.ChallengePacketID(), Username.text + "|" + Singleton.Instance.Me.Username + " challenged you to a game!"); Singleton.Instance.Connection.SendPacket(packet.getData()); var list = SingletonUI.Instance.Helper.GetComponentsInChildren <ChallengeResultScript>(); //If is already a challange result from that user close it. foreach (ChallengeResultScript x in list) { if (x.Message.text.Split(' ')[0] == Username.text) { x.destroyMyself(); break; } } }
public Game(Match match) { if (match.HomeTeamId != match.AwayTeamId) { this.Id = Guid.NewGuid(); this.MatchDuration = match.MatchDuration; this.MatchId = match.Id; this.HomeTeamId = match.HomeTeamId; this.AwayTeamId = match.AwayTeamId; this.SeriesId = match.SeriesId; this.Location = match.Location; this.MatchDate = match.MatchDate; this.Protocol = new GameProtocol(this.HomeTeamId, this.AwayTeamId); } else { throw new GameContainsSameTeamTwiceException(); } }
public void SendAllowedMoves() { if (FirstUser.Player.IsHisTurn) { List <BoardPosition> tempList = Gameboard.GetAllLegalMoves(FirstUser.Player.DiskColor); string tempString = ""; int count = 1; foreach (BoardPosition aux in tempList) { if (count == tempList.Count) { tempString += aux.Row.ToString() + ':' + aux.Column; continue; } tempString += aux.Row.ToString() + ':' + aux.Column + '|'; count++; } MessageRoomPacket Packet = new MessageRoomPacket(GameProtocol.BoardMoves(), ID, tempString); Othello.Server.SendPacket(FirstUser.Socket, Packet.getData()); } else if (SecondUser.Player.IsHisTurn) { List <BoardPosition> tempList = Gameboard.GetAllLegalMoves(SecondUser.Player.DiskColor); string tempString = ""; int count = 1; foreach (BoardPosition aux in tempList) { if (count == tempList.Count) { tempString += aux.Row.ToString() + ':' + aux.Column; continue; } tempString += aux.Row.ToString() + ':' + aux.Column + '|'; count++; } MessageRoomPacket Packet = new MessageRoomPacket(GameProtocol.BoardMoves(), ID, tempString); Othello.Server.SendPacket(SecondUser.Socket, Packet.getData()); } }
public void doLogin(Socket clientSocket) { string[] fields = Message.Split('|'); string username = fields[0]; string password = fields[1]; User user = Singleton.Singleton.Instance.DatabaseConnection.isPasswordRight(username, password); if (user != null) { if (Singleton.Singleton.Instance.isUserLogged(username)) { BasicPacket bp = new BasicPacket(GameProtocol.AlreadyOnlinePacketID()); Othello.Server.SendPacket(clientSocket, bp.getData()); } else { user.InGame = false; user.Socket = clientSocket; Singleton.Singleton.Instance.ListOfUsersLogged.Add(user); //Send to current user logged the list with all the users logged and the status of them string PacketMessage = ""; foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == username) { continue; } PacketMessage += u.Username + ":" + u.IsChallenged + ":" + u.InGame + "|"; MessagePacket messagePacket = new MessagePacket(GameProtocol.AlertUsersNewUserLoggedID(), username + ":False"); Othello.Server.SendPacket(u.Socket, messagePacket.getData()); } MessagePacket packet = new MessagePacket(GameProtocol.UsersLoggedListPacketID(), PacketMessage); Othello.Server.SendPacket(user.Socket, packet.getData()); } } else { BasicPacket bp = new BasicPacket(GameProtocol.FailedLoginPacketID()); Othello.Server.SendPacket(clientSocket, bp.getData()); } }
public void notifyUsersAfterChallengeRefuse() { string[] splits = Message.Split(':'); MessagePacket mp = new MessagePacket(GameProtocol.ChangeUserToOnline(), splits[0]); foreach (User u in Singleton.Singleton.Instance.ListOfUsersLogged) { if (u.Username == splits[0]) { u.IsChallenged = false; } else if (u.Username == splits[1]) { Othello.Server.SendPacket(u.Socket, new MessagePacket(GameProtocol.UserRefusedChallengePacketID(), splits[0]).getData()); } else { Othello.Server.SendPacket(u.Socket, mp.getData()); } } }
public int TestTransimitKCP() { double deltaTime = GetDeltaTime1(); if (mDataTime - deltaTime <= 0) { if (mSocket == null) { return(-1); } mNextUpdateTime = 0; GameProtocol gp = new GameProtocol(); int time = new Random().Next(3, 8); for (int i = 0; i < time; i++) { gp.data += gp.data; } gp.data = Math.Pow(2, time) * 53 + ":" + gp.data; byte[] data = StructureToByte <GameProtocol>(gp); Console.WriteLine("TestTransimit............." + mDataTimeStamp); int length = data.Length; var n = mKCP.Send(data, 0, length); if (mKCP.WaitSnd >= mKCP.SndWnd || !WriteDelay) { mKCP.Flush(false); } mKeepLiveTime = 1000; mDataTime = new Random().Next(500, 5000); mDataTimeStamp = mDataTime; return(n); } mDataTime -= deltaTime; return(0); }
public void playerAcceptedTheChallenge() { string[] usernames = Message.Split(':'); if (usernames.Length == 2) { User firstUser = null; User secondUser = null; foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == usernames[0]) { firstUser = user; } else if (user.Username == usernames[1]) { secondUser = user; } } if (firstUser != null && secondUser != null) { firstUser.IsChallenged = false; firstUser.InGame = true; secondUser.IsChallenged = false; secondUser.InGame = true; notifyUsersWithInGameStatus(firstUser); notifyUsersWithInGameStatus(secondUser); Room room = new Room(firstUser, secondUser, Singleton.Singleton.Instance.RoomIDHelper); Singleton.Singleton.Instance.RoomIDHelper += 1; MessageRoomPacket packet = new MessageRoomPacket(GameProtocol.LoadGameScene(), room.ID, firstUser.Username + ":" + secondUser.Username); //MessagePacket packet = new MessagePacket(GameProtocol.LoadGameScene(), room.ID, ) // + "|" + firstUser.Username + ":" + secondUser.Username); Othello.Server.SendPacket(firstUser.Socket, packet.getData()); Othello.Server.SendPacket(secondUser.Socket, packet.getData()); Singleton.Singleton.Instance.ListOfRooms.Add(room); } } }
private void SendLegalMoves(User x) { if (x.Player.IsHisTurn) { List <BoardPosition> tempList = Gameboard.GetAllLegalMoves(x.Player.DiskColor); string tempString = ""; int count = 1; foreach (BoardPosition aux in tempList) { if (count == tempList.Count) { tempString += aux.Row.ToString() + ':' + aux.Column + '!' + Gameboard.GetNumberOfChanges(aux.Row, aux.Column, x.Player.DiskColor); continue; } tempString += aux.Row.ToString() + ':' + aux.Column + '!' + Gameboard.GetNumberOfChanges(aux.Row, aux.Column, x.Player.DiskColor) + '|'; count++; } MessageRoomPacket movesPacket = new MessageRoomPacket(GameProtocol.BoardMoves(), ID, tempString); //DelayPacket(100); Othello.Server.SendPacket(x.Socket, movesPacket.getData()); } }
//Function called when you get a challenge. public IEnumerator getChallenged() { if (!SingletonUI.Instance.LogoutModalPanel.activeSelf) { Singleton.Singleton.Instance.Me.IsChallenged = true; SingletonUI.Instance.AcceptChallengeButton.enabled = true; SingletonUI.Instance.AcceptChallengeButton.GetComponent <Image>().color = SingletonUI.Instance.defaultButtonCollor; SingletonUI.Instance.ChallengePanel.SetActive(true); SingletonUI.Instance.ChallengeText.text = Message; } else { //If u get challenge when logout panel is active refuse them and dont show the challenge panel. string[] splits = Message.Split(' '); MessagePacket packet = new MessagePacket(GameProtocol.ChallengeRefusedPacketID(), Singleton.Singleton.Instance.Me.Username + ":" + splits[0]); Singleton.Singleton.Instance.Connection.SendPacket(packet.getData()); Singleton.Singleton.Instance.Me.IsChallenged = false; SingletonUI.Instance.ChallengePanel.SetActive(false); } yield return(null); }
//Open Logout panel. public void logoutAction() { //IF the challenge panel is active then send to the callenger refuse and close the panel if (SingletonUI.Instance.ChallengePanel.activeSelf) { string[] splits = SingletonUI.Instance.ChallengeText.text.Split(' '); MessagePacket packet = new MessagePacket(GameProtocol.ChallengeRefusedPacketID(), Singleton.Singleton.Instance.Me.Username + ":" + splits[0]); Singleton.Singleton.Instance.Connection.SendPacket(packet.getData()); Singleton.Singleton.Instance.Me.IsChallenged = false; SingletonUI.Instance.ChallengePanel.SetActive(false); } //Destroy every result panels ( if exists) var list = SingletonUI.Instance.Helper.GetComponentsInChildren <ChallengeResultScript>(); foreach (ChallengeResultScript x in list) { x.destroyMyself(); } SingletonUI.Instance.LogoutModalPanel.SetActive(true); }
private void sendMsg(GameProtocol pID, JsonObject msg, Action <Message> action) { sendMsg((uint)pID, msg, action); }
static void Main(string[] args) { var connection = new UDPSession(); connection.Connect("10.0.2.156", 7788); // 服务器 // connection.Connect("10.0.18.57", 7788);// 吴宇 var firstSend = true; var buffer = new byte[1024 * 32]; var counter = 0; UDPStatus lUDPStatus = UDPStatus.eUDPStatus_CONNECT_REQ; bool waitTransmit = false; while (true) { connection.Update(); switch (lUDPStatus) { case UDPStatus.eUDPStatus_CONNECT_REQ: { if (connection.ConnectUDP() < 0) { Console.WriteLine("Write message failed."); break; } lUDPStatus = UDPStatus.eUDPStatus_CONNECTING; } break; case UDPStatus.eUDPStatus_CONNECTING: { var n = connection.RecvUDP(buffer, 0, buffer.Length); if (n == 0) { Thread.Sleep(10); continue; } else if (n < 0) { Console.WriteLine("Receive Message failed."); break; } else { UDPProtocolConnectRsp rsp = UDPSession.ByteToStructure <UDPProtocolConnectRsp>(buffer); if (rsp != null) { if (connection.CheckConv(rsp.conv)) { if (rsp.p_type == (int)UDPStatus.eUDPStatus_CONNECT_RSP) { lUDPStatus = UDPStatus.eUDPStatus_CONNECTED; Console.WriteLine("Connect Success"); } } else { lUDPStatus = UDPStatus.eUDPStatus_UNKNOWN; Console.WriteLine("Session Conv Error"); break; } } } } break; case UDPStatus.eUDPStatus_CONNECTED: case UDPStatus.eUDPStatus_KEEP_LIVE: { bool live = false; connection.SendKeepLive(); connection.TestTransimitKCP(); UDPStatus n = connection.RecvKcp(buffer, 0, buffer.Length); switch (n) { case UDPStatus.eUDPStatus_KEEP_LIVE: { keepLiveCount++; Console.WriteLine("KeepLive:" + keepLiveCount); lUDPStatus = UDPStatus.eUDPStatus_KEEP_LIVE; live = true; } break; case UDPStatus.eUDPStatus_TRANSMIT: { GameProtocol data = UDPSession.ByteToStructure <GameProtocol>(buffer); if (data != null) { Console.WriteLine("TestTransimit............ receive data : " + data.data); live = true; } waitTransmit = false; } break; } if (live) { liveCount = 0; } else { liveCount++; // Thread.Sleep(1000); } if (liveCount > 5) { // lUDPStatus = UDPStatus.eUDPStatus_DISCONNECT; } // int rn = 0; // if (lUDPStatus == UDPStatus.eUDPStatus_KEEP_LIVE) // { // if (!waitTransmit) // { // rn = connection.TestTransimitKCP(); // waitTransmit = true; // if (rn < 0) // { // Console.WriteLine("TestTransimit............failed !!!!!."); // } // else if (rn > 0) // { // var n = connection.RecvUDP(buffer, 0, buffer.Length); // if (n > 0) // { // UDPProtocolTransmit transmit = UDPSession.ByteToStructure<UDPProtocolTransmit>(buffer); // if (transmit != null && // connection.CheckConv(transmit.conv) && // transmit.p_type == (int)UDPStatus.eUDPStatus_TRANSMIT) // { // Console.WriteLine("TestTransimit............ receive data : " + transmit); // } // } // } // } // else // { // var n = connection.RecvUDP(buffer, 0, buffer.Length); // if (n > 0) // { // UDPProtocolTransmit transmit = UDPSession.ByteToStructure<UDPProtocolTransmit>(buffer); // if (transmit != null && // connection.CheckConv(transmit.conv) && // transmit.p_type == (int)UDPStatus.eUDPStatus_TRANSMIT) // { // Console.WriteLine("TestTransimit............ receive."); // waitTransmit = false; // } // } // } // } ///////////////////////////////////////////////////////////////// // Thread.Sleep(1100); // rn = connection.SendKeepLive(); // if (rn < 0) // { // Console.WriteLine("Write message failed."); // break; // } // else if (rn > 0) // { // Thread.Sleep(10); // var n = connection.RecvUDP(buffer, 0, buffer.Length); // if (n > 0) // { // UDPProtocolKeepLive keepLive = // UDPSession.ByteToStructure<UDPProtocolKeepLive>(buffer); // if (keepLive != null && // connection.CheckConv(keepLive.conv) && // keepLive.p_type == (int)UDPStatus.eUDPStatus_KEEP_LIVE) // { // lUDPStatus = UDPStatus.eUDPStatus_KEEP_LIVE; // keepLiveCount++; // Console.WriteLine("KeepLive:" + keepLiveCount); // break; // } // } // } // int checkNum = 0; // for (int i = 0; i < 5; i++) // { // Thread.Sleep(1000); // rn = connection.SendKeepLive(); // if (rn < 0) // { // Console.WriteLine("Write message failed."); // checkNum++; // continue; // } // else if (rn > 0) // { // Thread.Sleep(10); // var n = connection.RecvUDP(buffer, 0, buffer.Length); // if (n > 0) // { // UDPProtocolKeepLive keepLive = // UDPSession.ByteToStructure<UDPProtocolKeepLive>(buffer); // if (keepLive != null && // connection.CheckConv(keepLive.conv) && // keepLive.p_type == (int)UDPStatus.eUDPStatus_KEEP_LIVE) // { // keepLiveCount++; // Console.WriteLine("KeepLive:" + keepLiveCount); // break; // } // } // checkNum++; // continue; // } // checkNum++; // } // // if (checkNum > 4) // { // lUDPStatus = UDPStatus.eUDPStatus_DISCONNECT; // } } break; case UDPStatus.eUDPStatus_DISCONNECT: { Console.WriteLine("DISCONNECT"); lUDPStatus = UDPStatus.eUDPStatus_DEAD; } break; case UDPStatus.eUDPStatus_DEAD: break; } // // if (firstSend) { // //firstSend = false; // // Console.WriteLine("Write Message..."); // var text = Encoding.UTF8.GetBytes(string.Format("Hello KCP: {0}", ++counter)); // if (connection.Send(text, 0, text.Length) < 0) { // Console.WriteLine("Write message failed."); // break; // } // } // // var n = connection.Recv(buffer, 0, buffer.Length); // if (n == 0) { // Thread.Sleep(10); // continue; // } else if (n < 0) { // Console.WriteLine("Receive Message failed."); // break; // } // // var resp = Encoding.UTF8.GetString(buffer, 0, n); // Console.WriteLine("Received Message: " + resp); } }
public void backToLobby() { MessageRoomPacket packet = new MessageRoomPacket(GameProtocol.BackToLobby(), Singleton.Instance.RoomID, Singleton.Instance.Me.Username); Singleton.Instance.Connection.SendPacket(packet.getData()); }
public void sendTheChallenge(User challengedUser, string message) { MessagePacket packet = new MessagePacket(GameProtocol.ChallengePacketID(), message); Othello.Server.SendPacket(challengedUser.Socket, packet.getData()); }
public void SendGameOver(User x, string message) { MessagePacket packet = new MessagePacket(GameProtocol.GameOverPacket(), message); Othello.Server.SendPacket(x.Socket, packet.getData()); }
public void opponentQuitAfterEndGame() { if (CurrentRoom.FirstUser != null && CurrentRoom.FirstUser.Username == Message) { Console.WriteLine("Client " + Message + " Disconnected!"); CurrentRoom.FirstUser.Socket.Shutdown(SocketShutdown.Both); CurrentRoom.FirstUser.Socket.Close(); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.FirstUser); if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } else { MessagePacket mp = new MessagePacket(GameProtocol.OpponentQuitAfterEndGame(), Message); Othello.Server.SendPacket(CurrentRoom.SecondUser.Socket, mp.getData()); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.SecondUser.Username) { continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.FirstUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.FirstUser = null; } } else if (CurrentRoom.SecondUser != null && CurrentRoom.SecondUser.Username == Message) { Console.WriteLine("Client " + Message + " Disconnected!"); CurrentRoom.SecondUser.Socket.Shutdown(SocketShutdown.Both); CurrentRoom.SecondUser.Socket.Close(); Singleton.Singleton.Instance.ListOfUsersLogged.Remove(CurrentRoom.SecondUser); if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } else { MessagePacket mp = new MessagePacket(GameProtocol.OpponentQuitAfterEndGame(), Message); Othello.Server.SendPacket(CurrentRoom.FirstUser.Socket, mp.getData()); foreach (User user in Singleton.Singleton.Instance.ListOfUsersLogged) { if (user.Username == CurrentRoom.FirstUser.Username) { continue; } MessagePacket p = new MessagePacket(GameProtocol.UserDisconnected(), CurrentRoom.SecondUser.Username); Othello.Server.SendPacket(user.Socket, p.getData()); } CurrentRoom.SecondUser = null; } } if (CurrentRoom.FirstUser == null && CurrentRoom.SecondUser == null) { Singleton.Singleton.Instance.ListOfRooms.Remove(CurrentRoom); } }
public void SendGameBoard(User x) { BoardPacket Packet = new BoardPacket(GameProtocol.BoardTableGamePacketID(), this); Othello.Server.SendPacket(x.Socket, Packet.getData()); }