static public void EnterRoomHandler(NetConnection connection, object data) { int roomid = (int)data; if (roomid < 0 || Env.RoomNum <= roomid) { return; } if (!PlayerCaches.AuthDone(connection)) { return; } if (!PlayerCaches.InLobby(connection)) { return; } if (Lobby.GetRoomState(roomid) == 1) { return; } Lobby.EnterRoom(connection, roomid); GSrv.Send(MessageType.ReplySuccessEnterRoom, connection, NetDeliveryMethod.ReliableOrdered); Lobby.SendRoomStateToAll(roomid); }
static public void PushHandler(NetConnection connection, object data) { PushData push = null; try { push = GSrv.Deserialize <PushData>((byte[])data); } catch { return; } if (!PlayerCaches.AuthDone(connection)) { return; } if (PlayerCaches.InLobby(connection)) { return; } int roomid = PlayerCaches.GetRoomID(connection); if (Lobby.GetRoomState(roomid) == 0) { return; } Lobby.Push(connection, roomid, push); }
static public void LeaveRoomHandler(NetConnection connection, object data) { if (!PlayerCaches.AuthDone(connection)) { return; } if (PlayerCaches.InLobby(connection)) { return; } int roomid = PlayerCaches.GetRoomID(connection); if (Lobby.GetRoomState(roomid) == 1) { return; } Lobby.LeaveRoom(connection, roomid); Lobby.SendRoomStateToAll(roomid); Lobby.CheckBattleStart(roomid); GSrv.Send(MessageType.ReplySuccessLeaveRoom, connection, NetDeliveryMethod.ReliableOrdered); LobbyState state = Lobby.GetLobbyState(); GSrv.Send(MessageType.ReplyLobbyState, GSrv.Serialize <LobbyState>(state), connection, NetDeliveryMethod.ReliableOrdered); }
static public void SendFireHandler(NetConnection connection, object data) { FireData fire = null; try { fire = GSrv.Deserialize <FireData>((byte[])data); } catch { return; } if (!PlayerCaches.AuthDone(connection)) { return; } if (PlayerCaches.InLobby(connection)) { return; } int roomid = PlayerCaches.GetRoomID(connection); if (Lobby.GetRoomState(roomid) == 0) { return; } Lobby.ReplyFire(connection, roomid, fire); }
public void ReplySetBlock(BlockData block) { foreach (Player player in players.Values) { GSrv.Send(MessageType.ReplySetBlock, GSrv.Serialize <BlockData>(block), player.connection, NetDeliveryMethod.ReliableOrdered); } }
public void ReplyFire(NetConnection connection, FireData fire) { int userid = players[connection].userid; foreach (Player player in players.Values) { GSrv.Send(MessageType.ReplyFire, userid, player.connection, NetDeliveryMethod.ReliableOrdered); } if (fire.hit) { Player player = players.Values.FirstOrDefault(x => x.userid == fire.userid); if (player != null) { if (fire.parts == 0) { player.Damaged(100); } else if (fire.parts == 1) { player.Damaged(50); } else { player.Damaged(25); } } } }
static void SendSnapshot() { SnapShotData data = new SnapShotData(); Players.AddSyncs(data.syncs); data.pixels.AddRange(World.GetBuffer()); GSrv.SendToAll(MessageType.Snapshot, GSrv.Serialize <SnapShotData>(data), NetDeliveryMethod.ReliableUnordered); }
static public void ConnectHandler(NetConnection connection, object data) { PlayerSyncData sync = Players.GeneratePlayer(connection); BMPInitData bmps = World.GetBMP(); InitData init = new InitData() { sync = sync, bmps = bmps }; GSrv.Send(MessageType.ConnectSuccess, GSrv.Serialize <InitData>(init), connection, NetDeliveryMethod.ReliableOrdered); }
static void Server() { Lobby.Init(); //GSQLite.Open(sqlpath); GSrv.Init(); GSrv.SetConnectPacketHandler(ConnectHandler); GSrv.SetDisconnectPacketHandler(DisconnectHandler); GSrv.SetDebugPacketHandler(DebugHandler); GSrv.SetPacketHandler(MessageType.SendUserName, DataType.String, SendUserNameHandler); GSrv.SetPacketHandler(MessageType.RequestLobbyState, DataType.Null, RequestLobbyStateHandler); GSrv.SetPacketHandler(MessageType.EnterRoom, DataType.Int32, EnterRoomHandler); GSrv.SetPacketHandler(MessageType.LeaveRoom, DataType.Null, LeaveRoomHandler); GSrv.SetPacketHandler(MessageType.ChangeFaction, DataType.Null, ChangeFactionHandler); GSrv.SetPacketHandler(MessageType.ChangeReady, DataType.Null, ChangeReadyHandler); GSrv.SetPacketHandler(MessageType.Push, DataType.Bytes, PushHandler); GSrv.SetPacketHandler(MessageType.TouchCore, DataType.Null, TouchCoreHandler); GSrv.SetPacketHandler(MessageType.SetBlock, DataType.Bytes, SetBlockHandler); GSrv.SetPacketHandler(MessageType.SendFire, DataType.Bytes, SendFireHandler); GSrv.Listen("ILSnowballFight0.1", port); while (!exit) { DateTime startTime = DateTime.Now; GSrv.Receive(); sendCount++; if (sendCount == 3) { sendCount = 0; SendSnapshot(); } Update(frameSpan); TimeSpan span = DateTime.Now - startTime; if (span.TotalMilliseconds < frameSpan * 1000) { Thread.Sleep((int)(frameSpan * 1000) - (int)span.TotalMilliseconds); } } GSrv.Shutdown(); /*Players.SaveAllPlayer(); * GSQLite.Close();*/ }
public void TouchCore(NetConnection connection) { if (wonTimer > 0) { return; } wonTimer = 5.0f; int faction = players[connection].faction; foreach (Player player in players.Values) { GSrv.Send(MessageType.ReplyWon, faction, player.connection, NetDeliveryMethod.ReliableOrdered); } }
public void SendRoomStateToAll() { RoomState state = new RoomState(); state.roomid = roomid; foreach (Player player in players.Values) { state.players.Add(new PlayerState { userid = player.userid, username = player.username, faction = player.faction, ready = player.ready }); } foreach (Player player in players.Values) { GSrv.Send(MessageType.ReplyRoomState, GSrv.Serialize <RoomState>(state), player.connection, NetDeliveryMethod.ReliableOrdered); } }
public void SendSnapShot() { if (roomstate == 0) { return; } PlayerSyncDatas syncs = new PlayerSyncDatas(); foreach (Player player in players.Values) { syncs.datas.Add(player.sync); } foreach (Player player in players.Values) { GSrv.Send(MessageType.Snapshot, GSrv.Serialize <PlayerSyncDatas>(syncs), player.connection, NetDeliveryMethod.UnreliableSequenced); } }
static public void SetBlockHandler(NetConnection connection, object data) { BlockData block = null; try { block = GSrv.Deserialize <BlockData>((byte[])data); } catch { return; } if (!PlayerCaches.AuthDone(connection)) { return; } if (PlayerCaches.InLobby(connection)) { return; } int roomid = PlayerCaches.GetRoomID(connection); if (Lobby.GetRoomState(roomid) == 0) { return; } if (!Env.IsInsideWorld(block.x, block.y, block.z)) { return; } if (Env.IsCorePos(block.x, block.y, block.z)) { return; } Lobby.ReplySetBlock(roomid, block); }
static void Server() { //GSQLite.Open(sqlpath); GSrv.Init(); GSrv.SetConnectPacketHandler(ConnectHandler); GSrv.SetDisconnectPacketHandler(DisconnectHandler); GSrv.SetDebugPacketHandler(DebugHandler); GSrv.SetPacketHandler(MessageType.Push, DataType.Bytes, PushHandler); GSrv.SetPacketHandler(MessageType.Draw, DataType.Bytes, DrawHandler); GSrv.Listen("TwoDimBlocks0.1", port); while (!exit) { DateTime startTime = DateTime.Now; GSrv.Receive(); sendCount++; if (sendCount == 3) { sendCount = 0; SendSnapshot(); } Update(frameSpan); TimeSpan span = DateTime.Now - startTime; if (span.TotalMilliseconds < frameSpan * 1000) { Thread.Sleep((int)(frameSpan * 1000) - (int)span.TotalMilliseconds); } } GSrv.Shutdown(); /*Players.SaveAllPlayer(); * GSQLite.Close();*/ }
static public void SendUserNameHandler(NetConnection connection, object data) { string username = (string)data; if (username == "") { username = "******"; } if (PlayerCaches.AuthDone(connection)) { return; } Console.WriteLine("Enter:" + username); int userid = Lobby.AddPlayer(connection, username); GSrv.Send(MessageType.ReplyUserID, userid, connection, NetDeliveryMethod.ReliableOrdered); LobbyState state = Lobby.GetLobbyState(); GSrv.Send(MessageType.ReplyLobbyState, GSrv.Serialize <LobbyState>(state), connection, NetDeliveryMethod.ReliableOrdered); }
static public void DrawHandler(NetConnection connection, object data) { DrawData draw = GSrv.Deserialize <DrawData>((byte[])data); World.Buffer(draw); }
public void CheckBattleStart() { if (players.Count == 0) { return; } Player not_ready_player = players.FirstOrDefault(x => x.Value.ready == false).Value; if (not_ready_player != null) { return; } PlayerInitDatas datas = new PlayerInitDatas(); foreach (Player player in players.Values) { if (player.faction == 0) { PlayerSyncData sync = new PlayerSyncData { userid = player.userid, xpos = 16.0f, ypos = 17.0f, zpos = 1.0f, xrot = 0.0f, yrot = 0.0f, animestate = 0, hp = 100 }; player.sync = sync; } else { PlayerSyncData sync = new PlayerSyncData { userid = player.userid, xpos = 16.0f, ypos = 17.0f, zpos = 63.0f, xrot = 0.0f, yrot = 180.0f, animestate = 0, hp = 100 }; player.sync = sync; } player.ResetRespawnTimer(); datas.datas.Add(new PlayerInitData { username = player.username, faction = player.faction, sync = player.sync }); } foreach (Player player in players.Values) { GSrv.Send(MessageType.BattleStart, GSrv.Serialize <PlayerInitDatas>(datas), player.connection, NetDeliveryMethod.ReliableOrdered); } roomstate = 1; }
static public void RequestLobbyStateHandler(NetConnection connection, object data) { LobbyState state = Lobby.GetLobbyState(); GSrv.Send(MessageType.ReplyLobbyState, GSrv.Serialize <LobbyState>(state), connection, NetDeliveryMethod.ReliableOrdered); }
static public void PushHandler(NetConnection connection, object data) { PushData push = GSrv.Deserialize <PushData>((byte[])data); Players.Push(connection, push); }