Skip to content

Commit

Permalink
1. More Posse work. (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
codermanchris committed Jun 4, 2020
1 parent d1f6c50 commit 2751b3d
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 7 deletions.
8 changes: 8 additions & 0 deletions client/_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ function Helpers.MessageUI(target, method, data)
SendNUIMessage({ target = target, method = method, data = data })
end

function Helpers.SetUIFocus(value)
SetNuiFocus(value, value)
end

function Helpers.LoadModel(model)
while not HasModelLoaded(model) do
RequestModel(model)
Expand All @@ -239,4 +243,8 @@ end
-- NUI Callbacks
RegisterNUICallback('CloseMenu', function(data, cb)
Helpers.CloseUI(false)
end)

RegisterNUICallback('LoseUIFocus', function(data, cb)
Helpers.SetUIFocus(false)
end)
42 changes: 41 additions & 1 deletion client/posse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,40 @@ if (GameplayConfig.UsePosse) then
end

-- Packet Handlers
Helpers.PacketHandler('posse:OpenCreate', function(data)
if (Posse.PosseInfo ~= nil) then
return
end

Helpers.MessageUI('posse', 'showCreate', nil)
Helpers.SetUIFocus(true)
end)

Helpers.PacketHandler('posse:Open', function(data)
Helpers.OpenUI('posse', nil)
if (Posse.PosseInfo == nil) then
return
end
Helpers.OpenUI('posse', { Posse = Posse.PosseInfo, Members = Posse.PosseMembers, Rank = Posse.PosseRank })
end)

Helpers.PacketHandler('posse:OnCreate', function(data)
Posse.OnCreate(data.Success)
end)

Helpers.PacketHandler('posse:SetMember', function(data)
print('set member in posse ' .. data.PosseId)
end)

Helpers.PacketHandler('posse:SetMembers', function(data)
Posse.PosseInfo = data.Posse
Posse.PosseMembers = data.Members
Posse.PosseRank = data.Rank
end)

-- Nui Callbacks
RegisterNUICallback('posse:Create', function(data, cb)
Helpers.Packet('posse:Create', { PosseName = data.posseName })
Helpers.CloseUI(true)
end)

-- Class Functions
Expand Down Expand Up @@ -36,3 +68,11 @@ end
function Posse.HandleCivilian()

end

function Posse.OnCreate(success)
if (success) then
Helpers.CloseUI()
else
print('failed to create')
end
end
18 changes: 18 additions & 0 deletions client/ui/css/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@
background-size: 900px 600px;
}

#createposse {
display: none;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -150px;

width: 300px;
height: 200px;
color: #000;

background-image: url('../images/oldparchmentbg.jpg');
background-repeat: no-repeat;
background-size: 300px 200px;
font-family: ChineseRocks;
}

#posse {
display: none;
position: fixed;
Expand Down
28 changes: 28 additions & 0 deletions client/ui/js/posse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,36 @@ var posse = {
},
open: function(data) {
$("#posse").show();

$("#possename").html(data.Posse.Name);
$("#posse .members").html("");

$.each(data.Members, function(key, value) {
var html = "<div>" + value.firstname + " " + value.lastname + "</div>";
$("#posse .members").append(html);
});
},
close: function() {
$("#posse").hide();
},

showCreate: function() {
$("#createposse").show();
},
closeCreate: function() {
$("#createposse").hide();
core.sendPost("LoseUIFocus", null, function(data){});
},
create: function() {
var posseName = $("#newpossename").val();
if (posseName.length == 0) {
return;
}

core.sendPost("posse:Create", { posseName: posseName }, function(data){});
$("#newpossename").attr("disabled", true);
},
onCreated: function(data) {

}
}
20 changes: 15 additions & 5 deletions client/ui/ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,23 @@ <h2>Hunter</h2>

</div>

<div id='createposse'>
In order to create a new posse, you will need to pay $25 and be level 5.<br />
If you can handle that, go ahead and create one.<br /><br />
<input type='text' id='newpossename' placeholder='POSSE NAME' /><br /><br />
<input type='button' value='CANCEL' onclick='posse.closeCreate();' />
<input type='button' value='CREATE POSSE' onclick='posse.create();' />
</div>

<div id='posse'>
<div class='new'>
POSSE NAME:
<div class='padded-container'>
<h2>Posse</h2>
<h3><span id='possename'></span></h3>
<h3>Members</h3>
<div class='members'>

</div>
</div>
<div class='existing'>
POSSE
</div>
</div>

