private Server() { players = new Dictionary<int, Player>(); keys = new Dictionary<int, Dictionary<string, bool>>(); boxes = new List<Box>(); Box box1 = new Box(); box1.position = new System.Drawing.Point(300, 100); Box box2 = new Box(); box2.position = new System.Drawing.Point(200, 360); boxes.Add(box1); boxes.Add(box2); }
public void OnReceive(IPEndPoint endpoint, string msg) { JObject json = JObject.Parse(msg); int cmd = (int)json[Tag.COMMAND]; if (cmd == Cmd.CONNECT && (int)json[Tag.STATUS] == Status.SUCCESS) { int gamePort = (int)json[Tag.PORT]; id = (int)json[Tag.ID]; this.udp.Close(); this.udp = new Udp(gamePort, OnReceive); this.udp.SetDestination(hostIP, Server.SERVER_PORT); JArray jps = json[Tag.PLAYERS] as JArray; if (null != jps) { foreach (JObject jp in jps) { int _id = (int)jp[Tag.ID]; string ip = (string)jp[Tag.IP]; int port = (int)jp[Tag.PORT]; string name = (string)jp[Tag.NAME]; int tx = (int)jp[Tag.TX]; int ty = (int)jp[Tag.TY]; Player p = new Player(new IPEndPoint(IPAddress.Parse(ip), port), name, _id); p.position = new System.Drawing.Point(tx, ty); players[_id] = p; } } JArray jbs = json[Tag.BOXES] as JArray; if (null != jbs) { foreach (JObject jb in jbs) { int tx = (int)jb[Tag.TX]; int ty = (int)jb[Tag.TY]; Box box = new Box(); box.position = new System.Drawing.Point(tx, ty); boxes.Add(box); } } } else if (cmd == Cmd.ADD_PLAYER) { int _id = (int)json[Tag.ID]; string ip = (string)json[Tag.IP]; int port = (int)json[Tag.PORT]; string name = (string)json[Tag.NAME]; int tx = (int)json[Tag.TX]; int ty = (int)json[Tag.TY]; Player p = new Player(new IPEndPoint(IPAddress.Parse(ip), port), name, _id); p.position = new System.Drawing.Point(tx, ty); players[_id] = p; } else if (cmd == Cmd.DISCONNECT) { int _id = (int)json[Tag.ID]; players.Remove(_id); } else if (cmd == Cmd.SERVER_CLOSED) { Disconnect(); } else if (cmd == Cmd.UPDATE) { JArray jpos = json[Tag.POSITIONS] as JArray; foreach (JObject jp in jpos) { int _id = (int)jp[Tag.ID]; if (players.ContainsKey(_id)) { int tx = (int)jp[Tag.TX]; int ty = (int)jp[Tag.TY]; Player p = players[_id]; p.position.X = tx; p.position.Y = ty; } } } }