swf flash c# Lobby V2 Coded By:Rafael Macapagal

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...

dede454531

ReportSpammer
Banlı Üye
Katılım
15 Ara 2017
Mesajlar
300
Beğeni Puanı
22
Puanları
18
C#:
    public class MatchmakingGroup
    {
        public string gameID;
        public List<Player> players = new List<Player>();
        public int timer = 5;
        public string gameType;
        public bool readyForStart = false;
    }
    
    [RoomType("lobby")]
    public class LobbyCode : Game<Player>
    {
        public int mpversion = 18;

        public int timestamp;
        public List<MatchmakingGroup> matchmakings;
        bool joinable;
        public override void GameStarted()
        {
            joinable = true;
            matchmakings = new List<MatchmakingGroup>();
            PreloadPlayerObjects = true;
            timestamp = 0;
            AddTimer(delegate
            {
                timestamp += 1;
            }, 250);
        }
        
        public override void UserJoined(Player player)
        {
            string playerList = "";
            foreach (Player p in Players)
            {
                if (p.Id != player.Id)
                    playerList += "|" + p.Id + ";" + p.ConnectUserId.Split('b')[1] + ";" + p.PlayerObject.GetString("fbname") + ";" + p.PlayerObject.GetString("skin") + ";" + p.PlayerObject.GetInt("highscore") + ";" + p.PlayerObject.GetInt("rounds") + ";" + p.PlayerObject.GetInt("mp_won") + ";" + p.PlayerObject.GetInt("mp_lost") + ";" + p.PlayerObject.GetInt("level") + ";" + p.PlayerObject.GetInt("xp") + ";" + p.mp_permissions + ";" + p.PlayerObject.GetArray("achievements").Count + ";" + p.PlayerObject.GetInt("prestige") + ";" + (p.wannaPlay == true ? 1 : 0) + ";";
            }
            if (playerList.Length > 1)
                playerList = playerList.Substring(1, playerList.Length - 2);

            player.Send("privmsg", player.Id, "joinSuccess", playerList);
            Broadcast("pubmsg", player.Id, "PlayerJoined", player.ConnectUserId.Split('b')[1], player.PlayerObject.GetString("fbname"), player.PlayerObject.GetString("skin"), player.PlayerObject.GetInt("highscore"), player.PlayerObject.GetInt("rounds"), player.PlayerObject.GetInt("mp_won"), player.PlayerObject.GetInt("mp_lost"), player.PlayerObject.GetInt("level"), player.PlayerObject.GetInt("xp"), player.mp_permissions, player.PlayerObject.GetArray("achievements").Count, player.PlayerObject.GetInt("prestige"));
        }

        public override void UserLeft(Player player)
        {
            if (player.inMatchmaking)
            {
                leaveMatchmaking(player);
            }
            Broadcast("pubmsg", player.Id, "PlayerLeft");
        }

        public void searchMatchFor(Player player)
        {
            for (int i = 0; i < matchmakings.Count; i++)
            {
                if (matchmakings[i].players.Count < 4)
                {
                    if (matchmakings[i].gameType == player.wannaPlayGametype)
                    {
                        joinMatchmaking(i, player);
                        return;
                    }
                }
            }

            List<Player> opponentsThere = new List<Player>();
            foreach (Player p in Players)
            {
                if (p.wannaPlay && p.Id != player.Id && p.wannaPlayGametype == player.wannaPlayGametype)
                {
                    opponentsThere.Add(p);
                }
            }
            if (opponentsThere.Count < 1)
                return;

            MatchmakingGroup cmg = new MatchmakingGroup();
            Random gameIDrand = new Random();
            cmg.gameID = player.wannaPlayGametype + "_" + gameIDrand.Next(0, 100000000);
            cmg.gameType = player.wannaPlayGametype;
            matchmakings.Add(cmg);
            joinMatchmaking(matchmakings.IndexOf(cmg), player);
            for (int i = 0; i < opponentsThere.Count; i++)
            {
                joinMatchmaking(matchmakings.IndexOf(cmg), opponentsThere[i]);
            }
        }
        public void joinMatchmaking(int id, Player player)
        {
            matchmakings[id].players.Add(player);

            Message joinMMGroup = Message.Create("privmsg");
            joinMMGroup.Add("joinGroup");
            joinMMGroup.Add(matchmakings[id].gameID);
            joinMMGroup.Add(matchmakings[id].gameType);
            for (int i = 0; i < matchmakings[id].players.Count; i++)
            {
                joinMMGroup.Add(matchmakings[id].players[i].Id + ";" + (matchmakings[id].players[i].matchmakingReady == true ? 1 : 0));
            }
            player.Send(joinMMGroup);

            Message playerJoinMMGroup = Message.Create("privmsg");
            playerJoinMMGroup.Add("playerJoinedGroup");
            playerJoinMMGroup.Add(player.Id);

            foreach (Player p in matchmakings[id].players)
            {
                if (p.Id != player.Id)
                    p.Send(playerJoinMMGroup);
            }

            player.wannaPlay = false;
            player.wannaPlayGametype = "";
            player.inMatchmaking = true;
            Broadcast("pubmsg", player.Id, "wannaPlay", false);
        }
        public void leaveMatchmaking(Player player)
        {
            int id = -1;
            for (int i = 0; i < matchmakings.Count; i++)
            {
                if (matchmakings[i].players.Contains(player))
                {
                    id = i;
                }
            }
            if (id == -1)
            {
                return;
            }
            matchmakings[id].players.Remove(player);
            Message playerJoinMMGroup = Message.Create("privmsg");
            playerJoinMMGroup.Add("playerLeftGroup");
            playerJoinMMGroup.Add(player.Id);

            foreach (Player p in matchmakings[id].players)
            {
                p.Send(playerJoinMMGroup);
            }

            player.matchmakingReady = false;
            player.inMatchmaking = false;

            Console.WriteLine("Player" + player.Id + " left matchmaking " + matchmakings[id].gameID);

            if (matchmakings[id].players.Count < 1)
            {
                Console.WriteLine("Removing matchmaking " + matchmakings[id].gameID);
                matchmakings[id] = null;
                matchmakings.RemoveAt(id);
                Console.WriteLine("Matchmakings left: " + matchmakings.Count);
                return;
            }

            if (matchmakings[id].readyForStart)
            {
                if (!isReadyMatchmaking(id))
                {
                    matchmakings[id].readyForStart = false;
                }
            }
        }
        public void readyMatchmaking(Player player)
        {
            int id = -1;
            for (int i = 0; i < matchmakings.Count; i++)
            {
                if (matchmakings[i].players.Contains(player))
                {
                    id = i;
                }
            }
            if (id == -1)
            {
                return;
            }
            Message playerJoinMMGroup = Message.Create("privmsg");
            playerJoinMMGroup.Add(player.Id);
            playerJoinMMGroup.Add("playerReady");

            foreach (Player p in matchmakings[id].players)
            {
                p.Send(playerJoinMMGroup);
            }
            player.matchmakingReady = true;
            checkReadyMatchmaking(id);
        }
        public void checkReadyMatchmaking(int id)
        {
            if (isReadyMatchmaking(id))
                startCountdownMatchmaking(matchmakings[id]);
        }
        public bool isReadyMatchmaking(int id)
        {
            if (matchmakings[id].players.Count < 2)
                return false;
            if (matchmakings[id].players.Count > 3)
            {
                return true;
            }
            foreach (Player p in matchmakings[id].players)
            {
                if (!p.matchmakingReady)
                {
                    return false;
                }
            }
            return true;
        }
        public void startCountdownMatchmaking(MatchmakingGroup mmg)
        {
            if (mmg.readyForStart)
                return;
            mmg.readyForStart = true;
            mmg.timer = 6;
            tickMatchmaking(mmg);
        }
        public void tickMatchmaking(MatchmakingGroup mmg)
        {
            if (mmg == null)
                return;
            if (!mmg.readyForStart)
                return;

            mmg.timer--;
            if (mmg.timer > 0)
            {
                foreach (Player p in mmg.players)
                {
                    p.Send("privmsg", "tickGroup", mmg.timer);
                }
                ScheduleCallback(delegate() { tickMatchmaking(mmg); }, 1000);
            }
            else
            {
                String playerUids = "";
                String playerNames = "";
                foreach (Player p in mmg.players)
                {
                    playerUids += p.ConnectUserId.Split('b')[1] + ";";
                    playerNames += p.PlayerObject.GetString("fbname") + ";";
                }
                playerUids = playerUids.Substring(0, playerUids.Length - 1);
                playerNames = playerNames.Substring(0, playerNames.Length - 1);
                foreach (Player p in mmg.players)
                {
                    p.Send("privmsg", "joinGame", mmg.gameID, mmg.gameType, playerUids, playerNames);
                }
                for (int i = 0; i < matchmakings.Count; i++)
                {
                    if (matchmakings[i].gameID == mmg.gameID)
                    {
                        Console.WriteLine("deleting matchmaking group at " + i);
                        matchmakings[i] = null;
                        matchmakings.RemoveAt(i);
                        Console.WriteLine("Matchmakings left: " + matchmakings.Count);
                        return;
                    }
                }
            }
        }

        public override void GotMessage(Player player, Message message)
        {
            switch (message.Type)
            {
                case "pubmsg":
                    if (message[0].ToString() == "adminCMD")
                    {
                        return;
                    }
                    if (message.GetString(0) == "chatMSG")
                    {
                        if (player.mp_permissions == 0)
                        {
                            if (aU.chat_muted)
                                return;
                            if (player.lastChatTime + 19 >= timestamp)
                            {
                                player.chatSpamCount++;
                                return;
                            }
                            if (player.lastChatMsg == message.GetString(1))
                            {
                                player.chatSpamCount++;
                                return;
                            }
                            else
                            {
                                //player.chatSpamCount--;
                            }

                            if (player.chatSpamCount > 8)
                            {
                                player.Disconnect();
                                return;
                            }

                            if (msgCheck.check(message.GetString(1), player))
                            {
                                return;
                            }

                            player.lastChatTime = timestamp;
                            player.lastChatMsg = message.GetString(1);
                        }
                    }
                    Message m = Message.Create("pubmsg");
                    m.Add(player.Id);
                    for (uint i = 0; i < message.Count; i++)
                    {
                        m.Add(message[i]);
                    }
                    Broadcast(m);

                    if (message.GetString(0) == "wannaPlay")
                    {
                        player.wannaPlay = message.GetBoolean(1);
                        if (message.Count > 2)
                            player.wannaPlayGametype = message.GetString(2);
                        else
                            player.wannaPlayGametype = "";
                        searchMatchFor(player);
                    }

                    Console.WriteLine("Public Message from: " + player.Id + "\ncontent:" + m.ToString());

                    break;
                case "privmsg":
                    Message pm = Message.Create("privmsg");

                    pm.Add(player.Id);
                    for (uint i = 1; i < message.Count; i++)
                    {
                        pm.Add(message[i]);
                    }

                    foreach (Player p in Players)
                    {
                        if (p.Id == message.GetInt(0))
                        {
                            p.Send(pm);
                        }
                    }
                    Console.WriteLine("Private Message from: " + player.Id + " to: " + message.GetInt(0) + "\ncontent:" + pm.ToString());
                    break;
                case "sysmsg":
                    if (message[0].ToString() == "adminCMD")
                    {
                        aU.cmd(player,message.GetString(1),Players);
                    }
                    else if (message.GetString(0) == "leaveCLG")
                    {
                        if (player.inMatchmaking)
                        {
                            leaveMatchmaking(player);
                        }
                    }
                    else if (message.GetString(0) == "readyCLG")
                    {
                        if (player.inMatchmaking)
                        {
                            readyMatchmaking(player);
                        }
                    }
                    break;
            }
        }
    }

    [RoomType("mpmatch")]
    public class GameCode : Game<Player>
    {
        public int[] playerStatuses = { 0, 0, 0, 0 };
        public string[] playerAccounts = new string[10];
        public bool timersInited = false;
        public bool preJoinState = true;
        public bool gameOn = false;
        public int[] spawnsX = { 174, 136, 98, 59 };
        public int startRoundCountdown = 0;
        public bool gameRunning = false;
        public int obstaclesAmount = 0;
        public bool[] playersAlive = new bool[10];
        public uint currentRound = 0;
        public uint[] playerRoundScores = new uint[10];
        public uint[] playerRoundGamepoints = new uint[10];
        public uint[] playerAllGamepoints = new uint[10];
        public uint[] playerAllScores = new uint[10];
        public bool[] playersReadyForNextRound = new bool[10];
        public bool gameIsOver = false;
        public void initRoomTimers()
        {
            ScheduleCallback(delegate
            {
                allJoinedTimeout();
            }, 6000);
            timersInited = true;
        }
        public override void GameStarted()
        {
            AddTimer(delegate
            {
            }, 25);

            AddTimer(delegate
            {
                RefreshDebugView();
            }, 250);
        }

        public override void GameClosed()
        {
            Console.WriteLine("RoomId: " + RoomId);
        }

        public override bool AllowUserJoin(Player player)
        {
            if (PlayerCount >= 4)
            {
                return false;
            }
            return base.AllowUserJoin(player);
        }

        public override void UserJoined(Player player)
        {
            if (Array.IndexOf(RoomData["playersUids"].Split(new char[] { ';' }), player.JoinData["uid"]) != -1)
            {
                playerStatuses[Array.IndexOf(RoomData["playersUids"].Split(new char[] { ';' }), player.JoinData["uid"])] = 1;

                player.Send("privmsg", player.Id, "joinSuccess", RoomData["playersUids"], RoomData["playersNames"], playerStatuses[0].ToString() + ";" + playerStatuses[1].ToString() + ";" + playerStatuses[2].ToString() + ";" + playerStatuses[3].ToString());

                Broadcast("pubmsg", player.Id, "PlayerJoined", player.JoinData["uid"]);

                Console.WriteLine("User Joined: " + player.Id);

                CheckGameReady();
            }
            else
            {
                player.Send("privmsg", player.Id, "joinFail", "Unexpected game-joining:" + RoomData["playerUids"]);
                Console.WriteLine("User Joined unexpected (kicked): " + player.Id);
            }
            if (!timersInited)
            {
                initRoomTimers();
            }
        }
        public void allJoinedTimeout()
        {
            if (gameOn)
                return;
            if (!preJoinState)
                return;
            for (uint i = 0; i < RoomData["playersUids"].Split(new char[] { ';' }).Length; i++)
            {
                if (playerStatuses[i] == 0)
                    playerStatuses[i] = 2;
            }
            if (PlayerCount < 2)
            {
                Broadcast("srvrmsg", "CloseRoom");
                return;
            }
            CheckGameReady();
        }
        public void CheckGameReady()
        {
            if (gameOn)
                return;
            for (uint i = 0; i < RoomData["playersUids"].Split(new char[] { ';' }).Length; i++)
            {
                if (playerStatuses[i] == 0)
                    return;
            }
            uint readyPlayersNum = 0;
            for (uint i = 0; i < RoomData["playersUids"].Split(new char[] { ';' }).Length; i++)
            {
                if (playerStatuses[i] == 1)
                    readyPlayersNum++;
            }
            preJoinState = false;
            if (readyPlayersNum != RoomData["playersUids"].Split(new char[] { ';' }).Length)
            {
                if (readyPlayersNum >= 2)
                {
                    Broadcast("srvrmsg", "preGameStatus", 1);
                    preinitGame();
                }
                else
                {
                    Broadcast("srvrmsg", "preGameStatus", 2);
                    ScheduleCallback(delegate { Broadcast("srvrmsg", "CloseRoom"); }, 3000);
                }
            }
            else
            {
                Broadcast("srvrmsg", "preGameStatus", 0);
                preinitGame();
            }
        }
        public void preinitGame()
        {
            ScheduleCallback(delegate { gameOn = true; initGame(); }, 3000);
        }
        public void initGame()
        {
            string spawnMpIds = "";
            string spawnPositions = "";
            string spawnAccounts = "";

            uint ci = 0;
            foreach (Player p in Players)
            {
                spawnMpIds += p.Id + ";";
                spawnPositions += spawnsX[ci] + ";";
                spawnAccounts += playerAccounts[p.Id] + ";";
                ci++;
            }
            spawnMpIds = spawnMpIds.Remove(spawnMpIds.Length - 1);
            spawnPositions = spawnPositions.Remove(spawnPositions.Length - 1);
            spawnAccounts = spawnAccounts.Remove(spawnAccounts.Length - 1);
            string c_map = "";
            Random maprand = new Random();
            for (uint i = 0; i < 20; i++)
            {
                c_map += maprand.Next(0, obstaclesAmount) + ";";
            }
            c_map = c_map.Remove(c_map.Length - 1);
            Broadcast("srvrmsg", "spawnAll", spawnMpIds, spawnPositions, spawnAccounts, c_map);
            foreach (Player p in Players)
            {
                playersAlive[p.Id] = true;
            }
            startRoundCountdown = 3;
            startRoundTick();
        }
        public void startRoundTick()
        {
            if (currentRound >= 3)
                return;
            if (startRoundCountdown <= 0)
            {
                startRoundCountdown = 0;
                gameRunning = true;
                Broadcast("srvrmsg", "startRoundRun");
                return;
            }
            Broadcast("srvrmsg", "roundCountdown", startRoundCountdown);
            startRoundCountdown -= 1;
            ScheduleCallback(delegate { startRoundTick(); }, 1000);
        }
        public void CheckAllDead()
        {
            if (!gameRunning)
                return;
            foreach (Player p in Players)
            {
                if (playersAlive[p.Id])
                    return;
            }
            gameRunning = false;
            ScheduleCallback(delegate { endRound(); }, 1000);
        }
        public void endRound()
        {
            currentRound++;
            string roundGamepointsString = "";
            foreach (Player p in Players)
            {
                uint playersBetterNum = 0;
                foreach (Player p2 in Players)
                {
                    if (playerRoundScores[p2.Id] > playerRoundScores[p.Id])
                    {
                        playersBetterNum++;
                    }
                }
                playerRoundGamepoints[p.Id] = (uint)PlayerCount - playersBetterNum - 1;
                playerAllGamepoints[p.Id] += playerRoundGamepoints[p.Id];
                roundGamepointsString += p.Id + ":" + playerRoundGamepoints[p.Id] + ";";
            }
            roundGamepointsString.Remove(roundGamepointsString.Length - 1);
            Broadcast("srvrmsg", "roundEnd", currentRound, roundGamepointsString);
            ScheduleCallback(delegate { startNextRound(); }, 10000);

        }
        public void CheckAllReadyForNextRound()
        {
            if (currentRound >= 3)
                return;
            foreach (Player p in Players)
            {
                if (!playersReadyForNextRound[p.Id])
                    return;
            }
            playersReadyForNextRound = new bool[10];
            startNextRound();
        }
        public void startNextRound()
        {
            if (currentRound >= 3)
            {
                endGame();
                return;
            }
            if (gameRunning)
                return;
            foreach (Player p in Players)
            {
                playersAlive[p.Id] = true;
                playersReadyForNextRound[p.Id] = false;
            }
            gameRunning = true;
            startRoundCountdown = 3;
            startRoundTick();
        }
        public void endGame()
        {
            string results_data = "";
            foreach (Player p in Players)
            {
                results_data += p.Id + ":" + playerAllGamepoints[p.Id] + ":" + playerAllScores[p.Id] + ";";
            }
            results_data.Remove(results_data.Length - 1);
            Broadcast("srvrmsg", "endGame", results_data);
            gameIsOver = true;
        }
        public override void UserLeft(Player player)
        {
            if (gameIsOver)
                return;
            Broadcast("pubmsg", player.Id, "PlayerLeft");
            if (gameOn)
            {
                if (gameRunning)
                {
                    CheckAllDead();
                }
                return;
            }
            if (Array.IndexOf(RoomData["playersUids"].Split(new char[] { ';' }), player.JoinData["uid"]) != -1)
            {
                playerStatuses[Array.IndexOf(RoomData["playersUids"].Split(new char[] { ';' }), player.JoinData["uid"])] = 2;
            }
            CheckGameReady();

            Console.WriteLine("User Left: " + player.Id);
        }

        public override void GotMessage(Player player, Message message)
        {
            if (gameIsOver)
                return;
            switch (message.Type)
            {
                case "pubmsg":
                    if (message[0].ToString() == "adminCMD")
                    {
                        return;
                    }
                    Message m = Message.Create("pubmsg");
                    m.Add(player.Id);
                    for (uint i = 0; i < message.Count; i++)
                    {
                        m.Add(message[i]);
                    }
                    Broadcast(m);

                    if (message.GetString(0) == "die")
                    {
                        playersAlive[player.Id] = false;
                        playerRoundScores[player.Id] = message.GetUInt(1);
                        playerAllScores[player.Id] += message.GetUInt(1);
                        CheckAllDead();
                    }
                    else if (message.GetString(0) == "readyForNextRound")
                    {
                        playersReadyForNextRound[player.Id] = true;
                        CheckAllReadyForNextRound();
                    }

                    Console.WriteLine("Public Message from: " + player.Id + "\ncontent:" + m.ToString());

                    break;
                case "privmsg":
                    Message pm = Message.Create("privmsg");

                    pm.Add(player.Id);
                    for (uint i = 1; i < message.Count; i++)
                    {
                        pm.Add(message[i]);
                    }

                    foreach (Player p in Players)
                    {
                        if (p.Id == message.GetInt(0))
                        {
                            p.Send(pm);
                        }
                    }
                    Console.WriteLine("Private Message from: " + player.Id + " to: " + message.GetInt(0) + "\ncontent:" + pm.ToString());
                    break;
                case "sysmsg":
                    if (message.GetString(0) == "myAccount")
                    {
                        playerAccounts[player.Id] = message.GetString(1);
                        obstaclesAmount = message.GetInt(2);
                    }
                    break;
            }
        }
    }
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...

Üst Alt