public ProfilS GetProfil(string name, string tokenid = "")
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                var profil = db.Profil.SingleOrDefault(p => p.Name == name);

                if (profil == null)
                {
                    return CreateProfil(name, tokenid);
                }
                else if (tokenid == profil.TokenId)
                {
                    if (tokenid == "")
                    {
                        Random random = new Random();
                        tokenid = random.Next().ToString();
                        profil.TokenId = tokenid;
                        db.Profil.InsertOnSubmit(profil);
                        db.SubmitChanges();
                    }

                    return new ProfilS()
                    {
                        id = profil.idProfil,
                        name = profil.Name,
                        avatar = profil.Avatar,
                        rank = profil.Rank,
                        tokenid = profil.TokenId
                    };
                }
                else
                {
                    return new ProfilS()
                    {
                        id = -1,
                        name = "",
                        avatar = "",
                        rank = 0,
                        tokenid = ""
                    };
                }
            }
        }
        public int CreateOrUpdateEvent(int id, string name, DateTime startDate, DateTime endDate, byte[] image, string description)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Event eventGame = null;
                if (id == 0)
                {
                    eventGame = new Event()
                    {
                        Name = name,
                        StartDate = startDate,
                        EndDate = endDate,
                        Image = "",
                        Description = description
                    };

                    db.Event.InsertOnSubmit(eventGame);
                }
                else
                {
                    eventGame = db.Event.SingleOrDefault(e => e.idEvent == id);
                    if (name != "")
                    {
                        eventGame.Name = name;
                    }
                    eventGame.StartDate = startDate;
                    eventGame.EndDate = endDate;
                    if (description != "")
                    {
                        eventGame.Description = description;
                    }
                }
                try
                {
                    if (image != null)
                    {
                        if (id == 0)
                            db.SubmitChanges();

                        string path = IMAGE_PATH + "events\\" + eventGame.idEvent + "-" + eventGame.Name;
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(IMAGE_PATH + "events\\" + eventGame.idEvent + "-" + eventGame.Name);

                        using (FileStream fs = new FileStream(path + "\\" + "cover", FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            fs.Write(image, 0, image.Length);
                            eventGame.Image = path + "\\" + "cover";
                        }
                    }

                    db.SubmitChanges();

                    return eventGame.idEvent;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR CreateEvent " + ex.Message);
                    return -1;
                }
            }
        }
        public bool DeletePricepool(int id)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                var pricepool = db.PricePool.SingleOrDefault(e => e.idPricePool == id);
                if (pricepool != null)
                {
                    db.PricePool.DeleteOnSubmit(pricepool);

                    try
                    {
                        db.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("delete price " + ex.Message);
                    }
                }
                return false;
            }
        }
        public bool DeleteEvent(int id)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Event eventGame = null;
                eventGame = db.Event.SingleOrDefault(e => e.idEvent == id);

                if (eventGame != null)
                {
                    if (Directory.Exists(IMAGE_PATH + "event\\" + eventGame.idEvent + "-" + eventGame.Name))
                        Directory.Delete(IMAGE_PATH + "event\\" + eventGame.idEvent + "-" + eventGame.Name);

                    db.Event.DeleteOnSubmit(eventGame);

                    try
                    {
                        db.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR Delete Event " + ex.Message);

                    }
                }
                return false;
            }
        }
        public bool DeletePrice(int id)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Price price = null;
                price = db.Price.SingleOrDefault(p => p.idPrice == id);

                if (price != null)
                {
                    if (Directory.Exists(IMAGE_PATH + "prices\\" + price.idPrice + "-" + price.Name))
                        Directory.Delete(IMAGE_PATH + "prices\\" + price.idPrice + "-" + price.Name);

                    db.Price.DeleteOnSubmit(price);

                    try
                    {
                        db.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR Delete Price " + ex.Message);

                    }
                }
                return false;
            }
        }
        public ProfilS CreateProfil(string name, string tokenId = "")
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Profil profil = null;
                profil = db.Profil.SingleOrDefault(p => p.Name == name);

                if (profil == null)
                {
                    if (tokenId == "")
                    {
                        Random random = new Random();
                        tokenId = random.Next().ToString();
                    }

                    profil = new Profil();
                    profil.Name = name;
                    profil.TokenId = tokenId;
                    profil.Avatar = "";
                    profil.Rank = 0;
                    db.Profil.InsertOnSubmit(profil);

                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR CreateProfil " + ex.Message);
                        return null;
                    }
                }
                else
                {
                    profil = new Profil()
                    {
                        idProfil = -1,
                        Name = "",
                        Avatar = "",
                        Rank = 0,
                        TokenId = ""
                    };
                }

                return new ProfilS() {
                    id = profil.idProfil,
                    name = profil.Name,
                    avatar = profil.Avatar,
                    rank = profil.Rank,
                    tokenid = profil.TokenId
                };
            }
        }
        public bool CreatePricePool(int priceId, int eventId, int placeRangeMin, int placeRangeMax, float placePercent)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Event group = db.Event.SingleOrDefault(g => g.idEvent == eventId);
                if (group != null)
                {
                    PricePool pricePool = new PricePool()
                    {
                        idPrice = priceId,
                        idEvent = eventId,
                        placeRangeMin = placeRangeMin,
                        placeRangeMax = placeRangeMax,
                        placePurcent = placePercent
                    };

                    db.PricePool.InsertOnSubmit(pricePool);

                    try
                    {
                        db.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("CREATE PRICEPOOL " + ex.Message);
                    }
                }

                return false;
            }
        }
        public bool CreatePrice(string name, string image, string description, string path)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Price price = new Price() {
                    Name = name,
                    Image = image,
                    Description = description,
                    Path = path
                };

                db.Price.InsertOnSubmit(price);

                try
                {
                    db.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR CreatePrice " + ex.Message);
                }

                return false;
            }
        }
        public int CreateOrUpdatePrice(int id, string name, byte[] image, byte[] asset, string description)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Price price = null;
                if (id == 0)
                {
                    price = new Price()
                    {
                        Name = name,
                        Image = "",
                        Path = "",
                        Description = description
                    };

                    db.Price.InsertOnSubmit(price);
                }
                else
                {
                    price = db.Price.SingleOrDefault(e => e.idPrice == id);

                    if (name != "")
                        price.Name = name;

                    if (description != "")
                        price.Description = description;
                }
                try
                {
                    if (image != null || asset != null)
                    {
                        if (id == 0)
                            db.SubmitChanges();

                        string path = IMAGE_PATH + "prices\\" + price.idPrice + "-" + price.Name;
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(IMAGE_PATH + "prices\\" + price.idPrice + "-" + price.Name);

                        if (image != null)
                        {
                            using (FileStream fs = new FileStream(path + "\\" + "cover", FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                fs.Write(image, 0, image.Length);
                                price.Image = path + "\\" + "cover";
                            }
                        }

                        if (asset != null)
                        {
                            using (FileStream fs = new FileStream(path + "\\" + "asset", FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                fs.Write(asset, 0, asset.Length);
                                price.Path = path + "\\" + "asset";
                            }
                        }
                    }

                    db.SubmitChanges();

                    return price.idPrice;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR CreateEvent " + ex.Message);
                    return -1;
                }
            }
        }
        public bool CreateOrUpdateGroupSign(int eventId, int profilId, int score)
        {
            using (MasterDBDataContext db = new MasterDBDataContext())
            {
                Event eventGame = db.Event.SingleOrDefault(e => e.idEvent == eventId);
                Profil profil = db.Profil.SingleOrDefault(p => p.idProfil == profilId);

                if (eventGame != null && profil != null)
                {
                    GroupSign groupSign = null;
                    groupSign = db.GroupSign.SingleOrDefault(gs => gs.idEvent == eventId && gs.idProfil == profilId);

                    if (groupSign == null)
                    {
                        groupSign = new GroupSign()
                        {
                            idEvent = eventId,
                            idProfil = profil.idProfil,
                            Score = score
                        };

                        db.GroupSign.InsertOnSubmit(groupSign);
                    }
                    else
                    {
                        if (groupSign.Score < score)
                        {
                            groupSign.Score = score;
                        }
                    }

                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Create Group Sign " + ex.Message);
                        return false;
                    }
                }
            }
            return true;
        }