md /draft/draft_functions.php

Language PHP Lines 217
MD5 Hash 16e6599aad7ed13fd99ed177907d2816 Estimated Cost $2,993 (why?)
Repository https://github.com/beakid/md.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?
#denna fil har tillgŒng till $draft_info som Šr en select * from md_draft where pk_draft_id = $draft_id (stŠlls i draft.php);

include("../generatepack.php");
include_once("../functions.php");


if($_POST[action] == "create_draft") {
	//verify data - yawn


	//make draft_name
	$draft_name_result = mysql_query("SELECT exp_shortname FROM md_exp WHERE pk_exp_id = $pack1 UNION ALL SELECT exp_shortname FROM md_exp WHERE pk_exp_id = $pack2 UNION ALL SELECT exp_shortname FROM md_exp WHERE pk_exp_id = $pack3");
	while($draft_name_values = mysql_fetch_array($draft_name_result)) {
		$draft_name = $draft_name . " " . $draft_name_values[exp_shortname];
	ltrim($draft_name);
	
	
	//insert data for now
	mysql_query("INSERT INTO md_draft(draft_password, draft_is_tournament, draft_name, max_players, show_picks, draft_speed, pack_1, pack_2, pack_3) VALUES('$password', $tour_after, '$draft_name', $max_players, $show_picks, $speed, $pack1, $pack2, $pack3)");
	$draft_id = mysql_insert_id();
	
	//join player (as seat #1, "the Creator")
	mysql_query("INSERT INTO md_draft2user(fk_draft_id, fk_user_id, seat_number) VALUES(".$draft_id.", $_SESSION[md_userid], 1)");

	//ett welcome-chat-message frŒn mdboten
	mysql_query("INSERT INTO md_chat (fk_draft_id, fk_user_id, chat_message, chat_date)
	VALUES ('$draft_id','1','Welcome to draft #$draft_id. Good luck and have fun!', NOW())");

	}
	header("Location: draft.php?id=".$draft_id);
	die();
}
elseif($_POST["action"] == "create_sealed")
{
		//insert data for now
		$sealed_name = mysql_result(mysql_query("SELECT exp_shortname FROM md_exp WHERE pk_exp_id = $tourpack"),0);
		mysql_query("INSERT INTO md_draft(draft_name, max_players, draft_status, pack_1, pack_2, pack_3, draft_start, draft_is_sealed) VALUES('Sealed $sealed_name', 1, 3, '$tourpack', '$boosterpack1', '$boosterpack2', NOW(), 1)");
		$draft_id = mysql_insert_id();

		//join player
		mysql_query("INSERT INTO md_basicland (fk_user_id, fk_draft_id) VALUES ($_SESSION[md_userid], $draft_id)");
		mysql_query("INSERT INTO md_draft2user(fk_draft_id, fk_user_id) VALUES('$draft_id', $_SESSION[md_userid])");
		add_tourpack($draft_id,$tourpack,$boosterpack1,$boosterpack2);	
		header("Location: draft.php?id=".$draft_id);
		die();
}

if($action == "join_draft") {
	//stoppa in spelaren i md_draft2user om man inte redan Ĺ r med
	if(!mysql_num_rows(mysql_query("SELECT fk_user_id FROM md_draft2user WHERE fk_user_id = '$_SESSION[md_userid]' AND fk_draft_id = '$draft_id'")))
	{	
		$highestseatnumber = mysql_result(mysql_query("SELECT seat_number FROM md_draft2user ORDER BY seat_number DESC LIMIT 0,1"),0);
		mysql_query("INSERT INTO md_draft2user(fk_draft_id, fk_user_id, seat_number) VALUES($draft_id,$_SESSION[md_userid], '".($highestseatnumber+1)."')");
	}
}

if($action == "leave_draft") {
	#finns det inte nŒgra andra fšrutom mig sjŠlv (och ev. bots, id 1)
	if(!mysql_num_rows(mysql_query("SELECT * FROM md_draft2user WHERE fk_draft_id = $draft_id AND fk_user_id != '1' AND fk_user_id != $_SESSION[md_userid]"))) {
		mysql_query("DELETE FROM md_draft WHERE pk_draft_id = $draft_id");
		mysql_query("DELETE FROM md_chat WHERE fk_draft_id = $draft_id");
		mysql_query("DELETE FROM md_basicland WHERE fk_draft_id = $draft_id");
		$packs = mysql_query("SELECT * FROM md_pack WHERE fk_draft_id = $draft_id");
		while($pack = mysql_fetch_array($packs))
		{
			mysql_query("DELETE FROM md_packcard WHERE fk_pack_id = $pack[pk_pack_id]");
		}
		mysql_query("DELETE FROM md_pack WHERE fk_draft_id = $draft_id");
		mysql_query("DELETE FROM md_draft2user WHERE fk_draft_id = $draft_id AND fk_user_id = $_SESSION[md_userid]");
		header("Location: index.php");
		die();
	}

	$myseatnumber = @mysql_result(mysql_query("SELECT seat_number FROM md_draft2user WHERE fk_user_id = $_SESSION[md_userid] AND fk_draft_id = $draft_id"),0);
	#if draft is not running, decrease higher seats with 1
	if($draft_info["draft_status"] == 0)
	{
		mysql_query("UPDATE md_draft2user SET seat_number = seat_number - 1 WHERE seat_number > $myseatnumber AND fk_draft_id = $draft_id");
		mysql_query("DELETE FROM md_draft2user WHERE fk_draft_id = $draft_id AND fk_user_id = $_SESSION[md_userid]");
	}
	elseif($draft_info["draft_status"] == 1)
	{
		mysql_query("DELETE FROM md_draft2user WHERE fk_draft_id = $draft_id AND fk_user_id = $_SESSION[md_userid]");
		mysql_query("UPDATE md_draft SET draft_status = 0 WHERE pk_draft_id = $draft_id");
	}
	else
	#annars, i en aktiv draft, plockar vi en bot (ID 1)
	{
		mysql_query("UPDATE md_draft2user SET fk_user_id = 1 WHERE fk_user_id = $_SESSION[md_userid] AND fk_draft_id = $draft_id");
	}

	header("Location: index.php");
	die();
}

if($_POST["action"] == "draft") {
	//hämtar diverse info om draften o kontrollerar sĺ man inte fuskar
	$pack_id = mysql_fetch_row(mysql_query("SELECT fk_pack_id, fk_card_id FROM md_packcard WHERE pk_packcard_id = $_POST[chosen_card] LIMIT 1"));
		//fundera ut vad fusk är och vad som ska kollas
		
	//draftar kortet man valt
	$pick_number = mysql_fetch_row(mysql_query("SELECT count(*) FROM md_packcard WHERE fk_user_id = 0 AND fk_pack_id = $pack_id[0] GROUP BY fk_pack_id"));
	mysql_query("UPDATE md_packcard SET fk_user_id = $_SESSION[md_userid], pick_number = 16 - $pick_number[0] WHERE pk_packcard_id = $_POST[chosen_card]");
	$_SESSION["draft_start"] = false;

	//hŠr fixar vi med statsen - labbvarning
	//$ignore_rating true -> ignore... annars tom
	
	//antal kort i packet - kortet man vŠljer i slutet bšr pŒverka mkt lite
	//man skickar in en klump med kort-id o ratings, inkl ett pick - antalet pŒverkar k-vŠrdet, likasŒ bšr userns rating gšra
	//lagar vi flera olika ratings eller bara en? tveksamt men jag tror nej till att bšrja med
	//hur viktar vi userns rating? som kvoten mot 1600? det dubbla? +12,5% resp +25% fšr en spelare med 1800
	//jag tror pŒ minimal pŒverkan av 1600-users... 
	//ska varje pick gŒ in i statsen eller slumpat t.ex. 10%? fšr att minska last
	//1800-1550 -> 250
	//1600-1550 -> 50
	//1600 -> 50/100 -> x0,5
	//1800 -> 250/100 -> x2,5
	//2100 -> 550/100 ->x5,5
	//allt detta mŒste gŒs igenom o viktas om - utan tvekan
	//lŠgg till analys av vad man pickat sŒ att fŠrger Šr en del i analysen
	//t.ex:
	//man har gŒtt gršnt -> nŠr man pickar gršnt - det gršna kortet gŒr upp, de andra gršna gŒr ner,artifakt gŒr ner, gršna lŠnder gŒr ner(eller fŠrglšsa) MEN
	//allt som vi taggat som inte gršnt pŒverkas bara ytterst lite(eller ens alls?)
	//omvŠnt, gick man gršnt och plštsligt pickar ett blŒtt kort pŒverkar det alla kort som vanligt. detta innebŠr sammantaget att ju lŠngre in i draften man
	//kommer, ju fŠrre kort pŒverkar man genom sina val om man hŒller sig i sin fŠrg... hmm
	//grundvŠrde pŒ kort kontro aktuellt vŠrde fšr spelaren
	//dessutom tŠnker jag mig att hur lŒngt man kommit in i ett pack reflekterar pŒverkar kvŠrdet. detta gŠller ocksŒ efterkommande pack men frŒgan Šr: ska man
	//bšrja helt frŒn bšrjan eller ska man liksom gŒ i tre vŒgor nedŒt i k-vŠrdet dŠr starten pŒ varje pack Šr stšrre Šn dŠr man slutade fšrra packet men botten sen blir
	//lŠgre Šn fšrra packet osv...
	//hur fan vŠger alla dessa faktorer ihop... reflekterar det vad vi tror det gšr...
	//allt pickande o rŠknande syftar att rŠkna ut ett basvŠrde pŒ kortet - bŠst info fŒr man nŠr spelaren inte har pickat en massa ELLER om den pickat en massa o plštsligt byter fŠrg

	//berŠkna spelarens pickpool
	//just nu siktar vi bara pŒ fŠrger man man kan tŠnka sig att vŠga in critters/etc oxŒ
	//rad1-5 = fŠrgerna WUBRG
	//rad6 antal kort totalt
	$color_chk = mysql_query("SELECT count(*) as color FROM md_packcard, md_cards WHERE fk_user_id = $_SESSION[md_userid] AND fk_card_id = pk_card_id AND find_in_set('W',card_colortag)
	UNION ALL SELECT count(*) as color FROM md_packcard, md_cards WHERE fk_user_id = $_SESSION[md_userid] AND fk_card_id = pk_card_id AND find_in_set('U',card_colortag)
	UNION ALL SELECT count(*) as color FROM md_packcard, md_cards WHERE fk_user_id = $_SESSION[md_userid] AND fk_card_id = pk_card_id AND find_in_set('R',card_colortag)
	UNION ALL SELECT count(*) as color FROM md_packcard, md_cards WHERE fk_user_id = $_SESSION[md_userid] AND fk_card_id = pk_card_id AND find_in_set('B',card_colortag)
	UNION ALL SELECT count(*) as color FROM md_packcard, md_cards WHERE fk_user_id = $_SESSION[md_userid] AND fk_card_id = pk_card_id AND find_in_set('G',card_colortag)
	UNION ALL SELECT count(*) as color FROM md_packcard  WHERE fk_user_id = $_SESSION[md_userid]");
	for($x = 1; $x<=6; $x++) {
		$color_values = mysql_fetch_array($color_chk);
		$color[$x] = $color_values[color];
	}
	
	$pick_id = $pack_id[1];
	$pick = $cardarray[$pick_id];
	
	//plocka ut picket frŒn alla kort
	unset($cardarray[$pick_id]);
	
	$x = count($cardarray);
	$query = "REPLACE into md_stats(fk_card_id, rating) VALUES";
	
	foreach ($cardarray as $fk_card_id => $trash) {
			$k_value = $x*($_SESSION[md_user_rating]-1550)*0.0025;
			$pick_rating_change=round($k_value*(1-(1/(pow(10, (($trash-$pick) / 400)) + 1))));
			$total_rating_change = $total_rating_change + $pick_rating_change;
			$query = $query."($fk_card_id, $trash-$pick_rating_change),";
	}
	$query = $query."($pick_id, $pick+$total_rating_change)";
	//echo $query;
	mysql_query($query);
	
	//labbvarning slut - inga mer stats



	//skickar packet till nästa spelare och gÜmmer det tills alla spelare skickat sina packs
	//pack 2 gĂĽr andra hĂĽllet	
	if($draft_info[present_pack] == 2) {
		mysql_query("UPDATE md_pack SET pack_status = 1, seat_number = if(seat_number > 1, seat_number - 1, $draft_info[max_players]) WHERE pk_pack_id = $pack_id[0]");
	} else {
		mysql_query("UPDATE md_pack SET pack_status = 1, seat_number = if(seat_number < $draft_info[max_players], seat_number + 1, 1) WHERE pk_pack_id = $pack_id[0]");		
	}
	//uppdaterer status pĂĽ spelaren - lite overkill men det funkar
	mysql_query("UPDATE md_draft2user SET seat_status = 3 WHERE fk_draft_id = $draft_id AND fk_user_id = $_SESSION[md_userid]");
	
	//har alla spelare pickat? visa dü nästa stack, eventuellt nästa pack
	$packs_status = mysql_fetch_row(mysql_query("SELECT count(*) FROM md_pack, md_draft WHERE pack_status = 1 AND fk_draft_id = $draft_id AND md_draft.present_pack = md_pack.pack_number AND md_draft.pk_draft_id = md_pack.fk_draft_id"));
	if($packs_status[0] == $draft_info[max_players]) {
		//om man just tog 15e kortet och alla andra oxü plockat sina kort: Üppna nästa pack, sätt nuvarande som färdigt (status 2)
		if($pick_number[0] == 1) {
			mysql_query("UPDATE md_draft SET draft_status = IF(present_pack = 3, 3, 2), present_pack = IF(present_pack < 3, present_pack + 1, 0) WHERE pk_draft_id = $draft_id");
			mysql_query("UPDATE md_pack SET pack_status = 2 WHERE fk_draft_id = $draft_id AND pack_status = 1");
			//tog vi sista packet
		} else {
			mysql_query("UPDATE md_pack SET pack_status = 0 WHERE fk_draft_id = $draft_id AND pack_status = 1");
		}
		mysql_query("UPDATE md_draft2user SET seat_status = 2 WHERE fk_draft_id = $draft_id");
	}	
	header("Location: draft.php?id=".$draft_id);
	die();
}


#check som kšrs vid reload av ostartad draft
if($draft_info["draft_status"] == "0" && $_SESSION["md_userid"] && $draft_id)
{
	//kontrollera antalet spelare i draften - Ĺ r vi max_players: slumpa ut seats, aktivera draften, skapa packs
	//man ska nog se till att stŠda i md_draft2user ofta, typ vi kanske i samband med inlogg ska skilja pŚ offline, online (men inte aktiv sista 5)
	//samt online(aktiv)

	//spelaren som joinar sist behÜver obv inte konfirma att han är där utan kommer bara till sidan "waiting for other players to confirm"
	$present_players = mysql_fetch_row(mysql_query("SELECT count(*) FROM md_draft2user WHERE fk_draft_id = $draft_id"));
	if($present_players[0] >= $draft_info["max_players"]) 
	{
		start_draft($draft_id);
		header("Location: draft.php?id=".$draft_id);
	}
}

?>
Back to Top