forked from kestasjk/webDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamemessage.php
executable file
·141 lines (121 loc) · 4.32 KB
/
gamemessage.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
<?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/>.
*/
defined('IN_CODE') or die('This script can not be run by itself.');
/**
* Send a message to a member of a countryID in a game, from another member. Used by GameMaster in processGame, and
* Chatbox
*
* @package Base
* @subpackage Game
*/
class libGameMessage
{
/**
* Send a game message. Messages are sanitized
*
* @param string $toCountryID The countryID being sent to. 'Global' sends to all.
* @param string $fromCountryID The county being sent from. 'GameMaster' can also be used.
* @param string|array $message The message(s) to be sent (Can be an array of messages for)
* @param int[optional] $gameID The game ID to use. If not given the current global Game is sent to.
*
* @return int $timeSent The time of this message in the DB.
*/
static public function send($toCountryID, $fromCountryID, $message, $gameID=-1)
{
global $DB, $Game, $MC;
if ( ! is_object($Game) )
{
$Variant=libVariant::loadFromGameID($gameID);
$Game = $Variant->Game($gameID);
}
$message = $DB->msg_escape($message);
if ( !is_numeric($toCountryID) )
$toCountryID=0;
if ( !is_numeric($fromCountryID) )
{
$message = '<strong>'.$fromCountryID.':</strong> '.$message;
$fromCountryID=0;
}
if( 65000 < strlen($message) )
{
throw new Exception(l_t("Message too long"));
}
$timeSent = time();
if ($toCountryID == 0) {
$MC->set("lastmsgtime_{$Game->id}_0", $timeSent); // spectators
foreach($Game->Members->ByCountryID as $countryID => $member) {
$MC->set("lastmsgtime_{$Game->id}_{$countryID}", $timeSent);
}
} else {
$MC->set("lastmsgtime_{$Game->id}_{$fromCountryID}", $timeSent);
$MC->set("lastmsgtime_{$Game->id}_{$toCountryID}", $timeSent);
}
$DB->sql_put("INSERT INTO wD_GameMessages
(gameID, toCountryID, fromCountryID, turn, message, phaseMarker, timeSent)
VALUES(".$Game->id.",
".$toCountryID.",
".$fromCountryID.",
".$Game->turn.",
'".$message."',
'".$Game->phase."',
".$timeSent.")");
if ($toCountryID != $fromCountryID || $fromCountryID == 0)
{
libGameMessage::notify($toCountryID, $fromCountryID);
}
require_once('lib/pusher.php');
$channel = "private-game" . $Game->id . "-country";
if ($toCountryID == 0) {
foreach($Game->Members->ByCountryID as $countryID => $member) {
libPusher::trigger($channel . $countryID, 'message', 'messageSent');
}
} else {
$channel = $channel . $toCountryID;
libPusher::trigger($channel, 'message', 'messageSent');
}
return $timeSent;
}
/**
* Notify a countryID that you sent them a message, uses the global Game
*
* @param string $toCountryID The countryID sent to, can be 'Global'
* @param string $fromCountryID The countryID sent from
* @param Game $Game The game being referred to
*/
private static function notify($toCountryID, $fromCountryID)
{
global $DB, $Game;
$DB->sql_put("COMMIT"); // Prevent deadlocks
if ( $toCountryID == 0 )
{
$DB->sql_put("UPDATE wD_Members
SET newMessagesFrom = IF( (newMessagesFrom+0) = 0,
'0',
CONCAT_WS(',',newMessagesFrom,'0') )
WHERE gameID = ".$Game->id." AND NOT countryID=".$fromCountryID);
}
else
{
$DB->sql_put("UPDATE wD_Members
SET newMessagesFrom = IF( (newMessagesFrom+0) = 0,
'".$fromCountryID."',
CONCAT_WS(',',newMessagesFrom,'".$fromCountryID."') )
WHERE gameID = ".$Game->id." AND countryID=".$toCountryID);
}
$DB->sql_put("COMMIT");
}
}
?>