New Lua events implemented:
handle_game_create, handle_game_report, handle_game_end, handle_game_destroy, handle_game_changestatus, handle_user_joingame, handle_user_leftgame, New Lua function: get_account
This commit is contained in:
parent
4ea106be50
commit
8438259077
8 changed files with 510 additions and 116 deletions
|
@ -81,16 +81,25 @@ namespace pvpgn
|
|||
register unsigned int h;
|
||||
register unsigned int len = std::strlen(username);
|
||||
|
||||
int c;
|
||||
for (h = 5381; len > 0; --len, ++username) {
|
||||
h += h << 5;
|
||||
if (std::isupper((int)*username) == 0)
|
||||
|
||||
c = (int)*username;
|
||||
// FIXME: (HarpyWar) I add this condition because if we call connlist_find_connection_by_accountname
|
||||
// with wrong account name, then it fails on std::isupper(c)
|
||||
if (c < -1 || c > 255)
|
||||
break;
|
||||
|
||||
if (std::isupper(c) == 0)
|
||||
h ^= *username;
|
||||
else
|
||||
h ^= std::tolower((int)*username);
|
||||
h ^= std::tolower(c);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static t_account * account_create(char const * username, char const * passhash1)
|
||||
{
|
||||
t_account * account;
|
||||
|
|
|
@ -76,6 +76,10 @@
|
|||
#include "common/setup_after.h"
|
||||
#include "icons.h"
|
||||
|
||||
#ifdef WITH_LUA
|
||||
#include "luainterface.h"
|
||||
#endif
|
||||
|
||||
namespace pvpgn
|
||||
{
|
||||
|
||||
|
@ -2185,6 +2189,7 @@ namespace pvpgn
|
|||
if (!(c->protocol.game = gamelist_find_game_available(gamename, c->protocol.client.clienttag, type))
|
||||
&& !gamelist_find_game_available(gamename, c->protocol.client.clienttag, game_type_all)) {
|
||||
/* do not allow creation of games with same name of same clienttag when game is not started or done */
|
||||
// create game with initial values
|
||||
c->protocol.game = game_create(gamename, gamepass, gameinfo, type, version, c->protocol.client.clienttag, conn_get_gameversion(c));
|
||||
|
||||
if (c->protocol.game && conn_get_realm(c) && conn_get_charname(c)) {
|
||||
|
@ -2195,11 +2200,18 @@ namespace pvpgn
|
|||
}
|
||||
|
||||
if (c->protocol.game) {
|
||||
// add new player to the game
|
||||
if (game_add_player(conn_get_game(c), gamepass, version, c) < 0) {
|
||||
c->protocol.game = NULL; // bad password or version #
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef WITH_LUA
|
||||
// handle game create when it's owner joins the game
|
||||
if (c == game_get_owner(c->protocol.game))
|
||||
lua_handle_game(c->protocol.game, luaevent_game_create);
|
||||
#endif
|
||||
|
||||
if (game_is_ladder(c->protocol.game)) {
|
||||
if (c == game_get_owner(c->protocol.game))
|
||||
message_send_text(c, message_type_info, c, "Created ladder game");
|
||||
|
@ -2207,6 +2219,8 @@ namespace pvpgn
|
|||
message_send_text(c, message_type_info, c, "Joined ladder game");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else c->protocol.game = NULL;
|
||||
|
||||
|
|
|
@ -42,6 +42,9 @@
|
|||
#include "game_conv.h"
|
||||
#include "common/setup_after.h"
|
||||
|
||||
#ifdef WITH_LUA
|
||||
#include "luainterface.h"
|
||||
#endif
|
||||
namespace pvpgn
|
||||
{
|
||||
|
||||
|
@ -471,6 +474,10 @@ namespace pvpgn
|
|||
eventlog(eventlog_level_error, __FUNCTION__, "got NULL game");
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_game(game, luaevent_game_destroy);
|
||||
#endif
|
||||
|
||||
elist_del(&game->glist_link);
|
||||
glist_length--;
|
||||
|
@ -736,6 +743,10 @@ namespace pvpgn
|
|||
return -1;
|
||||
}
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_game(game, luaevent_game_end);
|
||||
#endif
|
||||
|
||||
if (game->clienttag == CLIENTTAG_WARCRAFT3_UINT || game->clienttag == CLIENTTAG_WAR3XP_UINT)
|
||||
// war3 game reporting is done elsewhere, so we can skip this function
|
||||
return 0;
|
||||
|
@ -1098,6 +1109,10 @@ namespace pvpgn
|
|||
}
|
||||
|
||||
std::fprintf(fp, "\nThis game lasted %lu minutes (elapsed).\n", ((unsigned long int)std::difftime(now, game->start_time)) / 60);
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_game(game, luaevent_game_report);
|
||||
#endif
|
||||
|
||||
if (std::fclose(fp) < 0)
|
||||
{
|
||||
|
@ -1437,6 +1452,10 @@ namespace pvpgn
|
|||
if (status == game_status_started && game->start_time == (std::time_t)0)
|
||||
game->start_time = now;
|
||||
game->status = status;
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_game(game, luaevent_game_changestatus);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -1730,6 +1749,9 @@ namespace pvpgn
|
|||
|
||||
game_choose_host(game);
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_user(c, game, luaevent_user_leftgame);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -76,7 +76,9 @@
|
|||
#include <win32/winmain.h>
|
||||
#endif
|
||||
#include "common/setup_after.h"
|
||||
|
||||
#ifdef WITH_LUA
|
||||
#include "luainterface.h"
|
||||
#endif
|
||||
namespace pvpgn
|
||||
{
|
||||
|
||||
|
@ -3793,6 +3795,10 @@ namespace pvpgn
|
|||
else
|
||||
eventlog(eventlog_level_info, __FUNCTION__, "[%d] \"%s\" joined game \"%s\"", conn_get_socket(c), conn_get_username(c), gamename);
|
||||
|
||||
#ifdef WITH_LUA
|
||||
lua_handle_user(c, game, luaevent_user_joingame);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -4029,6 +4035,7 @@ namespace pvpgn
|
|||
else {
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "[%d] unknown startgame4 status %d (clienttag: %s)", conn_get_socket(c), status, clienttag_uint_to_str(conn_get_clienttag(c)));
|
||||
}
|
||||
|
||||
}
|
||||
else if ((status & CLIENT_STARTGAME4_STATUSMASK_INIT_VALID) == status) {
|
||||
/*valid creation status would be:
|
||||
|
@ -4041,11 +4048,13 @@ namespace pvpgn
|
|||
gtype = bngtype_to_gtype(conn_get_clienttag(c), bngtype);
|
||||
if ((gtype == game_type_ladder && account_get_auth_createladdergame(conn_get_account(c)) == 0) || (gtype != game_type_ladder && account_get_auth_createnormalgame(conn_get_account(c)) == 0))
|
||||
eventlog(eventlog_level_info, __FUNCTION__, "[%d] game start for \"%s\" refused (no authority)", conn_get_socket(c), conn_get_username(c));
|
||||
else {
|
||||
else
|
||||
{
|
||||
//find is there any existing game with same name and allow the host to create game
|
||||
// with same name only when another game is already started or already done
|
||||
if ((!(game = gamelist_find_game_available(gamename, conn_get_clienttag(c), game_type_all))) &&
|
||||
(conn_set_game(c, gamename, gamepass, gameinfo, gtype, STARTVER_GW4) == 0)) {
|
||||
(conn_set_game(c, gamename, gamepass, gameinfo, gtype, STARTVER_GW4) == 0))
|
||||
{
|
||||
game_set_option(conn_get_game(c), bngoption_to_goption(conn_get_clienttag(c), gtype, option));
|
||||
if (status & CLIENT_STARTGAME4_STATUS_PRIVATE)
|
||||
game_set_flag(conn_get_game(c), game_flag_private);
|
||||
|
@ -4054,6 +4063,7 @@ namespace pvpgn
|
|||
if (bngtype == CLIENT_GAMELISTREQ_LOADED) /* PELISH: seems strange but it is really needed for loaded games */
|
||||
game_set_status(conn_get_game(c), game_status_loaded);
|
||||
//FIXME: still need special handling for status disc-is-loss and replay
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
#define GAME_INTERNAL_ACCESS
|
||||
|
||||
#include "common/setup_before.h"
|
||||
#include "command.h"
|
||||
|
||||
|
@ -23,6 +25,8 @@
|
|||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
|
||||
#include "compat/strcasecmp.h"
|
||||
#include "compat/snprintf.h"
|
||||
#include "common/tag.h"
|
||||
|
@ -71,8 +75,8 @@
|
|||
|
||||
#include "attrlayer.h"
|
||||
|
||||
|
||||
#include "luawrapper.h"
|
||||
#include "luainterface.h"
|
||||
|
||||
namespace pvpgn
|
||||
{
|
||||
|
@ -83,34 +87,53 @@ namespace pvpgn
|
|||
|
||||
void _register_functions();
|
||||
|
||||
std::map<std::string, std::string> get_account_object(const char *username);
|
||||
std::map<std::string, std::string> get_account_object(t_connection * c);
|
||||
std::map<std::string, std::string> get_game_object(t_game * game);
|
||||
|
||||
|
||||
template <class T, class A>
|
||||
T join(const A &begin, const A &end, const T &t);
|
||||
|
||||
int _sum(lua_State* L);
|
||||
int __message_send_text(lua_State* L);
|
||||
int __eventlog(lua_State* L);
|
||||
int __get_account(lua_State* L);
|
||||
|
||||
char xmsgtemp[MAX_MESSAGE_LEN];
|
||||
char xmsgtemp2[MAX_MESSAGE_LEN];
|
||||
char _msgtemp[MAX_MESSAGE_LEN];
|
||||
char _msgtemp2[MAX_MESSAGE_LEN];
|
||||
|
||||
|
||||
/* Reload all the lua scripts */
|
||||
/* Unload all the lua scripts */
|
||||
extern void lua_unload()
|
||||
{
|
||||
|
||||
// nothing to do, "vm.initialize()" already destroys lua vm before initialize
|
||||
}
|
||||
|
||||
/* Initialize lua, register functions and load scripts */
|
||||
extern void lua_load(char const * scriptdir)
|
||||
{
|
||||
eventlog(eventlog_level_info, __FUNCTION__, "Loading Lua interface...");
|
||||
|
||||
try
|
||||
{
|
||||
// initialize
|
||||
vm.initialize();
|
||||
|
||||
std::vector<std::string> files = dir_getfiles(std::string(scriptdir), ".lua", true);
|
||||
|
||||
|
||||
// load all files from the script directory
|
||||
for (int i = 0; i < files.size(); ++i)
|
||||
{
|
||||
vm.load_file(files[i].c_str());
|
||||
|
||||
snprintf(_msgtemp, sizeof(_msgtemp), "%s", files[i].c_str());
|
||||
eventlog(eventlog_level_info, __FUNCTION__, _msgtemp);
|
||||
}
|
||||
|
||||
_register_functions();
|
||||
|
||||
snprintf(_msgtemp, sizeof(_msgtemp), "Lua sripts were successfully loaded (%u files)", files.size());
|
||||
eventlog(eventlog_level_info, __FUNCTION__, _msgtemp);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
|
@ -122,60 +145,220 @@ namespace pvpgn
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/* Register C++ functions to be able use them from lua scripts */
|
||||
void _register_functions()
|
||||
{
|
||||
// global variables
|
||||
lua::table g(vm);
|
||||
g.update("PVPGN_SOFTWARE", PVPGN_SOFTWARE);
|
||||
g.update("PVPGN_VERSION", PVPGN_VERSION);
|
||||
|
||||
// register CFunction
|
||||
vm.reg("sum", _sum);
|
||||
vm.reg("message_send_text", __message_send_text);
|
||||
vm.reg("eventlog", __eventlog);
|
||||
vm.reg("get_account", __get_account); // FIXME:
|
||||
|
||||
|
||||
// register package 'event'
|
||||
static const luaL_Reg event[] =
|
||||
{
|
||||
{ 0, 0 }
|
||||
};
|
||||
vm.reg("event", event);
|
||||
}
|
||||
|
||||
/* Lua Events */
|
||||
|
||||
/* Lua Events (called from scripts) */
|
||||
#ifndef _LUA_EVENTS_
|
||||
|
||||
extern int lua_handle_command(t_connection * c, char const * text)
|
||||
{
|
||||
unsigned int sessionkey; // FIXME: unsigned int
|
||||
int result;
|
||||
|
||||
sessionkey = conn_get_sessionkey(c);
|
||||
|
||||
lua::transaction(vm) << lua::lookup("handle_command") << sessionkey << text << lua::invoke >> result << lua::end;
|
||||
|
||||
int result = 0;
|
||||
std::map<std::string, std::string> o_account = get_account_object(c);
|
||||
try
|
||||
{
|
||||
// invoke lua method
|
||||
lua::transaction(vm) << lua::lookup("handle_command") << o_account << text << lua::invoke >> result << lua::end;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
extern void lua_handle_game(t_game * game, t_luaevent_type luaevent)
|
||||
{
|
||||
const char * func_name;
|
||||
switch (luaevent)
|
||||
{
|
||||
case luaevent_game_create:
|
||||
func_name = "handle_game_create";
|
||||
break;
|
||||
case luaevent_game_report:
|
||||
func_name = "handle_game_report";
|
||||
break;
|
||||
case luaevent_game_end:
|
||||
func_name = "handle_game_end";
|
||||
break;
|
||||
case luaevent_game_destroy:
|
||||
func_name = "handle_game_destroy";
|
||||
break;
|
||||
case luaevent_game_changestatus:
|
||||
func_name = "handle_game_changestatus";
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::map<std::string, std::string> o_game = get_game_object(game);
|
||||
lua::transaction(vm) << lua::lookup(func_name) << o_game << lua::invoke << lua::end;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
}
|
||||
|
||||
extern void lua_handle_user(t_connection * c, t_game * game, t_luaevent_type luaevent)
|
||||
{
|
||||
const char * func_name;
|
||||
switch (luaevent)
|
||||
{
|
||||
case luaevent_user_joingame:
|
||||
func_name = "handle_user_joingame";
|
||||
break;
|
||||
|
||||
case luaevent_user_leftgame:
|
||||
func_name = "handle_user_leftgame";
|
||||
break;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> o_account = get_account_object(c);
|
||||
std::map<std::string, std::string> o_game = get_game_object(game);
|
||||
try
|
||||
{
|
||||
lua::transaction(vm) << lua::lookup("handle_user_leftgame") << o_account << o_game << lua::invoke << lua::end;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* --- Lua Functions (called from scripts) */
|
||||
/* --- Lua Functions (called from scripts) */
|
||||
#ifndef _LUA_FUNCTIONS_
|
||||
|
||||
/* Send message text to user */
|
||||
int __message_send_text(lua_State* L)
|
||||
{
|
||||
lua::stack st(L);
|
||||
unsigned int sessionkey_src, sessionkey_dst;
|
||||
const char *text;
|
||||
const char *username_src, *username_dst;
|
||||
int message_type;
|
||||
t_connection *c_src, *c_dst;
|
||||
t_connection *c_src = NULL, *c_dst = NULL;
|
||||
|
||||
// get vars
|
||||
st.at(1, sessionkey_dst);
|
||||
st.at(2, message_type);
|
||||
st.at(3, sessionkey_src);
|
||||
st.at(4, text);
|
||||
try
|
||||
{
|
||||
lua::stack st(L);
|
||||
// get args
|
||||
st.at(1, username_dst);
|
||||
st.at(2, message_type);
|
||||
st.at(3, username_src);
|
||||
st.at(4, text);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
|
||||
// get user connections
|
||||
c_dst = connlist_find_connection_by_sessionkey(sessionkey_dst);
|
||||
c_src = connlist_find_connection_by_sessionkey(sessionkey_src);
|
||||
if (t_account * account = accountlist_find_account(username_dst))
|
||||
c_dst = account_get_conn(account);
|
||||
|
||||
if (username_src)
|
||||
if (t_account * account = accountlist_find_account(username_src))
|
||||
c_src = account_get_conn(account);
|
||||
|
||||
// send message
|
||||
message_send_text(c_dst, (t_message_type)message_type, c_src, text);
|
||||
// (source can be NULL, but destination cant)
|
||||
if (c_dst)
|
||||
message_send_text(c_dst, (t_message_type)message_type, c_src, text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Log text into logfile */
|
||||
int __eventlog(lua_State* L)
|
||||
{
|
||||
int loglevel;
|
||||
const char *text, *function;
|
||||
|
||||
try
|
||||
{
|
||||
lua::stack st(L);
|
||||
// get args
|
||||
st.at(1, loglevel);
|
||||
st.at(2, function);
|
||||
st.at(3, text);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
eventlog(t_eventlog_level(loglevel), function, text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Log text into logfile */
|
||||
int __get_account(lua_State* L)
|
||||
{
|
||||
const char *username;
|
||||
std::map<std::string, std::string> o_account;
|
||||
|
||||
try
|
||||
{
|
||||
lua::stack st(L);
|
||||
// get args
|
||||
st.at(1, username);
|
||||
o_account = get_account_object(username);
|
||||
|
||||
st.push(o_account);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int _sum(lua_State* L)
|
||||
|
@ -194,5 +377,134 @@ namespace pvpgn
|
|||
|
||||
#endif
|
||||
|
||||
|
||||
std::map<std::string, std::string> get_account_object(const char *username)
|
||||
{
|
||||
std::map<std::string, std::string> o_account;
|
||||
if (t_connection *c = connlist_find_connection_by_accountname(username))
|
||||
o_account = get_account_object(c);
|
||||
|
||||
return o_account;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> get_account_object(t_connection * c)
|
||||
{
|
||||
std::map<std::string, std::string> o_account;
|
||||
|
||||
t_account * account = conn_get_account(c);
|
||||
if (!account)
|
||||
return o_account;
|
||||
|
||||
o_account["id"] = std::to_string(account_get_uid(account));
|
||||
o_account["name"] = account_get_name(account);
|
||||
o_account["email"] = account_get_email(account);
|
||||
o_account["commandgroups"] = std::to_string(account_get_command_groups(account));
|
||||
o_account["locked"] = account_get_auth_lock(account) ? "true" : "false";
|
||||
o_account["muted"] = account_get_auth_mute(account) ? "true" : "false";
|
||||
o_account["country"] = conn_get_country(c);
|
||||
o_account["clientver"] = conn_get_clientver(c);
|
||||
o_account["latency"] = std::to_string(conn_get_latency(c));
|
||||
if (t_clienttag clienttag = conn_get_clienttag(c))
|
||||
o_account["clienttag"] = clienttag_uint_to_str(clienttag);
|
||||
if (t_game *game = conn_get_game(c))
|
||||
o_account["game_id"] = std::to_string(game_get_id(game));
|
||||
if (t_channel *channel = conn_get_channel(c))
|
||||
o_account["channel_id"] = std::to_string(channel_get_channelid(channel));
|
||||
|
||||
return o_account;
|
||||
}
|
||||
|
||||
|
||||
std::map<std::string, std::string> get_game_object(t_game * game)
|
||||
{
|
||||
std::map<std::string, std::string> o_game;
|
||||
|
||||
if (!game)
|
||||
return o_game;
|
||||
|
||||
o_game["id"] = std::to_string(game->id);
|
||||
o_game["name"] = game->name;
|
||||
o_game["pass"] = game->pass;
|
||||
o_game["info"] = game->info;
|
||||
o_game["type"] = std::to_string(game->type);
|
||||
o_game["flag"] = std::to_string(game->flag);
|
||||
|
||||
o_game["address"] = addr_num_to_ip_str(game->addr);
|
||||
o_game["port"] = std::to_string(game->port);
|
||||
o_game["status"] = std::to_string(game->status);
|
||||
o_game["currentplayers"] = std::to_string(game->ref);
|
||||
o_game["totalplayers"] = std::to_string(game->count);
|
||||
o_game["maxplayers"] = std::to_string(game->maxplayers);
|
||||
o_game["mapname"] = game->mapname;
|
||||
o_game["option"] = std::to_string(game->option);
|
||||
o_game["maptype"] = std::to_string(game->maptype);
|
||||
o_game["tileset"] = std::to_string(game->tileset);
|
||||
o_game["speed"] = std::to_string(game->speed);
|
||||
o_game["mapsize_x"] = std::to_string(game->mapsize_x);
|
||||
o_game["mapsize_y"] = std::to_string(game->mapsize_y);
|
||||
if (t_connection *c = game->owner)
|
||||
{
|
||||
if (t_account *account = conn_get_account(c))
|
||||
o_game["owner"] = account_get_name(account);
|
||||
}
|
||||
|
||||
std::vector<std::string> players;
|
||||
for (int i = 0; i < game->ref; i++)
|
||||
{
|
||||
if (t_account *account = game->players[i])
|
||||
players.push_back(account_get_name(account));
|
||||
}
|
||||
o_game["players"] = join(players.begin(), players.end(), std::string(","));
|
||||
|
||||
|
||||
o_game["bad"] = std::to_string(game->bad); // if 1, then the results will be ignored
|
||||
|
||||
std::vector<std::string> results;
|
||||
if (game->results)
|
||||
{
|
||||
for (int i = 0; i < game->count; i++)
|
||||
results.push_back(std::to_string(game->results[i]));
|
||||
}
|
||||
o_game["results"] = join(results.begin(), results.end(), std::string(","));
|
||||
// UNDONE: add report_heads and report_bodies: they are XML strings
|
||||
|
||||
o_game["create_time"] = std::to_string(game->create_time);
|
||||
o_game["start_time"] = std::to_string(game->start_time);
|
||||
o_game["lastaccess_time"] = std::to_string(game->lastaccess_time);
|
||||
|
||||
o_game["difficulty"] = std::to_string(game->difficulty);
|
||||
o_game["version"] = vernum_to_verstr(game->version);
|
||||
o_game["startver"] = std::to_string(game->startver);
|
||||
|
||||
if (t_clienttag clienttag = game->clienttag)
|
||||
o_game["clienttag"] = clienttag_uint_to_str(clienttag);
|
||||
|
||||
|
||||
if (game->description)
|
||||
o_game["description"] = game->description;
|
||||
if (game->realmname)
|
||||
o_game["realmname"] = game->realmname;
|
||||
|
||||
return o_game;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Join two vector objects to string by delimeter */
|
||||
template <class T, class A>
|
||||
T join(const A &begin, const A &end, const T &t)
|
||||
{
|
||||
T result;
|
||||
for (A it = begin;
|
||||
it != end;
|
||||
it++)
|
||||
{
|
||||
if (!result.empty())
|
||||
result.append(t);
|
||||
result.append(*it);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,12 +39,39 @@ namespace pvpgn
|
|||
|
||||
namespace bnetd
|
||||
{
|
||||
typedef enum {
|
||||
luaevent_game_create,
|
||||
luaevent_game_report,
|
||||
luaevent_game_end,
|
||||
luaevent_game_destroy,
|
||||
luaevent_game_changestatus,
|
||||
|
||||
luaevent_channel_create,
|
||||
luaevent_channel_destroy,
|
||||
|
||||
luaevent_user_joingame,
|
||||
luaevent_user_leftgame,
|
||||
luaevent_user_joinchannel,
|
||||
luaevent_user_leftchannel,
|
||||
|
||||
luaevent_user_message, // user-to-channel
|
||||
luaevent_user_whisper, // user-to-user
|
||||
luaevent_user_login,
|
||||
luaevent_user_disconnect
|
||||
|
||||
} t_luaevent_type;
|
||||
|
||||
|
||||
extern void lua_load(char const * scriptdir);
|
||||
extern void lua_unload();
|
||||
|
||||
extern int lua_handle_command(t_connection * c, char const * text);
|
||||
extern void lua_handle_game(t_game * game, t_luaevent_type luaevent); // game events
|
||||
extern void lua_handle_user(t_connection * c, t_game * game, t_luaevent_type luaevent); //
|
||||
//extern void lua_handle_user(t_connection * c, t_connection * c_dst, const char * text, t_luaevent_type luaevent);
|
||||
//extern void lua_handle_user(t_connection * c, t_channel * channel, const char * text, t_luaevent_type luaevent);
|
||||
//extern void lua_handle_user(t_connection * c, t_channel * channel, t_luaevent_type luaevent);
|
||||
//extern void lua_handle_user(t_connection * c, t_luaevent_type luaevent);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,138 +1,138 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) Anton Burdinuk
|
||||
/*
|
||||
* Copyright (C) Anton Burdinuk
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "luawrapper.h"
|
||||
|
||||
namespace lua
|
||||
{
|
||||
invoke_type invoke;
|
||||
m_invoke_type m_invoke;
|
||||
release_type end;
|
||||
invoke_type invoke;
|
||||
m_invoke_type m_invoke;
|
||||
release_type end;
|
||||
}
|
||||
|
||||
|
||||
void lua::throw_lua_exception(lua_State* st,const std::string& addinfo) throw(std::exception)
|
||||
void lua::throw_lua_exception(lua_State* st, const std::string& addinfo) throw(std::exception)
|
||||
{
|
||||
size_t len=0;
|
||||
|
||||
const char* p=lua_tolstring(st,-1,&len);
|
||||
|
||||
std::string s(p,len);
|
||||
|
||||
if(addinfo.length())
|
||||
{
|
||||
s.append(" ",1);
|
||||
s.append(addinfo);
|
||||
}
|
||||
size_t len = 0;
|
||||
|
||||
lua_pop(st,1);
|
||||
|
||||
throw(exception(s));
|
||||
const char* p = lua_tolstring(st, -1, &len);
|
||||
|
||||
std::string s(p, len);
|
||||
|
||||
if (addinfo.length())
|
||||
{
|
||||
s.append(" ", 1);
|
||||
s.append(addinfo);
|
||||
}
|
||||
|
||||
lua_pop(st, 1);
|
||||
|
||||
throw(exception(s));
|
||||
}
|
||||
|
||||
void lua::vm::initialize(void) throw(std::exception)
|
||||
{
|
||||
done();
|
||||
done();
|
||||
|
||||
st=lua_open();
|
||||
st = lua_open();
|
||||
|
||||
if(!st)
|
||||
throw(exception("can`t create lua virtual machine instance"));
|
||||
if (!st)
|
||||
throw(exception("can`t create lua virtual machine instance"));
|
||||
|
||||
luaL_openlibs(st);
|
||||
luaL_openlibs(st);
|
||||
}
|
||||
|
||||
void lua::vm::load_file(const char* file) throw(std::exception)
|
||||
{
|
||||
if(!st)
|
||||
throw(exception("lua virtual machine is not ready"));
|
||||
|
||||
if(luaL_loadfile(st,file) || lua_pcall(st,0,0,0))
|
||||
throw_lua_exception(st);
|
||||
if (!st)
|
||||
throw(exception("lua virtual machine is not ready"));
|
||||
|
||||
if (luaL_loadfile(st, file) || lua_pcall(st, 0, 0, 0))
|
||||
throw_lua_exception(st);
|
||||
}
|
||||
|
||||
|
||||
void lua::vm::eval(const std::string& stmt,int offset) throw(std::exception)
|
||||
void lua::vm::eval(const std::string& stmt, int offset) throw(std::exception)
|
||||
{
|
||||
enum {max_chunk_name_len=64};
|
||||
enum { max_chunk_name_len = 64 };
|
||||
|
||||
if(offset>stmt.length())
|
||||
offset=stmt.length();
|
||||
if (offset > stmt.length())
|
||||
offset = stmt.length();
|
||||
|
||||
const char* p=stmt.c_str()+offset;
|
||||
const char* p = stmt.c_str() + offset;
|
||||
|
||||
int n=stmt.length()-offset;
|
||||
int n = stmt.length() - offset;
|
||||
|
||||
if(luaL_loadbuffer(st,p,n,n>max_chunk_name_len?(std::string(p,max_chunk_name_len)+"...").c_str():p) || lua_pcall(st,0,0,0))
|
||||
throw_lua_exception(st);
|
||||
if (luaL_loadbuffer(st, p, n, n > max_chunk_name_len ? (std::string(p, max_chunk_name_len) + "...").c_str() : p) || lua_pcall(st, 0, 0, 0))
|
||||
throw_lua_exception(st);
|
||||
}
|
||||
|
||||
|
||||
void lua::bind::lookup(const char* name) throw()
|
||||
{
|
||||
if(!refuse)
|
||||
{
|
||||
if(lua_type(st,cur_index)!=LUA_TNIL)
|
||||
if (!refuse)
|
||||
{
|
||||
lua_getfield(st,cur_index,name);
|
||||
cur_index=size();
|
||||
if (lua_type(st, cur_index) != LUA_TNIL)
|
||||
{
|
||||
lua_getfield(st, cur_index, name);
|
||||
cur_index = size();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lua::bind::end(void) throw()
|
||||
{
|
||||
if(!refuse && mutex)
|
||||
{
|
||||
int n=size()-st_top;
|
||||
if(n>0)
|
||||
lua_pop(st,n);
|
||||
if (!refuse && mutex)
|
||||
{
|
||||
int n = size() - st_top;
|
||||
if (n > 0)
|
||||
lua_pop(st, n);
|
||||
|
||||
mutex=0;
|
||||
|
||||
args_number=retvals_number=0;
|
||||
cur_index=LUA_GLOBALSINDEX;
|
||||
}
|
||||
mutex = 0;
|
||||
|
||||
args_number = retvals_number = 0;
|
||||
cur_index = LUA_GLOBALSINDEX;
|
||||
}
|
||||
}
|
||||
|
||||
void lua::bind::invoke(void) throw(std::exception)
|
||||
{
|
||||
int function_index=-(args_number+1);
|
||||
|
||||
if(!refuse && lua_isfunction(st,function_index))
|
||||
{
|
||||
int n=size();
|
||||
|
||||
if(lua_pcall(st,args_number,LUA_MULTRET,0))
|
||||
throw_lua_exception(st);
|
||||
int function_index = -(args_number + 1);
|
||||
|
||||
retvals_number=size()+args_number+1-n;
|
||||
}
|
||||
if (!refuse && lua_isfunction(st, function_index))
|
||||
{
|
||||
int n = size();
|
||||
|
||||
if (lua_pcall(st, args_number, LUA_MULTRET, 0))
|
||||
throw_lua_exception(st);
|
||||
|
||||
retvals_number = size() + args_number + 1 - n;
|
||||
}
|
||||
}
|
||||
|
||||
void lua::bind::m_invoke(void) throw(std::exception)
|
||||
{
|
||||
if(!refuse)
|
||||
{
|
||||
int function_index=-(args_number+1);
|
||||
int this_index=function_index-1;
|
||||
|
||||
if(lua_isfunction(st,function_index) && lua_istable(st,this_index))
|
||||
if (!refuse)
|
||||
{
|
||||
lua_pushvalue(st,this_index);
|
||||
args_number++;
|
||||
lua_insert(st,-args_number);
|
||||
|
||||
int function_index = -(args_number + 1);
|
||||
int this_index = function_index - 1;
|
||||
|
||||
int n=size();
|
||||
|
||||
if(lua_pcall(st,args_number,LUA_MULTRET,0))
|
||||
throw_lua_exception(st);
|
||||
if (lua_isfunction(st, function_index) && lua_istable(st, this_index))
|
||||
{
|
||||
lua_pushvalue(st, this_index);
|
||||
args_number++;
|
||||
lua_insert(st, -args_number);
|
||||
|
||||
retvals_number=size()+args_number+1-n;
|
||||
|
||||
int n = size();
|
||||
|
||||
if (lua_pcall(st, args_number, LUA_MULTRET, 0))
|
||||
throw_lua_exception(st);
|
||||
|
||||
retvals_number = size() + args_number + 1 - n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,8 +141,8 @@ namespace lua
|
|||
void get(int& v, int index) throw() { v = lua_tointeger(st, index); }
|
||||
void get(double& v, int index) throw() { v = lua_tonumber(st, index); }
|
||||
void get(const char*& v, int index) throw()
|
||||
{
|
||||
size_t len = 0;
|
||||
{
|
||||
size_t len = 0;
|
||||
v = lua_tolstring(st, index, &len);
|
||||
}
|
||||
void get(std::string& v, int index) throw()
|
||||
|
|
Loading…
Add table
Reference in a new issue