The online racing simulator
First InSim move
2
(48 posts, started )
I have trouble with admins... Admins are defined in admin.txt and that looks like this
#Ovde se definisu admini

Admin=nesrulz
Admin=M.M.L.
Admin=gagag

My code for reading admin.txt:
string[] lines = System.IO.File.ReadAllLines("admin.txt");
foreach (string line in lines)
{
//Do something with line
if (line.StartsWith("#", false, System.Globalization.CultureInfo.InvariantCulture))
{

}
else
{
if (line.ToLower().Contains("admin="))
{
try
{
string medjuvr = line.Remove(0, 6);
Console.WriteLine(medjuvr);
if (medjuvr == ncn.UName)
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 1,
});
}
else
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 0,
});
}
}
catch
{
}
}
}
}

This is for buttons:
if (_connections[ncn.UCID].ServerAdmin == 1)

{

insim.Send(new IS_BTN
{
H = 10,
W = 20,
L = 25,
T = 25,
Text = "^2SHOW GREEN FLAG",
UCID = ncn.UCID,
ClickID = 1,
ReqI = 255,
BStyle = ButtonStyles.ISB_CLICK
});
....................
....................
....................

You're converting the username to lowercase with this line

line.ToLower().Contains("admin=")

If you're trying the server with yourself as the admin, it obviously won't work as string comparison is case sensitive. Also avoid using == to compare strings (or any other non-primitive types). In Java using == compares pointers to the objects, not the objects themselves, I suppose C# is the same in this respect. Use CompareTo() function.
It works only for the first admin in the list. Also works with ToLower and withuot.
It's impossible to help without better description of the problem, "it doesn't work" really doesn't give us much to go with. By any chance, does this work?

string[] lines = System.IO.File.ReadAllLines("admin.txt");
foreach (string line in lines)
{
//Do something with line
if (!line.StartsWith("#", false, System.Globalization.CultureInfo.InvariantCulture))
{
int delimiterIdx = line.IndexOf("="); //Find first "=" on the line
//Extract text from the beginning of the line to the last character before the first "="
string lineKey = line.Substring(0, delimiterIdx - 1).ToLower();
if (lineKey.Equals("admin"))
{
try
{
//Get the name of the admin
string medjuvr = line.Substring(delimiterIdx + 1);
Console.WriteLine(medjuvr);
if (medjuvr.Equals(ncn.UName))
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 1,
});
}
else
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 0,
});
}
}
catch
{
}
}
}
}

DarkTimes and co. compatible version (don't use for educational purposes)

string[] lines = System.IO.File.ReadAllLines("admin.txt");
foreach (string line in lines)
{
//Do something with line
if (!line.StartsWith("#", false, System.Globalization.CultureInfo.InvariantCulture))
{
int delimiterIdx = line.IndexOf("=");
if (line.Substring(0, delimiterIdx - 1).ToLower().Equals("admin"))
{
try
{
if (line.Substring(delimiterIdx + 1).Equals(ncn.UName))
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 1,
});
}
else
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 0,
});
}
}
catch
{
}
}
}
}

I don't have a C# compiler set up here (nor the knowledge of the language) so minor modifications might be required to get it compiled.
foreach (string line in lines)
{

if (line.StartsWith("#", false, System.Globalization.CultureInfo.InvariantCulture))
{

} //Skiping the lines if start with #
else
{
if (line.Contains("Admin="))
{
try
{
string medjuvr = line.Remove(0, 6); //medjuvr is username of admin
Console.WriteLine(medjuvr);
if (medjuvr == ncn.UName) //if medjuvr match with ncn.UName
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 1,
});
}
else //if not match
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 0,
});
}
}
catch
{
}
}
}
}

If in txt I have three admins, console.writeline(medjuwr); prints 3times muliply 3admins and insim disconnects. I added comments.
I get exception "KeyNot found" the given key is not present in the dictionary
Can you post the actual output of your program? Also LFS usually gives error messages when it terminates connection to an InSim app.
I change function and now works ok... Thanks for help

