aboutsummaryrefslogtreecommitdiffstats
path: root/inc/messages.inc
blob: 8583dc0d4f89552a5e28f5d1e3802e780c759e0b (plain)
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
<?php
/**
 * Server to client error/info messages list and helpers.
 *
 * @package inc\messages.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

/**
 * Messages list.
 */
$messages = array(
	"shop_already_built" => "You have already built a shop.",
	"gold_insufficient" => "You don't have enough gold.",
	"shop_missing_item" => "This item does not exist.",
	"guild_not_yet_created" => "You need to create a guild first.",
	"guild_already_built" => "You have aready built a guild.",
	"dungeon_already_available" => "You can already access the dungeon",

	"gamesave_ok" => "Game saved.",
	"gamesave_error" => "An error occured when trying to save the game.",
	"gamesave_not_found" => "Couldn't find the specified save file.",
	"gamesave_delete_fail" => "Couldn't delete the specified save file.",
	"gamesave_delete_success" => "Game save successfully removed from server",

	"upload_fail" => "Could not upload save file.",
	"upload_success" => "Save file uploaded successfully: %s",
);

/**
 * Sends a message to the client.
 *
 * @param string $type  message type, for example "info" or "error"
 * @param string $msg   message content
 * @param string $fmt   optional parameters to apply when formating message string
 * @return void
 */
function sendMessage($type, $msg, $fmt = null) {
	global $messages;
	$text = $messages[$msg];
	if($fmt) $text = vsprintf($text, $fmt);
	echo json_encode(array($type => $text));
}

/**
 * Sends an error message to the client.
 * Simple wrapper calling sendMessage with "error" as $type.
 *
 * @param string $msg   message content
 * @param string $fmt   optional parameters to apply when formating message string
 * @return void
 */
function sendError($msg, $fmt = null) {
	sendMessage("error", $msg, $fmt);
}

/**
 * Sends an info message to the client.
 * Simple wrapper calling sendMessage with "info" as $type.
 *
 * @param string $msg   message content
 * @param string $fmt   optional parameters to apply when formating message string
 * @return void
 */
function sendInfo($msg, $fmt = null) {
	sendMessage("info", $msg, $fmt);
}

?>