-
-
Notifications
You must be signed in to change notification settings - Fork 664
/
Copy pathgame.lua
135 lines (122 loc) · 3.13 KB
/
game.lua
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
function getGlobalStorage(key)
local keyNumber = tonumber(key)
if not keyNumber then
key = "'" .. key .. "'"
end
local resultId = db.storeQuery("SELECT `value` FROM `global_storage` WHERE `key` = " .. key)
if resultId ~= false then
local isNumber = tonumber(Result.getString(resultId, "value"))
if isNumber then
local val = Result.getNumber(resultId, "value")
Result.free(resultId)
return val
else
local val = Result.getString(resultId, "value")
Result.free(resultId)
return val
end
end
return -1
end
function setGlobalStorage(key, value)
local keyNumber = tonumber(key)
if not keyNumber then
key = "'" .. key .. "'"
end
local valueNumber = tonumber(value)
if not valueNumber then
value = "'" .. value .. "'"
end
db.query("INSERT INTO `global_storage` (`key`, `value`) VALUES (" .. key .. ", " .. value .. ") ON DUPLICATE KEY UPDATE `value` = " .. value)
end
function Game.broadcastMessage(message, messageType)
if not messageType then
messageType = MESSAGE_GAME_HIGHLIGHT
end
for _, player in ipairs(Game.getPlayers()) do
player:sendTextMessage(messageType, message)
end
end
function Game.convertIpToString(ip)
local band = bit.band
local rshift = bit.rshift
return string.format("%d.%d.%d.%d", band(ip, 0xFF), band(rshift(ip, 8), 0xFF), band(rshift(ip, 16), 0xFF), rshift(ip, 24))
end
function Game.getHouseByPlayerGUID(playerGUID)
local houses, house = Game.getHouses()
for i = 1, #houses do
house = houses[i]
if house:getOwnerGuid() == playerGUID then
return house
end
end
return nil
end
function Game.getPlayersByIPAddress(ip, mask)
if not mask then
mask = 0xFFFFFFFF
end
local masked = bit.band(ip, mask)
local result = {}
local players, player = Game.getPlayers()
for i = 1, #players do
player = players[i]
if bit.band(player:getIp(), mask) == masked then
result[#result + 1] = player
end
end
return result
end
function Game.getReverseDirection(direction)
if direction == WEST then
return EAST
elseif direction == EAST then
return WEST
elseif direction == NORTH then
return SOUTH
elseif direction == SOUTH then
return NORTH
elseif direction == NORTHWEST then
return SOUTHEAST
elseif direction == NORTHEAST then
return SOUTHWEST
elseif direction == SOUTHWEST then
return NORTHEAST
elseif direction == SOUTHEAST then
return NORTHWEST
end
return NORTH
end
function Game.getSkillType(weaponType)
if weaponType == WEAPON_CLUB then
return SKILL_CLUB
elseif weaponType == WEAPON_SWORD then
return SKILL_SWORD
elseif weaponType == WEAPON_AXE then
return SKILL_AXE
elseif weaponType == WEAPON_DISTANCE or weaponType == WEAPON_MISSILE then
return SKILL_DISTANCE
elseif weaponType == WEAPON_SHIELD then
return SKILL_SHIELD
end
return SKILL_FIST
end
if not globalStorageTable then
globalStorageTable = {}
end
function Game.getStorageValue(key)
return globalStorageTable[key] or -1
end
function Game.setStorageValue(key, value)
if key == nil then
logger.error("[Game.setStorageValue] Key is nil")
return
end
if value == -1 then
if globalStorageTable[key] then
globalStorageTable[key] = nil
end
return
end
globalStorageTable[key] = value
end