forked from kestasjk/webDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
executable file
·140 lines (109 loc) · 4.44 KB
/
ajax.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
<?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/>.
*/
define('AJAX', true); // Makes header.php ignore some of the unneeded stuff, mainly loading $User
require_once('header.php');
/*
* This function logs a huge amount of javascript errors, sent from javascript/utility.js on error,
* but it seems to log trivial errors in lots of different languages, and isn't very useful except
* perhaps for development.
function logJavaScriptError() {
$errorVars=array('Location','Message','URL','Line');
$errorVals=array();
foreach($errorVars as $varName)
{
if( !isset($_REQUEST['error'.$varName]) ) return;
$errorVals[$varName] = $_REQUEST['error'.$varName];
}
if( isset($_SERVER['HTTP_USER_AGENT']) )
$errorVars['UserAgent'] = $_SERVER['HTTP_USER_AGENT'];
trigger_error('JavaScript error logged');
}
logJavaScriptError();
*/
$results = array('status'=>'Invalid', 'notice'=>'No valid action specified');
if( isset($_GET['likeMessageToggleToken']) ) {
if( libAuth::likeToggleToken_Valid($_GET['likeMessageToggleToken']) ) {
$token = explode('_', $_GET['likeMessageToggleToken']);
$userID = (int) $token[0];
$likeMessageID = (int) $token[1];
list($likeExists) = $DB->sql_row("SELECT COUNT(*) FROM wD_LikePost WHERE userID = ".$userID." AND likeMessageID = ".$likeMessageID);
if( $likeExists == 0 )
$DB->sql_put("INSERT INTO wD_LikePost ( userID, likeMessageID ) VALUES ( ".$userID.", ".$likeMessageID." )");
else
$DB->sql_put("DELETE FROM wD_LikePost WHERE userID = ".$userID." AND likeMessageID = ".$likeMessageID);
}
}
elseif( isset($_REQUEST['context']) && isset($_REQUEST['contextKey']) && isset($_REQUEST['orderUpdates']) )
{
require_once('board/orders/orderinterface.php');
try
{
$O = OrderInterface::newJSON($_REQUEST['contextKey'], $_REQUEST['context']);
$O->load();
$newReady=$oldReady=$O->orderStatus->Ready;
if( $O->orderStatus->Ready && isset($_REQUEST['notready']) )
$newReady=$O->readyToggle();
$O->set($_REQUEST['orderUpdates']);
$O->validate();
if( !$O->orderStatus->Ready && isset($_REQUEST['ready']) )
$newReady=$O->readyToggle();
$O->writeOrders();
$O->writeOrderStatus();
$DB->sql_put("COMMIT");
$results = $O->getResults();
if( $newReady && !$oldReady )
{
$results['process']='Checked';
$Game = libVariant::$Variant->Game($O->gameID);
if( $Game->processStatus!='Crashed' && $Game->attempts > count($Game->Members->ByID)*2 )
{
$DB->sql_put("COMMIT");
require_once('gamemaster/game.php');
$Game =libVariant::$Variant->processGame($Game->id);
$Game->crashed();
$DB->sql_put("COMMIT");
}
elseif( $Game->needsProcess() )
{
$DB->sql_put("UPDATE wD_Games SET attempts=attempts+1 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
$results['process']='Attempted';
require_once('gamemaster/game.php');
$Game = libVariant::$Variant->processGame($O->gameID);
if( $Game->needsProcess() )
{
$Game->process();
$DB->sql_put("UPDATE wD_Games SET attempts=0 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
$results['process']='Success';
$results['notice']='Game processed, click <a href="board.php?gameID='.$Game->id.'&nocache='.rand(0,1000).'">here</a> to refresh..';
}
}
}
}
catch(Exception $e)
{
if( $e->getMessage() == "Abandoned" || $e->getMessage() == "Cancelled" )
$DB->sql_put("COMMIT");
else
$DB->sql_put("ROLLBACK");
$results = array('invalid'=>true, 'statusIcon'=>'<img src="images/icons/alert.png" alt="Error" title="Error alert" />',
'statusText'=>'', 'notice'=>'Exception: '.$e->getMessage(), 'orders'=>array());
}
}
header('X-JSON: ('.json_encode($results).')');
close();
?>