Example #1
0
        public void Start(lib.ILogger logger)
        {
            // init variables
            lib.RCON_Client rcon = new lib.RCON_Client(addr, port, passwd, logger);
            lib.RCON_Event  latest_evt;
            bool            ongoing_game;

            lib.EventType latest_evt_type;
            dynamic       json_obj;

            // current bet and next bet
            Bet[] bets        = new Bet[2];
            int   current_bet = 0;
            int   next_bet    = 1;

            Player[] connected_players    = new Player[20];
            Player[] disconnected_players = new Player[200];

            // start doing stuff
            int amout_of_games = 0;

            rcon.Connect();

            // enable mutators before anything else
            sendRequest(rcon, lib.RequestType.command, "enablemutators");

            while (amout_of_games < 10)
            {
                ongoing_game = true;
                while (ongoing_game)
                {
                    latest_evt      = receiveEvt(rcon);
                    latest_evt_type = (lib.EventType)latest_evt.EventID;
                    json_obj        = latest_evt.JsonAsObj;

                    switch (latest_evt_type)
                    {
                    case lib.EventType.match_end:
                        Console.WriteLine("End of the game");
                        ongoing_game = false;
                        break;

                    case lib.EventType.rcon_ping:
                        sendRequest(rcon, lib.RequestType.ping, "pong");
                        break;

                    case lib.EventType.rcon_disconnect:
                        rcon.Connect();
                        break;

                    case lib.EventType.player_connect:
                    {
                        Profile profile_connect = createProfile((string)json_obj.Profile, (string)json_obj.Store);
                        int     index           = indexPlayerGivenProfile(disconnected_players, profile_connect);
                        int     null_index      = indexFirstNull(connected_players);
                        // if player exists (already joined the ongoing game before)
                        if (index != -1)
                        {
                            if (null_index == -1)
                            {
                                Console.WriteLine("PROBLEM: more than 20 players in server should be impossible.");
                                ongoing_game   = false;
                                amout_of_games = 10;
                            }
                            else
                            {
                                disconnected_players[index].Connected();
                                connected_players[null_index] = disconnected_players[index];
                                disconnected_players[index]   = null;
                            }
                        }
                        // if first time player joined the ongoing game
                        else
                        {
                            Player player = new Player((string)json_obj.PlayerName, profile_connect);
                            connected_players[null_index] = player;
                        }

                        //display all connected players
                        printPlayers(connected_players, true);
                    }
                    break;

                    case lib.EventType.player_disconnect:
                    {
                        Profile profile_disconnect = createProfile(json_obj.Profile.ToString());
                        int     index      = indexPlayerGivenProfile(connected_players, profile_disconnect);
                        int     null_index = indexFirstNull(disconnected_players);

                        Player player = connected_players[index];

                        player.Disconnected();
                        player.IsAlive = false;

                        if (bets[current_bet] != null)
                        {
                            bets[current_bet].UpdateDeadPlayer(player);
                        }

                        disconnected_players[null_index] = connected_players[index];
                        connected_players[index]         = null;

                        // display all disconnected players
                        printPlayers(disconnected_players, false);
                    }
                    break;
                    }
                }
                amout_of_games++;
            }

            rcon.Disconnect();
        }
Example #2
0
        static int Main(string[] args)
        {
            lib.ILogger logger = new lib.ConsoleLogger();
            logger.StopWriting();
            try
            {
                /*
                 * Example on how to use the BM rcon lib
                 * The program reads from the chat.
                 * If in the chat is written "!bigtext Hello",
                 * it will call the "eventtext" command with "Hello" as string in red.
                 * If a player taunts, the program will stops.
                 */
                string body        = passwd;
                string bigtext_cmd = "!bigtext";
                // init rcon object with address, port and password
                lib.RCON_Client rcon_obj = new lib.RCON_Client(addr, port, body, logger);
                // connect the rcon client to addr:port with body
                rcon_obj.Connect();
                Console.WriteLine("");

                // enable mutators on server if not enabled
                // sendRequest(rcon_obj, RequestType.command, "enablemutators");

                lib.RCON_Event evt;
                while (true)
                {
                    // receive the latest event
                    evt = rcon_obj.ReceiveEvent();
                    Console.WriteLine("");

                    // check if somebody type something in the chat
                    if (evt.EventID == (short)lib.EventType.chat_message)
                    {
                        // get the message
                        String msg = evt.JsonAsObj.Message.ToString();

                        // check if "!bigtext" is in the message
                        int index = msg.IndexOf(bigtext_cmd);
                        if (index != -1)
                        {
                            // get the text to display
                            string bigtext = msg.Substring(index + bigtext_cmd.Length);
                            // send a request which is the command "eventtext" with its parameters
                            sendRequest(rcon_obj, RequestType.command, $"eventtext \"{bigtext}\" \"255\"");
                        }
                    }

                    // if the server ping the rcon client,
                    // ping it back to keep the connection alive
                    if (evt.EventID == (short)lib.EventType.rcon_ping)
                    {
                        sendRequest(rcon_obj, RequestType.ping, "I pinged");
                    }
                    // if a player taunts, stop the loop
                    if (evt.EventID == (short)lib.EventType.player_taunt)
                    {
                        break;
                    }
                }

                // avoid sending to oblivion the last request made
                Thread.Sleep(160);

                // disconnect the rcon client
                rcon_obj.Disconnect();
            }
            // if something goes wrong, you will end up here
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Something went wrong in the main.");
            }

            logger.StopWriting();

            // press 'Enter' to exit the console
            Console.Read();
            return(0);
        }