Freeciv21
Develop your civilization from humble roots to a global empire
api_server_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 /* common/scriptcore */
12 #include "luascript.h"
13 
14 // server
15 #include "score.h"
16 #include "settings.h"
17 #include "srv_main.h"
18 
19 /* server/sqavegame */
20 #include "savemain.h"
21 
22 /* server/scripting */
23 #include "script_server.h"
24 
25 #include "api_server_base.h"
26 
30 int api_server_player_civilization_score(lua_State *L, Player *pplayer)
31 {
33  LUASCRIPT_CHECK_SELF(L, pplayer, 0);
34 
35  return get_civ_score(pplayer);
36 }
37 
41 bool api_server_was_started(lua_State *L)
42 {
43  LUASCRIPT_CHECK_STATE(L, false);
44 
45  return game_was_started();
46 }
47 
51 bool api_server_save(lua_State *L, const char *filename)
52 {
53  LUASCRIPT_CHECK_STATE(L, false);
54 
55  // Limit the allowed characters in the filename.
56  if (filename != nullptr && !is_safe_filename(filename)) {
57  return false;
58  }
59 
60  save_game(filename, "User request (Lua)", false);
61 
62  return true;
63 }
64 
68 bool api_play_music(lua_State *L, Player *pplayer, const char *tag)
69 {
70  struct packet_play_music p;
71 
72  LUASCRIPT_CHECK_STATE(L, false);
73  LUASCRIPT_CHECK_SELF(L, pplayer, false);
74  LUASCRIPT_CHECK_ARG_NIL(L, tag, 3, API_TYPE_STRING, false);
75 
76  sz_strlcpy(p.tag, tag);
77 
78  lsend_packet_play_music(pplayer->connections, &p);
79 
80  return true;
81 }
82 
87 const char *api_server_setting_get(lua_State *L, const char *sett_name)
88 {
89  struct setting *pset;
90  static char buf[512];
91 
92  LUASCRIPT_CHECK_STATE(L, nullptr);
93  LUASCRIPT_CHECK_ARG_NIL(L, sett_name, 2, API_TYPE_STRING, nullptr);
94 
95  pset = setting_by_name(sett_name);
96 
97  if (!pset) {
98  return nullptr;
99  }
100 
101  return setting_value_name(pset, false, buf, sizeof(buf));
102 }
bool api_server_was_started(lua_State *L)
Returns TRUE if the game was started.
const char * api_server_setting_get(lua_State *L, const char *sett_name)
Return the formated value of the setting or nullptr if no such setting exists,.
int api_server_player_civilization_score(lua_State *L, Player *pplayer)
Return the civilization score (total) for player.
bool api_play_music(lua_State *L, Player *pplayer, const char *tag)
Play music track for player.
bool api_server_save(lua_State *L, const char *filename)
Save the game (a manual save is triggered).
#define LUASCRIPT_CHECK_STATE(L,...)
Definition: luascript.h:110
#define LUASCRIPT_CHECK_SELF(L, value,...)
Definition: luascript.h:139
#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type,...)
Definition: luascript.h:131
void save_game(const char *orig_filename, const char *save_reason, bool scenario)
Unconditionally save the game, with specified filename.
Definition: savemain.cpp:139
int get_civ_score(const struct player *pplayer)
Return the civilization score (a numerical value) for the player.
Definition: score.cpp:380
struct setting * setting_by_name(const char *name)
Returns the setting to the given name.
Definition: settings.cpp:3039
const char * setting_value_name(const struct setting *pset, bool pretty, char *buf, size_t buf_len)
Compute the name of the current value of the setting.
Definition: settings.cpp:3946
bool is_safe_filename(const QString &name)
Check if the name is safe security-wise.
Definition: shared.cpp:210
bool game_was_started()
Returns iff the game was started once upon a time.
Definition: srv_main.cpp:251
Definition: player.h:231
struct conn_list * connections
Definition: player.h:280
#define sz_strlcpy(dest, src)
Definition: support.h:140