Beispiel #1
0
        public bool Equals(LocationInfo obj)
        {
            if (obj == null) return false;

            if (latitude == obj.latitude && longitude == obj.longitude)
                return true;

            return false;
        }
Beispiel #2
0
        public Account(string name)
        {
            this.name = name;

            keep = new KeepDictionary();
            stats = new Stats();
            resources = new ResourceDictionary(true);
            food = new FoodDictionary(true);
            units = new UnitDictionary(true);
            location = new LocationInfo(0, 0);
        }
Beispiel #3
0
 private static Account CreateDummyAccount(string name, string password, LocationInfo location)
 {
     string guid = Guid.NewGuid().ToString();
     accounts.Add(guid, new Account(name));
     accounts[guid].location = location;
     return accounts[guid];
 }
Beispiel #4
0
        public static void DataCommunicationThread(object cSocket)
        {
            Socket clientSocket = (Socket)cSocket;

            byte[] buffer;
            int readByteAmt;
            for (; ; )
            {
                try
                {
                    buffer = new byte[clientSocket.SendBufferSize];
                    readByteAmt = clientSocket.Receive(buffer);

                    if (readByteAmt > 0)
                    {
                        Packet p = new Packet(buffer);
                        //if (p.packetType == Packet.PacketType.ModifyUser)
                        //{
                        //    string userID = p.sendString[0];
                        //    string password = p.sendString[1];

                        //    if (p.sendString[0] != null)
                        //    {
                        //        if (p.sendBool[0] == true) //Create Account
                        //        {
                        //            if (!loginInfo.ContainsKey(userID))
                        //            {
                        //                loginInfo.Add(userID, new LoginInfo(password));
                        //                accounts.Add(getGuidAsString(userID), new Account(userID));
                        //                Packet returnPacket = new Packet(true, "Successfully created new account. You may now log in.");
                        //                WriteLine("New account created! (Name: " + userID + ", Source: " + clientSocket.RemoteEndPoint + ")");
                        //            }
                        //            else
                        //            {
                        //                Packet returnPacket = new Packet(false, "Failed to create new account. User name already taken.");
                        //                WriteLine("Failed to create account! (User name already taken)");
                        //            }
                        //        }
                        //        else if (p.sendBool[0] == false) //Delete Account
                        //        {
                        //            if (loginInfo.ContainsKey(userID) && loginInfo[userID].password == password)
                        //            {
                        //                accounts.Remove(getGuidAsString(userID));
                        //                loginInfo.Remove(userID);
                        //                Packet returnPacket = new Packet(true, "Account deleted successfully.");
                        //                WriteLine("Successfully deleted account! (Name: " + userID + ", Source: " + clientSocket.RemoteEndPoint + ")");
                        //            }
                        //            else
                        //            {
                        //                Packet returnPacket = new Packet(false, "Incorrect user name or password");
                        //                WriteLine("Failed to delete account. (Name: " + userID + ", Source: " + clientSocket.RemoteEndPoint + ")");

                        //            }
                        //        }
                        //    }
                        //    else
                        //    {
                        //        Packet returnPacket = new Packet(false, "Incorrectly supplied data.");
                        //        WriteLine("Client supplied incorrect data.");
                        //    }
                        //}
                        //else if (p.packetType == Packet.PacketType.Login) //Login
                        //{
                        //    string userID = p.sendString[0];
                        //    string password = p.sendString[1];
                        //    string GCMDeviceID = p.sendString[2];

                        //    if (p.sendString[0] != null && loginInfo.ContainsKey(userID) && loginInfo[userID].password == password)
                        //    {
                        //        clientAccount = getAccount(p.sendString[0]);
                        //        clientAccount.gcmDeviceID = GCMDeviceID;                           
                        //        Packet returnPacket = new Packet(getGuidAsString(p.sendString[0]), true, "You have successfully logged in.");
                        //        GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                        //        GCM.GCM.SendGCM(GCMDeviceID, "Welcome, " + userID + "!", "You have successfully logged into Settle the World.", "");
                        //        GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.resources.ToSendable(), 0));
                        //        GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.food.ToSendable(), 1));
                        //        GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.units.ToSendable(), 2));
                        //        WriteLine("Client has successfully logged in. (Name: " + userID + ", GUID: " + clientGuid + ", Source: " + clientSocket.RemoteEndPoint + " GCM: " + GCMDeviceID + ")");
                        //    }
                        //    else
                        //    {
                        //        Packet returnPacket = new Packet(false, "Incorrect user name or password.");
                        //        GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                        //        WriteLine("Client failed to log in. (Name: " + userID + ", Source: " + clientSocket.RemoteEndPoint + " GCM: " + GCMDeviceID + ")");
                        //    }
                        //}
                        string userID = "";
                        Account clientAccount = null;

                        if (p.sendString[0] != null) userID = p.sendString[0];                  
                        if (accounts.ContainsKey(userID)) clientAccount = accounts[userID];


                        if (p.packetType == Packet.PacketType.FacebookLogin)
                        {
                            string accessToken = p.sendString[1];
                            string GCMDeviceID = p.sendString[2];

                            FacebookClient c = new FacebookClient(accessToken);
                            dynamic client = c.Get("/me");

                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                clientAccount.gcmDeviceID = GCMDeviceID;
                                clientAccount.accessToken = accessToken;
                                clientAccount.userID = userID;
                                Packet returnPacket = new Packet(userID, true, "You have successfully logged in.");
                                GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                                GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                                GCM.GCM.SendGCM(GCMDeviceID, "Welcome, " + client.first_name + "!", "You have successfully logged into Settle the World.", "");
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.resources.ToSendable(), 0));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.food.ToSendable(), 1));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.units.ToSendable(), 2));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.keep.ToSendable(), 3));
                                SendWorkerInformation(clientAccount);
                                WriteLine("Facebook Client has successfully logged in. (Name: " + client.first_name + ", Source: " + clientSocket.RemoteEndPoint + " GCM: " + GCMDeviceID + ")", "client");
                                AddFacebookFriends(userID);
                                //Send Friends
                                string[][] data = new string[2][];
                                data[0] = new string[clientAccount.friends.Count];
                                data[1] = new string[clientAccount.friends.Count];
                                int i = 0;
                                foreach (KeyValuePair<string, string> entry in clientAccount.friends)
                                {
                                    data[0][i] = entry.Key;
                                    data[1][i] = entry.Value;
                                    i++;
                                }
                                returnPacket = new Packet(data[0], 0);
                                Packet returnPacket2 = new Packet(data[1], 1);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket2);
                                WriteLine("Sent friends! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");

                                //Gender + name
                                Console.WriteLine(client.gender);
                            }
                            else if (p.sendString[0] != null)
                            {
                                //loginInfo.Add(userID, new LoginInfo(""));
                                accounts.Add(userID, new Account(client.first_name));
                                clientAccount = accounts[userID];
                                clientAccount.gcmDeviceID = GCMDeviceID;
                                clientAccount.accessToken = accessToken;
                                clientAccount.userID = userID;
                                Packet returnPacket = new Packet(userID, true, "Successfully created account.");
                                GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                                GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                                GCM.GCM.SendGCM(GCMDeviceID, "Welcome, " + client.first_name + "!", "You have successfully logged into Settle the World.", "");
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.resources.ToSendable(), 0));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.food.ToSendable(), 1));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.units.ToSendable(), 2));
                                GCM.GCM.SendGCM(GCMDeviceID, new Packet(clientAccount.keep.ToSendable(), 3));
                                SendWorkerInformation(clientAccount);
                                WriteLine("Facebook Account successfully created. (Name: " + client.first_name + ", Source: " + clientSocket.RemoteEndPoint + " GCM: " + GCMDeviceID + ")", "client");
                                AddFacebookFriends(userID);
                                //Send Friends
                                string[][] data = new string[2][];
                                data[0] = new string[clientAccount.friends.Count];
                                data[1] = new string[clientAccount.friends.Count];
                                int i = 0;
                                foreach (KeyValuePair<string, string> entry in clientAccount.friends)
                                {
                                    data[0][i] = entry.Key;
                                    data[1][i] = entry.Value;
                                    i++;
                                }
                                returnPacket = new Packet(data[0], 0);
                                Packet returnPacket2 = new Packet(data[1], 1);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket2);
                                WriteLine("Sent friends! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");

                                //Gender + name
                                bool b = false;
                                if (client.gender == "male") b = true;
                                Packet info = new Packet(client.first_name, b);
                                GCM.GCM.SendGCM(GCMDeviceID, info);
                            }
                            else
                            {
                                Packet returnPacket = new Packet(false, "Incorrect data supplied.");
                                GCM.GCM.SendGCM(GCMDeviceID, returnPacket);
                                WriteLine("Incorrect data supplied by client.", "error");
                            }
                        }
                        else if (p.packetType == Packet.PacketType.Coordinates)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                LocationInfo coordinates = new LocationInfo(p.sendFloat[0], p.sendFloat[1]);
                                clientAccount.lastLogin = DateTime.Now;
                                clientAccount.location = coordinates;
                                string[][] data = GetNearbyPlayers(userID);
                                Packet returnPacket = new Packet(data[0], 0.0f);
                                Packet returnPacket2 = new Packet(data[1], 1.0f);
                                SendResourceInformation(clientAccount);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket2);
                               
                                //WriteLine("Received Location Data! LAT: " + p.sendFloat[0] + ", LON: " + p.sendFloat[1] + " (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                            }
                            else
                            {
                                Packet returnPacket = new Packet(false, "You are not logged in.");
                                WriteLine("Player was not logged in.", "error");
                            }
                        }
                        else if (p.packetType == Packet.PacketType.Request)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                if (p.sendString[1] == "resources")
                                {
                                    Packet returnPacket = new Packet(clientAccount.resources.ToSendable(), 0);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    WriteLine("Sent resources! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                                }
                                else if (p.sendString[1] == "food")
                                {
                                    Packet returnPacket = new Packet(clientAccount.food.ToSendable(), 1);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    WriteLine("Sent food! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                                }
                                else if (p.sendString[1] == "units")
                                {
                                    Packet returnPacket = new Packet(accounts[p.sendString[0]].units.ToSendable(), 2);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    WriteLine("Sent units! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                                }
                                else if (p.sendString[1] == "friends")
                                {
                                    string[][] data = new string[2][];
                                    data[0] = new string[clientAccount.friends.Count];
                                    data[1] = new string[clientAccount.friends.Count];
                                    int i = 0;
                                    foreach (KeyValuePair<string, string> entry in clientAccount.friends)
                                    {
                                        data[0][i] = entry.Key;
                                        data[1][i] = entry.Value;
                                        i++;
                                    }
                                    Packet returnPacket = new Packet(data[0], 0);
                                    Packet returnPacket2 = new Packet(data[1], 1);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket2);
                                    WriteLine("Sent friends! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                                }
                                else if (p.sendString[1] == "keep")
                                {
                                    Packet returnPacket = new Packet(accounts[p.sendString[0]].keep.ToSendable(), 3);
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    WriteLine("Sent keep! (Client: " + clientAccount.name + ", Source: " + clientSocket.RemoteEndPoint + ")", "client");
                                }
                                else if (p.sendString[1] == "workers")
                                {
                                    SendWorkerInformation(clientAccount);
                                }
                                else if (p.sendString[1] == "attack")
                                {
                                    //
                                }
                            }
                            else if (p.sendString[0] != null)
                            {
                                Packet returnPacket = new Packet(false, "You are not logged in.");
                                WriteLine("Player was not logged in.", "error");
                            }
                        }
                        else if (p.packetType == Packet.PacketType.Trade)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                string target = p.sendString[1];
                                int mode = p.sendInt[0];

                                Account targetAcc = accounts[target];

                                if (mode == 0)
                                {
                                    bool check = true;
                                    foreach (KeyValuePair<int, uint> entry in p.sendDictionary)
                                        if (clientAccount.resources.Dict[(ResourceNames)entry.Key] < entry.Value) check = false;

                                    foreach (KeyValuePair<int, uint> entry in p.sendDictionary2)
                                        if (clientAccount.food.Dict[(FoodNames)entry.Key] < entry.Value) check = false;

                                    foreach (KeyValuePair<int, uint> entry in p.sendDictionary3)
                                        if (targetAcc.resources.Dict[(ResourceNames)entry.Key] < entry.Value) check = false;

                                    foreach (KeyValuePair<int, uint> entry in p.sendDictionary4)
                                        if (targetAcc.food.Dict[(FoodNames)entry.Key] < entry.Value) check = false;

                                    if (check)
                                    {
                                        Packet sendPacket = new Packet("server", target, p.sendDictionary, p.sendDictionary2, p.sendDictionary3, p.sendDictionary4, 0);
                                        GCM.GCM.SendGCM(accounts[target].gcmDeviceID, sendPacket);
                                    }
                                }
                                else if (mode == 1)
                                {
                                    SendItems(p.sendString[0], target, "food", p.sendDictionary3);
                                    SendItems(p.sendString[0], target, "resources", p.sendDictionary4);
                                    SendItems(target, p.sendString[0], "food", p.sendDictionary);
                                    SendItems(target, p.sendString[0], "resources", p.sendDictionary2);
                                }                             
                            }
                        }
                        else if (p.packetType == Packet.PacketType.SendItems)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                string target = p.sendString[1];
                                string type = p.sendString[2];
                                Dictionary<int, uint> dict = p.sendDictionary;

                                SendItems(p.sendString[0], target, type, dict);

                                WriteLine(accounts[p.sendString[0]].name + " traded with " + accounts[target].name+"!", "success");
                               
                            }
                            else
                            {
                                Packet returnPacket = new Packet(false, "You are not logged in.");
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                WriteLine("Player was not logged in.", "error");
                            }
                        }
                        else if (p.packetType == Packet.PacketType.Attack)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                if (Attack(clientAccount.name, p.sendString[1], p.sendDictionary))
                                {
                                    Packet returnPacket = new Packet(true, "Attack order successful.");
                                    GCM.GCM.SendGCM(accounts[p.sendString[0]].gcmDeviceID, returnPacket);
                                    WriteLine(clientAccount.name + " just issued an attack order against " + p.sendString[1], "success");
                                }
                                else
                                {
                                    Packet returnPacket = new Packet(false, "Insufficient units for attack order.");
                                    GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                    WriteLine(clientAccount.name + " failed to create attack order" + p.sendString[1] + " (Insufficient units for attack order)", "success");
                                }
                            }
                            else
                            {
                                Packet returnPacket = new Packet(false, "You are not logged in.");
                                GCM.GCM.SendGCM(clientAccount.gcmDeviceID, returnPacket);
                                WriteLine("Player was not logged in.", "error");
                            }
                        }
                        else if (p.packetType == Packet.PacketType.SendWorker)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {
                                float longitude = p.sendFloat[1];
                                float latitude = p.sendFloat[0];
                                int seconds = p.sendInt[0];
                                Console.WriteLine("RECEIVED WORKER", "error");
                                ResourceNames resource = GetCoordsResource(new LocationInfo(latitude, longitude));
                                WriteLine(((int)resource).ToString(), "error");
                                if (clientAccount.units.Dict[UnitNames.Worker] > 0)
                                {
                                    clientAccount.units.Dict[UnitNames.Worker]--;
                                    WorkOrder w = new WorkOrder(WorkOrder.OrderType.Resource, (int)resource, seconds);
                                    w.coords = new LocationInfo(latitude, longitude);
                                    clientAccount.workOrders.Add(w);
                                    SendWorkerInformation(clientAccount);
                                }                              
                            }
                        }
                        else if (p.packetType == Packet.PacketType.WorkOrder)
                        {
                            if (p.sendString[0] != null && accounts.ContainsKey(userID))
                            {

                                string type = p.sendString[1];
                                int id = p.sendInt[0];
                                int amount = p.sendInt[1];
                                WriteLine(type, "error");
                                if (type == "units")
                                {
                                    Console.WriteLine("!!!!!!!!!!!!!");
                                    bool check = true;
                                    foreach (ResourceCost r in Data.UnitContainer[(UnitNames)id].buildCost)
                                    {
                                        if (clientAccount.resources.Dict[r.item] < r.amount) check = false;
                                    }
                                    if (check)
                                    {
                                        clientAccount.workOrders.Add(new WorkOrder(WorkOrder.OrderType.Unit, id, amount));
                                        foreach (ResourceCost r in Data.UnitContainer[(UnitNames)id].buildCost)
                                        {
                                            clientAccount.resources.Dict[r.item] -= (uint)r.amount;
                                        }
                                    }
                                }
                                else if (type == "resources")
                                {
                                    WriteLine("WORKER RECEIVED", "error");
                                    clientAccount.workOrders.Add(new WorkOrder(WorkOrder.OrderType.Resource, id, amount));
                                }
                                else if (type == "food")
                                {
                                    if (id == (int)FoodNames.Wheat || id == (int)FoodNames.Fruit)
                                    {
                                        bool check = true;
                                        if (amount > clientAccount.keep.Dict[KeepNames.Farm] + 2) check = false;
                                        foreach (WorkOrder w in clientAccount.workOrders)
                                            if (w.type == WorkOrder.OrderType.Food && w.amount == amount) check = false;
                                        if (check)
                                        {
                                            clientAccount.workOrders.Add(new WorkOrder(WorkOrder.OrderType.Food, id, amount));
                                            SendWorkOrders(clientAccount.userID);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("AKSDJALKSJD");
                                        clientAccount.workOrders.Add(new WorkOrder(WorkOrder.OrderType.Food, id, amount));
                                        SendWorkOrders(clientAccount.userID);

                                    }
                                }
                                else if (type == "keep")
                                {
                                    WriteLine("RECEIVED KEEP WORK ORDER REQUEST!", "error");
                                    bool check = true;
                                    foreach (WorkOrder w in clientAccount.workOrders)
                                        if (w.type == WorkOrder.OrderType.Keep && w.enumType == id) check = false;

                                    foreach (ResourceCost r in Data.KeepContainer[(KeepNames)id].buildCost)
                                        if (clientAccount.resources.Dict[r.item] < r.amount) check = false;

                                    if (check)
                                    {
                                        foreach (ResourceCost r in Data.KeepContainer[(KeepNames)id].buildCost)
                                            clientAccount.resources.Dict[r.item] -= (uint)r.amount;

                                        WriteLine("Started Work order", "error");
                                        clientAccount.workOrders.Add(new WorkOrder(WorkOrder.OrderType.Keep, id, amount));
                                        SendWorkOrders(clientAccount.userID);
                                        SendResources(clientAccount);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (SocketException e)
                {
                    WriteLine("==> Client disconnected.", "success");
                    break;
                }

            }
        }
Beispiel #5
0
 private static ResourceNames GetCoordsResource(LocationInfo location)
 {
     int latInt = (int)(location.latitude * 100);
     int longInt = (int)(location.longitude * 100);
     Random gen = new Random(latInt + longInt);
     int rand = gen.Next(101);
     return (ResourceNames)((latInt + longInt) % System.Enum.GetValues(typeof(ResourceNames)).Length);
     //else return (ResourceNames)(-1);
 }