<div id='vehiclerental'>
Expand Down
118 changes: 117 additions & 1 deletion server/posse.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,121 @@
-- Slash Commands
RegisterCommand('newposse', function(source, args, rawCommand)
local playerId = source
Helpers.Packet(playerId, 'posse:OpenCreate', nil)
end, false)
RegisterCommand('posse', function(source, args, rawCommand)
local playerId = source
Helpers.Packet(playerId, 'posse:Open', nil)
end, false)
end, false)

-- Packet Handlers
Helpers.PacketHandler('posse:Create', function(playerId, data)
Posse.Create(playerId, data.PosseName)
end)

-- We need to tie into the redemrp:playerLoaded event so we can properly get this players posse information on character select
AddEventHandler('redemrp:playerLoaded', function(character)
local _source = source
Posse.OnPlayerLoaded(character)
end)

-- Class Functions
function Posse.Create(playerId, name)
if (not Posse.IsNameValid(name)) then
Helpers.Packet(playerId, 'posse:OnCreate', { Success = false })
Helpers.Respond(playerId, '^1That name is invalid. Please try another.')
return
end

Helpers.GetCharacter(playerId, function(character)
local realId = character.getSessionVar('realcharid')
local query = 'insert into `posses` (Name, OwnerId) values (@Name, @OwnerId);'
local params = {
['@Name'] = name,
['@OwnerId'] = realId
}

MySQL.Async.insert(query, params, function(posseId)
if (posseId <= 0) then
Helpers.Packet(playerId, 'posse:OnCreate', { Success = false })
return
end

Helpers.Packet(playerId, 'posse:OnCreate', { Success = true })

-- set session var
character.setSessionVar('PosseId', posseId)

-- add this member
Posse.AddMember(playerId, realId, posseId)
end)
end)
end

function Posse.IsNameValid(name)
local query = 'select Id from `posses` where Name=@Name'
local params = {
['@Name'] = name
}
local results = MySQL.Sync.fetchScalar(query, params)
return results == nil or results == 0
end

function Posse.AddMember(playerId, characterId, posseId)
local query = 'insert into `possemembers` (PosseId, CharacterId, Rank) values (@PosseId, @CharacterId, 0);'
local params = {
['@PosseId'] = posseId,
['@CharacterId'] = characterId,
}

--
MySQL.Async.insert(query, params, function(newId)
Helpers.Packet(playerId, 'posse:SetMember', { PosseId = posseId, Rank = 0 })
end)
end

-- note: this required a modification to redem_roleplay to sv_main.lua line 55ish. the code is commented below
-- it's possible the actual db id was exposed somewhere, but i don't know redem that well yet.
-- NOTE:
-- This is a custom redemrp_gameplay modification
-- Users[_source].setSessionVar("realcharid", _user.id)
-- todo
-- this is pretty ugly, but functional. still, clean it up.
function Posse.OnPlayerLoaded(playerId)
Citizen.CreateThread(function()
Helpers.GetCharacter(playerId, function(character)
local realId = character.getSessionVar('realcharid')
local query = 'select Id, PosseId, Rank from `possemembers` where CharacterId=@CharacterId'
local params = {
['@CharacterId'] = realId
}

MySQL.Async.fetchAll(query, params, function(results)
if (results == nil or results[1] == nil) then
return
end

local q2 = 'select Id, Name, OwnerId from `posses` where Id=@PosseId'
local p2 = {
['@PosseId'] = results[1].PosseId
}
MySQL.Async.fetchAll(q2, p2, function(r2)
if (r2 == nil or r2[1] == nil) then
return
end

character.setSessionVar('PosseId', tonumber(r2[1].Id))

local q3 = 'select pm.Id, pm.CharacterId, pm.Rank, c.firstname, c.lastname from `possemembers` as pm inner join `characters` as c on c.Id = pm.CharacterId where pm.PosseId = @PosseId'
local p3 = {
['@PosseId'] = character.getSessionVar('PosseId')
}

MySQL.Async.fetchAll(q3, p3, function(r3)
Helpers.Packet(playerId, 'posse:SetMembers', { Posse = r2[1], Rank = results[1].Rank, Members = r3 })
end)
end)
end)
end)
end)
end
8 changes: 8 additions & 0 deletions sql/create_possemember.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE `possemember` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`PosseId` int(11) NOT NULL,
`CharacterId` int(11) NOT NULL,
`Rank` smallint(6) NOT NULL,
`JoinDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
7 changes: 7 additions & 0 deletions sql/create_posses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE `posses` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(50) CHARACTER SET utf8 NOT NULL,
`OwnerId` int(11) NOT NULL,
`CreateDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

0 comments on commit 2751b3d

Please sign in to comment.