You are on page 1of 4

private void mtimer_Tick(object sender, EventArgs e)

{
String data = Communicator.getInstance().reply;
String request = "";
if (data.Contains("#"))
{
request = data.Substring(0, data.IndexOf("#"));
}

if (request.Contains(Constant.S2C_ALREADYADDED))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_CELLOCCUPIED))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_CONTESTANTSFULL))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_FALLENTOPIT))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_GAMEJUSTFINISHED))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_GAMEOVER))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_GAMESTARTED))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_INVALIDCELL))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_NOTACONTESTANT))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_NOTALIVE))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_NOTSTARTED))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_REQUESTERROR))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_SERVERERROR))
{
//////////////////////////////////////////
}
else if (request.Contains(Constant.S2C_TOOEARLY))
{
//////////////////////////////////////////
}
else
{

string[] tokens = request.Split(new char[] { ';', ',', ':' }); ;


if ((request.Contains(":") | (isPlayerUpdate(tokens[0]))) &
(myShipIndex == -1))//This is the initial map update
{
tokens = request.Split(new char[] { ':' });
String tempIndex = tokens[0].TrimStart(new char[] { 'P' });

int myIndex = Int32.Parse(tempIndex); //my index


int mapSize = Int32.Parse(tokens[1]); //mapsize
String pitString = tokens[2];
String obstacals = tokens[3];
Point[] pits = breakIntoPoints(tokens[2]);
Point[] obstacals = breakIntoPoints(tokens[3]);

//gm.relayMapSize(mapSize);
//gm.relayInitiateMapData(pits, obstacals);
//gm.setMyIndex(myIndex);
// myShipIndex = myIndex;

}
else if (request.Contains(":") | (isPlayerUpdate(tokens[0])))
{
String[] tokens1 = request.Split(new char[] { ';', ',',
':' });
if (isPlayerUpdate(tokens1[0]))//Ship creation data or ship
updates
{
if (!shipsAdded)//ship creation data
{
shipsAdded = true;
for (int i = 0; i < tokens1.Length; i++)
{
if (isPlayerUpdate(tokens1[i]))
{
numberOfPlayers++;
}
}
int[][] shipCreationData = new int[numberOfPlayers][];
for (int i = 0; i < numberOfPlayers; i++)
{
shipCreationData[i] = new int[3];
shipCreationData[i][0] = Convert.ToInt32(tokens1[3
* i + 1]);//x
shipCreationData[i][1] = Convert.ToInt32(tokens1[3
* i + 2]);//y
if (getPlayerIndex(tokens1[3 * i]) == myShipIndex)
{
shipCreationData[i][2] = 1;//It is my ship
}
else
{
shipCreationData[i][2] = 0;//It is an enemy
}
}
//ThreadPool.QueueUserWorkItem(new
WaitCallback(gm.relayAddShips), shipCreationData);

}
else//Ship update data
{
int[][] shipPositionUpdateData = new
int[numberOfPlayers][];
int[] shipPointsUpdateData = new int[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++)
{
shipPositionUpdateData[i] = new int[2];
shipPositionUpdateData[i][0] =
Convert.ToInt32(tokens1[4 * i + 1]);//x
shipPositionUpdateData[i][1] =
Convert.ToInt32(tokens1[4 * i + 2]);//y
shipPointsUpdateData[i] =
Convert.ToInt32(tokens1[4 * i + 3]);//Points
}
//ThreadPool.QueueUserWorkItem(new
WaitCallback(gm.relayShipPositionUpdate), shipPositionUpdateData);
//ThreadPool.QueueUserWorkItem(new
WaitCallback(gm.relayShipPointsUpdateData), shipPointsUpdateData);
}
}
}
else if ((tokens.Length == 4) & (!request.Contains(":")))//new
treasure
{
int[] tresureData = new int[4];
tresureData[0] = Int32.Parse(tokens[0]); //tres x
tresureData[1] = Int32.Parse(tokens[1]); //tres y
tresureData[2] = Int32.Parse(tokens[2]);//life
tresureData[3] = Int32.Parse(tokens[3]);//points
//
}
else
{
tokens = request.Split(new char[] { ';', ',', ':' });
if (isPlayerUpdate(tokens[0]))
{
tokens[0] = tokens[0].TrimStart(new char[] { 'P' });
//remove the 'P'
int currentShipIndex = -1;
try
{
currentShipIndex = Convert.ToInt32(tokens[0]);
}
catch (Exception)
{
//game end data
System.Windows.Forms.MessageBox.Show("Game end data" +
request);
}
if ((tokens.Length == 4) & (currentShipIndex ==
myShipIndex))//My update only; might be redundant
{
int points = Convert.ToInt32(tokens[1]);
int curX = Convert.ToInt32(tokens[2]);
int curY = Convert.ToInt32(tokens[3]);
int[] update = new int[] { curX, curY, points };
// gm.personalUpdates(update);
}
else if (currentShipIndex >= 0)//mass update
{

}
else
{
//Game ended
System.Windows.Forms.MessageBox.Show("Game ended" +
request);
}
}
else
{

if (tokens[0].Contains(Constant.S2C_HITONOBSTACLE))
{
//gm.sendReport("Penalty! -" + tokens[1], false);
//gm.weMetOb();
}
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CSE07"
group.
To post to this group, send email to cse_batch_07@googlegroups.com
To unsubscribe from this group, send email to
cse_batch_07+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/cse_batch_07?hl=en
-~----------~----~----~----~------~----~------~--~---

You might also like