Skip to content

Commit

Permalink
update to latest sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreyas-Sreelal committed May 14, 2019
1 parent 1314a86 commit 6920555
Show file tree
Hide file tree
Showing 16 changed files with 484 additions and 456 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
*dll
*so
*zip
*log

/dependencies/
/dependencies/
/gamemodes/
/plugins/
/scriptfiles/
/filterscripts/

announce
samp03svr
Expand Down
222 changes: 158 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tgconnector"
version = "0.1.0"
version = "0.1.1"
authors = ["Sreyas-Sreelal"]
edition = "2018"

Expand All @@ -13,7 +13,10 @@ lto = true
panic = "abort"

[dependencies]
samp-sdk = {git = "https://github.com/ZOTTCE/samp-sdk"}
samp = {git="https://github.com/ZOTTCE/samp-rs/",branch="async-amx",features = ["async"]}
log = "0.4.6"
fern = "0.5.7"
threadpool = "1.7.1"
minihttp = {git = "https://github.com/Sreyas-Sreelal/minihttp"}
serde_json = "1.0.38"
serde = "1.0.86"
Expand Down
6 changes: 3 additions & 3 deletions include/tgconnector.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif
#define _tgconnector_included

#define TGCONNECTOR_VERSION 10
#define TGCONNECTOR_VERSION 11
#define INVALID_MESSAGE_ID (TGMessage:-1)
#define INVALID_BOT_ID (TGBot:-1)

Expand All @@ -29,8 +29,8 @@ enum TGUserStatus {
};

//basic
native TGBot:TGConnect(const token[]);
native TGBot:TGConnectFromEnv(const variable[]);
native TGBot:TGConnect(const token[],thread_limit=3);
native TGBot:TGConnectFromEnv(const variable[],thread_limit=3);
native TGSendMessage(TGBot:bot,const TGChatId:chatid[],const text[],TGMessage:reply_id=INVALID_MESSAGE_ID,TGParseMode:parse_mode=TGParseMode:-1,callback[]="");
native TGDeleteMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid);
native TGEditMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid,text[],TGParseMode:parse_mode=TGParseMode:-1);
Expand Down
17 changes: 7 additions & 10 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ ifdef OS
TOOLCHAIN = +stable-i686-pc-windows-msvc
BINARYNAME = tgconnector.dll
OUPUTNAME = tgconnector.dll
CP_RELEASE = cp .\target\release\$(BINARYNAME) .\test-server\plugins\$(OUPUTNAME)
CP_DEBUG = cp .\target\debug\$(BINARYNAME) .\test-server\plugins\$(OUPUTNAME)
CP_RELEASE = cp .\target\release\$(BINARYNAME) .\plugins\$(OUPUTNAME)
CP_DEBUG = cp .\target\debug\$(BINARYNAME) .\plugins\$(OUPUTNAME)
else
ifeq ($(shell uname), Linux)
TOOLCHAIN = +stable-i686-unknown-linux-gnu
BINARYNAME = libtgconnector.so
OUPUTNAME = tgconnector.so
CP_RELEASE = cp target/release/$(BINARYNAME) test-server/plugins/$(OUPUTNAME)
CP_DEBUG = cp target/debug/$(BINARYNAME) test-server/plugins/$(OUPUTNAME)
CP_RELEASE = cp target/release/$(BINARYNAME) plugins/$(OUPUTNAME)
CP_DEBUG = cp target/debug/$(BINARYNAME) plugins/$(OUPUTNAME)
endif
endif

Expand All @@ -23,18 +23,15 @@ debug:
$(CP_DEBUG)

setup:
cd test-server && mkdir plugins
cd test-server && mkdir gamemodes
sampctl package ensure
sampctl package build
cd test-server && sampctl server ensure

ensure:
sampctl package ensure

run:
sampctl package build
cd test-server && sampctl server run
sampctl package run

clean:
cargo clean
8 changes: 4 additions & 4 deletions pawn-tests/test.pwn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define RUN_TESTS

#include <a_samp>
#include <YSI\y_testing>
#include <YSI_Core\y_testing>

#include "../include/tgconnector.inc"

