public static void createMission(Mission m)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(userXML);
            XmlNodeList  elemList = doc.GetElementsByTagName("mission");
            int          newid    = elemList.Count + 1;
            XmlElement   e        = doc.CreateElement("mission");
            XmlAttribute a        = doc.CreateAttribute("id");

            a.Value = newid.ToString();
            e.Attributes.Append(a);
            XmlElement titlechild = doc.CreateElement("title");

            titlechild.AppendChild(doc.CreateTextNode(m.title));
            XmlElement typechild = doc.CreateElement("type");

            typechild.AppendChild(doc.CreateTextNode(m.type.ToString()));
            XmlElement lvlstartchild = doc.CreateElement("lvlStartEligible");

            lvlstartchild.AppendChild(doc.CreateTextNode(m.lvlStartEligible.ToString()));
            XmlElement lvlendchild = doc.CreateElement("lvlEndEligible");

            lvlendchild.AppendChild(doc.CreateTextNode(m.lvlEndEligible.ToString()));
            XmlElement missionstartchild = doc.CreateElement("missionStart");

            missionstartchild.AppendChild(doc.CreateTextNode(m.missionStart.ToString()));
            XmlElement missionendchild = doc.CreateElement("missionEnd");

            missionendchild.AppendChild(doc.CreateTextNode(m.missionEnd.ToString()));
            XmlElement xprewardchild = doc.CreateElement("xpreward");

            xprewardchild.AppendChild(doc.CreateTextNode(m.xpreward.ToString()));
            XmlElement goldrewardchild = doc.CreateElement("goldreward");

            goldrewardchild.AppendChild(doc.CreateTextNode(m.goldreward.ToString()));
            e.AppendChild(titlechild);
            e.AppendChild(typechild);
            e.AppendChild(lvlstartchild);
            e.AppendChild(lvlendchild);
            e.AppendChild(missionstartchild);
            e.AppendChild(missionendchild);
            e.AppendChild(xprewardchild);
            e.AppendChild(goldrewardchild);

            int currentquestionnumber = 1;

            foreach (Question q in m.questions)
            {
                XmlElement   nodee = doc.CreateElement("question");
                XmlAttribute nodea = doc.CreateAttribute("number");
                nodea.Value = currentquestionnumber.ToString();
                nodee.Attributes.Append(nodea);

                XmlElement promptchild = doc.CreateElement("prompt");
                promptchild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getPrompt()));
                XmlElement answerachild = doc.CreateElement("AnswerA");
                answerachild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getAnswers()[0]));
                XmlElement answerbchild = doc.CreateElement("Answer B");
                answerbchild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getAnswers()[1]));
                XmlElement answercchild = doc.CreateElement("AnswerC");
                answercchild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getAnswers()[2]));
                XmlElement answerdchild = doc.CreateElement("AnswerD");
                answerdchild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getAnswers()[3]));
                XmlElement correctanswerchild = doc.CreateElement("correctanswer");
                correctanswerchild.AppendChild(doc.CreateTextNode(m.questions[currentquestionnumber - 1].getCorrectAnswer().ToString()));
                nodee.AppendChild(promptchild);
                nodee.AppendChild(answerachild);
                nodee.AppendChild(answerbchild);
                nodee.AppendChild(answercchild);
                nodee.AppendChild(answerdchild);
                nodee.AppendChild(correctanswerchild);

                e.AppendChild(nodee);
                currentquestionnumber++;
            }
            doc.ChildNodes.Item(1).AppendChild(e);
            doc.Save(userXML);
        }
