- Code: Select all
namespace TicTacToe {
public class Player : BasePlayer {
public Boolean IsReady = true;
}
public class Tile {
public String Type;
}
[RoomType("TicTacToe")]
public class GameCode : Game<Player> {
private Player player1;
private Player player2;
// This method is called when an instance of your the game is created
public override void GameStarted() {
Console.WriteLine("Game is started------------------------------");
}
// This method is called when the last player leaves the room, and it's closed down.
public override void GameClosed() {
Console.WriteLine("RoomId: " + RoomId);
}
// This method is called whenever a player joins the game
public override void UserJoined(Player player) {
Console.WriteLine("User Joined =>=>");
Console.WriteLine(player.ConnectUserId);
joinGame(player);
//Send info about all already connected users to the newly joined users chat
Message m = Message.Create("ChatInit");
foreach(Player p in Players) {
m.Add(p.Id, p.ConnectUserId);
}
//player.Send(m);
}
// This method is called when a player leaves the game
public override void UserLeft(Player player) {
if(player == player1) {
player1 = null;
} else if(player == player2) {
player2 = null;
} else
return;
Broadcast("left", player1 != null ? player1.ConnectUserId : "", player2 != null ? player2.ConnectUserId : "");
}
// This method is called when a player sends a message into the server code
public override void GotMessage(Player player, Message message)
{
Console.WriteLine("Got Message:");
Console.WriteLine(player.ConnectUserId);
switch(message.Type) {
case "join": {
joinGame(player);
break;
}
}
}
private void joinGame(Player user) {
if(player1 == null) {
user.Send("init", 0, user.ConnectUserId);
player1 = user;
} else if(player2 == null) {
user.Send("init", 1, user.ConnectUserId);
player2 = user;
} else {
}
if(player1 != null && player2 != null) {
Console.WriteLine("BOTH HAVE JOINED");
Console.WriteLine(player1.ConnectUserId);
Console.WriteLine(player2.ConnectUserId);
Broadcast("join", player1.ConnectUserId, player2.ConnectUserId);
//user.Send("join", player1.ConnectUserId, player2.ConnectUserId);
}
}
}
}
Here is the output :
- Code: Select all
=====[ Room Started: TicTacToe ]=======
Game is started------------------------------
>User testUser0.20 (1): Joined game
User Joined =>=>
testUser0.20
Got Message:
testUser0.20
BOTH HAVE JOINED
testUser0.20
testUser0.20
>User testUser0.20 (1): Left game
RoomId: vn3V08ele0m8azjUOrDHnw
=====[ Room Closed ]===================
I am confused from where does the "GotMessage" function is called ?? ( I have checked my Actionscript code, and have made sure, that no signal is reflecting back, to call this function) . This is troubling me, because as soon as the 1st player joins in, it calls "GotMessage" and 2nd player ( same as 1st player ) also gets added !