forked from Sleepcap/vDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemaster.php
executable file
·174 lines (142 loc) · 5.44 KB
/
gamemaster.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
<?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 GameMaster
*/
require_once('header.php');
require_once(l_r('gamemaster/game.php'));
require_once(l_r('gamemaster/misc.php'));
if ( $Misc->Panic )
{
libHTML::notice(l_t('Game processing disabled'),
l_t("Game processing has been temporarily disabled while we take care of an ".
"unexpected problem. Please try again later, sorry for the inconvenience."));
}
if ( !( $User->type['Moderator']
or ( isset($_REQUEST['gameMasterSecret']) and $_REQUEST['gameMasterSecret'] == Config::$gameMasterSecret )
or ( isset($_REQUEST['gameMasterToken']) and libAuth::gamemasterToken_Valid($_REQUEST['gameMasterToken']) )
) )
{
libHTML::notice(l_t('Denied'), l_t('Only the cron script and moderators can run the gamemaster script.'));
}
if ( isset($_REQUEST['gameMasterSecret']) && $User->type['User'] && !$User->type['Moderator'] && $Misc->LastProcessTime == 0 )
{
// The server has just been installed; make this user the admin now.
$DB->sql_put("UPDATE wD_Users SET type = CONCAT(type,',Moderator,Admin') WHERE id = ".$User->id);
$User->type['Moderator']=$User->type['Admin']=true;
$Misc->LastProcessTime = time();
$Misc->write();
libHTML::notice(l_t('Admin'),l_t("You have been made admin. Please continue with the install instructions in README.txt."));
}
libHTML::starthtml(l_t('GameMaster'));
print '<div class="content">';
$DB->sql_put("COMMIT"); // Unlock our user row, to prevent deadlocks below
// This means our $User object should only be used for reading from
ini_set('memory_limit',"40M");
ini_set('max_execution_time','40');
/*
* - Update session table
* - Update misc values (if running as admin/mod)
* - Check last process time, pause processing/save current process time
* - Check queue and games table for games to process, votes to enact, and system functions to perform
*/
print l_t('Updating session table').'<br />';
libGameMaster::updateSessionTable();
$statsDir=libCache::dirName('stats');
$onlineFile=$statsDir.'/onlineUsers.json';
$tabl=$DB->sql_tabl("SELECT userID FROM wD_Sessions");
$onlineUsers=array();
while(list($userID)=$DB->tabl_row($tabl))
$onlineUsers[]=$userID;
file_put_contents($onlineFile, 'onlineUsers=$A(['.implode(',',$onlineUsers).']);');
//- Update misc values (if running as admin/mod)
if( !$User->type['System'] || (time()%(15*60)<=5*60) )
{
print l_t('Updating Misc values').'<br />';
miscUpdate::errorLog();
miscUpdate::forum();
miscUpdate::game();
miscUpdate::user();
}
//- Check last process time, pause processing/save current process time
if ( ( time() - $Misc->LastProcessTime ) > Config::$downtimeTriggerMinutes*60 )
{
libHTML::notice(l_t('Games not processing'),libHTML::admincp('resetLastProcessTime',null,l_t('Continue processing now')));
}
$Misc->LastProcessTime = time();
$Misc->write();
$startTime = time(); // Only do ~30 sec of processing per cycle
$tabl = $DB->sql_tabl("SELECT * FROM wD_Games
WHERE processStatus='Not-processing' AND processTime <= ".time()." AND NOT phase='Finished'");
while( (time() - $startTime)<30 && $gameRow=$DB->tabl_hash($tabl) )
{
$Variant=libVariant::loadFromVariantID($gameRow['variantID']);
$Game=$Variant->Game($gameRow);
print '<a href="board.php?gameID='.$Game->id.'">gameID='.$Game->id.': '.$Game->name.'</a>: ';
try
{
if( $Game->processStatus!='Crashed' && $Game->attempts > count($Game->Members->ByID)*2 )
{
$Game = $Variant->processGame($Game->id);
$Game->crashed();
$DB->sql_put("COMMIT");
print 'Crashed.';
}
elseif( $Game->needsProcess() )
{
$DB->sql_put("UPDATE wD_Games SET attempts=attempts+1 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
print 'Rechecking.. ';
$Game = $Variant->processGame($Game->id);
if( $Game->needsProcess() )
{
print l_t('Processing..').' ';
$Game->process();
$DB->sql_put("UPDATE wD_Games SET attempts=0 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
print l_t('Processed.');
}
}
}
catch(Exception $e)
{
if( $e->getMessage() == "Abandoned" || $e->getMessage() == "Cancelled" )
{
$DB->sql_put("COMMIT");
print l_t('Abandoned.');
}
else
{
$DB->sql_put("ROLLBACK");
print l_t('Crashed: "%s".',$e->getMessage());
}
}
print '<br />';
}
// If it took over 30 secs there may still be games to process
if( (time() - $startTime)>=30 )
{
/*
* For when you're developing and just reloaded the DB from a backup,
* you usually have to refresh a few times before it runs out of games
* to process
*/
header('refresh: 4; url=gamemaster.php');
print '<p class="notice">'.l_t('Timed-out; re-running').'</p>';
}
print '</div>';
libHTML::footer();
?>