-
Notifications
You must be signed in to change notification settings - Fork 115
/
botgamecreate.php
283 lines (239 loc) · 10.8 KB
/
botgamecreate.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
Copyright (C) 2004-2010 Kestas J. Kuliukas
This file is part of webDiplomacy.
webDiplomacy is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
webDiplomacy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with webDiplomacy. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @package Base
* @subpackage Forms
*/
// Check whether we're in play-now mode:
define('IN_CODE',true);
require_once('config.php');
if( Config::isOnPlayNowDomain() ) define('PLAYNOW',true);
require_once('header.php');
global $User, $Misc, $DB;
if ( $Misc->Panic )
{
libHTML::notice(l_t('Game creation disabled'),
l_t("Game creation has been temporarily disabled while we take care of an unexpected problem. Please try again later, sorry for the inconvenience."));
}
if( defined('PLAYNOW') )
{
if( !isset($User) || $User->type['Guest'] || !$User->type['User'] )
{
// Make a User
// Save their key, if present
// Until no key
// Set their new key their key
//libAuth::keyWipe();
// Generate user key
$acct = 'diplonow_'.round(rand(0,100000));
while( 0 != $DB->sql_row("SELECT COUNT(1) FROM wD_Users WHERE username='" . $acct . "'")[0] )
{
$acct = 'diplonow_'.round(rand(0,100000));
}
$pass = (string)(rand(0,1000000000));
//$DB->sql_put("INSERT INTO wd_Users (username,type,email,points,comment,homepage,timejoined,timeLastSessionEnded,password) VALUES ('".$acct."', 'User', '".$acct."', 0, '', '', ".time().", ".time().", UNHEX('".$passHash."'));");
$DB->sql_put("INSERT INTO wD_Users(
`username`,`email`,`points`,`comment`,`homepage`,`hideEmail`,`timeJoined`,`locale`,`timeLastSessionEnded`,`lastMessageIDViewed`,`password`,`type`,`notifications`,`muteReports`,`silenceID`,`cdCount`,`nmrCount`,`cdTakenCount`,`phaseCount`,`gameCount`,`reliabilityRating`,`deletedCDs`,`tempBan`,`emergencyPauseDate`,`yearlyPhaseCount`,`tempBanReason`,`optInFeatures`
)
SELECT '".$acct."' `username`,'".$acct."' `email`, 100 `points`,`comment`,`homepage`,`hideEmail`,`timeJoined`,`locale`,".time()." `timeLastSessionEnded`,".time()."`lastMessageIDViewed`,UNHEX('".libAuth::pass_Hash($pass)."'),'User' `type`,`notifications`,`muteReports`,`silenceID`,`cdCount`,`nmrCount`,`cdTakenCount`,`phaseCount`,`gameCount`,`reliabilityRating`,`deletedCDs`,`tempBan`,`emergencyPauseDate`,`yearlyPhaseCount`,`tempBanReason`,1
FROM wD_Users
WHERE id = 1");
list($newUserID) = $DB->sql_row("SELECT LAST_INSERT_ID()");
//$NewUser = new User($newUserID);
$key = libAuth::userPass_Key($acct, $pass); // Password is never uysed
$cookieKey = $key;//libAuth::generateKey($newUserID, $pass);
setcookie('wD-Key',$cookieKey,['expires'=>time()+365*24*60*60,'samesite'=>'Lax']);
global $User;
$User = new User($newUserID);
}
$_REQUEST['newGame'] = array('variantID'=>1, 'name'=>$User->username, 'countryID'=>0);
}
if( !$User->type['User'] )
{
libHTML::notice(l_t('Not logged on'),l_t("Only a logged on user can create games. Please <a href='logon.php' class='light'>log on</a> to create your own games."));
}
// Limit users to 3 bot games at a time unless they are a moderator.
if ($User->getBotGameCount() > 2)
{
if (!$User->type['Moderator'])
{
libHTML::notice('3 bot games at a time.','Sorry, only 3 bot games at a time, please finish one of your current ones to start another!');
}
}
libHTML::starthtml();
if( isset($_REQUEST['newGame']) and is_array($_REQUEST['newGame']) )
{
try
{
$form = $_REQUEST['newGame']; // This makes $form look harmless when it is unsanitized; the parameters must all be sanitized
$input = array();
$required = array('variantID', 'name', 'countryID');
if ( !isset($form['missingPlayerPolicy']) ) {$form['missingPlayerPolicy'] = 'Normal'; }
foreach($required as $requiredName)
{
if ( isset($form[$requiredName]) ) { $input[$requiredName] = $form[$requiredName]; }
else{ throw new Exception(l_t('The variable "%s" is needed to create a game, but was not entered.',$requiredName)); }
}
unset($required, $form);
// Enable / disable full-press mode
$input['fullPress'] = isset($_REQUEST['fullPress']) && $_REQUEST['fullPress'] == '1' ? 1 : 0;
$input['variantID']=(int)$input['variantID'];
$input['countryID']=(int)$input['countryID'];
if( !in_array($input['variantID'],Config::$apiConfig['variantIDs']) ) { throw new Exception(l_t("Variant ID given (%s) doesn't represent a real variant.",$input['variantID'])); }
// If the name isn't unique or is too long the database will stop it
$input['name'] = $DB->escape($input['name']);
if ( !$input['name'] ) { throw new Exception(l_t("No name entered.")); }
list($countryCount) = $DB->sql_row("SELECT countryCount FROM wD_VariantInfo WHERE variantID=".$input['variantID']);
if ($input['countryID'] < 0 or $input['countryID'] > $countryCount)
{
throw new Exception(l_t("%s is an invalid country ID.",(string)$input['countryID']));
}
// Create Game record & object
require_once(l_r('gamemaster/game.php'));
$Game = processGame::create($input['variantID'],$input['name'],'',5,'Unranked',4320, -1, 4320, -1, 60,'No','Regular','Normal','draw-votes-public',0,4,'MemberVsBots');
// Prevent temp banned players from making new games.
if ($User->userIsTempBanned())
{
processGame::eraseGame($Game->id);
libHTML::notice('You are blocked from creating new games.', 'You are blocked from creating new games.');
}
// Create first Member record & object
processMember::create($User->id, 5, $input['countryID']);
//Add Bots
$botNum = $countryCount - 1;
// $tabl = $DB->sql_tabl("SELECT id FROM wD_Users WHERE type LIKE '%bot%' LIMIT ".$botNum);
// Use a specific bot for a specific variant for now. A new Config:: function is needed to be able to flexibly map variants to certain specialized bots
$tabl = $DB->sql_tabl("SELECT id FROM wD_Users WHERE type LIKE '%bot%' ".
($input['variantID']==15 ?
" AND username='FairBot2' "
:($input['fullPress']==1 ?
" AND username LIKE 'dipgpt%'"
:" AND NOT username LIKE 'dipgpt%') " // TODO: Make bot selection not rely on the bot username
))." LIMIT ".$botNum);
$currCountry = 1;
while (list($botID) = $DB->tabl_row($tabl))
{
if($currCountry == $input['countryID'])
{
$currCountry += 1;
}
if ($input['countryID'] == 0)
{
processMember::create($botID, 5, 0);
}
else
{
processMember::create($botID, 5, $currCountry);
}
$currCountry += 1;
}
// Get the game started straight away
$DB->sql_put('UPDATE wD_Games SET processTime = ' . time() . ' WHERE id = ' . $Game->id);
$MC->append('processHint',','.$Game->id);
$Game->Members->joinedRedirect(true); // When playing against AI it's going to be classic, so go straight into the beta UI
}
catch(Exception $e)
{
print '<div class="content">';
print '<p class="notice">'.$e->getMessage().'</p>';
print '</div>';
}
}
if ( $User->points >= 5 ) { $defaultPoints = 5; }
else
{
print l_t("You cannot create a new game because you have less than 5%s, you only have %s%s. ".
"You will always have at least 100 points, including the points that you have bet into active games, so if you want ".
"to start a new game just wait until your other games have finished (<a href='points.php#minpoints' class='light'>read more</a>).",libHTML::points(),$User->points,libHTML::points());
print '</div>';
libHTML::footer();
}
if( isset($input) && isset($input['points']) ) { $formPoints = $input['points']; }
else { $formPoints = $defaultPoints; }
print '<div class="content-bare content-board-header content-title-header">
<div class="pageTitle barAlt1">Create a new game against bots</div>
<div class="pageDescription">Start a new game of Diplomacy against bots.</div>
</div>
<div class="content content-follow-on">
<p><a href="gamecreate.php">Play a game against humans</a></p>
<div class = "gameCreateShow">
<p>All games against bots are unranked, with 3 day phases and 4 excused missed turns. However, anytime you ready up your orders, the game will immediately move to the next phase.</p>
<form method="post">
<p>
<strong>Game Name:</strong></br>
<input class = "gameCreate" type="text" name="newGame[name]" value="" size="30">
</p>
<strong>Variant type (map choices):</strong>
<select id="variantID" class = "gameCreate" name="newGame[variantID]" onChange="setExtOptions(this.value)">';
?>
<script type="text/javascript">
function setExtOptions(i){
document.getElementById('countryID').options.length=0;
switch(i)
{
<?php
$checkboxes=array();
$first='';
foreach(Config::$variants as $variantID=>$variantName)
{
if (in_array($variantID, Config::$apiConfig['variantIDs']))
{
$Variant = libVariant::loadFromVariantName($variantName);
$checkboxes[$Variant->fullName] = '<option value="'.$variantID.'"'.(($first=='')?' selected':'').'>'.$Variant->fullName.'</option>';
if($first=='')
{
$first='"'.$variantID.'"';
$defaultName=$Variant->fullName;
}
print "case \"".$variantID."\":\n";
print "document.getElementById('countryID').options[0]=new Option ('Random','0');";
for ($i=1; $i<=count($Variant->countries); $i++)
print "document.getElementById('countryID').options[".$i."]=new Option ('".$Variant->countries[($i -1)]."', '".$i."');";
print "break;\n";
}
ksort($checkboxes);
}
?>
}
}
</script>
<?php
print implode($checkboxes);
print '</select>';
print '</br></br>
<strong>Country: </strong>
<select id="countryID" class="gameCreate" name="newGame[countryID]">
</select>
</br></br>
<strong>Full-press AI: (Experimental / Beta)</strong>
<select id="fullPress" class="gameCreate" name="fullPress">
<option value="0" selected>No, gunboat / no-press</option>
<option value="1" selected>Yes, full-press bots (note full-press bots have longer response times)</option>
</select>
</br></br>
<p class="notice">
<input class = "green-Submit" type="submit" value="Create">
</p>';
?>
<script type="text/javascript">
setExtOptions(<?php print $first;?>);
</script>
<?php
print '</form>
</div>';
print '</div>';
libHTML::footer();
?>