online multiplayer chess game (note server currently down)
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
extends Control
class_name Chat

onready var list: MessageList = $v/MessageList

var regexes := [
	[compile("_([^_]+)_"), "[i]$1[/i]"],
	[compile("\\*\\*([^\\*\\*]+)\\*\\*"), "[b]$1[/b]"],
	[compile("\\*([^\\*]+)\\*"), "[i]$1[/i]"],
	[compile("```([^`]+)```"), "[code]$1[/code]"],
	[compile("`([^`]+)`"), "[code]$1[/code]"],
	[compile("~~([^~]+)~~"), "[s]$1[/s]"],
	[compile("#([^#]+)#"), "[rainbow freq=.3 sat=.7]$1[/rainbow]"],
	[compile("%([^%]+)%"), "[shake rate=20 level=25]$1[/shake]"],
	[compile("\\[([^\\]]+)\\]\\(([^\\)]+)\\)"), "[url=$2]$1[/url]"],
	[compile("([-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*))"), "[url]$1[/url]"],
]
var emoji_replace_regex := compile(":[^:]{1,15}:")

const piece_emoji_path = "res://assets/pieces/cburnett/"
const emoji_path = "res://assets/emojis/"
const emojis := {
	[":cold:"]: emoji_path + "cold.png",
	[":bigsmile:"]: emoji_path + "bigsmile.png",
	[":cry:", ":sad:"]: emoji_path + "cry.png",
	[":happy:"]: emoji_path + "happy.png",
	[":hmm:"]: emoji_path + "hmm.png",
	[":huh:"]: emoji_path + "huh.png",
	[":smile:"]: emoji_path + "smile.png",
	[":unhappy:"]: emoji_path + "unhappy.png",
	[":upsidedown_smile:"]: emoji_path + "upsidedown_smile.png",
	[":weary:"]: emoji_path + "weary.png",
	[":what:"]: emoji_path + "what.png",
	[":wink_tongue:"]: emoji_path + "wink_tongue.png",
	[":wink:"]: emoji_path + "wink.png",
	[":wow:"]: emoji_path + "wow.png",
	[":zany:"]: emoji_path + "zany.png",
	[":...:"]: emoji_path + "3dots.png",
	[":R:", ":rook:"]: piece_emoji_path + "wR.png",
	[":N:", ":knight:"]: piece_emoji_path + "wN.png",
	[":B:", ":bishop:"]: piece_emoji_path + "wB.png",
	[":Q:", ":queen:"]: piece_emoji_path + "wQ.png",
	[":K:", ":king:"]: piece_emoji_path + "wK.png",
	[":P:", ":pawn:"]: piece_emoji_path + "wP.png",
}
var expanded_emojis = {}


# create smokey centered text
func server(txt: String) -> void:
	list.add_label("[center][b][color=#a9a9a9]%s[/color][/b][/center]" % md2bb(txt))


func _init():
	Globals.chat = self


func _exit_tree():
	Globals.chat = null


func _ready():
	for trigger_list in emojis:
		for trigger in trigger_list:
			expanded_emojis[trigger] = emojis[trigger_list]

	if PacketHandler:
		PacketHandler.connect("chat", self, "add_label_with")
	server("Welcome!")
	yield(get_tree().create_timer(.4), "timeout")
	server("You can use markdown(sort of)!")
	$v/TextInput.setup_emojis(emojis)


static func compile(src: String) -> RegEx:
	var regex := RegEx.new()
	regex.compile(src)
	return regex


func add_label_with(data: Dictionary) -> void:
	var string := "[b]{who}[color=#f0e67e]:[/color][/b] {text}".format(data)
	list.add_label(string)


func send(t: String) -> void:
	t = t.strip_edges()
	if !t:
		return
	t = md2bb(emoji2bb(t))
	var name_data = SaveLoad.get_data("id").name
	var name = name_data if name_data else "Anonymous"
	name += "(%s)" % ("Spectator" if Globals.spectating else Globals.get_team())
	if PacketHandler:
		PacketHandler.relay_signal({"text": t, "who": name}, PacketHandler.RELAYHEADERS.chat)
	else:
		add_label_with({text = t, who = name})  # for testing


# markdown to bbcode
func md2bb(input: String) -> String:
	for replacement in regexes:
		var result = replacement[0].search(input)
		if result:
			var index = input.find(result.strings[0]) - 1
			var char_before = input[index]
			if not char_before in "\\":  # taboo characters go here
				if replacement[1] == "[url]$1[/url]" and "png" in result.strings[0]:  # url one must avoid recognizing res://
					continue
				input = replacement[0].sub(input, replacement[1], true)
	input = input.replace("\\", "")  # remove escapers
	return input


func emoji2bb(input: String) -> String:
	for i in emoji_replace_regex.search_all(input):
		var emoji = i.strings[0]
		if emoji in expanded_emojis:
			input = input.replace(emoji, "[img=30]%s[/img]" % expanded_emojis[emoji])
	return input