public LastHitInfo DoAttack(string nickname, int myslot, int enslot) { int dmg = -1; LastHitInfo lhi = null; try { Program.GameThreadLock.EnterReadLock(); Game game = Program.OnlineGames.Find(g => g.Gamers.Contains(nickname)); Program.GameThreadLock.ExitReadLock(); bool isWin = false; if (game != null) { lock (game) { if (game.currUsr != nickname) { return(lhi); } foreach (var item in game.firstGamerCards) { item.IsAttacked = false; } foreach (var item in game.twoGamerCards) { item.IsAttacked = false; } game.lastHitInfo.isMissed = false; game.lastHitInfo.IsCritical = false; if (game.gameState == 2) { Card myC = game.firstGamerCards.Find(cc => cc.slot == myslot); Card enC = game.twoGamerCards.Find(cc => cc.slot == enslot); //myC.Enabled = false; if (!Formulas.IsMiss()) //miss? { //calc real damage dmg = Formulas.CalculateDamage(myC.dmg, enC.def); if (dmg <= 0) { dmg = 1; } else { if (Formulas.IsCrit()) //crit damage { dmg = Formulas.CalculateCritDamage(dmg); game.lastHitInfo.IsCritical = true; } } int temp = enC.hp; //save curr hp enC.hp = enC.hp - dmg; //test //enC.dmg = enC.dmg - dmg/2; //if (enC.dmg < 1) enC.dmg = 1; if (enC.hp <= 0) { if (enC.hp < 0) { dmg = temp; //if overhit -> change hp } enC.hp = 0; enC.Enabled = false; } else { enC.TryEnjured((double)dmg, (double)temp); } } else { game.lastHitInfo.isMissed = true; } game.lastHitInfo.slot = myslot; game.lastHitInfo.dmg = dmg; lhi = game.lastHitInfo; enC.IsAttacked = true; isWin = game.CheckWinner(); //if (!isWin && game.firstGamerCards.Find(cc => cc.Enabled == true) == null) if (!isWin) //3 or < cards per turn... test { /* int cardsPerTurn = 3; * * if (game.firstGamerCards.FindAll(cc=> cc.hp > 0).Count >= 3) * cardsPerTurn = 3; * else if (game.firstGamerCards.FindAll(cc => cc.hp > 0).Count == 2) * cardsPerTurn = 2; * else if (game.firstGamerCards.FindAll(cc => cc.hp > 0).Count == 1) * cardsPerTurn = 1; * * * if (game.firstGamerCards.FindAll(cc => cc.Enabled == false && cc.hp > 0).Count >= cardsPerTurn) * { * foreach (var item in game.firstGamerCards) * { * if (item.hp > 0) item.Enabled = true; * } */ game.currUsr = game.Gamers[1]; game.gameState = 3; //} } else if (isWin) { setReward(game.Gamers[0], game.WinGamerReward, game, true); setReward(game.Gamers[1], game.LooseGamerReward, game, false); } } else if (game.gameState == 3) { Card myC = game.twoGamerCards.Find(cc => cc.slot == myslot); Card enC = game.firstGamerCards.Find(cc => cc.slot == enslot); //myC.Enabled = false; if (!Formulas.IsMiss()) //miss? { //calc real damage dmg = Formulas.CalculateDamage(myC.dmg, enC.def); if (dmg <= 0) { dmg = 1; } else { if (Formulas.IsCrit()) //crit damage { dmg = Formulas.CalculateCritDamage(dmg); game.lastHitInfo.IsCritical = true; } } int temp = enC.hp; //save curr hp enC.hp = enC.hp - dmg; //test //enC.dmg = enC.dmg - dmg/2; //if (enC.dmg < 1) enC.dmg = 1; if (enC.hp <= 0) { if (enC.hp < 0) { dmg = temp; //if overhit -> change hp } enC.hp = 0; enC.Enabled = false; } else { enC.TryEnjured((double)dmg, (double)temp); } } else { game.lastHitInfo.isMissed = true; } game.lastHitInfo.slot = myslot; game.lastHitInfo.dmg = dmg; lhi = game.lastHitInfo; enC.IsAttacked = true; isWin = game.CheckWinner(); //if (!isWin && game.twoGamerCards.Find(cc => cc.Enabled == true) == null) if (!isWin) //3 or < cards per turn... test { /*int cardsPerTurn = 3; * * if (game.twoGamerCards.FindAll(cc => cc.hp > 0).Count >= 3) * cardsPerTurn = 3; * else if (game.twoGamerCards.FindAll(cc => cc.hp > 0).Count == 2) * cardsPerTurn = 2; * else if (game.twoGamerCards.FindAll(cc => cc.hp > 0).Count == 1) * cardsPerTurn = 1; * * * if (game.twoGamerCards.FindAll(cc => cc.Enabled == false && cc.hp > 0).Count >= cardsPerTurn) * { * foreach (var item in game.twoGamerCards) * { * if (item.hp > 0) item.Enabled = true; * } */ game.currUsr = game.Gamers[0]; game.gameState = 2; // } } else if (isWin) { setReward(game.Gamers[1], game.WinGamerReward, game, true); setReward(game.Gamers[0], game.LooseGamerReward, game, false); } } } } } catch (Exception exc) { Console.WriteLine("ERROR: " + exc.Message + "\r\n" + exc.StackTrace); Program.dumpException(exc); } return(lhi); }
public List <Card> BuyCard(string user, int number) { if (sqlInjection.Words.Any(word => user.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0)) { return(null); } int char_id = -1; bool udovlet = true; List <Card> result = new List <Card>(); SqlConnection db_connection = new SqlConnection(Program.connectionString); try { db_connection.Open(); SqlCommand cmd = new SqlCommand("SELECT id, character_level, score FROM characters where account='" + user + "'", db_connection); SqlDataReader res = cmd.ExecuteReader(); if (res.Read()) { char_id = (int)res["id"]; int score = (int)res["score"]; int char_level = (int)res["character_level"]; res.Close(); if (char_level < 3 && number > 1) { udovlet = false; } if (udovlet && ((score >= 2000 && number == 1) || (score >= 4000 && number == 2) || (score >= 8000 && number == 3))) { int slot = GetFreeSlotNumberAllCards(user); List <Card> cclist; int card_id1 = -1; int card_id2 = -1; int card_id3 = -1; bool find = false; cclist = Card.GetAllavailableCards(user); if (cclist.Count > 0) { if (number == 1) { result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); card_id1 = result[0].id; card_id2 = result[1].id; card_id3 = result[2].id; score -= 2000; } else if (number == 2) { find = false; while (!find) { Card cc = cclist[Formulas.RndNext(0, cclist.Count)]; if (cc.cardRarity >= 2) { result.Add(cc); find = true; } } result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); card_id1 = result[0].id; card_id2 = result[1].id; card_id3 = result[2].id; score -= 4000; } else if (number == 3) { find = false; while (!find) { Card cc = cclist[Formulas.RndNext(0, cclist.Count)]; if (cc.cardRarity >= 2) { result.Add(cc); find = true; } } find = false; while (!find) { Card cc = cclist[Formulas.RndNext(0, cclist.Count)]; if (cc.cardRarity >= 2) { result.Add(cc); find = true; } } result.Add(cclist[Formulas.RndNext(0, cclist.Count)]); card_id1 = result[0].id; card_id2 = result[1].id; card_id3 = result[2].id; score -= 8000; } if (card_id1 != -1 && card_id2 != -1 && card_id3 != -1) { cmd = new SqlCommand("UPDATE characters SET score=" + score + " WHERE id=" + char_id, db_connection); cmd.ExecuteNonQuery(); cmd = new SqlCommand("INSERT INTO character_cards(char_id, card_id, slot) VALUES (" + char_id + ", " + card_id1 + ", " + slot + ")", db_connection); cmd.ExecuteNonQuery(); cmd = new SqlCommand("INSERT INTO character_cards(char_id, card_id, slot) VALUES (" + char_id + ", " + card_id2 + ", " + (slot + 1) + ")", db_connection); cmd.ExecuteNonQuery(); cmd = new SqlCommand("INSERT INTO character_cards(char_id, card_id, slot) VALUES (" + char_id + ", " + card_id3 + ", " + (slot + 2) + ")", db_connection); cmd.ExecuteNonQuery(); } } } } res.Close(); } catch (Exception exc) { Console.WriteLine("ERROR: " + exc.Message + "\r\n" + exc.StackTrace); Program.dumpException(exc); } db_connection.Close(); return(result); }