string[] lines = System.IO.File.ReadAllLines("admin.txt");
foreach (string line in lines)
{

if (line.StartsWith("#", false, System.Globalization.CultureInfo.InvariantCulture))
{

} //Skiping the lines if start with #
else
{
if (line.Contains("Admin="))
{
try
{
string medjuvr = line.Remove(0, 6); //medjuvr is username of admin
Console.WriteLine(medjuvr);
if (medjuvr == ncn.UName) //if medjuvr match with ncn.UName
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 1,
});
}
}
catch { }
}
}
}
if (_connections.ContainsKey(ncn.UCID))
{

}
else
{
_connections.Add(ncn.UCID, new Connections
{
UCID = ncn.UCID,
UName = ncn.UName,
PName = ncn.PName,
IsAdmin = ncn.Admin,
ServerAdmin = 0,
});
}

When is contact between cars, i want to only admins see message about that.

static void Kontakt(InSim insim, IS_CON con)
{

insim.Send("/msg ^1Kontakt izmedju: ^8"+_players[con.A.PLID].PName+" i "+_players[con.B.PLID].PName);
}

So I need to edit this function... Do I need to create one new class Admin where will be ucid and how to read all ucid and send message to all admins?
Wouldn't something like

foreach(Connections conn in _connections) {
if(conn.IsAdmin == 1)
insim.Send(new IS_MTC {ucid = conn.UCID,
Msg = "blah" });
}

do?
Cannot convert type 'System.Collections.Generic.KeyValuePair<byte,ConsoleApplication1.Connections>' to 'ConsoleApplication1.Connections' C:\Users\Damir\documents\visual studio 2010\Projects\insimmm\insimmm\Program.cs

Underline foreach
I have no knowledge of C#, so pardon my occasional ignorance, apparently the correct syntax is

