using System.Net.Sockets;
using System.Text.RegularExpressions;
int listening_port = 33333;
string hl_ip = "127.0.0.1";
string hl_password = "ololo";
const int first_half = 1;
const int change_team = 2;
const int second_half = 3;
const bool not_ready = false;
readonly string[] warmups_cvars = { "mp_startmoney \"16000.0\"", "mp_freezetime \"0.0\"", "mp_buytime \"1.75\"", "mp_friendlyfire \"0.0\"", "mp_roundtime \"1.0\"", "mp_forcecamera \"2.0\"", "mp_forcechasecam \"2.0\"", "sv_restart \"3.0\"", "say \"** Warmup after restart!\"", "say \"** The game hasn't started! Warmup!\""};
readonly string[] halfs_cvars = { "mp_startmoney \"800.0\"", "mp_c4timer \"35.0\"", "mp_freezetime \"13.0\"", "mp_buytime \"0.25\"", "mp_friendlyfire \"1.0\"", "mp_roundtime \"1.75\"", "mp_forcecamera \"2.0\"", "mp_forcechasecam \"2.0\"", "sv_restart \"3.0\"", "say \"** The game will start after restart!\"", "say \"** Live! Live! Live! Good luck and Have fun!\""};
string[] players_name = new string[1024];
bool[] players_status = new bool[1024];
int[] players_userid = new int[1024];
string[] players_team = new string[1024];
int[] score = new int[3];
int connected_players = 0;
public static void Main() {
byte[] data = new byte[1024];
main_class main = new main_class();
IPEndPoint ip_port = new IPEndPoint(IPAddress.Any, main.listening_port);
UdpClient new_socket = new UdpClient(ip_port);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = new_socket.Receive(ref sender);
string msg = Encoding.ASCII.GetString(data, 0, data.Length);
msg = main.log_type(msg); // gogogo
//data = Encoding.ASCII.GetBytes(main.parse_info(msg));
new_socket.Send(data, data.Length, sender);
public string log_type(string text) {
string name = "", message = "";
main_class main = new main_class();
if(text.Contains("CTs_Win") || text.Contains("Bomb_Defused") || text.Contains("Target_Saved")) {
if (game_status == first_half || game_status == second_half) { score[ct]++; check_score(); } return message;
else if(text.Contains("Terrorists_Win") || text.Contains("Target_Bombed")) {
if(game_status == first_half || game_status == second_half) { score[tt]++; check_score(); } return message;
if(text.Contains("Rcon") || text.Contains("Server") || text.Contains("World") || text.Contains("Team")) return message;
name = text.Substring(text.IndexOf("\"") + 1, text.IndexOf("<") - text.IndexOf("\"") - 1);
userid = Convert.ToInt32(text.Substring(text.IndexOf("<") + 1, text.IndexOf(">") - text.IndexOf("<") - 1));
if(text.Contains("entered")) {
players_name[userid] = name;
players_status[userid] = not_ready;
check_player(userid, name);
else if(text.Contains("joined team")) {
if (text[text.IndexOf("joined team") + 13] == 'C') players_team[userid] = "CT";
else players_team[userid] = "TERRORIST";
Console.WriteLine("team azaza");
else if(text.Contains("disconnected") || text.Contains("was kicked")) {
players_name[userid] = "";
players_status[userid] = not_ready;
else if(text.Contains("changed name")) {
players_name[userid] = name;
else if(text.Contains("say")) {
if(text[text.IndexOf("say") + 5] == '!') {
switch (text[text.IndexOf("say") + 6]) {
if(!players_status[userid]) {
players_status[userid] = ready;
message = String.Format("say \"** Player {0} ready to play!\"", players_name[userid]);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
if(players_status[userid]) {
players_status[userid] = not_ready;
message = String.Format("say \"** Player {0} unready to play!\"", players_name[userid]);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
case change_team: goto case warmup;
case warmup: { message = String.Format("** Game status: Warmup."); break; }
case change_team: { message = String.Format("** Game status: Team changing. Counter-Terrorists {0} - {1} Terrorists.", score[ct], score[tt]); break; }
case first_half: { message = String.Format("** Game status: First half. Counter-Terrorists {0} - {1} Terrorists.", score[ct], score[tt]); break; }
case second_half: { message = String.Format("** Game status: Second half. Counter-Terrorists {0} - {1} Terrorists.", score[ct], score[tt]); break; }
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
public static void check_player(int userid, string name) {
main_class main = new main_class();
if(main.connected_players > pl) {
message = String.Format("kick #{0} \"Server is full, you can't spectating.\"", userid);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
switch(main.game_status) {
if(!main.players_name.Contains(name)) {
message = String.Format("kick #%d \"You aren't playing in this match.\"", userid);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
main.players_status[userid] = ready;
message = String.Format("say \"Player {0} returned.\"", main.players_name[userid]);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
case second_half: goto case first_half;
case change_team: goto case first_half;
main.players_status[userid] = not_ready;
message = String.Format("say \"Type !r for ready and match will start.\"");
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
public static void check_status() {
main_class main = new main_class();
switch(main.game_status) {
if(main.connected_players == pl && main.ready_players == pl) {}/*start_match();*/
else if(main.connected_players == pl && main.ready_players != pl) {
message = "say \"** Waiting for ready:";
for(int i = 0; i < main.players_status.Length; i++) {
if(!main.players_status[main.players_userid[i]]) {
message = String.Format("{0} {1}", message, main.players_name[main.players_userid[i]]);
message = String.Format("{0}\"", message);
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
else if(main.connected_players < pl) {
message = String.Format("say \"** Not enough players to start a game.\"");
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
case change_team: goto case warmup;
public static void check_score() {
main_class main = new main_class();
if(main.score[ct] + main.score[tt] == 15) {
main.score[tt] = main.score[ct];
else if (main.score[ct] == 16) end_match(ct);
else if (main.score[tt] == 16) end_match(tt);
else if (main.score[tt] == 15 && main.score[ct] == 15) end_match(0);
public static void start_warmup(bool num) {
main_class main = new main_class();
if(num) main.game_status = warmup;
if(main.game_status == first_half) main.game_status = change_team;
else main.game_status = warmup;
for(int i = 0; i < main.warmups_cvars.Length; i++) {
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, main.warmups_cvars[i]);
public static void start_match() {
main_class main = new main_class();
if(main.game_status == warmup) main.game_status = first_half;
else if(main.game_status == change_team) main.game_status = second_half;
for (int i = 0; i < main.halfs_cvars.Length; i++) {
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, main.halfs_cvars[i]);
public static void end_match(int team) {
main_class main = new main_class();
case tt: { message = String.Format("say \"** Terrorists win!\""); break; }
case ct: { message = String.Format("say \"** Counter-Terrorists win!\""); break; }
case 0: { message = String.Format("say \"** Draw!\""); break; }
for (int i = 0; i < 3; i++) {
main.sendRCON(main.hl_ip, main.hl_port, main.hl_password, message);
Array.Clear(main.players_status, 0, main.players_status.Length);
/* Credits to Waqas-Badar, <noindex><a rel="nofollow" href="https://c-s.net.ua/url?u=http%3A%2F%2Fwww.codeproject.com%2F" target="_blank">http://www.codeproject.com/</a></noindex> */
public byte[] prepareCommand(string command) {
byte[] bufferTemp = Encoding.ASCII.GetBytes(command);
byte[] bufferSend = new byte[bufferTemp.Length + 4];
//intial 5 characters as per standard
bufferSend[0] = byte.Parse("255");
bufferSend[1] = byte.Parse("255");
bufferSend[2] = byte.Parse("255");
bufferSend[3] = byte.Parse("255");
//copying bytes from challenge rcon to send buffer
for(int i = 0; i < bufferTemp.Length; i++) {
bufferSend[j++] = bufferTemp[i];
public string sendRCON(string serverIp, int serverPort, string rconPassword, string rconCommand)
main_class main = new main_class();
UdpClient client = new UdpClient();
client.Connect(serverIp, serverPort);
//sending challenge command to counter strike server
string getChallenge = "challenge rcon\n";
byte[] bufferSend = this.prepareCommand(getChallenge);
//send challenge command and get response
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, main.listening_port);
client.Send(bufferSend, bufferSend.Length);
byte[] bufferRec = client.Receive(ref RemoteIpEndPoint);
//retrive number from challenge response
string challenge_rcon = Encoding.ASCII.GetString(bufferRec);
challenge_rcon = string.Join(null, Regex.Split(challenge_rcon, "[^\\d]"));
//preparing rcon command to send
string command = "rcon " + challenge_rcon + " \"" + rconPassword + "\" " + rconCommand + "\n";
bufferSend = this.prepareCommand(command);
client.Send(bufferSend, bufferSend.Length);
bufferRec = client.Receive(ref RemoteIpEndPoint);
return Encoding.UTF8.GetString(bufferRec);
/* Credits to Waqas-Badar, <noindex><a rel="nofollow" href="https://c-s.net.ua/url?u=http%3A%2F%2Fwww.codeproject.com%2F" target="_blank">http://www.codeproject.com/</a></noindex> */