Exemple #2
0
        public static void ReadCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;

                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;

                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);

                Console.WriteLine("Processing Packet of " + bytesRead + " bytes");
                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(
                                        state.buffer, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read
                    // more data.
                    content = state.sb.ToString();
                    Console.WriteLine(content);
                    if (content.IndexOf("<EOF>") > -1)
                    {
                        Console.WriteLine("<EOF> Found with a buffer of " + state.buffer[0]);
                        //Process data here:
                        byte[] loginpacket = new byte[6];
                        if (state.buffer[0] == 1)
                        {
                            //Packet type: Login verification protocol
                            Console.WriteLine("Loading login data...");
                            //String packet = Encoding.ASCII.GetString(state.buffer, 1,  state.buffer.Length-1);
                            String[] items     = content.Split('\0');
                            String   firstname = items[0];
                            firstname = firstname.Substring(1);
                            String lastname = items[1]; //NEEDS TRY CATCH
                            String password = items[2];

                            // String lastname = Encoding.ASCII.GetString(state.buffer, 22,  Int32.Parse(Encoding.ASCII.GetString(state.buffer, 21, 1)););
                            //String password = Encoding.ASCII.GetString(state.buffer, 42,  Int32.Parse(Encoding.ASCII.GetString(state.buffer, 41, 1)););
                            Console.WriteLine("Received login protocol: " + firstname + ", " + lastname + ", " + password);
                            //Check if the credentials correspond to actual

                            int id = findUser(firstname, lastname, password);
                            Console.WriteLine(id);
                            loginpacket[0] = 1;
                            if (id == 0 || id == -1)
                            {
                                loginpacket[1] = 0;
                            }
                            else
                            {
                                Random random = new Random();
                                loginpacket[1] = 1;
                                loginpacket[2] = Convert.ToByte(random.Next(0, 256));
                                loginpacket[3] = Convert.ToByte(random.Next(0, 256));
                                loginpacket[4] = Convert.ToByte(random.Next(0, 256));

                                byte[] userpacket = new byte[3];
                                userpacket[0] = loginpacket[2];
                                userpacket[1] = loginpacket[3];
                                userpacket[2] = loginpacket[4];

                                if (User.getSuperUser(id))
                                {
                                    loginpacket[5] = 1;
                                }
                                else
                                {
                                    loginpacket[5] = 0;
                                }
                                //USER FOUND WITH ID 'id'
                                Console.WriteLine("ID IS" + id);
                                userList.Add(userpacket, id);
                            }
                            SendBytes(handler, loginpacket);
                        }
                        else if (state.buffer[0] == 2)
                        {
                            //Received XP protocol
                            byte[] userpacket = new byte[3];
                            byte[] xppacket   = new byte[15];
                            xppacket[0]   = 2;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                xppacket[1] = 1; //Indicates success
                                xppacket[2] = userpacket[0];
                                xppacket[3] = userpacket[1];
                                xppacket[4] = userpacket[2];
                                int xp = User.getXP(id);
                                //retrieve xp from xml!
                                byte[] fnl = Encoding.ASCII.GetBytes("\0");
                                fnl.CopyTo(xppacket, 5);
                                byte[] temp = Encoding.ASCII.GetBytes(xp.ToString());
                                temp.CopyTo(xppacket, 6);
                                fnl.CopyTo(xppacket, 6 + Encoding.ASCII.GetByteCount(xp.ToString()));
                            }
                            else
                            {
                                Console.WriteLine("Failure");
                                xppacket[1] = 0; //Indicates failure
                                xppacket[2] = 0;
                                xppacket[3] = 0;
                                xppacket[4] = 0;
                            }
                            SendBytes(handler, xppacket);
                        }
                        else if (state.buffer[0] == 6)
                        {
                            //Received String Array protocol
                            byte[] userpacket  = new byte[3];
                            byte[] finalpacket = new byte[60];
                            finalpacket[0] = 6;
                            String[] b   = content.Split('\0');
                            String   fn  = b[0].Substring(1);
                            int      fnn = Convert.ToInt32(fn);
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                Console.WriteLine("Confirmed string array protocol");
                                finalpacket[1] = 1; //Indicates success
                                finalpacket[2] = userpacket[0];
                                finalpacket[3] = userpacket[1];
                                finalpacket[4] = userpacket[2];
                                byte[] fnl = Encoding.ASCII.GetBytes("\0");
                                fnl.CopyTo(finalpacket, 5);

                                //Add payload (String array) here:
                                String[] payload = new String[2];
                                payload[0] = User.getFirstName(fnn);
                                payload[1] = User.getLastName(fnn);
                                int arcount = 6;
                                foreach (String s in payload)
                                {
                                    if (s != null && s != String.Empty)
                                    {
                                        byte[] tempa = Encoding.ASCII.GetBytes(s);
                                        tempa.CopyTo(finalpacket, arcount);
                                        fnl.CopyTo(finalpacket, tempa.Length + arcount + 1);
                                        arcount += tempa.Length + 2;
                                    }
                                }
                                byte[] tempb = Encoding.ASCII.GetBytes("<EOF>");
                                tempb.CopyTo(finalpacket, arcount);
                            }
                            else
                            {
                                Console.WriteLine("Failure");
                                finalpacket[1] = 0; //Indicates failure
                                finalpacket[2] = 0;
                                finalpacket[3] = 0;
                                finalpacket[4] = 0;
                            }
                            SendBytes(handler, finalpacket);
                        }
                        else if (state.buffer[0] == 7)
                        {
                            //Remove user from logged in user's list
                            byte[] userpacket       = new byte[3];
                            byte[] userdeletepacket = new byte[2];
                            userdeletepacket[0] = 7;
                            userpacket[0]       = state.buffer[1];
                            userpacket[1]       = state.buffer[2];
                            userpacket[2]       = state.buffer[3];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                userdeletepacket[1] = 1;
                                userList.Remove(userpacket);
                            }
                            else
                            {
                                userdeletepacket[1] = 0;
                            }
                            SendBytes(handler, userdeletepacket);
                        }
                        else if (state.buffer[0] == 3)
                        {
                            //Level transmission processing
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[5];
                            payload[0]    = 3;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                int lvl = Settings.getLevel(User.getXP(id));
                                if (lvl > 0 && lvl < 256)
                                {
                                    payload[4] = 1;
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 4)
                        {
                            //MinXP transmission processing
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[9];
                            payload[0]    = 4;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                int    lvl = Settings.getLevel(User.getXP(id));
                                byte[] fnl = Encoding.ASCII.GetBytes("\0");
                                byte[] xp  = Encoding.ASCII.GetBytes(Settings.getXp(lvl).ToString());
                                fnl.CopyTo(payload, 4);
                                xp.CopyTo(payload, 5);
                                fnl.CopyTo(payload, xp.Length + 5);
                                if (xp.Length > 3)
                                {
                                    Console.WriteLine("FATAL: Byte overflow error in packet type 4 processing! XP length too long!");
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 5)
                        {
                            //MaxXP transmission processing
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[9];
                            payload[0]    = 5;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                int    lvl = Settings.getLevel(User.getXP(id)) + 1;
                                byte[] fnl = Encoding.ASCII.GetBytes("\0");
                                byte[] xp  = Encoding.ASCII.GetBytes(Settings.getXp(lvl).ToString());
                                fnl.CopyTo(payload, 4);
                                xp.CopyTo(payload, 5);
                                fnl.CopyTo(payload, xp.Length + 5);
                                if (xp.Length > 3)
                                {
                                    Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! XP length too long!");
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 8)
                        {
                            //Modify password
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[5];
                            payload[0]    = 8;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                Console.WriteLine("HERE");
                                String[] items = content.Split('\0');
                                //items[1] is the new password and items[2] is the old password
                                Console.WriteLine(items[1]);
                                Console.WriteLine(User.getPassword(id));
                                Console.WriteLine(items[2]);
                                if (items[2].Equals(User.getPassword(id)))
                                {
                                    Console.WriteLine("REACHED");
                                    User.setPassword(id, items[1]);
                                    payload[4] = 1;
                                    //Indicates succcess
                                }
                                else
                                {
                                    //Indicates wrong old password entered
                                    payload[4] = 2;
                                }
                            }
                            else
                            {
                                //Means that could not verify user based on logincode authorization
                                payload[4] = 0;
                            }
                            Console.WriteLine("HI");
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 13)
                        {
                            //Return positive that mission was successfully created
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[5];
                            payload[0]    = 13;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                payload[4] = 1;
                                byte[] missionpacket = new byte[state.buffer.Length - 9];
                                try
                                {
                                    Array.Copy(state.buffer, 4, missionpacket, 0, state.buffer.Length - 9);
                                    Object  obj = ByteArrayToObject(missionpacket);
                                    Mission mis = (Mission)obj;
                                    Missions.createMission(mis);
                                }
                                catch (Exception f)
                                {
                                    Console.WriteLine(f.ToString());
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 23)
                        {
                            //Gold transmision processing
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[9];
                            payload[0]    = 23;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                int    gold       = User.getGold(id);
                                byte[] fnl        = Encoding.ASCII.GetBytes("\0");
                                byte[] goldpacket = Encoding.ASCII.GetBytes(gold.ToString());
                                fnl.CopyTo(payload, 4);
                                goldpacket.CopyTo(payload, 5);
                                fnl.CopyTo(payload, goldpacket.Length + 5);
                                if (goldpacket.Length > 3)
                                {
                                    Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! Gold length too long!");
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 24)
                        {
                            //Mana transmission processing
                            byte[] userpacket = new byte[3];
                            byte[] payload    = new byte[9];
                            payload[0]    = 24;
                            userpacket[0] = state.buffer[1];
                            userpacket[1] = state.buffer[2];
                            userpacket[2] = state.buffer[3];
                            payload[1]    = userpacket[0];
                            payload[2]    = userpacket[1];
                            payload[3]    = userpacket[2];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                int    mana       = User.getMana(id);
                                byte[] fnl        = Encoding.ASCII.GetBytes("\0");
                                byte[] manapacket = Encoding.ASCII.GetBytes(mana.ToString());
                                fnl.CopyTo(payload, 4);
                                manapacket.CopyTo(payload, 5);
                                fnl.CopyTo(payload, manapacket.Length + 5);
                                if (manapacket.Length > 3)
                                {
                                    Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! Mana length too long!");
                                }
                            }
                            else
                            {
                                payload[4] = 0;
                            }
                            SendBytes(handler, payload);
                        }
                        else if (state.buffer[0] == 29)
                        {
                            //Request for list of users
                            byte[] userpacket  = new byte[3];
                            byte[] finalpacket = new byte[1024 + 5];
                            finalpacket[0] = 29;
                            userpacket[0]  = state.buffer[1];
                            userpacket[1]  = state.buffer[2];
                            userpacket[2]  = state.buffer[3];
                            int id = 0;
                            if (userList.TryGetValue(userpacket, out id))
                            {
                                finalpacket[1] = userpacket[0];
                                finalpacket[2] = userpacket[1];
                                finalpacket[3] = userpacket[2];
                                byte[] fnl = Encoding.ASCII.GetBytes("\0");
                                fnl.CopyTo(finalpacket, 4);

                                byte[] users = User.getUserList();
                                users.CopyTo(finalpacket, 5);
                            }
                            else
                            {
                                Console.WriteLine("Failure");
                                finalpacket[1] = 0; //Indicates failure
                                finalpacket[2] = 0;
                                finalpacket[3] = 0;
                                finalpacket[4] = 0;
                            }
                            SendBytes(handler, finalpacket);
                        }


                        // All the data has been read from the
                        // client. Display it on the console.
                        Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                                          content.Length, content);
                        // Echo the data back to the client.
                    }
                    else
                    {
                        // Not all data received. Get more.
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                             new AsyncCallback(ReadCallback), state);
                    }
                }
            }
            catch
            {
                return;
            }
        }
        public static Mission getMissionById(int id)
        {
            Mission     m   = new Mission();
            XmlDocument doc = new XmlDocument();

            doc.Load(userXML);
            XmlNodeList elemList = doc.GetElementsByTagName("user");

            for (int i = 0; i < elemList.Count; i++)
            {
                XmlNode x = elemList.Item(i);
                if (x.NodeType == XmlNodeType.Element)
                {
                    XmlElement e   = (XmlElement)x;
                    int        tid = Int32.Parse(e.GetAttribute("id"));
                    if (id == tid)
                    {
                        if (e.ChildNodes.Item(1).FirstChild.Value == "MultipleChoice")
                        {
                            m.type = EnumMission.MultipleChoice;
                        }
                        else if (e.ChildNodes.Item(1).FirstChild.Value == "FileUpload")
                        {
                            m.type = EnumMission.FileUpload;
                        }
                        else if (e.ChildNodes.Item(1).FirstChild.Value == "FreeResponse")
                        {
                            m.type = EnumMission.FreeResponse;
                        }
                        else if (e.ChildNodes.Item(1).FirstChild.Value == "Text")
                        {
                            m.type = EnumMission.Text;
                        }
                        m.title            = e.ChildNodes.Item(0).FirstChild.Value;
                        m.lvlStartEligible = Int32.Parse(e.ChildNodes.Item(2).FirstChild.Value);
                        m.lvlEndEligible   = Int32.Parse(e.ChildNodes.Item(3).FirstChild.Value);
                        m.missionStart     = DateTime.Parse(e.ChildNodes.Item(4).FirstChild.Value);
                        m.missionEnd       = DateTime.Parse(e.ChildNodes.Item(5).FirstChild.Value);
                        m.xpreward         = Int32.Parse(e.ChildNodes.Item(6).FirstChild.Value);
                        m.goldreward       = Int32.Parse(e.ChildNodes.Item(7).FirstChild.Value);

                        //Translate the questions
                        int currentnumber = 8;
                        while (e.ChildNodes.Item(currentnumber).FirstChild.Name == "question")
                        {
                            XmlNode questionnode = e.ChildNodes.Item(currentnumber).FirstChild;
                            if (questionnode.NodeType == XmlNodeType.Element)
                            {
                                XmlElement questione = (XmlElement)questionnode;
                                //Params: Prompt, AnswerA, AnswerB, AnswerC, AnswerD, correctanswer (int32)
                                Question q = new Question(questione.ChildNodes.Item(0).FirstChild.Value,
                                                          questione.ChildNodes.Item(1).FirstChild.Value,
                                                          questione.ChildNodes.Item(2).FirstChild.Value,
                                                          questione.ChildNodes.Item(3).FirstChild.Value,
                                                          questione.ChildNodes.Item(4).FirstChild.Value,
                                                          Int32.Parse(questione.ChildNodes.Item(5).FirstChild.Value));
                                m.questions.Add(q);
                            }
                            currentnumber++;
                        }
                    }
                }
            }
            return(m);
        }