foreach(KeyValuePair<byte, Connections> kvp in _connections) {
if(kvp.Value.IsAdmin == 1) ....

Now its good... Thanks
My much much smaller code for admins

private bool GetUserAdmin(string Username)
{
StreamReader CurrentFile = new StreamReader(SaveDataFolder + "/admins.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line.Trim() == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private void OnConnectionJoin(InSim insim, IS_NCN NCN)
{
try
{
_connections.Add(NCN.UCID, new Connections
{
UCID = NCN.UCID,
UName = NCN.UName,
PName = NCN.PName,
IsAdmin = NCN.Admin,

IsCustomAdmin = GetUserAdmin(NCN.UName)
});
}
catch (Exception EX) {}
}

A useful function to combine real admin and .ini admin

private bool IsConnectionAdmin(Connections CurrentConnection)
{
if (CurrentConnection.IsAdmin == true || CurrentConnection.IsCustomAdmin == true) return true;
return false;
}

And an in-game command to add admins(you will need to modify it a bit because im using if statements for each command instead of break.

else if (command[0] == "!makeadmin")
{
if (!IsConnectionAdmin(_connections[MSO.UCID]))
{
MessageToUCID(MSO.UCID, "You are not an admin");
return;
}
if (command.Length == 1)
{
MessageToUCID(MSO.UCID, "Invalid Format. ^7!makeadmin [username]");
return;
}

bool UsernameFound = false;
string Username = Text.Remove(0, command[0].Length + 1);

foreach (var CurrentConnection in _connections.Values)
{
if (CurrentConnection.UName.ToLower() == Username.ToLower())
{
UsernameFound = true;

if (IsConnectionAdmin(CurrentConnection)) MessageToUCID(MSO.UCID, "^7" + CurrentConnection.PName + " ^3is already an admin");
else
{
CurrentConnection.IsCustomAdmin = true;

StreamReader ApR = new StreamReader(SaveDataFolder + "/admins.ini");
string TempAPR = ApR.ReadToEnd();
ApR.Close();
StreamWriter Ap = new StreamWriter(SaveDataFolder + "/admins.ini");
Ap.WriteLine(TempAPR + CurrentConnection.UName);
Ap.Flush();
Ap.Close();
}

break;
}
}

if (UsernameFound == false) MessageToUCID(MSO.UCID, Username + " ^3not found");
}

You can replace MessageToUCID with your message function or the default way.(anything you like to use)
Thanks...

When I click on button
insim.Send("/rcm GREEN FLAG");
insim.Send("/rcm_all");
System.Threading.Thread.Sleep(5000);
insim.Send("/rcc_all");

But then, program is sleaping for 5sec, and will not accept any command until it passes 5sec.
#40 - PoVo
-
(M.M.L.) DELETED by M.M.L.
I get this error....
All I can imagine is that at some point in the fifteen seconds it takes the timer to elapse, the connection with LFS has been lost. Perhaps it would be better to place the IsConnected check in the anonymous method.


<?php 
timer
.Elapsed += delegate {
    if (
insim.IsConnected) {
        
insim.Send(TinyType.TINY_RST5);
    }
}
?>

Also just a tip, instead of using anonymous methods, it's generally recommended to use lambdas these days, but I guess it doesn't make much difference in this instance.


<?php 
timer
.Elapsed += (sendere) => {

};
?>

Right I have tried to make a sort of like gui program where i have green flag yellow ect in a form this is the code i have so far.

public partial class Form1 : Form
{
// Create new InSim object
InSim insim = new InSim();

public Form1()
{
InitializeComponent();

// Create new InSim object
InSim insim = new InSim();

}

private void Form1_Load(object sender, EventArgs e)
{
// Create new InSim object
InSim insim = new InSim();

// Initailize InSim
insim.Initialize(new InSimSettings
{
Host = "127.0.0.1", // Host where LFS is runing
Port = 29999, // Port to connect to LFS through
Admin = "xxx", // Optional game admin password
});
}

private void button1_Click(object sender, EventArgs e)
{
// Send message to LFS
insim.Send("/msg Hello, I am a InSim!");

// Send RCM to LFS
insim.Send("/rcm Hello PPL");
insim.Send("/rcm_all");

}

private void button2_Click(object sender, EventArgs e)
{
//Say's Hello
insim.Send("/msg ^7Hia All!");
}

And if i click button 1 or 2 then it says insim not connected is there anyway i can make it work??
cheers
Try this...


<?php 
public partial class Form1 Form
{
    
// Create new InSim object
    
InSim insim = new InSim();

    public 
Form1()
    {
        
InitializeComponent();
    }

    private 
void Form1_Load(object senderEventArgs e)
    {
        
// Initailize InSim
        
insim.Initialize(new InSimSettings
        
{
            
Host "127.0.0.1"// Host where LFS is runing
            
Port 29999// Port to connect to LFS through
            
Admin "xxx"// Optional game admin password
        
});
    }

    private 
void button1_Click(object senderEventArgs e)
    {
        
// Send message to LFS
        
insim.Send("/msg Hello, I am a InSim!");

        
// Send RCM to LFS
        
insim.Send("/rcm Hello PPL");
        
insim.Send("/rcm_all");

    }

    private 
void button2_Click(object senderEventArgs e)
    {
        
//Say's Hello
        
insim.Send("/msg ^7Hia All!");
    }
}
?>

Cheers worked perfect

_________________________

Just made my 1st insim. not the best but i still need to learn how to make buttons in lfs so i just made a app gui.
I have attached the files.
Please read readme.txt

Kind Regards,
J.Provost
Attached files
Race Control System.zip - 327 KB - 152 views
How to calculate drift points?
It depends on what you consider a drift and I can see that it would take a bit of work to get it right, but a very simple algorithm could work like this


if (car.Speed > MIN_SPEED) {
unsigned short driftAngle = abs(car.Direction - car.Heading);
if (driftAngle > MIN_DANGLE && driftAngle < MAX_DANGLE)
driftPoints[PLID] += COEFF * car.Speed * driftAngle;
}

This would of course require quite high sample rate to get any useful data.
cruis mod
може ли да ми дадете cruis мод за lfs 0.6B плс. много
-
(kristofferandersen) DELETED by Flame CZE : late bump
2

First InSim move
(48 posts, started )
FGED GREDG RDFGDR GSFDG