Expand All @@ -22,19 +22,19 @@ Test:TestValidToken() {
ASSERT(g_bot != INVALID_BOT_ID);
}
Test:TGGetBotUserId() {
new
new
TGUser:userid = TGGetBotUserId(g_bot),
name[34],
username[32];

TGGetDisplayNameFromId(g_bot,userid,TGChatId:"562896556",name);
new bool:name_check = !strcmp("samp",name);
ASSERT(name_check);

TGGetUserNameFromId(g_bot,userid,TGChatId:"562896556",username);
new bool:username_check = !strcmp("samptg_bot",username);
ASSERT(username_check);

new TGUserStatus:status = TGGetUserChatStatus(g_bot,userid,TGChatId:"562896556");
ASSERT(status == TG_MEMBER);

Expand Down
19 changes: 14 additions & 5 deletions pawn.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
"user": "Sreyas-Sreelal",
"repo": "tgconnector",
"entry": "pawn-tests/test.pwn",
"output": "test-server/gamemodes/test.amx",
"output": "gamemodes/test.amx",
"dependencies": ["sampctl/pawn-stdlib"],
"dev_dependencies": ["pawn-lang/YSI-Includes"],
"dev_dependencies": ["pawn-lang/YSI-Includes@5.x"],
"include_path": "include",
"local": true,
"builds": [
{
"name": "test",
"includes": ["./include"]
}
],
"runtime": {
"plugins": ["Sreyas-Sreelal/tgconnector"]
},
"runtimes": [
{
"rcon_password": "|%GuRd324$\u0026|",
"port": 7777,
"plugins":["tgconnector"],
"gamemodes":["test"],
"hostname": "test",
"maxplayers": 32,
"mode" :"y_testing"
}
],
"resources": [
{
"name": "tgconnector-linux-x86.zip",
Expand Down
58 changes: 28 additions & 30 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::http::{HttpMethod, HttpRequest};
use crate::methods::*;
use crate::types::*;
use samp_sdk::log;
use log::error;
use serde_json::{from_str, to_string};
use std::collections::VecDeque;
use std::sync::mpsc::{channel, Receiver, Sender};
use threadpool::ThreadPool;

pub struct BOT {
pub api_request_link: String,
Expand All @@ -13,10 +14,11 @@ pub struct BOT {
pub update_sender: Option<Sender<Update>>,
pub send_message_reciever: Option<Receiver<(Message, String)>>,
pub send_message_sender: Option<Sender<(Message, String)>>,
pub pool: ThreadPool,
}

impl BOT {
pub fn new(bot_token: String) -> Self {
pub fn new(bot_token: String, thread_count: i32) -> Self {
let (update_sender, update_reciever) = channel();
let (send_message_sender, send_message_reciever) = channel();

Expand All @@ -27,6 +29,7 @@ impl BOT {
update_sender: Some(update_sender),
send_message_reciever: Some(send_message_reciever),
send_message_sender: Some(send_message_sender),
pool: ThreadPool::new(thread_count as usize),
}
}

Expand All @@ -46,12 +49,12 @@ impl BOT {
self.get_updates();
true
} else {
log!("**[TGConnector] Error bot couldn't connect.{:?}", response);
error!("Bot couldn't connect.{:?}", response);
false
}
}
Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
false
}
}
Expand All @@ -63,7 +66,7 @@ impl BOT {

let mut getupdate = GetUpdates { offset: -2 };

std::thread::spawn(move || loop {
self.pool.execute(move || loop {
let request = HttpRequest {
url: format!("{}/getUpdates", api_link),
method: HttpMethod::Post,
Expand Down Expand Up @@ -96,7 +99,7 @@ impl BOT {
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
continue;
}
}
Expand All @@ -107,7 +110,7 @@ impl BOT {
let send_message_move = self.send_message_sender.clone();
let api_link = self.api_request_link.clone();

std::thread::spawn(move || {
self.pool.execute(move || {
let request = HttpRequest {
url: format!("{}/sendmessage", api_link),
method: HttpMethod::Post,
Expand All @@ -118,7 +121,7 @@ impl BOT {
Ok(response) => {
let response: APIResponse<Message> = from_str(&response).unwrap();
if !response.ok {
log!("**[TGConnector] Error Couldn't send message.{:?}", response);
error!("Couldn't send message.{:?}", response);
} else if callback != None {
let sender = send_message_move.as_ref().unwrap();
let send_data = (response.body.unwrap(), callback.unwrap());
Expand All @@ -127,7 +130,7 @@ impl BOT {
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
}
}
});
Expand All @@ -136,7 +139,7 @@ impl BOT {
pub fn delete_message(&self, delete_message_obj: DeleteMessage) {
let api_link = self.api_request_link.clone();

std::thread::spawn(move || {
self.pool.execute(move || {
let request = HttpRequest {
url: format!("{}/deletemessage", api_link),
method: HttpMethod::Post,
Expand All @@ -147,16 +150,15 @@ impl BOT {
Ok(response) => {
let response: APIResponse<bool> = from_str(&response).unwrap();
if !response.ok {
log!(
"**[TGConnector] Error Message {:?} couldn't delete. {:?}",
delete_message_obj,
response
error!(
"Message {:?} couldn't delete. {:?}",
delete_message_obj, response
);
}
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
}
}
});
Expand All @@ -165,7 +167,7 @@ impl BOT {
pub fn edit_message(&self, edit_message_obj: EditMessageText) {
let api_link = self.api_request_link.clone();

std::thread::spawn(move || {
self.pool.execute(move || {
let request = HttpRequest {
url: format!("{}/editmessagetext", api_link),
method: HttpMethod::Post,
Expand All @@ -176,16 +178,15 @@ impl BOT {
Ok(response) => {
let response: APIResponse<Message> = from_str(&response).unwrap();
if !response.ok {
log!(
"**[TGConnector] Error Message {:?} couldn't edit {:?}",
edit_message_obj,
response
error!(
"Message {:?} couldn't edit {:?}",
edit_message_obj, response
);
}
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
}
}
});
Expand All @@ -204,13 +205,13 @@ impl BOT {
if response.ok {
response.body
} else {
log!("**[TGConnector] Error get_chat_member.{:?}", response);
error!("get_chat_member.{:?}", response);
None
}
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
None
}
}
Expand All @@ -229,16 +230,13 @@ impl BOT {
if response.ok {
response.body
} else {
log!(
"**[TGConnector] Error get_chat_members_count.{:?}",
response
);
error!("get_chat_members_count.{:?}", response);
None
}
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
None
}
}
Expand All @@ -257,13 +255,13 @@ impl BOT {
if response.ok {
response.body
} else {
log!("**[TGConnector] Error get_chat.{:?}", response);
error!("get_chat.{:?}", response);
None
}
}

Err(err) => {
log!("{:?}", err);
error!("{:?}", err);
None
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use samp_sdk::amx::AMX;
use samp_sdk::{exec, log};
use log::error;
use samp::amx::AmxIdent;
use samp::exec_public;

pub fn on_tg_message(amx_list: &[usize], botid: usize, fromid: i32, message_id: i32) {
pub fn on_tg_message(amx_list: &[AmxIdent], botid: usize, fromid: i32, message_id: i32) {
execute!(amx_list,"OnTGMessage",botid;fromid,message_id);
}

pub fn on_tg_send_message(amx_list: &[usize], name: String, botid: usize, message_id: i32) {
pub fn on_tg_send_message(amx_list: &[AmxIdent], name: &str, botid: usize, message_id: i32) {
execute!(amx_list,name,botid;message_id);
}

pub fn on_tg_channel_post(amx_list: &[usize], botid: usize, message_id: i32) {
pub fn on_tg_channel_post(amx_list: &[AmxIdent], botid: usize, message_id: i32) {
execute!(amx_list,"OnTGChannelPost",botid;message_id);
}

pub fn on_tg_user_joined(amx_list: &[usize], botid: usize, userid: i32) {
pub fn on_tg_user_joined(amx_list: &[AmxIdent], botid: usize, userid: i32) {
execute!(amx_list,"OnTGUserJoined",botid;userid);
}

pub fn on_tg_user_left(amx_list: &[usize], botid: usize, userid: i32) {
pub fn on_tg_user_left(amx_list: &[AmxIdent], botid: usize, userid: i32) {
execute!(amx_list,"OnTGUserLeft",botid;userid);
}
Loading

0 comments on commit 6920555

Please sign in to comment.